Advanced Remote Method Invocations M L Liu 10232021

  • Slides: 32
Download presentation
Advanced Remote Method Invocations M. L. Liu 10/23/2021 Distributed Computing, M. L. Liu 1

Advanced Remote Method Invocations M. L. Liu 10/23/2021 Distributed Computing, M. L. Liu 1

RMI – Advanced topics The Java RMI API has a rich collection of features.

RMI – Advanced topics The Java RMI API has a rich collection of features. n We will look at some of RMI’s more interesting advanced features, namely: n n n stub downloading security manager client callback. Although these features are not inherent to the distributed object paradigm, they are helpful mechanisms and can be useful to application developers. 10/23/2021 Distributed Computing, M. L. Liu 2

The Java RMI Architecture 10/23/2021 Distributed Computing, M. L. Liu 3

The Java RMI Architecture 10/23/2021 Distributed Computing, M. L. Liu 3

Java RMI Client Server Interaction 10/23/2021 Distributed Computing, M. L. Liu 4

Java RMI Client Server Interaction 10/23/2021 Distributed Computing, M. L. Liu 4

RMI Stub Downloading n n n RMI is designed to allow stubs to be

RMI Stub Downloading n n n RMI is designed to allow stubs to be made available to the client dynamically. Doing so allows changes to be made in the remote methods without affecting the client program. The stub can be filed with an web server and be downloaded using HTTP. Security measures are needed to prevent both the client side and the server side: A java security policy file needs to be set on the server host and also on the client host. A Java Security Manager should be instantiated in both the client and server programs. 10/23/2021 Distributed Computing, M. L. Liu 5

Stub downloading n n If the stub is to be downloaded from a remote

Stub downloading n n If the stub is to be downloaded from a remote server, transfer the stub class to the appropriate directory on that server, e. g. , www. csc. calpoly. edu/~mliu/www, and make sure that the access permission to the file is set. When activating the server, specify command options: 10/23/2021 Distributed Computing, M. L. Liu 6

The java. policy file n n n The RMI security manager does not permit

The java. policy file n n n The RMI security manager does not permit network access. Exceptions can be made via the specification in a java. policy file. grant { // permits socket access to all common TCP ports, including the default // RMI registry port (1099) – need for both the client and the server. 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"; }; This file can be filed in the same directory as the server class file. When activiating the client, a java. policy file also should be specified: java -Djava. security. policy=java. policy Some. Client 10/23/2021 n See Distributed Computing, M. L. Liu 7

File Placements 10/23/2021 Distributed Computing, M. L. Liu 8

File Placements 10/23/2021 Distributed Computing, M. L. Liu 8

RMI Security Manager n n n Since RMI involves access to/from a foreign host,

RMI Security Manager n n n Since RMI involves access to/from a foreign host, and possibly object downloading, it is important for both the server and the client to protect its system from malicious access. The RMISecurity. Manager is a class provided Java, and can be instantiated in both the client and the server for limiting access priviledges. You can write your own security manager, if so desired. try { System. set. Security. Manager(new RMISecurity. Manager( )); } catch { …} 10/23/2021 Distributed Computing, M. L. Liu 9

Algorithm for building an RMI Application Server side: 1. 2. 3. 4. 5. 6.

Algorithm for building an RMI Application Server side: 1. 2. 3. 4. 5. 6. 7. 8. Open a directory for all the files to be generated for this application. Specify the remote-server interface, and compile it to generate the interface class file. Build the remote server class by implementing the interface, and compile it using javac. Use rmic to process the server class to generate a stub. class file and a skelton. class file: rmic Some. Server If stub downloading is desired, copy the stub file to an appropriate directory on the HTTP host. Activate the RMIRegistry, if it has not already been activated. Set up a java. policy file. Activate the server, specifying (i) the codebase if stub downloading is desired, (ii) the server host name, and (iii) the security policy file. 10/23/2021 Distributed Computing, M. L. Liu 10

Alogorithm for building an RMI Application Client side: 1. Open a directory for all

Alogorithm for building an RMI Application Client side: 1. Open a directory for all the files to be generated for this application. 2. Implement the client program or applet, and compile it to generate the client class. 3. If stub downloading is not in effect, copy the server interface stub class file by hand. 4. Set up a java. policy file. 5. Activate the client, specifying (i) the server host name, and (ii) the security policy file. 10/23/2021 Distributed Computing, M. L. Liu 11

RMI Callbacks 10/23/2021 Distributed Computing, M. L. Liu 12

RMI Callbacks 10/23/2021 Distributed Computing, M. L. Liu 12

Introduction n n In the client server model, the server is passive: the IPC

Introduction n n In the client server model, the server is passive: the IPC is initiated by the client; the server waits for the arrival of requests and provides responses. Some applications require the server to initiate communication upon certain events. Examples applications are: n n n n 10/23/2021 monitoring games auctioning voting/polling chat-toom message/bulletin board groupware Distributed Computing, M. L. Liu 13

Polling vs. Callback In the absence of callback, a client will have to poll

Polling vs. Callback In the absence of callback, a client will have to poll a passive server repeatedly if it needs to be notified that an event has occurred at the server end. 10/23/2021 Distributed Computing, M. L. Liu 14

Two-way communications n n n Some applications require that both sides may initiate IPC.

Two-way communications n n n Some applications require that both sides may initiate IPC. Using sockets, duplex communication can be achieved by using two sockets on either side. With connection-oriented sockets, each side acts as both a client and a server. 10/23/2021 Distributed Computing, M. L. Liu 15

RMI Callbacks n n A callback client registers itself with an RMI server. The

RMI Callbacks n n A callback client registers itself with an RMI server. The server makes a callback to each registered client upon the occurrence of a certain event. 10/23/2021 Distributed Computing, M. L. Liu 16

Callback Client-Server Interactions 10/23/2021 Distributed Computing, M. L. Liu 17

Callback Client-Server Interactions 10/23/2021 Distributed Computing, M. L. Liu 17

Callback application files 10/23/2021 Distributed Computing, M. L. Liu 18

Callback application files 10/23/2021 Distributed Computing, M. L. Liu 18

RMI Callback file placements 10/23/2021 Distributed Computing, M. L. Liu 19

RMI Callback file placements 10/23/2021 Distributed Computing, M. L. Liu 19

The Hello Application with Callback 10/23/2021 Distributed Computing, M. L. Liu 20

The Hello Application with Callback 10/23/2021 Distributed Computing, M. L. Liu 20

n n n The server provides a remote method which allows a client to

n n n The server provides a remote method which allows a client to register itself for callbacks. A Remote interface for the callback is needed, in addition to the server-side interface. The interface specifies a method for accepting a callback from the server. The client program is a subclass of Remote. Object and implements the callback interface, including the callback method. The client registers itself for callback in its main method. The server invokes the client’s remote method upon the occurrence of the anticipated event. 10/23/2021 Distributed Computing, M. L. Liu 21

Remote Interface for Server public interface Hello. Interface extends Remote { // remote method

Remote Interface for Server public interface Hello. Interface extends Remote { // remote method public String say. Hello() throws java. rmi. Remote. Exception; // method to be invoked by a client to add itself to the callback list public void add. Callback( Hello. Callback. Interface Callback. Object) throws java. rmi. Remote. Exception; } 10/23/2021 Distributed Computing, M. L. Liu 22

Remote Interface for Callback Client // an interface specifying a callback method public interface

Remote Interface for Callback Client // an interface specifying a callback method public interface Hello. Callback. Interface extends java. rmi. Remote { // method to be called by the server on callback public void call. Me ( String message ) throws java. rmi. Remote. Exception; } 10/23/2021 Distributed Computing, M. L. Liu 23

Hello. Server, with callback public class Hello. Server extends Unicast. Remote. Object implements Hello.

Hello. Server, with callback public class Hello. Server extends Unicast. Remote. Object implements Hello. Interface { static int RMIPort; // vector for store list of callback objects private static Vector callback. Objects; public Hello. Server() throws Remote. Exception { super(); // instantiate a Vector object for storing callback objects callback. Objects = new Vector(); } // method for client to call to add itself to its callback public void add. Callback( Hello. Callback. Interface Callback. Object) { // store the callback object into the vector System. out. println("Server got an 'add. Callback' call. "); callback. Objects. add. Element (Callback. Object); } 10/23/2021 Distributed Computing, M. L. Liu 24

Hello. Server, with callback - 2 public static void main(String args[]) { … registry

Hello. Server, with callback - 2 public static void main(String args[]) { … registry = Locate. Registry. create. Registry(RMIPort); … callback( ); … } // end main private static void callback( ) { … for (int i = 0; i < callback. Objects. size(); i++) { System. out. println("Now performing the "+ i +"th callbackn"); // convert the vector object to a callback object Hello. Callback. Interface client = (Hello. Callback. Interface) callback. Objects. element. At(i); … client. call. Me ( "Server calling back to client " + i); … 10/23/2021 Distributed Computing, M. L. Liu 25

Alogorithm for building an RMI Callback Application Server side: 1. 2. 3. 4. 5.

Alogorithm for building an RMI Callback Application Server side: 1. 2. 3. 4. 5. 6. 7. 8. 9. Open a directory for all the files to be generated for this application. Specify the remote-server interface, and compile it to generate the interface class file. Build the remote server class by implementing the interface, and compile it using javac. Use rmic to process the server class to generate a stub. class file and a skelton. class file: rmic Some. Server If stub downloading is desired, copy the stub file to an appropriate directory on the HTTP host. Activate the RMIRegistry, if it has not already been activated. Set up a java. policy file. Activate the server, specifying (i) the codebase if stub downloading is desired, (ii) the server host name, and (iii) the security policy file. Obtain the Callback. Interface. Compile it with javac, then use rmic to generate the stub file for the callback. 10/23/2021 Distributed Computing, M. L. Liu 26

Alogorithm for building an RMI Callback Application Client side: 1. Open a directory for

Alogorithm for building an RMI Callback Application Client side: 1. Open a directory for all the files to be generated for this application. 2. Implement the client program or applet, and compile it to generate the client class. 3. If stub downloading is not in effect, copy the server interface stub class file by hand. 4. Implement the callback interface. Compile it using javac, then using rmic to generate a stub class and a skeleton class for it. 5. Set up a java. policy file. 6. Activate the client, specifying (i) the server host name, and (ii) the security policy file. 10/23/2021 Distributed Computing, M. L. Liu 27

Hello. Client, with callback Hello. Client() { // constructor System. set. Security. Manager(new RMISecurity.

Hello. Client, with callback Hello. Client() { // constructor System. set. Security. Manager(new RMISecurity. Manager()); // export this object as a remote object Unicast. Remote. Object. export. Object(this); // … Registry registry = Locate. Registry. get. Registry("localhost", RMIPort); h = (Hello. Interface) registry. lookup("hello. Liu"); h. add. Callback(this); // … } // end constructor // call back method - this displays the message sent by the server public void call. Me (String message) { System. out. println( "Call back received: " + message ); } public static void main(String args[]) { // … Hello. Client client = new Hello. Client(); // … while (true){ ; } // end while } // end main } // end Hello. Client class 10/23/2021 Distributed Computing, M. L. Liu 28

Stats. Server, Stats. Client 10/23/2021 Distributed Computing, M. L. Liu 29

Stats. Server, Stats. Client 10/23/2021 Distributed Computing, M. L. Liu 29

Summary-1 n Client callback: n n Client callback is useful for an application where

Summary-1 n Client callback: n n Client callback is useful for an application where the clients desire to be notified by the server of the occurrence of some event. Client callback allows an object server to make remote method call to a client via a reference to a client remote interface. 10/23/2021 Distributed Computing, M. L. Liu 30

Summary-2 n Client callback: n To provide client callback, the client-side software n n

Summary-2 n Client callback: n To provide client callback, the client-side software n n The object server: n n n supplies a remote interface, instantiate an object which implements the interface, passes a reference to the object to the server via a remote method call to the server. collects these client references in a data structure. when the awaited event occurs, the object server invokes the callback method (defined in the client remote interface) to pass data to the client. Two sets of stubskeletons are needed: one for the server remote interface, the other one for the client remote interface. Two sets of stub-skeletons are needed: one for the server remote interface, the other one for the client remote interface. 10/23/2021 Distributed Computing, M. L. Liu 31

Summary-3 n n n Stub downloading allows a stub class to be loaded to

Summary-3 n n n Stub downloading allows a stub class to be loaded to an object client at runtime, thereby allowing a remote object’s implementation to be modified and its stub class regenerated without affecting the software on the client host. A security manager oversees access restrictions specified in a Java security policy file, which can be a system-wide policy file, or a policy file applied to an individual application only. For security protection, the use of security managers is recommended in all RMI applications, regardless of whether stub downloading is involved. 10/23/2021 Distributed Computing, M. L. Liu 32