Network Programming Paradigms � Sockets programming: design a protocol CSCE 515: first, then implement clients and servers Computer Network that support the protocol. Programming � RMI: Develop an application, then move ------ Remote Method Invocation some objects to remote machines. reference: Dave Hollinger Wenyuan Xu � Not concerned with the details of the actual communication between processes – Department of Computer Science and Engineering everything is just method calls. University of South Carolina 2 Netprog: Java RMI Call Semantics Finding Remote Objects � Method Call Semantics – what does it mean to � It would be awkward if we needed to include a make a call to a method? hostname, port and protocol with every remote method invocation. � How many times is the method run? � How do we know the method ran at all? � RMI provides a Naming Service through the RMI Registry that simplifies how programs specify the location of remote objects. � RMI does a great job of providing natural call semantics for remote objects/methods. � This naming service is a JDK utility called rmiregistry that runs at a well known address (by � Simply a few additional Exceptions that you need to default). handle. 3 4 Netprog: Java RMI Netprog: Java RMI RMI Adds a few layers Remote Object References � The client acquires a reference to a remote object. � This part is different from creating a local object. Client App. Server App. Stubs Skeleton � The client calls methods on the remote object Remote Reference Remote Reference � No (syntactic) difference! � Just need to worry about a few new exceptions. Transport Transport 5 6 Netprog: Java RMI Netprog: Java RMI
Overview of RMI Programming Java Interfaces � Similar to Class � Define an interface that declares the methods that � No implementation! All methods are will be available remotely. abstract (virtual for C++ folks). � The server program must include a class that � Everything is public. implements this interface . � No fields defined, just Methods. � The server program must create a remote object and register it with the naming service. � No constructor � an Interface is an API that can be � The client program creates a remote object by asking implemented by a Class. the naming service for an object reference. 7 8 Netprog: Java RMI Netprog: Java RMI Interfaces and Inheritence Sample Interface public interface Shape { � In Java a class can only extend a single superclass (single inheritence). public getArea(); public draw(); � A class can implement any number of public fill(Color c); interfaces. } � end result is very similar to multiple inheritence. 9 10 Netprog: Java RMI Netprog: Java RMI Implementing an Interface Server Details – extending Remote public class Circle implements Shape { private double radius; � Create an interface the extends the private Point center; java.rmi.Remote interface. � This new interface includes all the public methods // define a constructor and other that will be available as remote methods. // methods import java.rmi.*; public interface MyRemote extends Remote { // MUST define the methods: public int foo(int x) throws RemoteException; // getArea(); public String blah(int y) throws RemoteException; // draw(); . . . // public fill(Color c); } } 11 12 Netprog: Java RMI Netprog: Java RMI
Server Details – Implementation How the interface will be used Class Remote Interface Class RemoteServer � Create a class that implements the provides methods extends extends interface. needed by Your Interface UnicastRemoteObject � The class should also extend UnicastRemoteObject * implements � This class needs a constructor that throws extends RemoteException ! Class for your Remote � This class is now used by rmic to create Object the stub and skeleton code. *It doesn’t have to extend UnicastRemoteObject , there is another way… 13 14 Netprog: Java RMI Netprog: Java RMI Remote Object Implementation Class Generating stubs and skeleton public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote { � Compile the remote interface and public MyRemoteImpl() throws RemoteException implementation: {} > javac MyRemote.java MyRemoteImpl.java public int foo(int x) { return(x+1); � Use rmic to generate MyRemoteImpl_stub.class, } MyRemoteImpl_skel.class public String blah(int y) { return(“Your number is “ + y); > rmic MyRemoteImpl } } 15 16 Netprog: Java RMI Netprog: Java RMI Client Details Server Detail – main() � The client needs to ask the naming service for a � The server main() needs to: reference to a remote object. � create a remote object. � The client needs to know the hostname or IP address � register the object with the Naming service. of the machine running the server. � The client needs to know the name of the remote object. public static void main(String args[]) { � The naming service uses URLs to identify try { remote objects. MyRemoteImpl r = new MyRemoteImpl(); Naming.bind(“joe”,r); } catch (RemoteException e) { . . . 17 18 Netprog: Java RMI Netprog: Java RMI
Using The Naming service Getting a Remote Object � Naming.lookup() method takes a string try { parameter that holds a URL indicating the Object o = remote object to lookup. Naming.lookup(“rmi://localhost/ReMath”); rmi://hostname/objectname MyRemote r = (MyRemote) o; // . . . Use r like any other Java object! � Naming.lookup() returns an Object ! } catch (RemoteException re) { � Naming.lookup() can throw . . . � RemoteException } catch (MalformedURLException up) { � MalformedURLException throw up; } 19 20 Netprog: Java RMI Netprog: Java RMI Starting the Server Sample Code � First you need to run the Naming service � There is sample RMI code on the course server: homepage: rmiregistry & � RemoteMathImpl: remote integer arithmetic � Now run the server: java ServerMain 21 22 Netprog: Java RMI Netprog: Java RMI
Recommend
More recommend