Transcript Lab 6

CSC 480
Software Engineering
Lab 6 – RMI
Nov 8, 2002
Remote Method Invocation

RMI applications are often comprised of
a
server



a
creates some remote objects
makes references to them accessible
waits for clients to invoke methods on these remote objects
client


gets a remote reference to one or more remote objects in the
server and then
invokes methods on them.
Distributed Object Application


RMI provides the mechanism by which the
server and the client communicate and pass
information back and forth.
Such an application is sometimes referred to as
a distributed object application.
Typical Activities

Distributed object applications need to
 Locate
remote objects
 Communicate with remote objects
 Load class bytecodes for objects that are passed
around
Locate Remote Objects

Applications can use one of two mechanisms to
obtain references to remote objects.
 Register
its remote objects with RMI's simple naming
facility, the rmiregistry, or
 Pass
and return remote object references as part of
its normal operation.
Communicate w/ Remote Objects


Details of communication between remote
objects are handled by RMI
To the programmer, remote communication
looks like a standard Java method invocation
Load Class Bytecodes

Load class bytecodes for objects that are
passed around: Because RMI allows a caller to
pass objects to remote objects, RMI provides the
necessary mechanisms for loading an object's
code, as well as for transmitting its data.
An Illustration
Client-Server via Socket
Client
Server
send request data
return response data
RMI
client
stub
Call stub
method locally
return value or
Throw exception
receiver
Send marshalled
parameters
Send marshalled
return value or
exception
Call server
method locally
server
Inheritance Diagram
Object
Remote
RemoteObject
RemoteStub
Product
RemoteServer
Unicast
RemoteObject
ProductImpl_
Stub
ProductImpl
The Product Interface


The client will not have a copy of the remote
object
It knows what the remote object can do from the
interface shared between the client and the
server
import java.rmi.*;
public interface Product extends Remote
{ String getDescription()
throws RemoteException;
}
The ProductImpl Class
On the server side, the methods advertised in
the remote interface should be implemented by
a class named ProductImpl

import java.rmi.*;
import java.rmi.server.*;
public class ProductImpl extends UnicastRemoteObject implements Product
{ public ProductImpl(String n) throws RemoteException { name = n; }
public String getDescription() throws RemoteException
{ return "I am a " + name + ". Buy me!"; }
private String name;
}
The Client Side

A stub class (ProductImpl_Stub.class) will
be created when the ProductImpl class is
compiled with rmic

A security policy file is needed to start the client
In the client program (ProductClient.java)
a security manager need to be set up

System.setSecurityManager(new RMISecurityManager());
The ProductClient Class



Set up a security manage
Specify the server URL
Access the remote object
String url = "rmi://localhost/";
try
{
Product c1 = (Product)Naming.lookup(url + "toaster");
} catch (Exception e) {
System.out.println("Error " + e);
}
The Security Policy File

For security purpose, to let the client to connect
to the RMI registry and the server object, a
policy file is needed to grant the permissions
grant
{ permission java.net.SocketPermission
"*:1024-65535", "connect,accept";
permission java.net.SocketPermission
"localhost:80", "connect";
};
Compile the Application


Do a normal compile
javac *.java
Run rmic on the implementation class
rmic ProductImpl
 You
may also specify a version. With v1.2, no
skeleton file will be generated
rmic –v1.2 ProductImpl
Running the Application

Start the RMI registry
rmiregistry &

Start the server
java ProductServer &

Run the client
java –Djava.sercurity.policy=
client.policy ProductClient