Block 8 Remote Method Invocation RMI Jin Sa

Block 8: Remote Method Invocation (RMI) Jin Sa 6/4/2021 Client-server programming 1

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 Hello. World example • RMI security 6/4/2021 Client-server programming 2

Message Passing vs. Distributed Objects 6/4/2021 Client-server programming 3

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. 6/4/2021 Client-server programming 4

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. 6/4/2021 Client-server programming 5

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. 6/4/2021 Client-server programming 6

Distributed Object Paradigm Distributed object paradigm is actionoriented: the focus is on the invocation of the operations, while the data passed takes on a secondary role. 6/4/2021 Client-server programming 7

The Distributed Objects Paradigm 6/4/2021 Client-server programming 8

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. 6/4/2021 Client-server programming 9

The Distributed Object Paradigm 6/4/2021 Client-server programming 10

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. 6/4/2021 Client-server programming 11

An Archetypal Distributed Objects System 6/4/2021 Client-server programming 12

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. 6/4/2021 Client-server programming 13

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. 6/4/2021 Client-server programming 14

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. 6/4/2021 Client-server programming 15

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. 6/4/2021 Client-server programming 16

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). 6/4/2021 Client-server programming 17

Java Remote Method Invocation 6/4/2021 Client-server programming 18

The Java RMI Architecture 6/4/2021 Client-server programming 19

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. 6/4/2021 Client-server programming 20

Java RMI API 6/4/2021 Client-server programming 21

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 – The Remote Interface – Stub – The client program 6/4/2021 Client-server programming 22

Remote Interface Template import java. rmi. * public interface Some. Interface extends Remote { // signature of first remote method public String some. Method 1( ) throws Remote. Exception; … … } // end interface 6/4/2021 Client-server programming 23

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. 6/4/2021 Client-server programming 24

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. 6/4/2021 Client-server programming 25

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. 6/4/2021 Client-server programming 26

The Remote Interface implementation template import java. rmi. *; import java. rmi. server. *; public class Some. Impl extends Unicast. Remote. Object implements Some. Interface { public Some. Impl() throws Remote. Exception { super( ); } public String some. Method 1( ) throws Remote. Exception { // code to be supplied } … … } // end class 6/4/2021 Client-server programming 27

Stub and Skeleton Generations -- skip this if using JDK 1. 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 Some. Impl Two proxy files will be generated, each prefixed with the implementation class name: Some. Impl_skel. class Some. Impl_stub. class • If JDK 1. 5 is used, no need to do this. 6/4/2021 Client-server programming 28

The stub file for the object -- skip if using JDK 1. 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. 6/4/2021 Client-server programming 29

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

The Object Server template import java. rmi. *; public class Some. Server { public static void main(String args[]) { try{ … … Some. Impl exported. Obj = new Some. Impl(); start. Registry(RMIPort); registry. URL = "rmi: //localhost: "+RMIPort+"/some"; Naming. rebind(registry. URL, exported. Obj); }// end try } // end main 6/4/2021 Client-server programming 31

The Object Server template private static void start. Registry(int RMIPort. Num) throws Remote. Exception{ try { Registry registry= Locate. Registry. get. Registry(RMIPort. Num); registry. list( ); } catch (Remote. Exception ex) { // No valid registry at that port; create one Registry registry= Locate. Registry. create. Registry(RMIPort. Num); } } // end start. Registry 6/4/2021 Client-server programming 32

The Object Server • • • In our object server template, the code for creating and exporting an object is a follows: Some. Impl exported. Obj = new Some. Impl(); registry. URL = "rmi: //localhost: " + RMIport + "/some"; Naming. rebind(registry. URL, exported. Obj); 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. 6/4/2021 Client-server programming 33

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. 6/4/2021 Client-server programming 34

The RMI Registry • The RMIRegistry can be started dynamically in the server class: import java. rmi. registry. Locate. Registry; … Locate. Registry. create. Registry(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. 6/4/2021 Client-server programming 35

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. 6/4/2021 Client-server programming 36

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. 6/4/2021 Client-server programming 37
![The Client-side Software template public class Some. Client { public static void main(String args[]) The Client-side Software template public class Some. Client { public static void main(String args[])](http://slidetodoc.com/presentation_image_h2/521f96f41714223d6140f02388d12d77/image-38.jpg)
The Client-side Software template public class Some. Client { public static void main(String args[]) { try { … … String registry. URL= "rmi: //localhost: " + RMIport + "/some"; Some. Interface h = (Some. Interface)Naming. lookup(registry. URL); String message = h. method 1(); … … } // end try catch (Exception e) { …… } } //end main }//end class 6/4/2021 Client-server programming 38

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. Some. Interface h = (Some. Interface)Naming. lookup(registry. URL); 6/4/2021 Client-server programming 39

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. method 1(); System. out. println(message); • Note that the syntax for the invocation of the remote methods is the same as for local methods. 6/4/2021 Client-server programming 40

Steps for building an RMI application 6/4/2021 Client-server programming 41

Steps for developing the serverside software 1. 2. 3. 4. 5. Specify and compile the remote interface in Some. Interface. java. Implement and compile the interface in Some. Impl. 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 Some. Impl The files generated can be found in the directory as Some. Impl_Skel. class and Some. Impl_Stub. class. (not visible if using JDK 1. 5. ) Steps 2 and 3 must be repeated each time that a change is made to the interface implementation. Create the object server program Some. Server. java. Run the object server 6/4/2021 Client-server programming 42

Steps for developing the client-side software 1. Obtain a copy of the remote interface class file. 2. (Skip if using JDK 1. 5) Obtain a copy of the stub file for the implementation of the interface: Some. Impl_Stub. class. 3. Develop and compile the client program Some. Client. java. 4. Run the client. 6/4/2021 Client-server programming 43

Placement of files for a RMI application (skip) 6/4/2021 Client-server programming 44

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. 6/4/2021 Client-server programming 45

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. 6/4/2021 Client-server programming 46

The Hello. World Example 6/4/2021 Client-server programming 47

Source files for the Hello application • • Hello. Interface. java Hello. Impl. java Hello. Client. java Hello. Server. java 6/4/2021 Client-server programming 48

6/4/2021 Client-server programming 49

6/4/2021 Client-server programming 50

6/4/2021 Client-server programming 51

RMI security 6/4/2021 Client-server programming 52

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 RMISecurity. Manager is a class provided in Java, and can be instantiated in both the client and the server for limiting access privileges. System. set. Security. Manager( new RMISecurity. Manager( )); • The RMI security manager object oversees all security-sensitive actions during the execution of the program. 6/4/2021 Client-server programming 53

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. 6/4/2021 Client-server programming 54

Examples of Java security policy grant { // permits socket access to all common TCP ports, // permission java. net. Socket. Permission "*: 1024 -65535", "connect, accept, resolve"; // permits socket access to port 80, // the default HTTP port – needed by client to // contact an HTTP server for stub downloading // permission java. net. Socket. Permission "*: 80", "connect"; // permits to do anything // permission java. security. Allpermission; }; 6/4/2021 Client-server programming 55

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 Some. Client 6/4/2021 Client-server programming 56

The Hello. World Example – revisited 6/4/2021 Client-server programming 57

Hello. Server try{ System. set. Security. Manager( new RMISecurity. Manager()); start. Registry(port. Num); Hello. Impl exported. Obj=new Hello. Impl(); registry. URL = "rmi: //localhost: " +port. Num + "/hello"; Naming. rebind(registry. URL, exported. Obj); …… 6/4/2021 Client-server programming 58

Hello. Client Try{ System. set. Security. Manager( new RMISecurity. Manager()); int port. Num=5555; String host. Name ="localhost"; String registry. URL = “rmi: //" + host. Name+ ": " + port. Num + "/hello"; Hello. Interface h = (Hello. Interface)Naming. lookup(registry. URL); String message = h. say. Hello("This is Client"); …… } 6/4/2021 Client-server programming 59

java. policy file In this example, we give permission to do anything. grant { permission java. security. All. Permission; }; Place a copy of a java. policy file in the same directory as your class on the server and the client. 6/4/2021 Client-server programming 60

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 6/4/2021 Client-server programming 61

Setting Security manager in Net. Beans • 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> 6/4/2021 Client-server programming 62

Revised steps for developing the server -side software with security using Net. Beans with JDK 1. 5 1. 2. 3. 4. 5. 6. Open a project on the server. Specify and compile the remote-server interface in Some. Interface. java. Implement and compile the interface in Some. Impl. java Create the object server program Some. Server. 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. 6/4/2021 Client-server programming 63

Revised steps for developing the clientside software with security using Netbeans with JDK 1. 5 1. 2. 3. 4. 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 Some. Client. 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. 6/4/2021 Client-server programming 64

Revised Hello program • In RMIClient 2 and RMIServer 2. 6/4/2021 Client-server programming 65

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 6/4/2021 Client-server programming 66

Developing the two numbers example • List what you need to develop on the server side: – – – 6/4/2021 the remote-server interface in Two. Num. IF. java. Implement the interface in Two. Num. Impl. java Create the object server program Two. Num. Server. 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=<java. policy> policy file>. Client-server programming 67

Developing the two numbers example • List what you need to develop on the client side: – – 6/4/2021 Obtain a copy of the remote interface file and compile it to generate the interface class file Develop and compile the client program Two. Num. Client. 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=java. policy Client-server programming 68

Server side: Two. Num. IF public interface Two. Num. IF extends Remote { public int add 2(int n 1, int n 2) throws Remote. Exception; } //end interface 6/4/2021 Client-server programming 69

Server side: implementation of the interface public class Two. Num. Impl extends Unicast. Remote. Object implements Two. Num. IF { public Two. Num. Impl() throws Remote. Exception { super( ); } public int add 2(int x, int y) throws Remote. Exception { return (x+y); } } // end class 6/4/2021 Client-server programming 70

Server side: server object System. set. Security. Manager( new RMISecurity. Manager()); start. Registry(port. Num); Two. Num. Impl exported. Obj = new Two. Num. Impl(); registry. URL = "rmi: //localhost: " + port. Num + "/addtwo"; Naming. rebind(registry. URL, exported. Obj); 6/4/2021 Client-server programming 71

Client side: client program System. set. Security. Manager(new RMISecurity. Manager()); String registry. URL = "rmi: //" + host. Name+ ": " + port. Num + "/addtwo"; twonumserver. Two. Num. IF two = (twonumserver. Two. Num. IF)Naming. lookup(registry. URL); Scanner in = new Scanner(System. in); System. out. println("Enter two numbers: "); int n 1 = in. next. Int(); int n 2=in. next. Int(); // invoke the remote method int sum = two. add 2(n 1, n 2); 6/4/2021 Client-server programming 72

Example 2 adding two numbers Server side: 1. Open project “twonumserver” on the server. 2. Specify and compile the remote-server interface in Two. Num. IF. java. 3. Implement and compile the interface in Two. Num. Impl. java 4. Create the object server program Two. Num. Server. java. Remember to instantiate a security manager. 5. Define a java security policy file, and put in the project folder. 6. 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. 6/4/2021 Client-server programming 73

Example 2 adding two numbers 1. 2. 3. 4. 5. 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 Two. Num. Client. 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. 6/4/2021 Client-server programming 74

Review questions • Compare and contrasting message passing and the distributed object paradigm • Describe the Java RMI architecture. What is the role of the RMI registry? 6/4/2021 Client-server programming 75
- Slides: 75