Presentation is loading. Please wait.

Presentation is loading. Please wait.

Block 8: Remote Method Invocation (RMI)

Similar presentations


Presentation on theme: "Block 8: Remote Method Invocation (RMI)"— Presentation transcript:

1 Block 8: Remote Method Invocation (RMI)
Jin Sa 1/21/2020 Client-server programming

2 Client-server programming
Outline Message Passing vs. Distributed Objects Distributed Object Paradigm An Archetypal Distributed Objects System Distributed Object System Java Remote Method Invocation The Java RMI Architecture The API for the Java RMI Remote Interface The Remote Interface Implementation Stub and Skeleton The Object Server The RMI registry The Client-side Software Steps for building an RMI application Comparison of the RMI and the socket APIs The HelloWorld example RMI security 1/21/2020 Client-server programming

3 Message Passing vs. Distributed Objects
1/21/2020 Client-server programming

4 Client-server programming
Message Passing The message-passing paradigm is a natural model for distributed computing, in the sense that it mimics inter-human communications. It is an appropriate paradigm for network services where processes interact with each other through the exchanges of simple messages. However, the abstraction provided by this paradigm does not meet the needs of the complexity of sophisticated network applications. 1/21/2020 Client-server programming

5 Message Passing (skip)
The message-passing paradigm is data-oriented. Each message contains data marshaled in a mutually agreed upon format, and is interpreted as a request or response according to the protocol. The receiving of each message triggers an action in the receiving process. It is inadequate for complex applications involving a large mix of requests and responses. In such an application, the task of interpreting the messages can become overwhelming. 1/21/2020 Client-server programming

6 Local Objects vs. Distributed Objects
Local objects are those whose methods can only be invoked by a local process, a process that runs on the same computer on which the object exists. A remote object is one whose methods can be invoked by a remote process, a process running on a computer connected via a network to the computer on which the object exists. 1/21/2020 Client-server programming

7 Distributed Object Paradigm
Distributed object paradigm is action-oriented: the focus is on the invocation of the operations, while the data passed takes on a secondary role. 1/21/2020 Client-server programming

8 The Distributed Objects Paradigm
1/21/2020 Client-server programming

9 The Distributed Object Paradigm (skip)
The distributed object paradigm is based on objects that exist in a distributed system. Network resources are represented by distributed objects. To request service for a network resource, a process invokes one of its methods, passing data as parameters to the method. The method is executed on the remote host, and the response is sent back to the requesting process as a return value. 1/21/2020 Client-server programming

10 The Distributed Object Paradigm
1/21/2020 Client-server programming

11 The Distributed Object Paradigm (skip)
A process running on host A makes a method call to a distributed object on host B, passing with the call data for the parameters, if any. The method call invokes an action performed by the method on host B, and a return value, if any, is passed from host B to host A. A process which makes use of a distributed object is said to be a client process of that object, and the methods of the object are called remote methods to the client process. 1/21/2020 Client-server programming

12 An Archetypal Distributed Objects System
1/21/2020 Client-server programming

13 Distributed Object System
A distributed object is provided, or exported, by a process, here called the object server. A facility, here called an object registry, must be present in the system architecture for the distributed object to be registered. To access a distributed object, a process –an object client – looks up the object registry for a reference to the object. This reference is used by the object client to make calls to the methods. 1/21/2020 Client-server programming

14 Distributed Object System
Logically, the object client makes a call directly to a remote method. In reality, the call is handled by a software component, called a client proxy, which interacts which the software on the client host that provides the runtime support for the distributed object system. The runtime support is responsible for the interprocess communication needed to transmit the call to the remote host, including the marshalling of the argument data that needs to be transmitted to the remote object. 1/21/2020 Client-server programming

15 Distributed Object System
A similar architecture is required on the server side, where the runtime support for the distributed object system handles the receiving of messages and the unmarshalling of data, and forwards the call to a software component called the server proxy. The server proxy interfaces with the distributed object to invoke the method call locally, passing in the unmarshalled data for the arguments. 1/21/2020 Client-server programming

16 Distributed Object System
The method call results in the performance of some tasks on the server host. The outcome of the execution of the method, including the marshalled data for the return value, is forwarded by the server proxy to the client proxy, via the runtime support and network support on both sides. 1/21/2020 Client-server programming

17 Client-server programming
RMI The distributed object paradigm has been widely adopted in distributed applications, for which a large number of mechanisms based on the paradigm are available. We will concentrate on Java Remote Method Invocation (RMI). 1/21/2020 Client-server programming

18 Java Remote Method Invocation
1/21/2020 Client-server programming

19 The Java RMI Architecture
1/21/2020 Client-server programming

20 Java Remote Method Invocation
Using RMI, an object server exports a remote object and registers it with a directory service. The object provides remote methods, which can be invoked in client programs. 1/21/2020 Client-server programming

21 Client-server programming
Java RMI API 1/21/2020 Client-server programming

22 Client-server programming
The API for the Java RMI The Remote Interface The Server-side Software The Remote Interface Implementation Stub and Skeleton Generations (not needed if using JDK 1.5) The Object Server The Client-side Software Stub The client program 1/21/2020 Client-server programming

23 Remote Interface Template
import java.rmi.* public interface SomeInterface extends Remote { // signature of first remote method public String someMethod1( ) throws RemoteException; … … } // end interface 1/21/2020 Client-server programming

24 Client-server programming
Remote interface The java.rmi.Remote Exception must be listed in the throws clause of each method signature. This exception may occur during the processing of a remote method call. Causes of such exceptions include exceptions that may occur during interprocess communications, such as access failures and connection failures, as well as problems unique to remote method invocations, including errors resulting from the object, the stub, or the skeleton not being found. 1/21/2020 Client-server programming

25 The Server-side Software
An object server creates some remote objects that implement each of the remote methods specified in the interface, registers an object which contains the implementation with a directory service. 1/21/2020 Client-server programming

26 The Remote Interface Implementation
A class which implements the remote interface should be provided. The syntax is similar to a class that implements a local interface. 1/21/2020 Client-server programming

27 The Remote Interface implementation template
import java.rmi.*; import java.rmi.server.*; public class SomeImpl extends UnicastRemoteObject implements SomeInterface { public SomeImpl() throws RemoteException { super( ); } public String someMethod1( ) throws RemoteException { // code to be supplied … … } // end class 1/21/2020 Client-server programming

28 Stub and Skeleton Generations -- skip this if using JDK1.5
In RMI, each distributed object requires a proxy each for the object server and the object client, known as the object’s skeleton and stub respectively. These proxies are generated from the implementation of a remote interface using the RMI compiler rmic. For example: rmic SomeImpl Two proxy files will be generated, each prefixed with the implementation class name: SomeImpl_skel.class SomeImpl_stub.class If JDK 1.5 is used, no need to do this. 1/21/2020 Client-server programming

29 The stub file for the object -- skip if using JDK1.5
The stub file for the object and the remote interface file are required for the client program to compile. A copy of each file may be provided to the object client by hand or by “stub downloading” which allows a stub file to be obtained by a client dynamically. 1/21/2020 Client-server programming

30 Client-server programming
The Object Server The object server class is a class whose code instantiates and exports an object of the remote interface implementation.   1/21/2020 Client-server programming

31 The Object Server template
import java.rmi.*; public class SomeServer { public static void main(String args[]) { try{ … … SomeImpl exportedObj = new SomeImpl(); startRegistry(RMIPort); registryURL = "rmi://localhost:"+RMIPort+"/some"; Naming.rebind(registryURL, exportedObj); }// end try } // end main 1/21/2020 Client-server programming

32 The Object Server template
private static void startRegistry(int RMIPortNum) throws RemoteException{ try { Registry registry= LocateRegistry.getRegistry(RMIPortNum); registry.list( ); } catch (RemoteException ex) { // No valid registry at that port; create one LocateRegistry.createRegistry(RMIPortNum); } // end startRegistry 1/21/2020 Client-server programming

33 Client-server programming
The Object Server In our object server template, the code for creating and exporting an object is a follows: SomeImpl exportedObj = new SomeImpl(); registryURL = "rmi://localhost:" + RMIport + "/some"; Naming.rebind(registryURL, exportedObj); The Naming class provides methods for storing and obtaining references from the registry. The rebind method allow an object reference to be stored in the registry with a URL in the form of rmi://<hostname>:<portnumber>/<referencename> The reference name is a name of your choice, and should be unique in the registry. 1/21/2020 Client-server programming

34 Client-server programming
The RMI Registry The RMIRegistry is a server located at port 1099 by default. It registers remote objects and provides naming services for looking up objects. 1/21/2020 Client-server programming

35 Client-server programming
The RMI Registry The RMIRegistry can be started dynamically in the server class: import java.rmi.registry.LocateRegistry; LocateRegistry.createRegistry(RMIport); Alternatively, an RMI registry can be activated by hand as follows: rmiregistry <port number> If no port number is specified, port number 1099 is assumed. 1/21/2020 Client-server programming

36 Client-server programming
The Object Server When an object server is executed, the exporting of the remote object causes the server process to begin to listen and wait for clients to connect and request the service of the object. An RMI object server is a concurrent server: each request from an object client is serviced using a separate thread of the server. 1/21/2020 Client-server programming

37 The Client-side Software
The program for the client class is like any other Java class. The syntax needed for RMI involves locating the RMI Registry in the server host, looking up the remote reference for the remote object; the reference can then be cast to the remote interface class and the remote methods invoked. 1/21/2020 Client-server programming

38 The Client-side Software template
public class SomeClient { public static void main(String args[]) { try { … … String registryURL= "rmi://localhost:" + RMIport + "/some"; SomeInterface h = (SomeInterface)Naming.lookup(registryURL); String message = h.method1(); } // end try catch (Exception e) { …… } } //end main }//end class 1/21/2020 Client-server programming

39 Looking up the remote object
The lookup method of the Naming class is used to retrieve the object reference in the registry. Note that the retrieved reference must be casted to the remote interface class. SomeInterface h = (SomeInterface)Naming.lookup(registryURL); 1/21/2020 Client-server programming

40 Invoking the Remote Method
The remote interface reference can be used to invoke any of the methods in the remote interface, as in the example: String message = h.method1(); System.out.println(message); Note that the syntax for the invocation of the remote methods is the same as for local methods. 1/21/2020 Client-server programming

41 Steps for building an RMI application
1/21/2020 Client-server programming

42 Steps for developing the server-side software
Specify and compile the remote interface in SomeInterface.java. Implement and compile the interface in SomeImpl.java (Skip if using JDK 1.5) Use the RMI compiler rmic to process the implementation class and generate the stub file and skeleton file for the remote object: rmic SomeImpl The files generated can be found in the directory as SomeImpl_Skel.class and SomeImpl_Stub.class. (not visible if using JDK1.5.) Steps 2 and 3 must be repeated each time that a change is made to the interface implementation. Create the object server program SomeServer.java. Run the object server 1/21/2020 Client-server programming

43 Steps for developing the client-side software
Obtain a copy of the remote interface class file. (Skip if using JDK1.5) Obtain a copy of the stub file for the implementation of the interface: SomeImpl_Stub.class. Develop and compile the client program SomeClient.java. Run the client. 1/21/2020 Client-server programming

44 Placement of files for a RMI application (skip)
1/21/2020 Client-server programming

45 Comparison of the RMI and the socket APIs
The remote method invocation API is an efficient tool for building network applications. It can be used in lieu of the socket API in a network application. 1/21/2020 Client-server programming

46 Comparison of the RMI and the socket APIs
The socket API is closely related to the operating system, and hence has less execution overhead. For applications which require high performance, this may be a consideration. The RMI API provides the abstraction which eases the task of software development. Programs developed with a higher level of abstraction are more comprehensible and hence easier to debug. 1/21/2020 Client-server programming

47 The HelloWorld Example
1/21/2020 Client-server programming

48 Source files for the Hello application
HelloInterface.java HelloImpl.java HelloClient.java HelloServer.java 1/21/2020 Client-server programming

49 Client-server programming
RMI security 1/21/2020 Client-server programming

50 Client-server programming
RMI Security Manager Since RMI involves access to/from a foreign host, it is important for both the server and the client to protect its system from malicious access. The RMISecurityManager is a class provided in Java, and can be instantiated in both the client and the server for limiting access privileges. System.setSecurityManager( new RMISecurityManager( )); The RMI security manager object oversees all security-sensitive actions during the execution of the program. 1/21/2020 Client-server programming

51 Client-server programming
RMI Security Manager An RMI security manager uses a security policy file to determine the access privileges. By default, there a systemwide security policy file. It is very restrictive. We can override it by specifying an alternative security policy file. 1/21/2020 Client-server programming

52 Examples of Java security policy
grant { // permits socket access to all common TCP ports, // permission java.net.SocketPermission "*: ", "connect,accept,resolve"; // permits socket access to port 80, // the default HTTP port – needed by client to // contact an HTTP server for stub downloading "*:80", "connect"; // permits to do anything // permission java.security.Allpermission; }; 1/21/2020 Client-server programming

53 The java security policy file
We can put a copy of this file in the server directory and the client directory. When running the server and the client, we need to specify the name of the policy file, e.g. java -Djava.security.policy=java.policy SomeClient 1/21/2020 Client-server programming

54 The HelloWorld Example – revisited See slide 44, slide 48
1/21/2020 Client-server programming

55 Client-server programming
HelloServer try{ System.setSecurityManager( new RMISecurityManager()); startRegistry(portNum); HelloImpl exportedObj=new HelloImpl(); registryURL = "rmi://localhost:" +portNum + "/hello"; Naming.rebind(registryURL,exportedObj); …… 1/21/2020 Client-server programming

56 Client-server programming
HelloClient Try{ System.setSecurityManager( new RMISecurityManager()); int portNum=5555; String hostName ="localhost"; String registryURL = “rmi://" + hostName+ ":" + portNum + "/hello"; HelloInterface h = (HelloInterface)Naming.lookup(registryURL); String message = h.sayHello("This is Client"); …… } 1/21/2020 Client-server programming

57 Client-server programming
java.policy file In this example, we give permission to do anything. grant { permission java.security.AllPermission; }; Place a copy of a java.policy file in the same directory as your class on the server and the client. 1/21/2020 Client-server programming

58 Running RMI applications with the security policy file
When running the server and the client, we need to specify the following command option to ensure the JVM knows which security policy file to use. Java –Djava.security.policy=<name of policy file> program 1/21/2020 Client-server programming

59 Setting Security manager in NetBeans
Security policy file: put a copy of this file in the project directory on the server and the client. Specify the command option in the “project properties” window. Click on “run”, in the filed VM options, enter –Djava.security.policy=<name of policy file> 1/21/2020 Client-server programming

60 Client-server programming
Revised steps for developing the server-side software with security using NetBeans with JDK1.5 Open a project on the server. Specify and compile the remote interface in SomeInterface.java. Implement and compile the interface in SomeImpl.java Create the object server program SomeServer.java. Define a java security policy file, and put in the project folder. Run the object server with the follow JVM option -Djava.security.policy=<secourity policy file>. This is specified in the “Run” category of the project properties window. 1/21/2020 Client-server programming

61 Client-server programming
Revised steps for developing the client-side software with security using Netbeans with JDK1.5 Open a project on the client. Obtain a copy of the remote interface file and compile it to generate the interface class file Develop and compile the client program SomeClient.java. Define a java security policy file, and put in the project folder. Run the client programme with the follow JVM option -Djava.security.policy=<secourity policy file>. This is specified in the “Run” category of the project properties window. 1/21/2020 Client-server programming

62 Client-server programming
Revised Hello program In RMIClient2 and RMIServer2. 1/21/2020 Client-server programming

63 Client-server programming
An exercise on RMI 1/21/2020 Client-server programming

64 Example 2 adding two numbers
Client invokes a remote method which takes two integers as parameters and returns the sum Server creates and exports a remote object which has a method returning the sum of two numbers 1/21/2020 Client-server programming

65 Developing the two numbers example
List what you need to develop on the server side: the remote interface in TwoNumIF.java. Implement the interface in TwoNumImpl.java Create the object server program TwoNumServer.java. Remember to instantiate a security manager. Define a java security policy file, and put in the project folder. Generate the proxy (No need if using JDK 1.5) Run the object server with the follow JVM option -Djava.security.policy=<java.policy> policy file>. 1/21/2020 Client-server programming

66 Developing the two numbers example
List what you need to develop on the client side: Obtain a copy of the remote interface file and compile it to generate the interface class file Develop and compile the client program TwoNumClient.java. Remember to instantiate a security manager. Define a java security policy file, and put in the project folder. Generate the proxy (No need if using JDK 1.5) Run the client programme with the follow JVM option -Djava.security.policy=java.policy 1/21/2020 Client-server programming

67 Client-server programming
Server side: TwoNumIF public interface TwoNumIF extends Remote { public int add2(int n1, int n2) throws RemoteException; } //end interface 1/21/2020 Client-server programming

68 Server side: implementation of the interface
public class TwoNumImpl extends UnicastRemoteObject implements TwoNumIF { public TwoNumImpl() throws RemoteException { super( ); } public int add2(int x, int y) throws RemoteException { return (x+y); } // end class 1/21/2020 Client-server programming

69 Server object: the key tasks
Set a security manager Start an RMI registry Create a remote object Export the object by binding the remote object with a string the RMI registry 1/21/2020 Client-server programming

70 Server side: server object
System.setSecurityManager( new RMISecurityManager()); startRegistry(portNum); TwoNumImpl exportedObj = new TwoNumImpl(); registryURL = "rmi://localhost:" + portNum + "/addtwo"; Naming.rebind(registryURL, exportedObj); 1/21/2020 Client-server programming

71 Client program: key tasks
Set security manager Look up the registry to obtain a reference on the remote object Remember to cast it to the appropriate object Get two numbers from the user Invoke the add2 method of the remote object 1/21/2020 Client-server programming

72 Client side: client program
System.setSecurityManager(new RMISecurityManager()); String registryURL = "rmi://" + hostName+ ":" + portNum + "/addtwo"; twonumserver.TwoNumIF two = (twonumserver.TwoNumIF)Naming.lookup(registryURL); Scanner in = new Scanner(System.in); System.out.println("Enter two numbers: "); int n1 = in.nextInt(); int n2=in.nextInt(); // invoke the remote method int sum = two.add2(n1, n2); 1/21/2020 Client-server programming

73 Example 2 adding two numbers
Server side: Open project “twonumserver” on the server. Specify and compile the remote-server interface in TwoNumIF.java. Implement and compile the interface in TwoNumImpl.java Create the object server program TwoNumServer.java. Remember to instantiate a security manager. Define a java security policy file, and put in the project folder. Run the object server with the follow JVM option -Djava.security.policy=<secourity policy file>. This is specified in the “Run” category of the project properties window. 1/21/2020 Client-server programming

74 Example 2 adding two numbers
Open project “twonumclient” on the client. Obtain a copy of the remote interface file and compile it to generate the interface class file Develop and compile the client program TwoNumClient.java. Remember to instantiate a security manager. Define a java security policy file, and put in the project folder. Run the client programme with the follow JVM option -Djava.security.policy=<secourity policy file>. This is specified in the “Run” category of the project properties window. 1/21/2020 Client-server programming

75 Client-server programming
Review questions Compare and contrasting message passing paradigm and the distributed object paradigm Describe the Java RMI architecture. What are the main components on the server side and the client side? What is the role of the RMI registry? 1/21/2020 Client-server programming

76 Slides for students to complete in the class
1/21/2020 Client-server programming

77 Developing the two numbers example
List what you need to develop on the server side: 1/21/2020 Client-server programming

78 Developing the two numbers example
List what you need to develop on the client side: 1/21/2020 Client-server programming

79 Client-server programming
Server side: TwoNumIF public interface TwoNumIF extends Remote { } //end interface 1/21/2020 Client-server programming

80 Server side: implementation of the interface
public class TwoNumImpl extends UnicastRemoteObject implements TwoNumIF { public TwoNumImpl() throws RemoteException { super( ); } } // end class 1/21/2020 Client-server programming

81 Server object: the key tasks
1/21/2020 Client-server programming

82 Server side: server object
1/21/2020 Client-server programming

83 Client program: key tasks
1/21/2020 Client-server programming

84 Client side: client program
1/21/2020 Client-server programming


Download ppt "Block 8: Remote Method Invocation (RMI)"

Similar presentations


Ads by Google