Distributed Computer
Systems
CT024-3-3-DCOMS and VE
Remote Method Invocation (RMI)
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 1
TOPIC LEARNING OUTCOMES
At the end of this topic, you should be able to:
1. Understanding of RPC
2. Understanding the stub and skeleton
3. Implementation of RMI
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 2
Contents & Structure
What is RPC?
Define what is RMI?
What are the steps involved in creating RMI?
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 3
Recap From Last Lesson
• What are the security issues in DS?
• List out the essential factors of security elements in DS
• How to set secure password?
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 4
Remote Procedure Call (RPC)
• The most common framework for newer protocols
and for middleware
• Used both by operating systems and by
applications
– NFS is implemented as a set of RPCs
– DCOM, CORBA, Java RMI, etc., are just RPC systems
• Fundamental idea: –
– Server process exports an interface of procedures or
functions that can be called by client programs
• similar to library API, class definitions, etc.
• Clients make local procedure/function calls
– As if directly linked with the server process
– Under the covers, procedure/function call is converted
into a message exchange with remote server process
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 5
5
Ordinary procedure/function call
count = read(fd, buf, nbytes)
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 6
6
Would like to do the same if called procedure or function is on a remote
server
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 7
7
Solution – a pair of stubs
• Client-side stub
– Looks like local server function
– Same interface as local function
– Bundles arguments into message, sends to server-side stub
– Waits for reply, un-bundles results
– returns
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 8
8
• Server-side stub
– Looks like local client function to server
– Listens on a socket for message from client stub
– Un-bundles arguments to local variables
– Makes a local function call to server
– Bundles result into reply message to client stub
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 9
9
Result
• The hard work of building messages, formatting, uniform
representation, etc., is buried in the stubs
• Where it can be automated!
• Client and server designers can concentrate on the semantics
of application
• Programs behave in familiar way
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 10
10
RPC Model
• A server defines the service interface using an interface
definition language (IDL)
– the IDL specifies the names, parameters, and types for all client-
callable server procedures
• A stub compiler reads the IDL declarations and produces two
stub functions for each server function
– Server-side and client-side
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 11
11
RPC Model (continued)
• Linking:–
– Server programmer implements the service’s functions and links with
the server-side stubs
– Client programmer implements the client program and links it with
client-side stubs
• Operation:–
– Stubs manage all of the details of remote communication between
client and server
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 12
12
RPC Stubs
• A client-side stub is a function that looks to the client as if it were a
callable server function
– I.e., same API as the server’s implementation of the function
• A server-side stub looks like a caller to the server
– I.e., like a hunk of code invoking the server function
• The client program thinks it’s invoking the server
– but it’s calling into the client-side stub
• The server program thinks it’s called by the client
– but it’s really called by the server-side stub
• The stubs send messages to each other to make the RPC happen
transparently (almost!)
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 13
13
Marshalling Arguments
• Marshalling is the packing of function parameters into a
message packet
– the RPC stubs call type-specific functions to marshal or unmarshal the
parameters of an RPC
• Client stub marshals the arguments into a message
• Server stub unmarshals the arguments and uses them to invoke the service
function
– on return:
• the server stub marshals return values
• the client stub unmarshals return values, and returns to the client program
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 14
14
Java Remote Object Invocation (RMI)
• Overview of RMI
• Java RMI allowed programmer to execute
remote function class using the same
semantics as local functions calls.
Local Machine (Client) Remote Machine (Server)
SampleServer remoteObject;
int s;
…
s = remoteObject.sum(1,2);
1,2
public int sum(int a,int b)
{
3 }
return a + b;
System.out.println(s);
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 15
The General RMI Architecture
Remote Machine
• The server must first
bind
bind its name to the RMI Server
registry Registry
• The client lookup the skeleton
server name in the
registry to establish return call lookup
remote references.
• The Stub serializing
stub
the parameters to
skeleton, the skeleton RMI Client
invoking the remote
method and serializing Local Machine
the result back to the
stub.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 16
The Stub and Skeleton
call
skeleton
Stub
RMI Client RMI Server
return
• A client invokes a remote method, the call is
first forwarded to stub.
• The stub is responsible for sending the remote
call over to the server-side skeleton
• The stub opening a socket to the remote
server, marshaling the object parameters and
forwarding the data stream to the skeleton.
• A skeleton contains a method that receives
the remote calls, unmarshals the parameters,
and invokes the actual remote object
implementation.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 17
Remote Method Invocation
• Remote method invocation(RMI) allow a java object to invoke method
on an object running on another machine. RMI provide remote
communication between java program. RMI is used for building
distributed application.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 18
18
Concept of RMI
• A RMI application can be divided into two part, Client program and
Server program. A Server program creates some remote object, make
their references available for the client to invoke method on it. A Client
program make request for remote objects on server and invoke method
on them. Stub and Skeleton are two important object used for
communication with remote object.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 19
19
Stub and Skeleton
• Stub act as a gateway for Client program. It resides on Client side and
communicate with Skeleton object. It establish the connection between
remote object and transmit request to it.
• Skeleton object resides on server program. It is responsible for passing
request from Stub to remote object.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 20
20
Stub and Skeleton
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 21
21
Creating a simple RMI application
• Define a remote interface.
• Implementing remote interface.
• create and start remote application
• create and start client application
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 22
22
Define a remote interface
• A remote interface specifies the methods that can be invoked remotely
by a client. Clients program communicate to remote interfaces, not to
classes implementing it. To be a remote interface, a interface must
extend the Remote interface of java.rmi package.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 23
23
Remote Interface
import java.rmi.*;
public interface AddServerInterface extends Remote
{
public int sum(int a,int b);
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 24
24
Implementation of remote interface
• For implementation of remote interface, a class must either extend
UnicastRemoteObject or use exportObject() method of
UnicastRemoteObject class.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 25
25
import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject
implements AddServerInterface
{
Adder()throws RemoteException
{
super();
}
public int sum(int a,int b)
{
return a+b;
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 26
26
Create AddServer and host rmi service
You need to create a server application and host rmi
service Adder in it.
This is done using rebind() method of
java.rmi.Naming class.
rebind() method take two arguments, first represent
the name of the object reference and second
argument is reference to instance of Adder
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 27
27
import java.rmi.*;
import java.rmi.registry.*;
public class AddServer
{
public static void main(String args[])
{ try
{
AddServerInterface addService=new Adder();
Naming.rebind("AddService",addService); //addService
object
is hosted with name
AddService.
}
catch(Exception e)
{
System.out.println(e);
}
} Remote Method Invocation (RMI)
CT024-3-3 Distributed Computer Systems 28 SLIDE 28
Create Client Application
Client application contains a java program that invokes the
lookup() method of the Naming class.
This method accepts one argument, the rmi URL and returns
a reference to an object of type AddServerInterface. All
remote method invocation is done on this object.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 29
29
import java.rmi.*;
public class Client
{
public static void main(String args[])
{
try
{
AddServerInterface
st=(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/Ad
dService");
System.out.println(st.sum(25,8));
}
catch(Exception e)
{
System.out.println(e);
}
}
} Remote Method Invocation (RMI)
CT024-3-3 Distributed Computer Systems 30 SLIDE 30
Execution
Compile all the files using javac
Start rmi registry
Run server
Run client
Output is : 33
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 31
31
Write a RMI application to reverse the given string .Write
all interfaces and required classes
• import java.rmi.*;
public interface StrRev extends Remote
{
public String reverse(String str) throws RemoteException;
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 32
32
• import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class StrRev_Impli extends UnicastRemoteObject
implements StrRev
{
public StrRev_Impli() throws RemoteException
{}
public String reverse(String str) throws RemoteException
{
String str1="";
for(int i=str.length()-1;i>=0;i--)
str1=str1+str.charAt(i);
return (str1);
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 33
33
• import java.rmi.*;
import java.net.*;
public class StrRev_Server
{
public static void main(String args[])
{
try
{
StrRev_Impli strimpli=new StrRev_Impli();
Naming.rebind("RmiReverse",strimpli);
}
catch(Exception ex){}
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 34
34
• import java.io.*;
import java.rmi.*;
import java.net.*;
public class StrRev_Client
{
public static void main(String args[])
{
try
{
String url="rmi://127.0.0.1/RmiReverse";
StrRev strf=(StrRev)Naming.lookup(url);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter String to reverse : ");
String str=br.readLine();
System.out.println("Reverse of entered string is
"+strf.reverse(str));
}
catch(Exception ex){}
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 35
35
Write a RMI application to check whether the given
number is Prime or not . Write all interfaces and required
classes.
• import java.rmi.*;
public interface Prime extends Remote
{
public int check_prime(int no) throws RemoteException;
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 36
36
• import java.rmi.*;
import java.net.*;
public class Prime_server
{
public static void main(String args[])
{
try
{
Prime_Impli primpli=new Prime_Impli();
Naming.rebind("RmiPrime",primpli);
}
catch(Exception ex){}
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 37
37
import java.io.*;
import java.rmi.*;
import java.net.*;
public class Prime_client
{
public static void main(String args[])
{
try
{
String url="rmi://127.0.0.1/RmiPrime";
Prime intf=(Prime)Naming.lookup(url);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter No : ");
int no=Integer.parseInt(br.readLine());
int x=intf.check_prime(no);
if(x==0)
System.out.println(no+" is prime number");
else
System.out.println(no+" is not prime number");
}
catch(Exception ex){}
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 38
38
Steps for Developing an RMI System
1. Define the remote interface
2. Develop the remote object by implementing
the remote interface.
3. Develop the client program.
4. Compile the Java source files.
5. Generate the client stubs and server
skeletons.
6. Start the RMI registry.
7. Start the remote server objects.
8. Run the client
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 39
Step 1: Defining the Remote
Interface
• To create an RMI application, the first step is
the defining of a remote interface between
the client and server objects.
/* SampleServer.java */
import java.rmi.*;
public interface SampleServer extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 40
Step 2: Develop the remote object
and its interface
• The server is a simple unicast remote server.
• Create server by extending
java.rmi.server.UnicastRemoteObject.
• The server uses the RMISecurityManager to protect its
resources while engaging in remote communication.
/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SampleServerImpl extends UnicastRemoteObject
implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
} Remote Method Invocation (RMI)
CT024-3-3 Distributed Computer Systems SLIDE 41
Step 2: Develop the remote object
and its interface
• Implement the remote methods
/* SampleServerImpl.java */
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
}
• The server must bind its name to the registry,
the client will look up the server name.
• Use java.rmi.Naming class to bind the
server name to registry. In this example the
name call “SAMPLE-SERVER”.
• In the main method of your server object, the
RMI security manager is created and installed.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 42
Step 2: Develop the remote object
and its interface
/* SampleServerImpl.java */
public static void main(String args[])
{
try
{
System.setSecurityManager(new RMISecurityManager());
//set the security manager
//create a local instance of the object
SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry
Naming.rebind("SAMPLE-SERVER" , Server);
System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 43
Step 3: Develop the client program
• In order for the client object to invoke methods
on the server, it must first look up the name of
server in the registry. You use the
java.rmi.Naming class to lookup the server
name.
• The server name is specified as URL in the
from ( rmi://host:port/name )
• Default RMI port is 1099.
• The name specified in the URL must exactly
match the name that the server has bound to
the registry. In this example, the name is
“SAMPLE-SERVER”
• The remote method invocation is programmed
CT024-3-3 Distributed Computer Systems
using the remote interface name
Remote Method Invocation (RMI) SLIDE 44
Step 3: Develop the client program
import java.rmi.*;
import java.rmi.server.*;
public class SampleClient
{
public static void main(String[] args)
{
// set the security manager for the client
System.setSecurityManager(new RMISecurityManager());
//get the remote object from the registry
try
{
System.out.println("Security Manager loaded");
String url = "//localhost/SAMPLE-SERVER";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) );
}
catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 45
Step 4 & 5: Compile the Java source
files & Generate the client stubs and
server skeletons
• Assume the program compile and executing at
elpis on ~/rmi
• Once the interface is completed, you need to
generate stubs and skeleton code. The RMI
system provides an RMI compiler (rmic) that
takes your generated interface class and
procedures stub code on its self.
elpis:~/rmi> set CLASSPATH=”~/rmi”
elpis:~/rmi> javac SampleServer.java
elpis:~/rmi> javac SampleServerImpl.java
elpis:~/rmi> rmic SampleServerImpl
elpis:~/rmi> javac SampleClient.java
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 46
Step 6: Start the RMI registry
• The RMI applications need install to Registry.
And the Registry must start manual by call
rmiregisty.
• The rmiregistry us uses port 1099 by
default. You can also bind rmiregistry to a
different port by indicating the new port
number as : rmiregistry <new port>
elpis:~/rmi> rmiregistry
• Remark: On Windows, you have to type in
from the command line:
> start rmiregistry
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 47
Steps 7 & 8: Start the remote server
objects & Run the client
• Once the Registry is started, the server can be
started and will be able to store itself in the
Registry.
• Because of the grained security model in Java
2.0, you must setup a security policy for RMI
by set java.security.policy to the file
policy.all
elpis:~/rmi> java –Djava.security.policy=policy.all
SampleServerImpl
elpis:~/rmi> java –Djava.security.policy=policy.all
SampleClient
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 48
Review Questions
• Define what is RPC?
• Define what is RMI?
• What are the necessary steps involved in creating RMI application?
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 49
Summary / Recap of Main Points
• RPC Model
• RPC Stubs
• Marshalling Arguments
• RMI
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 50
What To Expect Next Week
In Class Preparation for Class
• Write a program in RMI to • Socket Programming
perform addition of 2 numbers.
CT024-3-3 Distributed Computer Systems Remote Method Invocation (RMI) SLIDE 51