ClientServer Using RMI Joe Komar 112022 Komar Associates

  • Slides: 23
Download presentation
Client/Server Using RMI Joe Komar 1/1/2022 Komar Associates 1

Client/Server Using RMI Joe Komar 1/1/2022 Komar Associates 1

Remote Method Invocation (RMI) n n n Can invoke methods on a remote object

Remote Method Invocation (RMI) n n n Can invoke methods on a remote object Integrates a distributed object model ORB compliant Extends security allowing dynamic downloading of stub classes Passes local objects by value (serialization) Passes remote objects by reference 1/1/2022 Komar Associates 2

RMI Process (ten steps to success) n n Define an interface -- extends java.

RMI Process (ten steps to success) n n Define an interface -- extends java. rmi. Remote, all methods throw java. rim. Remote. Exception Implement the interface -- server class that extends java. rmi. Unicast. Remote. Object Compile the server class Run the stub compiler -- rmic classname u Generates stub for client and skeleton for server 1/1/2022 Komar Associates 3

RMI Process (ten steps to success) n n n Start the RMI registry (non-persistent

RMI Process (ten steps to success) n n n Start the RMI registry (non-persistent naming service) -- rmiregistry Start the serve objects -- load server classes and create instances of remote objects Register your remote objects with the registry -- use java. rmi. Naming class methods 1/1/2022 Komar Associates 4

RMI Process (ten steps to success) n n n Write your client code --

RMI Process (ten steps to success) n n n Write your client code -- use java. rmi. Naming class to locate remote object Compile the client code Start the client 1/1/2022 Komar Associates 5

RMI Interface // Count. Rmi Interface public interface Count. RMI extends java. rmi. Remote

RMI Interface // Count. Rmi Interface public interface Count. RMI extends java. rmi. Remote { int sum() throws java. rmi. Remote. Exception; void sum(int _val) throws java. rmi. Remote. Exception; public int increment() throws java. rmi. Remote. Exception; } 1/1/2022 Komar Associates 6

RMI Interface Implementation // Count. RMIImpl. java, Count. RMI implementation import java. rmi. *;

RMI Interface Implementation // Count. RMIImpl. java, Count. RMI implementation import java. rmi. *; import java. rmi. server. Unicast. Remote. Object; public class Count. RMIImpl extends Unicast. Remote. Object implements Count. RMI { private int sum; 1/1/2022 Komar Associates 7

RMI Interface Implementation public Count. RMIImpl(String name) throws Remote. Exception { super(); try {

RMI Interface Implementation public Count. RMIImpl(String name) throws Remote. Exception { super(); try { Naming. rebind(name, this); sum = 0; } catch (Exception e) { System. out. println("Exception: " + e. get. Message()); e. print. Stack. Trace(); } } 1/1/2022 Komar Associates 8

RMI Interface Implementation public int sum() throws Remote. Exception { return sum; } public

RMI Interface Implementation public int sum() throws Remote. Exception { return sum; } public void sum(int val) throws Remote. Exception { sum = val; } public int increment() throws Remote. Exception { sum++; return sum; } } 1/1/2022 Komar Associates 9

RMI Server // Count. RMIServer. java import java. rmi. *; import java. rmi. server.

RMI Server // Count. RMIServer. java import java. rmi. *; import java. rmi. server. *; public class Count. RMIServer { public static void main(String args[]) { System. set. Security. Manager(new RMISecurity. Manager()); try { Count. RMIImpl my. Count = new Count. RMIImpl("my Count. RMI"); System. out. println("Count. RMI Server ready. "); } catch (Exception e) { System. out. println("Exception: " + e. get. Message()); e. print. Stack. Trace(); } } 1/1/2022} Komar Associates 10

RMI Client // Count. RMIClient. java RMI Count client import java. rmi. *; import

RMI Client // Count. RMIClient. java RMI Count client import java. rmi. *; import java. rmi. registry. *; import java. rmi. server. *; public class Count. RMIClient { public static void main(String args[]) { // Create and install the security manager System. set. Security. Manager(new RMISecurity. Manager()); 1/1/2022 Komar Associates 11

RMI Client try { Count. RMI my. Count = (Count. RMI)Naming. lookup("rmi: //" +

RMI Client try { Count. RMI my. Count = (Count. RMI)Naming. lookup("rmi: //" + args[0] + "/" + "my Count. RMI"); System. out. println("Setting Sum to 0"); my. Count. sum(0); long start. Time = System. current. Time. Millis(); System. out. println("Incrementing"); int count = new Integer(args[1]). int. Value(); for (int i = 0 ; i < count ; i++ ) { my. Count. increment(); } 1/1/2022 Komar Associates 12

RMI Client long stop. Time = System. current. Time. Millis(); System. out. println("Avg Ping

RMI Client long stop. Time = System. current. Time. Millis(); System. out. println("Avg Ping = " + ((stop. Time - start. Time)/(float)count) + " msecs"); System. out. println("Sum = " + my. Count. sum()); } catch(Exception e) { System. err. println("System Exception" + e); } System. exit(0); } } 1/1/2022 Komar Associates 13

RMI Example Output On the Server: Count RMI Server ready. On the Client: Setting

RMI Example Output On the Server: Count RMI Server ready. On the Client: Setting sum to 0 Incrementing Avg Ping = 14. 83 msecs Sum = 500 1/1/2022 Komar Associates 14

RMI Hands On n n There are four files in \Nt 431Publickomarrmi that you

RMI Hands On n n There are four files in \Nt 431Publickomarrmi that you need to copy to your diskette On one system find the IP name (Start/Run WINIPCFG) Copy the files to a temporary directory on that “server” Use this directory as your home directory for the steps on the next page 1/1/2022 Komar Associates 15

RMI Hands On n n Compile the “. . . Server” java program which

RMI Hands On n n Compile the “. . . Server” java program which should compile all the classes needed for the server Run the RMI compiler using the “…Impl” file: u rmic n n n Count. RMIImpl Copy the “…_Stub. class” file to the diskette Run the RMI registry (start rmiregistry) Run the server (java Count. RMIServer) 1/1/2022 Komar Associates 16

RMI Hands On n Take your diskette to a second machine and do these

RMI Hands On n Take your diskette to a second machine and do these steps…. Compile the “…Client. java” program Run the client using the IP name as the first parameter and an iteration integer as the second: u java 1/1/2022 Count. RMIClient s 152. sci 1. stthomas. edu 500 Komar Associates 17

RMI Related classes and interfaces n n Remote. Exception -- superclass for all RMI

RMI Related classes and interfaces n n Remote. Exception -- superclass for all RMI exceptions. All remote methods must throw a Remote. Exception Remote -- interface that flags remote objects (contains no methods) Remote. Object -- remote version of the Java root Object class Remote. Server -- class to define methods to create server objects and export them 1/1/2022 Komar Associates 18

RMI Related classes and interfaces n Unicast. Remote. Object -- class implements a remote

RMI Related classes and interfaces n Unicast. Remote. Object -- class implements a remote server object u object only exists as long as the process that created it u requires a TCP connection based transport u client and server use a stream protocol to communicate n Remote. Stub -- class is superclass for all client stubs 1/1/2022 Komar Associates 19

RMI Related classes and interfaces n n n Registry interface -- methods let you

RMI Related classes and interfaces n n n Registry interface -- methods let you update entries in a registry Locate. Registry class -- overloaded get. Registry() static methods to find a registry Naming class -- retrieve and define remote objects using URL syntax: u rmi: //host: port/name (port usually 1099) u Client uses lookup() method, host uses rebind() method 1/1/2022 Komar Associates 20

RMI Related classes and interfaces n n RMISecurity. Manager class -- simple security manager

RMI Related classes and interfaces n n RMISecurity. Manager class -- simple security manager for RMI objects (Applets have their own security classes) RMIClass. Loader class -- load stubs and skeletons via a URL 1/1/2022 Komar Associates 21

RMI and CORBA (Common Object Request Broker Architecture) n n n Moving toward one

RMI and CORBA (Common Object Request Broker Architecture) n n n Moving toward one way in cooperation with OMG (Object Management Group consortium)… RMI over IIOP (Internet Inter. ORB Protocol) -will be offered by Java. Soft RMI/IDL (Interface Definition Language) -use Java syntax to specify CORBA interfaces 1/1/2022 Komar Associates 22

RMI Assignment n Create the Java code needed to do the following using RMI:

RMI Assignment n Create the Java code needed to do the following using RMI: u Perform addition, subtraction, multiplication, or division by executing a remote objects method u That remote method will accept three parameters (character operator, double val 1, double val 2) and return the resulting double 1/1/2022 Komar Associates 23