Remote Procedure Calls II (January 22)

Download Report

Transcript Remote Procedure Calls II (January 22)

Remote Object Invocation
• The idea of RPCs applies to invocations on
objects.
• Object model systems include



Java’s RMI
CORBA
DCOM
Remote Distributed Objects
• Data and operations encapsulated in an object
• Operations are implemented as methods and are accessible through
interfaces.
• Object offers only its interface to clients.
• Object server is responsible for a collection of objects
• Client stub (proxy) implements interface
• Server skeleton handles (un)marshalling and object invocation.
Client-to-Object Binding
• Object reference: Having an object reference allows a
client to bind to an object:



Reference denote server, object and communication
protocol and even an implementation handle which refers
to complete implementation of a proxy that has the
communications protocol that the client can dynamically
load when binding to an object.
Client loads associated stub code
Stub is instantiated and initialized for specific object.
• Two ways of binding


Implicit: Invoke methods directly on the referenced object.
Explicit: Client must first explicitly bind to object before
invoking it.
Client-to-Object Binding
• The trick here is have a way to define a reference
identifier.
• Reference may contain URL pointing to an
implementation file.
• (Server, object) pair is enough to locate object.
• We need only a standard protocol for loading and
instantiating code.
• A location server keeps track of the machine where
an object’s server is currently running.
Remote Object References
• The client acquires a reference to a remote
object.

This part is different from creating a local
object.
• The client calls methods on the remote
object


No (syntactic) difference!
Just need to worry about a few new exceptions.
Overview of RMI
• RMI provides a Naming Service through the
RMI Registry that simplifies how programs
specify the location of remote objects.

This naming service is a JDK utility called
rmiregistry that runs at a well known address
(by default).
Overview of RMI Programming
• Define an interface that declares the methods
that will be available remotely.
• The server program must include a class that
implements this interface.
• The server program must create a remote object and
register it with the naming service.
• The client program creates a remote object by asking
the naming service for an object reference.
Server Details – Extending
Remote
• Create an interface that extends the
java.rmi.Remote interface.
• This new interface includes all the public
methods that will be available as remote
methods.
Sample Interface
import java.rmi.*;
// The interface must extend the Remote interface
to become something // that RMI can serve up.
public interface RemoteMath extends Remote {
public int Add(int x, int y) throws RemoteException;
public int Sub(int x, int y) throws RemoteException;
public int Mult(int x, int y) throws RemoteException;
public int Div(int x, int y) throws RemoteException; }
Server Details – Implementation
Class
• Create a class that implements the interface.

The class should also extend UnicastRemoteObject*
• This class needs a constructor that throws
RemoteException !
• This class is now used by rmic to create the
stub and skeleton code.
Example Interface
Implementation
import java.net.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class RemoteMathImpl extends
UnicastRemoteObject implements
RemoteMath {
public RemoteMathImpl() throws RemoteException
{ super(); }
Example Interface
Implementation
public static void main(String args[]) {
try {
// create a RemoteMathImpl object
RemoteMathImpl r = new RemoteMathImpl();
// register the object with the rmi registry
// (registry must already be running)
Naming.rebind("ReMath",r);
System.out.println("Registered RemoteMath object\n");
} catch (RemoteException e) {
System.out.println("RemoteException: " + e.getMessage());
e.printStackTrace();
} catch (MalformedURLException m) {
System.out.println("URL Problem: " + m.getMessage());
m.printStackTrace();
}
Example Interface
Implementation
public int Add(int x, int y) throws RemoteException {
LogRequest("ADD: " + x + "+" + y);
return(x+y); }
…..
Generating stubs and skeleton
• Compile the remote interface and implementation:
> javac RemoteMath.java RemoteMathImpl.java
• Use rmic to generate
RemoteMathImpl_stub.class,
RemoteMathImpl_skel.class
> rmic RemoteMathImpl
Server Detail – main()
• The server main() needs to:


create a remote object.
register the object with the Naming service.
public static void main(String args[]) {
try {
RemoteMathImpl r = new RemoteMathImpl();
Naming.bind(“ReMath”,r);
} catch (RemoteException e) {
. . .
Client Details
• The client needs to ask the naming service for
a reference to a remote object.


The client needs to know the hostname or IP
address of the machine running the server.
The client needs to know the name of the remote
object.
• The naming service uses URLs to identify
remote objects.
Using The Naming service
• Naming.lookup() method takes a string
parameter that holds a URL indicating the remote
object to lookup.
rmi://hostname/objectname
• Naming.lookup() returns an Object!
• Naming.lookup() can throw
 RemoteException
 MalformedURLException
Getting a Remote Object
try {
Object o =
Naming.lookup(“uri://csd.uwo.ca/ReMath”);
RemoteMath r = (RemoteMath) o;
// . . . Use r like any other Java object!
} catch (RemoteException re) {
. . .
} catch (MalformedURLException up) {
throw up;
}
Starting the Server
• First you need to run the Naming service
server:
> rmiregistry
• Now run the server:
> java RemoteMathImpl
Summary
• Potentially the use of remote procedure calls
can abstract many of the details involved in
writing networked applications.
• Learning curve is often high.