Client and Server Stubs
Remote procedure call
•
A remote procedure call makes a call to a remote
service look like a local call
– RPC makes transparent whether server is local or remote
– RPC allows applications to become distributed transparently
client server
Steps of a Remote Procedure Call
network routines network routines server functions server functions server stub (skeleton) server stub (skeleton) network routines network routines
1. Client calls stub
client functions
client functions
client stub
client server
Stub functions
server functions server functions server stub (skeleton) server stub (skeleton) network routines network routines2. Stub marshals params to net message
client server
Stub functions
3. Network message sent to server
client server
Stub functions
4. Receive message: send to stub
client server
Stub functions
5. Unmarshal parameters, call server function
client server
Stub functions
6. Return from server function
client server
Stub functions
7. Marshal return value and send message
client server
Stub functions
8. Transfer message over network
client server
Stub functions
9. Receive message: direct to stub
client server
Stub functions
10. Unmarshal return, return to client code
Passing Value Parameters (1)
Passing Value Parameters (2)
a) Original message on the Pentium
b) The message after receipt on the SPARC
Doors
Asynchronous RPC (1)
a) The interconnection between client and server in a traditional RPC
Asynchronous RPC (2)
Sun RPC/ONC (Open Network Computing) RPC
•
It is designed for client-server communication over
Sun
NFS network file system.
•
UDP or TCP can be used. Generally, if UDP is used, the
message length is restricted to 64 KB, but 8 - 9 KB in
practice.
Sun/ONC RPC
•
Steps to create a Sun RPC application:
1. Create the IDL file (add.x), the client main code (add_client.c), and the server function code (add_server.c)
2. Run the RPC generator (rpcgen –a add.x) to generate client stub (add_clnt.c), server stub (add_svc.c), header file (add.h), and data coversion file (add_xdr.c).
3. Compile the client and server programs (gcc -o add_client add_client.c) and (gcc -o add_server add_server.c)
4. Start the server (./add_server)
RPC IDL File
•
The notation of Sun RPC IDL:
a program number and a version number are supplied.
The procedure number is used as a procedure definition.
Single input parameter and output result are being passed.
•
RPC IDL Specification likes this:
program-def:
"program" identifier "{“ version-def:
"version" identifier "{“ procedure-def:
ONC RPC Interface Compiler
RPC Specification RPC Compiler Shared Filters and Header Files Client Stub Server SkeletonBinding
Sun RPC does not have a network-wide binding service. Instead it provides a local binding service called the portmapper which runs on every computer. Each instance of a portmapper records the port in use by each service running locally.
RPC Messages
•
Two type of message involved in the implementation
of an RPC
• Call messages
Server implementation
•
Server may be of two types
• Stateful
• State information are stored
• Stateless
call semantics
• Client process and the server processes possibly located on different nodes
• It is possible for either the caller or the callee node to fail independently and later to be restarted
• Also failure of communication links between the caller and
callee node is also possible
• RPC may get disrupted due to one or more of following reason
call message gets lost
response message gets lost
Callee node crashes and it restarted
call semantics…
• call semantics of an RPC system that determines how often the remote procedure may be executed under fault condition depends on this part of the RPC runtime code
• RPC runtime code provide the flexibility to the application programmer to select from different possible call semantics supported by an RPC system
• Type of call semantics
• Possibly or May-be call Semantics
• Last -one -call Semantics
• Last -of-many call semantics
• At-least-Once call semantics
call semantics…
• Possibly or May-Be call Semantics :
Weakest semantics
Caller waits for predefined timeout
No guarantee of receipt of call message
Idle for LAN types of environment
• Last one call semantics
Semantic retransmits the call message based on timeout
Process repeats until caller receives the result
call semantics..
• Last of Many call semantics
It neglects orphan call by using call ID to identify each call
Client checks messages ID, accepts response if call ID matches the recently repeated call else ignore
• At- least once call semantics
Call is executed one or more times but does not specify which result caller gets
Nested calls having orphan calls, caller accepts the result of first response and ignore other
• Exactly Once call semantics
Strongest call semantics
Example:
RPC IDL File (add.x)
struct intpair { int a; int b; };
program ADD_PROG {
version ADD_VERS {
int ADD(intpair) = 1; } = 1;
Example (Sun RPC)
•
Program-number is usually assigned as follows:
0x00000000 - 0x1fffffff defined by SUN
0x20000000 - 0x3fffffff defined by user
0x40000000 - 0x5fffffff transient
RPC Client Code (1) [add_client.c]
#include "add.h" #include <stdio.h> void
add_prog_1( char* host, int a, int b ) {
CLIENT *clnt; int *result_1; intpair add_1_arg;
clnt = clnt_create(host, ADD_PROG, ADD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror(host); exit(1);
}
add_1_arg.a = a; add_1_arg.b = b;
result_1 = add_1(&add_1_arg, clnt); if (result_1 == NULL) {
clnt_perror(clnt, "call failed:"); }
else
print("return is %d\n", *result_1); clnt_destroy( clnt );
RPC Client Code (2) [add_client.c]
int
main(int argc, char *argv[]) {
char *host; int a, b;
if (argc != 4) {
printf ("usage: %s server_host num1 num2\n", argv[0]); exit(1);
}
host = argv[1];
if ((a = atoi(argv[2])) == 0 && *argv[2] != '0') { fprintf(stderr, "invalid value: %s\n", argv[2]); exit(1);
}
if ((b = atoi(argv[3])) == 0 && *argv[3] != '0') { fprintf(stderr, "invalid value: %s\n", argv[3]); exit(1);
}
RPC Server Code (add_server.c)
/* * RPC server code for the remote add function */
#include "add.h" int *
add_1_svc(intpair *argp, struct svc_req *rqstp) {
static int result;
result = argp->a + argp->b;
printf("add(%d, %d) = %d\n", argp->a, argp->b, result); return &result;
Compilation Linking
rpcgen –a add.x
gcc -c add_client.c -o add_client.o
gcc -c add_clnt.c -o add_clnt.o
gcc -c add_xdr.c -o add_xdr.o
gcc -o client add_client.o add_clnt.o add_xdr.o gcc -c add_server.c -o add_server.o
gcc -c add_svc.c -o add_svc.o
Internal Details of ONC RPC
•
Initialization
Server runs: register RPC with port mapper on server host (rpcinfo –p)
Client runs: clnt_create contacts server's port mapper and establishes TCP/UDP connection with server
•
Client
Client calls local procedure (client stub: add_prog_1), that is generated by rpcgen. Client stub packages arguments, puts them in standard format (XDR), and prepares network messages (marshaling).
Network messages are sent to remote system by the client stub.
Internal Details of ONC RPC
•
Server
Server stub (generated by rpcgen) unmarshals arguments from network messages. Server stub executes local procedure (add_1_svc) passing arguments received from network messages.
When server procedure is finished, it returns to server stub with return values.
Server stub converts return values (XDR), marshals them into network messages, and sends them back to client
•
Back to Client
Client stub reads network messages from kernel
RPC in Practice - DCE RPC
• The Distributed Computing Environment (DCE) RPC is developed by the Open Software Foundation (OSF)/Open Group.
• DCE is a middleware executing as an abstraction layer between (network) operating systems and distributed applications.
• Microsoft derived its version of RPC from DCE RPC (e.g., MIDL compiler, etc.)
•
DCE includes a number of services:
– Distributed file service
– Directory service
– Security service
Writing a Client and a Server
The steps in writing a client and a server in DCE RPC.
Client-to-Server Binding (DCE RPC)
•
Issues:
Client must locate server machine and
locate the server.
•
Execution of Client and Server
– The server registers its procedures with the portmapper. – Client must locate server machine: The client contacts
the portmapper to determine which port the server is listening to.
– The client communicates with the server on the assigned
port.
•
DCE uses a separate daemon for each server