Remote procedure call is a method to call a procedure located in another process.
A nice example is to synchronise 2 RPi's where an application on one RPi is waiting to start until an application on another RPi is up and running.
Using RPC, we can create an app on one RPi that "senses" at regular times the state of another app running on the other RPi.
When this is detected, the sensing RPi can then decide to continue activity, knowing that both RPi's are in sync with each other from that point onwards.
Since the setup of RPC calls is quite complex from a source code point of view, the Linux communitiy has created a tool called rpcgen. This tool needs an interface file as input and will generate the stub files for both the client and server. The stub files will contain basic skeleton code that has to be adapted to the needs of the RPC feature.
The interface file will contain the protocol definition for a given task.
Suppose we want to create a factorial program with the following content:
/*
A program to calculate Factorial numbers
*/
#include
int main() {
long int f_numb, calc_fact(int);
int number;
printf("Factorial Calculator");
printf("Enter a positive integer value ");
scanf("%d", &number);
if (number < 0)
printf("Positive values only!");
else if ((f_numb = calc_fact(number)) > 0)
printf("%d! = %d", number, f_numb);
else
printf("Sorry %d! is out of my range!", number);
return 0;
}
/*
Calculate the factorial number and return the result or return 0
if value is out of range.
*/
long int
calc_fact(int n){
long int total = 1, last = 0;
int idx;
for (idx = n; idx - 1; --idx) {
total *= idx;
if (total <= last) /* Have we gone out of range? */
return (0);
last = total;
}
return (total);
}
Let's say we want to turn this program into a client/server application whereby the client could request for a factorial value from the remote factorial server.
To accomplish this, we start by writing the protocol definition file. It's common habbit to give that protocol file the extension .x:
This is the content of the protocol definition file:
/*
The protocol definition file for the factorial program.
The programmer generates this file.
*/
program FACTORIAL {
version ONE {
long int CALC_FAC( int ) = 1;
} = 1;
} = 0x20000049;
When giving this to the rpcgen generator file there will be six files generated:
the header file, fact.h
the stub files fact_cntl.c (client) and fact_svc.c (server) files
the client template file: fact_client.c
the server template file: fact_svc.c
the Makefile: Makefile.fact
.
.
.
TO BE CONTINUED
.
.
.
Once all files are in place, run the command make -f Makefile.xxx where xxx is the postfix generated by rpcgen.