Lesson 3

18
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets Rethinking out tic-tac- toe game

description

Lesson 3. Remote Method Invocation (RMI) Mixing RMI and sockets Rethinking out tic-tac-toe game. Distributed Objects. Simple idea – objects existing on one machine (server) may be accessed from another machine through regular method call. - PowerPoint PPT Presentation

Transcript of Lesson 3

Page 1: Lesson 3

Lesson 3

Remote Method Invocation (RMI)

Mixing RMI and sockets

Rethinking out tic-tac-toe game

Page 2: Lesson 3

Distributed Objects

Simple idea – objects existing on one machine (server) may be accessed from another machine through regular method call.

Eliminates need to “marshal” and “unmarshal” data sent over sockets

Underlying socket code still exists, but is not programmed by user.

Page 3: Lesson 3

RMI vs. CORBA RMI is Java framework for creating distributed object

applications – Remote Method Invocation

CORBA is alternative technology based on open standard – Common Object Request Broker Architecture

RMI is only for pure Java applications; CORBA is language independent

JNI makes this distinction a little less rigid since it allows Java to interact with other languages

Page 4: Lesson 3

Socket flow of events -- synchronous

•Await client message•…•…•Receive client message•Decode client message•Perform action •Create client message•Send to client

•Get user input•Decode user input •Create server message•Send message to server•Await server response•…•…•…•Receive server message•Decode reply•Send output to user

Client Server

Method call on standalone object

Page 5: Lesson 3

Socket flow - asynchronous

•Wait •…•…•Receive client msg•Decode client msg•Perform action •Create client message•Send to client1•Send to client2

•User input (UI)•Decode UI•Create srvr msg•Send srvr msg•Await srvr reply•…•…•…•Receive server message•Decode reply•Output to user

Client1 Server

•User input (UI)•Decode UI•Create srvr msg•Send srvr msg•Await srvr reply•…•…•…•Receive server message•Decode reply•Output to user

Client2

Page 6: Lesson 3

RMI flow of events -- synchronous

•Instantiate object(s)•Bind to registry•…•…•…•…

•Get/cache remote obj ref•Get user input•Decode user input •Remote method call•Decode return value•Send output to user

Client Server

Page 7: Lesson 3

RMI Cartoon1

Page 8: Lesson 3

RMI Cartoon2

Page 9: Lesson 3

Steps for RMI Application

Implement both client and server on single machine to test

Create two directories– client– server

Page 10: Lesson 3

RMI steps, server side

Three files need to be created:– The implementation class (Foo.java)

– An interface listing the methods in the implementation class which you want to make remote (FooInterface.java)

– A server class, which creates one or more implementation objects and posts them to the registry (FooServer.java)

Page 11: Lesson 3

Creating the interface Interface file (e.g. StoreInterface.java)

– StoreInterface must extend java.rmi.Remote– All methods in interface must throw

java.rmi.RemoteException

Implementation file (e.g Store.java)– Store must extend

java.rmi.server.UnicastRemoteObject– Store must implement StoreInterface– Program implementations. Be sure to throw

RemoteException in remote methods– Explicitly include a null construct that calls super()

Page 12: Lesson 3

Creating Stubs

The stub or proxy code is generated automatically by using the rmic utility.

Once the interface and implementation classes are complete, generate the stubs as follows:rmic –v1.2 Store

This creates Store_Stub.java – the java networking layer that transparently handles the underlying message passing.

Page 13: Lesson 3

Creating the server class

Server class (e.g. StoreServer.java)– Create a new object instance– Call java.rmi.Naming.bind(…) to store the

register the object with the naming service – … contains String name associated with bound

object

Page 14: Lesson 3

Steps for RMI, cont.

Create the client– Change to the client dir and copy Store.class

and Store_Stub.class (or you can import them but remember that these will ultimately be on different machines).

– Create StoreClient.java and import:java.rmi.Naming;

java.rmi.RemoteException; java.net.MalformedURLException; java.rmi.NotBoundException;

Page 15: Lesson 3

Steps for rmi, cont.

– Call Naming.lookup() to get remote object reference (be sure to cast to interface type).

– Be sure to handle imported exceptions– Once you get remote object reference, handle as

regular object (there are some subtle differences that we’ll explore later).

Page 16: Lesson 3

Deploying the Application

Start the rmiregistry– rmiregistry & (Unix)– start rmiregistry (Windows)

Start the StoreServer class– java StoreServer & (Unix)

Run the client That’s it!

Page 17: Lesson 3

Additional Issues – covered next time Objects which are not remote are copied (slow!) Stub and interface codes can be downloaded by

client (typical for real distributed systems) Security issues in real system (see ch. 5 in Core

Java 2) Subtleties with Object methods (clone, etc) Using callbacks with RMI Synchronization Registering multiple objects Bottom Line: Don’t be too fancy!

Page 18: Lesson 3

Examples

tic-tac-toe reorganized as standalone back-end object

single-threaded test of TTT object multithreaded test of TTT object using

polling client-server TTT using RMI and polling client-server TTT using RMI over sockets client-server TTT using RMI callbacks.