Presentation is loading. Please wait.

Presentation is loading. Please wait.

Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step.

Similar presentations


Presentation on theme: "Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step."— Presentation transcript:

1 Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step RMI registry aka naming service Security issues

2 Old style communication Sockets –application level protocol –encode decode exchanged message –error-prone RPC (Remote Procedure Call) –abstract communication interface –working on procedure call instead of directly with socket –using XDR represents arguments and return values to encode/decode –does not translate well into distributed object systems (different address spaces, match the semantics of object invocation etc.)

3 Java RMI A set of API allows developers to call remote object from different Java Virtual Machine pure java-to-java mechanism Not compliant with CORBA yet ??

4 Java RMI in Distributed Object Application Locate remote objects –using RMI’s simple naming facility, the rmiregistry –pass and return remote object references as part of normal operation Communicate with remote objects –RMI handles the communication between remote objects –programmers use remote communication just like starndard java method invocation Load class bytecodes for objects that are passed as parameters or return values –able to load an object’s code and transmitting it as data –pass pure java objects to remote objects

5 RMI Architecture

6 Transport Layer Setting up connections to remote address spaces Managing connections Monitoring connection “liveness” Listening for incoming calls Maintaining a table of remote objects Pass the connection back up the client- side Remote Reference layer

7 Transport Layer (Cont.) Connection-used to transfer data. For any given connection, there exists a channel, at least two endpoints, and a transport Endpoint-used to denote an address space or java VM. In its implementation, an endpoint can be mapped to its transport. That is, given and endpoint, a specific transport instance can be obtained. Channel-used as a conduct or virtual connection between two address spaces. As such, it is responsible for managing connections between the local address space and the remote address space.

8 Transport Layer (Cont.) Transport - used to manage a specific channel, There exists exactly one transport per pair of address spaces or endpoints. Given an endpoint to a remote address space, a transport establishes a channel between itself and that address space.

9 Remote Reference Layer Provides a constant interface to the stubs and skeletons. Manages communication between stubs/skeleton and lower-level masking the differences between server protocol implementations. Manages references to remote objects. Manages reconnection strategies if an object should become unavailable

10 Stubs and Skeletons An interface between an application layer and the remaining RMI system. Transmits data to the RRL Client invoking a method use a stub or proxy –initiates a connection with the remote VM containing the remote object –marshals (writes and transmits) the parameters to the remote VM –waits for the result of the method invocation –unmarshals (reads) the return value or exception returned –returns the value to the caller

11 Stubs and Skeletons (Cont.) A skeleton is a server-side entity that dispatches calls to the actual remote object implementation. –Unmarshals (reads) the parameters for the remote method –invokes the method on the actual remote object implementation –marshals (writes and transmits) the result (return value or exceptions) to the caller

12 Stubs Communication The stub receives the remote method invocation and initiates a call to the remote object. RRL returns special I/O stream, called a marshal stream, which used to communicate with the server’s RRL Stub makes the remote method call, passing any arguments to the stream. RRL passes the method’s return value to the stub The stub acknowledges to the RRL that the method call is complete.

13 User Code Method call Method results Stub Method call Method results Server Client

14 Sleleton Communication The skeleton unmashals (receives and interprets) any arguments from the stream, established by the RRL Skeleton makes the up-call to the actual remote object implementation. The skeleton marshal (interprets and sends) the return value of the call (or an exception, if one occurred) onto the I/O stream

15 Appletviewer http://isel.cp.eng.chula.ac.th/~ g41wnn/aindex.html

16 Creating an RMI Application Define the remote object signature as java interface (IDL in Corba) Create implementation classes for the interfaces Compile the interface and implementation classes Generate stub and skeleton classes using “rmic” command Create a server application Create client to access the remote objects Start the “rmiregistry” Start the server application Start the client application

17 Defining a Remote Interface creates public interface in order to be available remotely extends the interface java.rmi.Remote declares java.rmi.RemoteException for each methos to improve robustness declares arguments and return value no implementation in this phase

18 Defining a Remote Interface 1:public interface RMITest extends java.rmi.Remote { 2:String sendString(int sequenceNumber, String message) 3:throws java.rmi.RemoteException; 4:MyPacket sendPacket(MyPacket packet) 5:throws java.rmi.RemoteException; }

19 Defining a Serialized Object 1:public class MyPacket implements java.io.Serializable { 2:int sequenceNumber; 3:String message; 4: 5:public MyPacket(int seqNum, String msg) { 6:sequenceNumber = seqNumber; 7:message = msg; }

20 Creating an Implementation class implements remote interface(s) by extending remote interface(s) defined in previous section defines the constructor for the remote object provides implementation for each method that can be invoked remotely writes ALL remote methods extends java.rmi.server.UnicastRemoteObject to use RMI’s default sockets-based transport for communication UnicastRemoteObject exports the remote object by listening for incoming calls to the remote object on an anonymous port

21 Creating an Implementation class 1:import java.rmi.*; 2:import java.rmi.server.*; 3:public class RMITestImpl extends UnicastRemoteObject 4: implements RMITest { 5:private String name; 6: public RMITestImpl(String s) throw java.rmi.RemoteException {... 7:} 8:public String sendString(int callNum, String inMessage) {...} 9: public MyPacket sendPacket(MyPacket myPacket) {....} }

22 Compiling Interface, Impl and Stub/Skeleton javac *.java // compile RMITest.java และ RMITestImpl.java and MyPacket.java -> RMITest.class RMITestImpl.class MyPacket.java rmic RMITestImpl // generates stub and skeleton ->RMITestImpl_Stub.class RMITestImpl_Skel.class

23 Creating a Server Application creates a server using application or applet install a security manager (for application only, applet has its own security manager) –if (System.getSecurityManager() = null) { System.setSecurityManager(new RMISecurityManager()); } creates one or more remote object instance(s) binds the instances to RMI remote object registry using java.rmi.Naming.bind() or rebind() supplies registry’s host name and port number and instance name

24 Creating a Server Application 1:public class RMITestServer { 2:public static void main(String args[]) { 3:System.setSecurityManager(new RMISecurityManager());... 4: Naming.rebind(“//myHost/testServer”, obj);... }

25 Creating a Client creates an application or applet constructs a registry stub instance using hostname, port number and instance’s name as arguments in java.rmi.Naming.lookup() the registry returns implementation stub instance bound to that name receives remote implementation stub as its interface invokes remote methods just like normal method invocation

26 Creating a Client 1:public class RMITestApplet extends java.applet.Applet 2: implements Runnable { 3:RMITest remoteObject;...... 4:public void init() { 5: try { 6:String myHost = getCodeBase().getHost(); 7:remoteObject = (RMITest) Naming.lookup( 8:“//myHost:1099/testServer”);... * myHost --> your ip or isel.cp.eng.chula.ac.th

27 Starting the rmiregistry Simple server-side name server allows clients to get a reference to a remote object do supply CLASSPATH corresponding to remote object interfaces excecute rmiregistry using “rmiregistry ” default port is “1099” rmiregistry

28 Addendum for starting a server and a client Server –specifies java.rmi.server.codebase in order to be able to download stubs dynamically –makes sure your CLASSPATH is set properly java -Djava.securiy.policy=all.policy RMITestServer Client –some browsers (Netscape/IE) have their own security model those are not fully compatible with sun’s model –java plug-in (formly java activator) can be used in order to use sun’s security model appletviewer RMITest.html or open RMITest.html in a browser

29 New Features in Java2 New security model –policy is needed in order to specify which permissions are available for code by supplying java.security.policy location –simple policy file is as follow (not recommended) grant { permission java.security.AllPermission; // Allow everything } Remote Object Activation –RMI daemon (rmid) can be written to register information about remote object implementations –RMI daemon can use these information to activate the objects on demand rather than running all the time

30 More Information Java 2 SDK, Standard Edition Documentation Enterprise Features, Remote Method Invocation http://java.sun.com http://isel.cp.eng.chula.ac.th/~ dtb

31 Try This... How to write Java RMI programs –Server Side Creates a method for adding two integer. Returns the result to the client. –Client Side Try remote method call to the server. Passing two parameters as x and x+1 Calls the remote method 10 times increasing by 1 and beginning with 0


Download ppt "Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step."

Similar presentations


Ads by Google