Remote Method Invocation (RMI), allows one host to run programs on another host that is running a program on a remote ho
CSE 425 Network Programming Lecture 5: Distributed Objects: RPC / RMI – Conceptual model
Fall - 2013 Hasan Mahmud
Contents • • • • •
Review of distributed computing architecture Basics of distributed objects What is RMI? Implementation of RMI CORBA
CSE 425 | Network programming | Fall - 2013
2
Object Oriented Paradigm
invoke method Object 1
Object 2 respond
CSE 425 | Network programming | Fall - 2013
3
Distributed Object Oriented Paradigm
Client Host/Process invoke method Object 1
Server Host/Process Object 2
respond
CSE 425 | Network programming | Fall - 2013
4
Distributed Object Oriented: implementation
Local – Client Host/Process
Remote Server Host/Process
Object 1
Object 2
“Post Office”
socket interaction
CSE 425 | Network programming | Fall - 2013
“Post Office”
5
Distributed Object Oriented: RMI paradigm
Local – Client Host/Process
RemoteServer Host/Process
Object 1
Object 2 magic
Stub of Object 2
CSE 425 | Network programming | Fall - 2013
Skeleton of Object 2
6
What is RMI? • Remote Method Invocation (RMI), allows one host to run programs on another host that is running a program on a remote host from a local machine. • RMI is a core Java API and class library that allows Java programs running in one Java virtual machine to call methods in objects running in a different virtual machine, even when the two virtual machines are running on physically separated hosts. • In essence, parts of a single Java program run on a local computer while other parts of the same program run on a remote host. CSE 425 | Network programming | Fall - 2013
7
RMI …. Remote Object • A remote object lives on a server. • Each remote object implements a remote interface that specifies which of its methods can be invoked by clients. • Clients invoke the methods of the remote object almost exactly as they invoke local methods. – For example, an object running on a local client can pass a database query as a String argument to a method in a database object running on a remote server to ask it to sum up a series of records. The server can return the result to the client as a double. This is more efficient than downloading all the records and summing them up locally.
CSE 425 | Network programming | Fall - 2013
8
RMI… Remote Object • A remote object is an object with methods that may be invoked from a different Java virtual machine than the one in which the object itself lives, generally one running on a different computer. • Each remote object implements one or more remote interfaces that declare which methods of the remote object can be invoked by the foreign system. • RMI is the facility by which a Java program running on one machine, say java.oreilly.com, can invoke a method in an object on a completely different machine, say www.ibiblio.org. CSE 425 | Network programming | Fall - 2013
9
Remote Interface example import java.rmi.*; import java.util.Date; public interface Weather extends Remote { public double getTemperature( ) throws RemoteException; public double getHumidity( ) throws RemoteException; public double getPressure( ) throws RemoteException; public double getWindSpeed( ) throws RemoteException; public double getWindDirection( ) throws RemoteException; public double getLatitude( ) throws RemoteException; public double getLongitude( ) throws RemoteException; public Date getTime( ) throws RemoteException; }
CSE 425 | Network programming | Fall - 2013
10
Remote Interface… explained • Java program running on my workstation at one virtual machine (e.g. JVM on stallion.elharo.com ) could look up the current weather object in the RMI registry at remote virtual machine (e.g. JVM on weather.centralpark.org). • The registry would send it a reference to the object running in remote (weather.centralpark.org's) virtual machine. • My program could then use this reference to invoke the getTemperature( ) method. • The getTemperature( ) method would execute on the server in remote machine (Central Park), not on my local machine. However, it would return the double value back to my local program running in local machine (at Brooklyn).
CSE 425 | Network programming | Fall - 2013
11
RMI layer model • To make the process as transparent to the programmer as possible, communication between a remote object client and a server is implemented in a series of layers, as shown in the figure below:
Client Program
Server Program
Stub
Skeleton
Remote Reference Layer Transport Layer
Remote Reference Layer The Internet
Transport Layer
CSE 425 | Network programming | Fall - 2013
12
Marshaling and Unmarshaling • Marshaling: – done by client (i.e., caller) – packing the parameters into a message – flatten structures – perform representation conversions if necessary – also done by server (i.e., callee) for results
• Unmarshaling: – done by receiver of message to extract parameters or results CSE 425 | Network programming | Fall - 2013
13
Implementation of RMI • To create a new remote object, first define an interface that extends the java.rmi.Remote interface. • Remote is a marker interface that does not have any methods of its own; its sole purpose is to tag remote objects so that they can be identified as such. • One definition of a remote object is an instance of a class that implements the Remote interface, or any interface that extends Remote.
CSE 425 | Network programming | Fall - 2013
14
Some RMI Classes and Interfaces • java.rmi.Remote – Interface that indicates interfaces whose methods may be invoked from a non-local JVM -- remote interfaces.
• java.rmi.Naming – The RMI Naming Service client that is used to bind a name to an object and to lookup an object by name.
• java.rmi.RemoteException – The common superclass for a number of communicationrelated RMI exceptions.
• java.rmi.server.UnicastRemoteObject – A class that indicates a non-replicated remote object. - marshals and unmarshals remote references to the object CSE 425 | Network programming | Fall - 2013
15
Application with Java RMI • Typical steps: 1. Define a remote interface(s) that extends java.rmi.Remote. 2. Develop a class (a.k.a. servant class) that implements the interface. 3. Develop a server class that provide a container for servants, i.e. creates the servants and registers them at the Naming Service. 4. Develop a client class that gets a reference to a remote object(s) and calls its remote methods. 5. Compile all classes and interfaces using javac. 6. (optional) Generate stub classes for classes with Remote interfaces using rmic – Since 1.5, stubs are generated dynamically by JIT
7. Start the Naming service rmiregistry 8. Start the server on a server host, and run the client on a client host. CSE 425 | Network programming | Fall - 2013
16
Example interface for remote object import public { public public public public }
java.rmi.*; interface sampleserver extends Remote int int int int
add(int sub(int mul(int div(int
a,int a,int a,int a,int
b) b) b) b)
throws throws throws throws
RemoteException; RemoteException; RemoteException; RemoteException;
• It is a simple interface for a remote object that calculates addition, subtraction, multiplication, and division of two integer numbers. • Remote determines which methods of the remote object clients may call. • RemoteException is the superclass for most of the exceptions that can be thrown when RMI is used. • Nothing in this interface says anything about how the calculation is implemented.
CSE 425 | Network programming | Fall - 2013
17
Class that implements the remote interface • The next step is to define a class that implements this remote interface. • This class should extend java.rmi.server.UnicastRemoteObject, either directly or indirectly (i.e., by extending another class that extends UnicastRemoteObject): •
public class UnicastRemoteObject extends RemoteServer
• Without going into too much detail, the UnicastRemoteObject provides a number of methods that make remote method invocation work. In particular, it marshals and unmarshals remote references to the object. (Marshalling is the process by which arguments and return values are converted into a stream of bytes that can be sent over the network. Unmarshalling is the reverse: the conversion of a stream of bytes into a group of arguments or a return value.)
CSE 425 | Network programming | Fall - 2013
18
Class that implements the remote object import java.io.*; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class sampleserverimpl extends UnicastRemoteObject implements sampleserver { sampleserverimpl() throws RemoteException { super(); } public int add(int a,int b) throws RemoteException { return(a+b); } public int sub(int a,int b) throws RemoteException { return(a-b); } public int mul(int a,int b) throws RemoteException { return(a*b); } public int div(int a,int b) throws RemoteException { return(a/b); } }; CSE 425 | Network programming | Fall - 2013
19
Class that implements the remote object • The sampleserverimpl() constructor just calls the superclass constructor that exports the object; that is, it creates a UnicastRemoteObject on some port and starts it listening for connections. The constructor is declared to throw RemoteException because the UnicastRemoteObject constructor can throw that exception. • Next, we need to write a server that makes the calculator (in this example sampleserver interface) remote object available to the world.
CSE 425 | Network programming | Fall - 2013
20
Locating remote objects • How does the caller get a reference to the remote object, i.e. stub? • One approach is to use a distributed Naming Service: – Associate a unique name with a remote object and bind the name to the object at the Naming Service. • The name must be unique in current context. • The record typically includes name, class name, object reference • The object reference contains location information.
– The object name is used by the client to lookup the Naming Service for the object reference (stub). – Problem of the primary reference: How does the client locate the Naming Service?
• Another way to get a reference to a remote object is to get it as a parameter or a return in remote method invocation CSE 425 | Network programming | Fall - 2013
21
Server class import java.rmi.*; import java.rmi.server.*; import java.net.*; public class server { public static void main(String args[])throws Exception { try { sampleserverimpl server = new sampleserverimpl(); Naming.rebind("rmi://localhost:4000/SERVER", server); System.out.println("SERVER IS WAITING"); } catch(Exception e) { System.out.println(e); } }}; CSE 425 | Network programming | Fall - 2013
22
Server class .. Example explained •
All it has is a main( ) method. It begins by entering a try block that catches RemoteException.
•
Then it constructs a new sampleserverimpl object and binds that object to the name "SERVER" using the Naming class to talk to the local registry.
•
A registry keeps track of the available objects on an RMI server and the names by which they can be requested. When a new remote object is created, the object adds itself and its name to the registry with the Naming.bind( )or Naming.rebind( ) method.
•
Clients can then ask for that object by name or get a list of all the remote objects that are available.
•
Note that there's no rule that says the name the object has in the registry has to have any necessary relation to the class name. For instance, we could have called this object “sample".
•
Indeed, there might be multiple instances of the same class all bound in a registry, each with a different name. After registering itself, the server prints a message on System.out signaling that it is ready to begin accepting remote invocations. If something goes wrong, the catch block prints a simple error message. CSE 425 | Network programming | Fall - 2013
23
Compiling the stubs •
RMI uses stub classes to mediate between local objects and the remote objects running on the server.
•
Each remote object on the server is represented by a stub class on the client. The stub contains the information in the Remote interface
•
Java 1.5 can sometimes generate these stubs automatically as they're needed, but in Java 1.4 and earlier, you must manually compile the stubs for each remote class. Even in Java 1.5, you still have to manually compile stubs for remote objects that are not subclasses of UnicastRemoteObject and are instead exported by calling UnicastRemoteObject.exportObject( ).
•
Fortunately, you don't have to write stub classes yourself: they can be generated automatically from the remote class's byte code using the rmic utility included with the JDK. To generate the stubs for the sampleserverimpl remote object, run rmic on the remote classes you want to generate stubs for. rmic sampleserverimpl is the command
• •
CSE 425 | Network programming | Fall - 2013
24
Starting the server • start rmiregistry • start rmiregistry & • start rmiregistry 4000
CSE 425 | Network programming | Fall - 2013
25
The client program import java.rmi.*; import java.rmi.server.*; import java.io.*; public class client { public static String s; public static void main(String args[])throws Exception { sampleserver remoteobject =(sampleserver)Naming.lookup("rmi://localhost:4000/SERVER"); InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); System.out.println("ENTER THE FIRST NUMBER:"); int a = Integer.parseInt(br.readLine()); System.out.println("ENTER THE SECOND NUMBER:"); int b = Integer.parseInt(br.readLine()); do{ System.out.println("*****ENTER YOUR CHOICE*****"); System.out.println("1.ADDITION"); System.out.println("2.SUBTRACTION"); System.out.println("3.MULTIPLICATION"); System.out.println("4.DIVISION"); CSE 425 | Network programming | Fall - 2013
26
The client program… int c = Integer.parseInt(br.readLine()); switch(c){ case 1 :int ans1 = remoteobject.add(a,b); System.out.println("THE REQUIRED ANSWER IS:"+ans1); break; case 2 :int ans2 = remoteobject.sub(a,b); System.out.println("THE REQUIRED ANSWER IS:"+ans2); break; case 3 :int ans3 = remoteobject.mul(a,b); System.out.println("THE REQUIRED ANSWER IS:"+ans3); break; case 4 :int ans4 = remoteobject.div(a,b); System.out.println("THE REQUIRED ANSWER IS:"+ans4); break; } System.out.println("DO YOU WANT TO CONTINUE?Y/N"); s = br.readLine(); } while(s.equals("y")||s.equals("Y")); } } CSE 425 | Network programming | Fall - 2013
27
Client program … • Notice that because the object that naming.lookup( ) returns is cast to a sampleserver , either the sampleserver.java or sampleserver.class file needs to be available on the local host.
CSE 425 | Network programming | Fall - 2013
28
Running the client • Run the commands of running a normal java program in the command promt.
CSE 425 | Network programming | Fall - 2013
29
Limitations of RMI and CORBA •
RMI isn't the final word in distributed object systems. Its biggest limitation is that you can call only methods written in Java. What if you already have an application written in some other language, such as C++, and you want to communicate with it?
•
The most general solution for distributed objects is CORBA, the Common Object Request Broker Architecture. CORBA lets objects written in different languages communicate with each other. Java hooks into CORBA through the Java-IDL. This goes beyond the scope of lecture; to find out about these topics, see:
•
Java-IDL (http://java.sun.com/products/jdk/idl/)
•
CORBA for Beginners (http://www.omg.org/gettingstarted/corbafaq.htm)
•
The CORBA FAQ list (http://www4.informatik.uni-erlangen.de/~geier/corba-faq/)
•
Client/Server Programming with Java and CORBA by Dan Harkey and Robert Orfali (Wiley)
CSE 425 | Network programming | Fall - 2013
30
CORBA (Common Object Request Broker Architecture) •
CORBA is a product of the OMG (Object Management Group)- a large consortium of companies devoted to improving remote object method invocation, developed the CORBA specification.
•
CORBA is not a specific implementation, but a specification for creating and using distributed objects. An individual implementation of this specification constitutes an ORB (Object Request Broker). Thus, it is not limited to a single language—CORBA services and clients can be written in a variety of languages.
•
Whereas RMI ORBs use a protocol called JRMP (Java Remote Method Protocol), CORBA ORBs use IIOP (Internet Inter-Orb Protocol), which is based on TCP/IP. It is IIOP that provides interoperability between ORBs from different vendors.
•
Another fundamental difference between RMI and CORBA is that, whereas RMI uses Java to define the interfaces for its objects, CORBA uses a special language called Interface Definition Language (IDL) to define those interfaces.
CSE 425 | Network programming | Fall - 2013
31
Architectural view of CORBA • CORBA is made up of a collection of objects and software applications that work together cooperatively. • A schema specification, written in IDL, describes the objects that are exported for remote usage. • An object request broker (ORB) implements the interface between remote object and software client. The ORB handles requests for an object's services, as well as sending a response.
CSE 425 | Network programming | Fall - 2013
32
Internet Inter-ORB Protocol (IIOP)
Client
Server
• Even though objects are treated as local objects, and you can invoke methods on them as if they were normal objects, there's an extra step involved. Acting as an intermediary between client and servant is the ORB. • Communication between client and servant occurs over the network via the Internet Inter-ORB Protocol (IIOP)
ORB
ORB Internet Inter-ORB Protocol (IIOP)
TCP/IP stack
CSE 425 | Network programming | Fall - 2013
33
CORBA service •
Within the CORBA architecture, software services are described by a schema and implemented by a servant.
•
The servant is a special piece of software that registers itself with a lookup service, so that other CORBA software can locate and access its services.
•
Typically a CORBA server will create a CORBA servant, and is then responsible for creating an ORB as the servant and registering the service for access by other clients.
CSE 425 | Network programming | Fall - 2013
34
CORBA client •
Clients, on the other hand, don't need to register with a name service. They do, however, want to use the name service to look up services
CSE 425 | Network programming | Fall - 2013
35