Rpc mechanism

5
RPC (Remote Procedure Call) Distributed System Programming RPC Programming is generally a Client-Server communication which is mostly peer-to- peer.Means client will directly call to server by specifying two things- a)Server address b)Server Port. Server Address is the address of machine on which server program is running. An Server port is the port number on server machine through which our sever program will transmit data. Well before starting the tutorial,let me do some clarification. 1)RPC programming can be done in two ways, First by using RPC Library or Second By using Rpcgen (a RPC per-compiler). we will do our programming using RPCGEN. 2)The code provided below is for linux system but you can change it to run on windows. 3) You have to install "PortMapper" before running RPCGEN. So without going further inside the RPC theory, lets directly start with the practical theroy. So now here is the quick guide to our first example in which we will make simple message transfer between server and client. Funda 1 ) RPCGEN requires you to make three files: a).X file: This is a file which contains all the remote functions declarations. That is the functions that client will require to call at server has to be declared here. b)Server.cpp: A cpp file for server in which we give the definitions for functions on Server c)Client.cpp: Client cpp file to call the functions on server. User will have access to client file only in real time scenario. File1- Rpcgen: Name it "rpcinter.x" ------------------code--------------- program MESSAGEPROG { version PRINTMESSAGEVERS { int PRINTMESSAGE(string) = 1; /* function number*/ } = 1; /*version number*/ } = 0x20000002; ------------------------------------- .X file consist of a program with multiple versions in it and each version with multiple functions. Therefore to distinguish them we assign identification number to them. In our example we have program named as "MESSAGEPROG" which has only one version "PRINTMESSAGEVERS" with id=1. This version has only one function PRINTMESSAGE with id=1; If you have more function than assign id in increasing sequence as 2,3... and similarly for versions.

Transcript of Rpc mechanism

Page 1: Rpc mechanism

RPC (Remote Procedure Call) Distributed System ProgrammingRPC Programming is generally a Client-Server communication which is mostly peer-to-peer.Means client will directly call to server by specifying two things- a)Server address b)Server Port.Server Address is the address of machine on which server program is running. An Server port is the port number on server machine through which our sever program will transmit data.Well before starting the tutorial,let me do some clarification.

1)RPC programming can be done in two ways, First by using RPC Library or Second By using Rpcgen (a RPC per-compiler).we will do our programming using RPCGEN.

2)The code provided below is for linux system but you can change it to run on windows.

3) You have to install "PortMapper" before running RPCGEN.

So without going further inside the RPC theory, lets directly start with the practical theroy. So now here is the quick guide to our first example in which we will make simple message transfer between server and client.

Funda 1) RPCGEN requires you to make three files:

a).X file: This is a file which contains all the remote functions declarations. That is the functions that client will require to call at server has to be declared here.b)Server.cpp: A cpp file for server in which we give the definitions for functions on Serverc)Client.cpp: Client cpp file to call the functions on server. User will have access to client file only in real time scenario.

File1- Rpcgen: Name it "rpcinter.x"

------------------code---------------program MESSAGEPROG {   version PRINTMESSAGEVERS {     int PRINTMESSAGE(string) = 1;   /* function number*/   } = 1; /*version number*/} = 0x20000002;-------------------------------------.X file consist of a program with multiple versions in it and each version with multiple functions. Therefore to distinguish them we assign identification number to them.In our example we have program named as "MESSAGEPROG" which has only one version "PRINTMESSAGEVERS" with id=1.This version has only one function PRINTMESSAGE with id=1; If you have more function than assign id in increasing sequence as 2,3... and similarly for versions.

Point to note:a)All names will be in capital.However when you write same function in server cpp file,the name will be in lower case letter as we will see soon.b)Different version can have same functions. Version number will be the identity used in that case to distinguish functions.c)Program itself will have a address.You can use the above by default. It will work ro can give any

Page 2: Rpc mechanism

random address.

File2- Server File: Name it as :rpcserver.c"

----------------code----------------/** rpcserver.c: implementation of the* remote procedure "printmessage"*/

#include <stdio.h>#include "rpcinter.h" /* rpc.h generated by rpcgen */#include <rpc/rpc.h>

int * printmessage_1(char **msg, CLIENT *client)

{   static int result; /* must be static! */

   printf("%s\n", *msg);   result = 1;   return (&result);}

int * printmessage_1_svc(char **input,struct svc_req *svc) {      /*rpc call*/  CLIENT *client;  return(printmessage_1(input,client));}

-----------------------------------This is our server file in which we define functions definitions that client would call.The program is almost familiar to c code with some more informations and that are:

a)You have to include two additional files in the program. One is <rpc/rpc.h> to enable use of rpc commands. And second is .h file of your .x file which is in this case is rpcinter.h. So if you have A.x file than include A.h file in this server cpp file. This ".h" will be generated when we compile our ".x" file.

b)Function name will be converted into lowercase.

c)Each function will have two definition. One is like our simple one and second is for rpc calling used by <rpc/rpc.h> module.The second definition for every function will have the same structure.Just change the function name,its return type,its argument. For example: in above case printmessage has return type int * (why we will se it soon) and two arguments,a char* and a client.Client is denoted by struct svc_req in rpc call and by CLIENT in user definition.

d) the "_1" in function name is version number and "_svc" is used in rpc function call.

e)Rpc works in Pointer mode that means everything should be in pointer. So if you want to return a

Page 3: Rpc mechanism

integer as in our case you will return "int *".Similaly we want to take input argument as string which is nothing but char array or char[].And Array is itself a pointer so char[]=char *. And rpc takes everthing in pointer so char * will convert to "char **".

f)By default our function will take one argument which is a CLIENT handle. You don't have to do anything in this. Its structure will remain the same.So if you don't have to pass any argument to our function than it will look like this:

int * printmessage_1( CLIENT *client)

File3-Rpc Client:Name it as "rpcclient.c"

--------------------code-------------------------

#include <stdio.h>#include "rpcinter.h" /* rpcinter.h generated by rpcgen */#include <rpc/rpc.h>

main(int argc, char **argv)

{  CLIENT *clnt;  int *result;  char *server;  char *message;

  if (argc != 3) {     fprintf(stderr, "usage: %s host message\n", argv[0]);     exit(1);    }

  server = argv[1];  message = argv[2];

  /*   * Create client "handle" used for   * calling MESSAGEPROG on the server   * designated on the command line.   */

  clnt = clnt_create(server, MESSAGEPROG, PRINTMESSAGEVERS, "tcp");   /*creating client and opening connection*/

  if (clnt == (CLIENT *)NULL) {   /*    * Couldn't establish connection    * with server.    * Print error message and die.

Page 4: Rpc mechanism

    */

   clnt_pcreateerror(server);   exit(1);   }

  /*   * Call the remote procedure   * "printmessage" on the server   */

   result = printmessage_1(&message, clnt);  /*funtion call*/

   if (result == (int *)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 (*result == 0) {

   /*    * Server was unable to print    * our message.    * Print error message and die.    */

    fprintf(stderr, "%s: could not print your message\n",argv[0]);    exit(1);    }

   /* The message got printed on the    * server's console    */

    printf("Message delivered to %s\n", server);    clnt_destroy( clnt );    exit(0);}----------------------------------------

So this is our client code.Lets look the important features of it:

Page 5: Rpc mechanism

a)Include same two header files as of server file.b)Create a CLIENT object(in our code it is clnt).c)Client object will take 4 arguments..Server address,Program Name,Version Name ,Connection type.d)Check that the result of the function call printmessage is stored in pointer becuase we are getting address from the serve and not a value.e)when you run client code you have to pass server address and message at command line. Server address is stored in variable server which is used by Client object for connectivity and message is stored in variable message which is used by printmessage funtion to pass to server. You can also assign it inside the program but we are giving it at command line for now.f)At the end you will destroy the client by clnt_destroy method.

----------------------------------

Steps for Compilation and Run:

Open terminal and follow the steps.First check whether you have portmapper installed by typing: rpcinfo -p localhost1)Compile rpcinter.x by typing "rpcgen rpcinter.x" -This will generate a client and server stub files and a .h file.2)compile server :cc -o server rpcserver.c rpcinter_svc.c3)compile client :cc -o client rpcclient.c rpcinter_clnt.c4)Start server: ./server5)in new terminal start client : ./client localhost hello A message will be displayed on server terminal as "hello" ans on client terminal as "Message delivered"