next up previous
Next: Transparent RPC Up: Communication across a Previous: OS Interface: Sockets

Sun RPC

Both UDP and TCP require a client to encode its request and parameters in the data area of the message, and then wait for a reply. The server in turn needs to decode the request, perform the service, and then encode the reply. It would be more useful if the client could directly call a remote procedure which the server executes to service the request.

Sun RPC demonstrates how a remote procedure call paradigm can be supported on top of UDP or TCP via library routines rather than compiler support. Each server registers a procedure pair via the registerrpc call,

registerrpc (prognum, versnum, procnum, procaddr, inproc, outproc)
A client can then invoke callrpc to call the remote procedure:
callrpc (host, prognum, versum, procnum, inproc, &in, outproc, &out)
The procedure callrpc takes as arguments the machine number of the server, the procedure identifier, and the input and output parameters. It sends an appropriate UDP or TCP message to the destination machine, waits for a reply, extracts the results, and returns them in the output parameters.

The XDR (eXternal Data Routines) are responsible for marshalling/unmarshalling procedure parameters onto/from XDR streams.

To illustrate, an add server, that adds an integer and a float, can declare the following ``interface'' in a header file:

#define PROGNUM 1
#define VERSNUM 0
#define ADDNUM  0


typedef struct {
    int f1;
    float f2 } S;

extern float add ();

Then it can provide an implementation of the add procedure:

float add (s)
	S *s;
{
    return (s->f1 + s->f2);
}

A procedure such as add that can be called remotely by Sun RPC is required to take a one-word argument. It takes only one argument so that callrpc does not have to deal with a variable number of arguments. Like any other procedure, callrpc has to fix the type of the argument. The trick to pass a user-defined value to a remote procedure through callrpc is to pass its address and then let the procedure cast it as a pointer to a value of the correct type.

Next it can provide an implementation of the following xdr procedure between the client and server:

xdr_s (xdrsp, arg)
    XDRS *xdrsp;
    S *arg;
{
    xdr_int (xdrsp, &arg->f1);
    xdr_float (xdrsp, &arg->f2);
}

It is called in the marshalling/unmarshalling modes by the client and server RPC system. In the marshalling mode, it writes the argument onto the stream and in the unmarshalling mode it extracts data from the stream into the argument.

Finally, it can register the procedure with the RPC system:

registerrpc (PROGNUM, VERSNUM, ADDNUM, add, xdr_s, xdr_float )

A client can now invoke this procedure:

S *s;
float *result;

s->f1 = 3;
s-f2 = 4.12;

callrpc (host, PROGNUM, VERSNUM, ADDNUM, xdr_s, s, xdr_float, result);



next up previous
Next: Transparent RPC Up: Communication across a Previous: OS Interface: Sockets



Prasun Dewan
Tue Jan 16 13:43:47 EST 2001