Linux Programming Learning-----Remote Procedure Call (RPC) __ block chain

Source: Internet
Author: User
Tags function prototype

The following text references from http://www.yuanma.org/data/2006/0918/article_1560.htm
I. Overview

In traditional programming concepts, a process is compiled locally by a programmer and can only be limited to a piece of code that runs locally, and that is, the running relationship between its main program and the process is the local call relationship. Therefore, this kind of structure has been unable to meet the actual demand today in the growing network. In a word, the traditional process call pattern cannot make full use of the resources of other hosts on the network (such as CPU, memory, etc.), nor can it improve the sharing of the code between entities, which makes the host resources waste.
In this paper, RPC programming is a good solution to the traditional process of a series of abuses. RPC allows us to take advantage of a multiprocessor environment that is not shared memory (for example, multiple workstations connected over a LAN), so that you can easily distribute your application across multiple workstations, as if the application were running on a multiprocessor computer. You can easily realize the process code sharing, improve the utilization of the system resources, can also be a large number of operations on the processing capacity of a strong system to run, so as to reduce the burden of front-end machines.
Second, the structure principle of RPC and its calling mechanism
As mentioned in the previous RPC is a C/S programming mode, a bit similar to C/S Socket programming mode, but it is higher than the layer. When we set up the RPC service, the client's invocation parameters pass through the underlying RPC transport channel, either UDP or TCP (also known as ti-rpc-), and go to the appropriate RPC application based on the destination address provided before the transfer and the RPC upper application number Porgramme Server, and the client at this point is waiting until a reply or time out timeout signal is received. The detailed flowchart is shown in Figure 1. When the server side obtains the request message, it performs the appropriate action and returns the result to the client according to the routine entry address of the RPC system when the RPC is registered.
When an RPC call completes, the corresponding thread sends the corresponding signal, and the client program continues to run.
Of course, a service host can have multiple remote procedures to provide services, so how to represent a single existing 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 procedure numbers. A program can have one or several different versions, and each version of the program contains a series of processes that can be invoked remotely, with the introduction of a version that allows RPC to be serviced at the same time in different versions. Each version contains a number of procedures that are available for remote invocation, and each process has its own uniquely 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 type of the return parameter, including the underlying transport type (either UDP or TCP) or, of course, the automatic selection of the connection type by the RPC underlying function to establish TI-RPC. The simplest method of protocol generation is to use the Protocol compilation tool, which is commonly used with Rpcgen, and I will describe it in detail 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. Different levels of interface provide varying degrees of control over RPC. Generally can be divided into 5 levels of programming interface, and then we discuss each layer provided by the functional functions.
1. Simple Layer Routines
The simple layer, designed for the rapid development of RPC application services, is intended for general RPC applications, and provides the following functional functions.

The name of the function

Function description

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 a procedure specified on a 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 establish a server-side handle before listening for the request. In this layer, the program is free to bind its application to all transport ports, which provides the following functional functions.

The name of the function

Function description

Clnt_create ()

This function is invoked by the program to tell the location of the underlying RPC server and its transport type

Clnt_create_timed ()

Defines the maximum time to timeout for each attempt to connect

Svc_create ()

Creates a server handle on a transport port of the specified type, telling the corresponding entry address of the underlying RPC event procedure

Clnt_call ()

Issuing 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 runs more efficiently, providing the following functional functions.

The name of the function

Function description

Clnt_tp_create ()

To establish a client handle on the specified transport port

Clnt_tp_create_timed ()

Define Maximum transmission delay

Svc_tp_creaet ()

Establish a service handle on the specified transport port

Clnt_call ()

Issuing RPC call requests to the server side

4. Expert Layer Routines
This layer provides a number of transport-related function calls, which provide the following functional functions.
The

Function name

Feature description

Clnt_tli_create (

Establish a client handle on the specified transport port

svc_tli_create ()

/td>

Creates a service handle on the specified transport port

Rpcb_set ()

by calling RP Cbind RPC service and network address map

Rpcb_unset ()

Delete rpcb_set () built The mapping relationship of

rpcb_getaddr ()

Call Rpcbind will specify the transport address for the RPC service /p>

Svc_reg ()

Associate the specified program and version number with the appropriate time routine

Svc_ureg ()

Delete associations 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.

The name of the function

Function description

Clnt_dg_create ()

Establish client handles to remote procedures on the client with no connection

Svc_dg_create ()

Establishing a service handle with no connection

Clnt_vc_create ()

Establish a client handle in a connection-oriented manner

Svc_vc_create ()

Establishing RPC service handles in a connection-oriented manner

Clnt_call ()

The client sends the call request to the server side

............ Reference text is not done, please see http://www.yuanma.org/data/2006/0918/article_1560.htm

About the RPC language
RPC language is also a specialized programming language, of course, we do not need to know too much, only to be able to understand the following basic structure:
Program TestProg {
Version Version {
String TEST (string) = 1;
} = 1;
} = 87654321;
Here TestProg and version are two variables that identify a single RPC interface. This is used by RPC service programs, such as Portmap, we can not care, variable names are also arbitrarily taken. But the value is unique in your system.
"String TEST (String) = 1;" This line shows that there are two functions test_version and test_version_svc, because the VERSION variable is 1, so the function name is test_1 and Test_1_svc, which are used to invoke on both the server side and the client, namely:
The Test_1 function is called on the client, and the server side calls the TEST_1_SVC function to process and return.
The type of a function is a string in the String,rpc language that is within C. So the above function has a string to pass as a parameter, and a string to return. That
char * * test_1 (char **argp, CLIENT *clnt) and Char **test_1_svc (char **argp, struct svc_req *rqstp)

Similarly, if the declaration is like this:
program rdictprog /* Name of the remote program (not used) */
{
  &nb Sp Version rdictvers /* Declaration of version (below) */
    {
    &nb sp;   int initw (void)      = 1; /* I-procedure in this program */
 &NBSP ;      int INSERTW (string) = 2; /* Second procedure in this program */
  &N bsp;     int Deletew (string) = 3; /* Third procedure in this program */
         int LOOKUPW (string) = 4; /* Fourth procedure in this program */
   } = 1; /* Definition of the program version */
} = 0x30090949; /* Remote program number (must is unique) * *
It means that there are four functions available in this RPC, that is, the client can invoke Initw_1, Insertw_1, Deletew_1, lookupw_1 four functions to send messages to the server, the server can use Initw_1_svc, Insertw_1_svc, DELETEW_1_SVC, lookupw_1_svc four functions to process the request and return the result.

Original task
Suppose you now have a program with the source code as follows:
/* dict.c--Main, INITW, Nextin, INSERTW, Deletew, LOOKUPW */
#include <stdio.h> ;
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define Maxword 50         * Maximum length of a command or Word */
#define DICTSIZ 100     & nbsp; /* Maximum number of entries in dictionary. */
Char Dict[dictsiz][maxword + 1];   /* Storage for a Dictionary of words */
int nwords = 0; & nbsp;         /* Number of words in the dictionary */
/* Function prototype */
int Nextin (char *cmd, Char *word);
int initw (void);
int INSERTW (const char *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.