CSS490 RPC and RMI
CSS490 RPC and RMI
Textbook Ch4
Instructor: Munehiro Fukuda
These slides were compiled from the course textbook, the reference books, and
the instructors original materials.
Winter, 2004
CSS490 RPC
Why RPC
Calling a function at a remote server:
Ease of use
RPC Model
Caller (Client)
Callee (Server)
Request message
including arguments
Suspended
Reply message
Including a return value
Resume execution
Winter, 2004
CSS490 RPC
Implementation Issues
Transparency property
Syntactic transparency
Semantic transparency
Caller capable of passing arguments
Caller suspended till a return from a function
Callee capable of returning a value to caller
Winter, 2004
CSS490 RPC
RPC Mechanism
Interface Definition Language
File
Client Program
Server Program
Define arguments
Return
Call
Return
Call
Register remote functions
IDL Compiler
Server Stub
Client Stub
Message
marshaling
Decoding Encoding
Retransmission
RPC Runtime acknowledgments
Receive
Send Routing
encryption
args
PRC id client id
(5) Exception?
Message
(4) Invalid arguments?
Decoding Encoding
(3) Invalid procedure?
RPC Runtime
Receive
type
msg id
(call)
msg id
Winter, 2004
CSS490 RPC
Stateful/Stateless Servers
Stateful servers:
Stateless servers:
Winter, 2004
CSS490 RPC
Instance-per-Call Servers
Instance-per-Session Servers
Persistent Servers
Winter, 2004
CSS490 RPC
Parameter-Passing
Semantics
Server
Client
class objA {
objB b;
objC c;
}
class objB {
}
class objC {
}
Winter, 2004
Call by Value
CSS490 RPC
Call Semantics
multiple times.
Last-One Call Semantics
Killing all orphan calls before restarting a
new call
Last-of-Many Call Semantics
Neglecting all orphan calls using
identifiers
At-Least-Once Call Semantics
Accepting the first response (maybe an
orphan response)
Exactly-Once Call Semantics
The strongest semantics as learnt in
message passing
Winter, 2004
CSS490 RPC
Node 1
Orphan
call
2nd
call
Node 2
Node 3
Client-Server Binding
(SunRPC)
Server Machine
Client Machine
Portmap
Daemon
port: 111
TCP or UDP
Winter, 2004
CSS490 RPC
10
Performance Improvement
in RPC
Callback RPC
Clinet:
specify a function that should be called back from the server.
Keep working its computation without blocked on a RPC.
Server:
Call back the client function as a response.
Multithreaded Client/Server
Client:
Winter, 2004
CSS490 RPC
11
SunRPC
Modify by yourself
example_client.o
example_client.c
rpcgen a example.x
Client program
Your client
example_clnt.c
example_client
example_clnt.o
Client stub
example_h.c
example.x
Interface
descriptions
gcc c -o
Header
example_xdr.c
ld -o
example_xdr.o
Marshalling
example_svc.c
example_svc.o
Server program
Server stub
example_server.c
example_server.o
example_server
Winter, 2004
CSS490 RPC
12
/*
* example.x - Speicification of some arithmetic/string service.
* Define 2 procedures:
*
fact( int n ) returns n!.
*
power( double x, double y ) returns x^^y.
*
strconc( struct strings ) concatenates src to dest.
*/
const BUFFER_SIZE = 1024;
struct doubles {
double a;
double b;
};
struct strings {
char src[BUFFER_SIZE];
char dst[BUFFER_SIZE];
};
program EXAMPLE_PROG {
version EXAMPLE_VERS {
int
FACT( int )
= 1;
double POWER( doubles )
= 2;
string STRCONC( strings ) = 3;
} = 1;
} = 0x31234567;
Winter, 2004
/* procedure number = 1 */
/* procedure number = 2 */
/* procedure number = 3 */
/* version number = 1 */
/* program number = 0x31234567 */
CSS490 RPC
13
power_1_arg.a = 2.0;
power_1_arg.b = 6.0;
result_2 = power_1(&power_1_arg, clnt);
if (result_2 == (double *) NULL) {
clnt_perror (clnt, "call failed");
}
printf( "power( 2.0, 6.0 ) = %f\n", *result_2 );
strncpy( strconc_1_arg.dst, "xyz\0", BUFFER_SIZE );
strncpy( strconc_1_arg.src, "abc\0", BUFFER_SIZE );
result_3 = strconc_1(&strconc_1_arg, clnt);
if (result_3 == (char **) NULL) {
clnt_perror (clnt, "call failed");
}
printf( "strconc( \"xyz\", \"abc\" ) = %s\n", *result_3 );
clnt_destroy (clnt);
Winter, 2004
CSS490 RPC
int
main (int argc, char *argv[])
{
char *host;
exit (0);
}
if (argc < 2) {
exit (1);
host = argv[1];
example_prog_1 (host);
14
return &result;
double *
power_1_svc(doubles *argp, struct svc_req *rqstp)
{
static double result;
result = pow( argp->a, argp->b );
}
return &result;
char **
strconc_1_svc(strings *argp, struct svc_req *rqstp)
{
static char * result;
result = strcat( argp->dst, argp->src );
}
return &result;
Winter, 2004
CSS490 RPC
15
Advantages
SunRPC
Java RMI
Template generation
Dynamic port
assignment
High performance
Automatic
serialization
Manual IP port
handling
Good security
Winter, 2004
Low performance
CSS490 RPC
16
Serialization
Manual Operations in C++
class SubObject {
public:
int id;
SubObject( int I ) { id = I; }
void print( ) { cout << SubObject: id= << id << endl; }
};
class MainObject {
public:
id id;
SubObject *subObj;
MainObject( int I, SubObject *s ) { id = I; subObj = j; }
void print( ) { cout << MainObject: id= << id << endl; }
};
MainObj
Read/write
Winter, 2004
CSS490 RPC
17
Serialization
Automatic Operations in Java
public class SubObject implements Serializable {
private int id;
public SubObject( int I ) { id = I; }
public void print( ) { System.out.println( SubObject: id = + id ); }
}
public class MainObject implements Serializable {
public int id;
public SubObject subObj;
public MainObject( int I, SubObject s ) { id = I; subObj = s; }
public void print( ) { System.out.println( MainObject: id = + id ); }
}
Public class Test {
public static void main( String args[] ) throws IOException, ClassNotFoundException {
FileOutputStream f1 = new FileOutputStream( sample.dat );
ObjectOutputStream o = new ObjectOutputStream( f1 );
MainObject obj1 = new MainObject( 1, new SubObject( 2 ) );
o.writeObject( obj1 );
//automatically write all objects traceable from obj1
o.flush( );
o.close( );
FileInputStream f2 = new FileInputStream( sample.dat );
ObjectInputStream i = new ObjectInputStream( f2 );
MainObject obj2 = (MainObject)I.readObject( );
// automatically read all object traceable from obj2
I.close( );
obj2.print( );
obj2.subObj.print( );
}
}
Winter, 2004
CSS490 RPC
18
Serialization
Serializable or Not Serializable
Serializable
Not Serializable
Winter, 2004
CSS490 RPC
19
RMI
Programming Procedure
(4) Program a Client.java class
(5) javac Client.java
(8) Run Client with java Client
Application Layer:
Client .java
Server.java
(implements remote interface)
Stub/Skeleton:
Stub
Skeleton
Server_Stub.class Server_Skel.class
Remote Reference:
rmiregistry [port#]
(object manager/name service)
Winter, 2004
TCP/IP
CSS490 RPC
20
RMI
Remote Interface & Return Object
// Remote interface: A server must implements this interface in its class define
import java.rmi.*
public interface ServerInterface extends Remote {
public ReturnObj get( ) throws RemoteException; // A server returns a ReturnObj to a client
}
Winter, 2004
CSS490 RPC
21
RMI
Server Implementation
import java.io.*;
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
Winter, 2004
CSS490 RPC
22
RMI
Client Implementation
Import java.rmi.*;
Import java.io.*;
Winter, 2004
CSS490 RPC
23
RMI
Compilation and Execution
% javac ReturnObject.java
// Compile a return class
% javac Server.java
// Compile a server class
% rmic Server // Create a stub and a skelton
% javac Client.java
// Compile a client class
% ls
ReturnObject.java ReturnObject.class Server.java
Server.class Server_Stub.class Server_Skel.class
% rmiregistry& // Invoke RMI registory
% java Server&
// Invoke a server
% java Client // Invoke a client
ReturnObject id = 0
SubObject id = 1
% java Client // Inovke a client again
ReturnObject id = 2
SubObject id = 3
%
Winter, 2004
CSS490 RPC
24
2.
3.
4.
5.
The caller process of an RPC must wait for a reply from the callee process
after making a call. Explain how this can actually be done.
Which types of server did you implement for the programming assignment
2, a stateful or a stateless server? Then, why did you implement such a
type of server?
Discuss the similarities and differences among the following parameter
passing:
1.
Call-by-object-reference
2.
Call-by-move
3.
Call-by-visit
What is an orphan call? How are orphan calls handled in the
implementation of the following types of call semantics:
1.
Last-one call semantics
2.
Last-of-many call semantics
3.
At-least-once call semantics
Discuss about the pros and the cons of dynamic binding in RPC.
Winter, 2004
CSS490 RPC
25