This paper briefly introduces the principle structure, characteristics and the programming interfaces of RPC (remote Procedure call long-distance process invocation) which are open to programmers at different levels. Examples demonstrate how to quickly develop RPC applications with Rpcgen compilation tools.
Summary:
This paper briefly introduces the principle structure and characteristics of RPC (remote Procedure Call long-distance procedure invocation),
And its open to programmers at different levels of programming interfaces. and example illustrates how to pass the Rpcgen compiler
Tools to quickly develop RPC applications.
I. Overview
In the traditional programming concept, the process is compiled locally by the programmer and can only be limited to a local run
Code, also known as the running relationship between its main program and procedure, is the local call relationship. So this structure in the network increasingly
The development of today has been unable to adapt to the actual demand. It is well known that traditional process invocation patterns do not fully utilize the network's
The resources of his host (such as CPU, memory, etc.), can not improve the sharing of the code between entities, so that the host funds
Source of waste.
In this paper, RPC programming is a good solution to the traditional process of a series of abuses. Through RPC I
You can take advantage of a multiprocessor environment that is not shared memory (for example, multiple workstations are connected through local Wang)
You can easily distribute your application across multiple workstations, and the application is like running on a multiprocessor computer
On the same. You can easily implement the process code sharing, improve system resources utilization, can also be a large number of numerical
The processing of the operation on the processing capacity of the system to run, thereby reducing the burden on the front-end machine.
Second, the structure principle of RPC and its calling mechanism
RPC In fact is also a kind of C/S programming mode, a bit similar to C/S Socket programming mode, but more than it
Higher layer. When we set up the RPC service, the invocation parameters of the client pass through the underlying RPC transport channel and can be
is UDP or TCP (also known as ti-rpc-Independent transmission), and based on the destination address provided before transmission and RPC
The upper application number is transferred to the appropriate RPC application Porgramme Server, and the client at this time is in the
Pending until an answer or time out timeout signal is received. Specific flowchart such as F1. When a request is received from the server side
Message, the corresponding action is performed according to the routine entry address of the RPC system when the RPC is registered, and the result is returned
to the client.
F1
When an RPC call completes, the corresponding thread sends the corresponding signal, and the client program continues to run.
Of course, there can be multiple remote process services on a single service host, so how to represent a single surviving
In the remote process. A remote procedure has three elements to uniquely determine: program number, version number, and procedure number.
A program number is a remote procedure that distinguishes a group of related and unique processes. A program can have one or several
A different version, and each version of the program contains a series of processes that can be invoked remotely, through the introduction of a version,
Enables RPC in different versions to provide services at the same time. Each version contains a number of procedures that are available for remote invocation, each
The procedure has its unique marked procedure number.
Third, RPC based application system development
With the above introduction to the RPC principle, we will continue to discuss how to develop an application system based on RPC.
In general, when developing RPC, we are typically divided into three steps:
A, define the communication protocol that describes the client/server.
The communication protocol here refers to the definition of the name of the service procedure, the data type of the calling parameter, and the data of the return parameter
Type, also includes the underlying transport type (either UDP or TCP), or it can be automatically selected by the RPC underlying function
The connection type establishes TI-RPC. The simplest method of protocol generation is the use of Protocol compilation tools, commonly used in Rpcgen,
I'll describe in detail how to use it in a later instance.
b, develop client programs.
C, the development of server-side programs.
When developing client and server-side programs, RPC provides us with different levels of development routines calling interfaces. No
The same level of interface provides varying degrees of control over RPC. Generally can be divided into 5 levels of programming interface, next we
The functional functions provided by each layer are discussed separately.
1. Simple Layer Routines
The simple layer, designed for the rapid development of RPC application services, is intended for use in ordinary RPC applications and provides
The following functional functions.
Functional description of Function name
Rpc_reg () Registers a procedure on a specific type of transport layer as the RPC program that provides the service
Rpc_call () remote invocation of procedures specified on the specified host
Rpc_broadcast () broadcasts a remote procedure call request to all transport ports of the specified type
2, high-level routines
At this level, the program needs to create a client handle before making the call request, or if it is listening
Create a server-side handle before you ask. Program in the layer can be free to bind their application to all the
On the transport port, it provides the following functional functions.
Functional description of Function name
The Clnt_create () program invokes this function to tell the location of the underlying RPC server and its transport type
Clnt_create_timed () defines the maximum timeout time for each attempt to connect
Svc_create () Creates a server handle on the transport port of the specified type, telling the corresponding entry address of the underlying RPC event procedure
Clnt_call () sends an RPC call request to the server side
3. Middle-tier routines
The middle tier provides a more detailed RPC control interface to the program, and this layer of code becomes more complex,
But it's also more efficient, and it provides the following functional functions.
Functional description of Function name
Clnt_tp_create () establishes a client handle on the specified transport port
Clnt_tp_create_timed () defines maximum transmission delay
Svc_tp_creaet () Creates a service handle on the specified transport port
Clnt_call () makes an RPC call request to the server side
4. Expert Layer Routines
This layer provides a number of transport-related function calls, which provide the following functional functions.
Functional description of Function name
Clnt_tli_create () establishes a client handle on the specified transport port
Svc_tli_create () Creates a service handle on the specified transport port
Rpcb_set () Maps RPC services and network addresses by calling Rpcbind
Rpcb_unset () Deletes the mapping relationship built by Rpcb_set ()
RPCB_GETADDR () Call Rpcbind will specify the transport address for the RPC service
Svc_reg () associates the specified program and version number with the appropriate time routines
Svc_ureg () deletes an association built with Svc_reg ()
Clnt_call () client initiates an RPC request to the specified server side
5, the underlying routine
This layer provides all the calling interfaces that control the transport options, which provides the following functional functions.
Function name feature description
Clnt_dg_ Create () Create a client handle to a remote procedure on the client
Svc_dg_create () with no connection () Establish a service handle without connection
Clnt_vc_create () establish client handles in a connection-oriented manner Br>svc_vc_create () Set up RPC service handles in a connection-oriented manner
Clnt_call () client sends call requests to server
Four, instance introduction
I'll introduce you to the reader through an example of a simple layer RPC implementation. Usually in this process we
will use the RPC Protocol compilation tool-rpcgen. The Rpcgen tool is used to generate the remote Program Interface module, which compiles the source code written in the RPC
language, which is structurally and syntactically similar to the C language. The C source program compiled by Rpcgen
can be compiled directly with the C compiler, so the entire compilation will be divided into two parts. Rpcgen
's source program ends with. x, the following file is generated through its compilation:
a) a header file (. h) includes instructions for server and client program variables, constants, types, and so on.
B) A series of XDR routines that can be handled by the data types defined in the header file.
C) A standard program framework on the server side.
D) A standard program framework on a client side.
Of course, these outputs can be selective, and the Rpcgen compilation options are described as follows:
option feature
'-Generate all template files
'-' Sc generate client template file
'-' Ss generation Server-side template Files
'-' Sm generates makefile files
(see Solaris Rpcgen manaul)
Rpcgen source program time.x:
/* Time.x:remote Time Printing protocol * *
Program Timeprog {
Version Printimevers {
String Printime (string) = 1;
} = 1;
} = 0x20000001;
TIME_PROC.C source program:
/* Time_proc.c:implementation of the remote procedure "Printime" * *
#include <stdio.h>
#include <rpc/rpc.h>/* Always needed * *
#include "time.h"/* time.h would be generated by Rpcgen * *
#include <time.h>
* Remote version of "Printime" * *
char * * printime_1 (char **msg,struct svc_req *req)
{
static char * result; /* Must be static! */
static Char tmp_char[100];
time_t Rawtime;
FILE *f;
f = fopen ("/tmp/rpc_result", "A +");
if (f = = (FILE *) NULL) {
strcpy (Tmp_char, "Error");
result = Tmp_char;;
return (&result);
}
fprintf (F, "%SN", *msg); Used for debugging
Fclose (f);
Time (&rawtime);
sprintf (Tmp_char, "Current time is:%s", CTime (&rawtime));
Result =tmp_char;
return (&result);
}
RTIME.C Source Code
/*
* Rtime.c:remote Version
* "PRINTIME.C"
*/
#include <stdio.h>
#include "time.h"/* time.h generated by Rpcgen * *
Main (int argc, char **argv)
{
CLIENT *clnt;
Char *result;
Char *server;
Char *message;
if (argc!= 3) {
fprintf (stderr, "Usage:%s host Messagen", Argv[0]);
Exit (1);
}
Server = argv[1];
message = Argv[2];
/*
* Create Client "handle" used for
* Calling Timeprog on the server
* Designated on the command line.
*/
CLNT = clnt_create (server, Timeprog, printimevers, "visible");
if (clnt = = (CLIENT *) NULL) {
/*
* couldn ' t establish connection
* with server.
* Print error message and die.
*/
Clnt_pcreateerror (server);
Exit (1);
}
/*
* Call the remote procedure
* "Printime" on the server
*/
Result =*printime_1 (&MESSAGE,CLNT);
if (result== (char *) NULL) {
/*
* An error occurred while calling
* The server.
* Print error message and die.
*/
Clnt_perror (clnt, server);
Exit (1);
}
/* Okay, we successfully called
* The remote procedure.
*/
if (strcmp (result, "Error") = = 0) {
/*
* Server is unable to print
* The time.
* Print error message and die.
*/
fprintf (stderr, "%s:could not get the Timen", argv[0]);
Exit (1);
}
printf ("From the time Server ...%sn", result);
Clnt_destroy (CLNT);
Exit (0);
}
With the above three pieces of code, the RPC protocol can be compiled using the Rpcgen compilation tool, which commands the following:
$rpcgen time.x
Rpcgen will automatically generate Time.h, TIME_SVC.C, time_clnt.c
C is compiled using GCC provided by the system, and commands are as follows:
$GCC rtime.c time_clnt.c-o RTIME-LNSL//Client compilation
$GCC time_proc.c time_svc.c-o time_server-lnsl//server-side compilation
After the compilation is successful, you can run Time_server on the server side and immediately bind the service to the RPC service port to provide
Service. Run on the client./rdate hostname msg (MSG is a string that the author used to test when established),
The time of the hostname end is returned immediately.
Because of the inability to obtain clock information on the remote server in Sun Solaris (without changing the
Earth server clock), the author has applied this program to the network management of synchronous monitoring of the clock server at the same time
In the system, the operation is stable and the result is good. It should be said that RPC's application is very extensive, especially
is particularly important in the field of distributed computing. Of course, the author is also just contact with RPC, there are many places
The solution is not deep enough, hope the broad masses of readers advice.
Reference documents:
"SUN Solaris8 onc+ Dev"