Java-RMI Lab Outline • Let first builds a simple home-made framework • This is useful to understand the main issues • We see later how java-rmi works and how it solves the same issues Generic architecture server client object A proxy for B skeleton & dispatcher for B’s class Request remote object B Reply Communication Remote reference module module Communication module Local proxy: Remote methods appear as local Remote Reference Module: local-remote object map Dispatcher: Dispatch the request to a Skeleton Skeleton: Invoke the method on B; send back the reply Remote reference module servant Implementation We’d like something like this… client proxy for Counter object A Receive input Client Object Communication Remote reference module module Call method on the proxy Implementation Proxy… object A client proxy for Counter public int inc(int value){ //marshal value //prepare a request //send the request to the Server //The address is in the //Remote Reference Module //… on in a remote reference data structure Communication Remote reference module module } public int dec(int value){ } Implementation Communication module… Dispatch to the Skeleton server skeleton & dispatcher for B’s class remote object B Initialize Register an object Communication Module (uses Object Table) Communication module Remote reference module servant Receive request Implementation Skeleton Dispatch to the Object server skeleton & dispatcher for B’s class Communication module remote object B Remote reference module Initialize (bound to a Counter) servant Skeleton & Dispatcher Receive request Send the reply Implementation details • See RMI-LAB on the web site • Exercise: add a new object… Lesson learned • • • • Key (and borrowing) aspects Managing communication Managing remote reference Implements support modules (proxy, skeleton) Java-rmi solution • Managing communication – Embedded into the JVM • Managing remote reference – Rmiregistry, remote reference layer • Implements support modules (proxy, skeleton) – Proxy: automatically generated from the code – Skeleton: no longer needed thanks to ‘reflection’ (see later) Lab1: Remote counter import java.rmi.*; //Import rmi API public interface Counter extends Remote //makes Counter a remote interface { int inc(int i) throws RemoteException; //manage or throws this exception…. int dec(int i) throws RemoteException; } Step2: interface implementation import java.rmi.*; public class Counter_impl implements Counter { private int counter; public Counter_impl() throws RemoteException {counter = 0;} public int inc(int i) throws RemoteException {counter++;return counter;} public int dec(int i) throws RemoteException {counter--;return counter;} } import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Server { public static void main(String args[]) throws RemoteException { //Create a remote object.. Counter c = new Counter_impl() ; Counter stub = (Counter)UnicastRemoteObject.exportObject(c, 0); //bind "counter" to the stub Registry registry = LocateRegistry.getRegistry(); registry.rebind("counter", stub); System.out.println("Counter bound"); } } Export object = It can receive requests Client import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class Client { public static void main(String args[]) { try{ Registry registry = LocateRegistry.getRegistry(args[0]); Counter c = (Counter) registry.lookup("counter"); int i = c.inc(10); System.out.println(i); } catch (Exception e){e.printStackTrace();} } } Running the example • On the same machine via classpath – Run rmiregisrty: rmiregistry (make sure CLASSPATH is unset) – Run the server: java -classpath . Server – Run the client: java -classpath . Client localhost Class downloading • JAVA-RMI allows to download the definition of an object's class if the class is not defined in the receiver's Java virtual machine. • Classes definitions are typically made network accessible through a web server Class downloading • Class downloading are controlled by a Security manager • Without a security manager installed, RMI will not download classes, other than from the local class path. • This restriction ensures that the operations performed by downloaded code are subject to a security policy. Installing Security Manager … if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); } … codebase = Where classes are network accessible java -Djava.rmi.server.codebase="http://xxxx. " -Djava.security.policy=“java.policy” policy file grant { permission java.security.AllPermission; }; LAB2 • Account example LAB3 • Shared whiteboard