This article briefly introduces the principle structure and features of RPC (Remote Procedure Call) and its open programming interfaces for programmers at different levels. Examples show how to quickly develop RPC applications using rpcgen compilation tools.
Abstract:
This article briefly introduces the structure and features of Remote Procedure Call (RPC,
And the APIS open to programmers at different levels. The example demonstrates how to use rpcgen to compile
To quickly develop RPC applications.
I. Overview
In the traditional programming concept, the process is completed by the programmer locally, and can only be limited to a local running segment.
Code, that is, the running relationship between the main program and the process is the local call relationship. Therefore, this structure is increasingly used in the Network
Today, the development cannot adapt to actual needs. In general, the traditional process call mode cannot make full use of its network
Its host resources (such as CPU and memory) cannot improve the degree of code sharing between entities, making host resources
A large waste of resources.
The RPC programming described in this article is a good solution to the drawbacks of the traditional process. Through RPC I
We can make full use of a multi-processor environment with non-shared memory (for example, connecting multiple workstations through local Wang ).
You can easily distribute your applications across multiple workstations. Applications are like running on a multi-processor computer.
. You can easily share the Process Code, improve the utilization of system resources, or use a large number of values
Processing operations are run on systems with strong processing capabilities, thus reducing the burden on front-end machines.
Ii. Structure and calling mechanism of RPC
As mentioned above, RPC is actually a C/S programming mode, a bit similar to the C/S socket programming mode,
Higher layer. After the RPC service is established, the call parameters of the client are transmitted through the underlying RPC transmission channel.
Is UDP, or TCP (that is, TI-RPC-unrelated transmission), and according to the destination address and RPC provided before transmission
The upper-layer application code is transferred to the corresponding RPC application porgramme server, and the client is in
Wait for status until the response or time out signal is received. The specific flowchart is shown in F1. When the server receives a request
Message, the system will tell the RPC system's routine entry address when registering RPC, perform the corresponding operation, and return the result
To the client.
F1
After an RPC call is completed, the client program continues to run only when the corresponding thread sends the corresponding signal.
Of course, a service host can provide services through multiple remote processes.
What about the remote process? A remote process has three unique elements: program number, version number, and process number.
The program number is used to differentiate a group of related remote processes with a unique process. A program can have one or more
Different versions, and each version of the program contains a series of processes that can be called remotely,
This allows RPC of different versions to provide services at the same time. Each version contains many remote calls.
A process has a unique process number.
Iii. RPC-based application system development
After introducing the RPC principles, we will continue to discuss how to develop RPC-based application systems.
Generally, RPC development involves three steps:
A. Define the communication protocol for the customer/server.
The communication protocol is used to define the name of a service process, the data type of the call parameters, and the data of the return parameters.
Type, which also includes the underlying transmission type (UDP or TCP). Of course, it can also be automatically selected by the underlying RPC function.
Establish Ti-RPC for the connection type. The simplest way to generate a protocol is to use a protocol compilation tool, commonly used rpcgen,
I will describe the usage in detail in the following examples.
B. Develop client programs.
C. Develop server programs.
When developing client and server programs, RPC provides interfaces for calling development routines at different levels. No
Interfaces at the same level provide different degrees of control over RPC. Generally, it can be divided into five levels of programming interfaces. Next we will
The functions provided by each layer are discussed separately.
1. Simple Layer routine
The simple layer is designed for common RPC applications to quickly develop RPC application services.
Function.
Function Description
Rpc_reg () registers a process on a specific type of transport layer to serve as the RPC program
Rpc_call () Remote Call of the specified process on the specified host
Rpc_broadcast () broadcasts a Remote Procedure Call request to all specified transmission ports.
2. High-level routine
At this layer, the program needs to create a client handle before sending a call request, or when listening
Create a server handle first. At this layer, the program can freely bind its own applications to all
It provides the following functions on the transmission port.
Function Description
The clnt_create () program uses this function to tell the location of the underlying RPC server and its transmission type.
Clnt_create_timed () defines the maximum timeout time for each connection attempt
Svc_create () creates a server handle on a specified type of transmission port, telling the corresponding entry address of the underlying RPC event Process
Clnt_call () sends an RPC call request to the server
3. intermediate layer routine
The middle layer provides more detailed RPC control interfaces for the program, and the code at this layer becomes more complex,
But the operation is more effective. It provides the following functions.
Function Description
Clnt_tp_create () create a client handle on the specified transmission Port
Clnt_tp_create_timed () defines the maximum transmission latency.
Svc_tp_creaet () creates a Service handle on the specified transmission Port
Clnt_call () sends an RPC call request to the server
4. Expert layer routine
This layer provides more transmission-related functions. It provides the following functions.
Function Description
Clnt_tli_create () create a client handle on the specified transmission Port
Svc_tli_create () create a Service handle on the specified transmission Port
Rpcb_set () maps the RPC service and network address by calling rpcbind
Rpcb_unset () deletes the ing relationship created by rpcb_set ().
Rpcb_getaddr () calls rpcbind to specify the transmission address of the RPC service.
Svc_reg () associates the specified program and version with the corresponding time routine.
Svc_ureg () deletes an association created by svc_reg ().
The clnt_call () client initiates an RPC request to the specified server.
5. Underlying routines
This layer provides all calling interfaces that control transmission options. It provides the following functions.
Function Description
Clnt_dg_create () establishes a client handle on the client in the remote process using the connectionless method.
Svc_dg_create () creates a Service handle without connection
Clnt_vc_create () uses connection-oriented methods to create customer handles
Svc_vc_create () establishes RPC Service handle in connection-oriented mode
The clnt_call () client sends a call request to the server.
Iv. instance Introduction
Below I will introduce the implementation of RPC through simple layer to readers through examples. In this process
The RPC compiling tool-rpcgen will be used. The rpcgen tool is used to generate the remote program interface module.
The source code written in the language is compiled. The structure and syntax of the RPC language are similar to that of the C language. Compiled by rpcgen
The C source program can be directly compiled using the C compiler. Therefore, the entire compilation process is divided into two parts. Rpcgen
The source program of is ended with. X, and the following file is generated through its compilation:
A) a header file (. h) contains descriptions of server and client program variables, constants, and types.
B) A series of XDR routines that can process data types defined in the object.
C) A standard program framework on the server side.
D) A standard program framework of the client.
Of course, these outputs can be optional. The rpcgen compilation options are described as follows:
Option
'-'A generates all template files
'-' SC: generate the template file of the Client
'-' SS generates the server-side Template File
'-'Sm generate MAKEFILE file
(For details, see Solaris rpcgen manaul)
Rpcgen source code time. X:
/* Time. X: Remote time Printing Protocol */
Program timeprog {
Version printimevers {
String printime (string) = 1;
} = 1;
} = 0x20000001;
Time_proc.c source code:
/* Time_proc.c: Implementation of the Remote Procedure "printime "*/
# Include <stdio. h>
# Include <rpc/rpc. h>/* always needed */
# Include "time. H"/* time. H will 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
* Of "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
* 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 was unable to print
* The time.
* Print error message and die.
*/
Fprintf (stderr, "% s: cocould not get the timen", argv [0]);
Exit (1 );
}
Printf ("from the time server... % Sn", result );
Clnt_destroy (clnt );
Exit (0 );
}
With the above three sections of code, you can use the rpcgen compilation tool to compile the RPC protocol. The command is as follows:
$ Rpcgen time. x
Rpcgen automatically generates time. H, time_svc.c, time_clnt.c
Use the GCC provided by the system to compile C. The command is 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, and immediately bind the service to the RPC service port to provide
Service. Run./rdate hostname MSG on the client (MSG is a string created during testing ),
The hostname End Time is returned immediately.
Because the remote server clock information cannot be obtained in Sun Solaris (this function is not changed
Local server clock), I have applied this program to the network management of the billing server and the clock server for Synchronous monitoring
The system runs stably and achieves good results. It should be said that RPC is widely used, especially
It is particularly important in the field of distributed computing. Of course, I am also new to RPC, and there are many other places
The solution is not profound enough. I hope you will give me more advice.
References:
Sun solaris8 onc + Dev