Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."— Presentation transcript:

1 CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

2 4/7/20052 Class RemoteObject Ultimate superclass of all remote objects Override hashCode(), equals(), toString() to reflect correct semantics of a remote object Implement interface Serializable to ensure correct serialization of remote objects

3 4/7/20053 Remote Objects as Parameters When a remote object implementation is sent as a parameter to a remote method call, a remote reference to the remote object, instead of a copy of remote object, is what is really sent When remote method calls method on the received remote object, further RMI calls will be made to the original remote object implementation

4 4/7/20054 Example of Remote Object Parameters public class MyImpl extends UnicastRemoteObject implements MyRemote { // … public static void main (String[] args) throws Exception { MyImpl mine = new MyImpl (); HisRemote his = (HisRemote) Naming.lookup (“//server/his”); his.invoke (mine); } public void invoke (Object object) throws RemoteException;  public void invoke (MyRemote remote) throws RemoteException;  public void invoke (MyImpl impl) throws RemoteException;X

5 4/7/20055 Class RemoteServer A subclass of RemoteObject Provide common functions for remote objects to be implemented as servers Static methods String getClientHost() throws ServerNotActiveException void setLog(OutputStream out) PrintStream getLog()

6 4/7/20056 Class UnicastRemoteObject Subclass of RemoteServer Superclass of most normal remote objects

7 4/7/20057 Class UnicastRemoteObject Constructors protected UnicastRemoteObject() throws RemoteException protected UnicastRemoteObject(int port) throws RemoteException protected UnicastRemoteObject(int port, RMIClientSocketFactory clients, RMIServerSocketFactory servers) throws RemoteException Static methods RemoteStub exportObject(Remote object) throws RemoteException RemoteStub exportObject(Remote object, int port) throws RemoteException RemoteStub exportObject(Remote object, int port, RMIClientSocketFactory clients, RMIServerSocketFactory servers) throws RemoteException boolean unexportObject(Remote object, boolean force) throws NoSuchObjectException

8 4/7/20058 Class RemoteStub Superclass for all remote stub classes Proxy all of remote methods into remote method calls on actual remote object implementation by connecting to and communicating with skeleton

9 4/7/20059 Interface Unreferenced RMI maintains a count of #references that exists to each remote object User code can use this interface to work with garbage collection mechanism Method void unreferenced()

10 4/7/200510 Class RMISocketFactory Static methods void setSocketFactory(RMISocketFactory factory) throws IOException RMISocketFactory getSocketFactory() RMISocketFactory getDefaultSocketFactory() void setFailureHandler(RMIFailureHandler failureHandler) RMIFailureHandler getFailureHandler() Methods abstract Socket createSocket(String host, int port) throws IOException abstract Socket createServerSocket(int port) throws IOException

11 4/7/200511 Interface RMIClientSocketFactory Describe a client-side socket factory Implement a per-object custom socket factory Method Socket createSocket(String host, int port) throws IOException

12 4/7/200512 Interface RMIServerSocketFactory Describe a server-side socket factory Implement a per-object custom socket factory Method Socket createServerSocket(int port) throws IOException

13 4/7/200513 Interface RMIFailureHandler A class that attempts to resolve the cause of RMI socket exception and correct it Method boolean failure(Exception ex)

14 4/7/200514 An RMI Chat Example An RMI-based client/server chat system Define remote interface RMIChatServer Server class RMIChatServerImpl accepts new messages and queries from client through interface RMIChatServer Client class RMIChatClient sends messages to server and uses an update thread to periodically query server

15 4/7/200515 Interface RMIChatServer /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.rmi.*; public interface RMIChatServer extends Remote { public static final String REGISTRY_NAME = "Chat Server"; public abstract String[] getMessages (int index) throws RemoteException; public abstract void addMessage (String message) throws RemoteException; }

16 4/7/200516 Class RMIChatServerImpl /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.rmi.*; import java.util.*; import java.rmi.server.*; import java.rmi.registry.*; public class RMIChatServerImpl extends UnicastRemoteObject implements RMIChatServer { // public RMIChatServerImpl () throws RemoteException … // public String[] getMessages (int index) … // public void addMessage (String message) … // public static void main (String[] args) throws RemoteException … }

17 4/7/200517 Methods of RMIChatServerImpl protected Vector messages; public RMIChatServerImpl () throws RemoteException { messages = new Vector (); } public String[] getMessages (int index) { int size = messages.size (); String[] update = new String[size - index]; for (int i = 0; i < size - index; ++ i) update[i] = (String) messages.elementAt (index + i); return update; } public void addMessage (String message) { messages.addElement (message); } public static void main (String[] args) throws RemoteException { RMIChatServerImpl chatServer = new RMIChatServerImpl (); Registry registry = LocateRegistry.getRegistry (); registry.rebind (REGISTRY_NAME, chatServer); }

18 4/7/200518 Class RMIChatClient /* Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.awt.*; import java.rmi.*; import java.awt.event.*; import java.rmi.registry.*; public class RMIChatClient implements Runnable, ActionListener { protected static final int UPDATE_DELAY = 10000; // public RMIChatClient (String host) … // public synchronized void start () throws RemoteException, NotBoundException … // public synchronized void stop () … // public void run () … // public void actionPerformed (ActionEvent ev) … // public static void main (String[] args) throws RemoteException, NotBoundException … }

19 4/7/200519 Constructor RMIChatClient protected String host; protected Frame frame; protected TextField input; protected TextArea output; public RMIChatClient (String host) { this.host = host; frame = new Frame ("RMIChatClient [" + host + "]"); frame.add (output = new TextArea (), "Center"); output.setEditable (false); frame.add (input = new TextField (), "South"); input.addActionListener (this); frame.addWindowListener (new WindowAdapter () { public void windowOpened (WindowEvent ev) { input.requestFocus (); } public void windowClosing (WindowEvent ev) { stop (); } }); frame.pack (); }

20 4/7/200520 Methods start and stop protected RMIChatServer server; protected Thread updater; public synchronized void start () throws RemoteException, NotBoundException { if (updater == null) { Registry registry = LocateRegistry.getRegistry (host); server = (RMIChatServer) registry.lookup (RMIChatServer.REGISTRY_NAME); updater = new Thread (this); updater.start (); frame.setVisible (true); } public synchronized void stop () { if (updater != null) { updater.interrupt (); updater = null; server = null; } frame.setVisible (false); }

21 4/7/200521 Method run public void run () { try { int index = 0; while (!Thread.interrupted ()) { String[] messages = server.getMessages (index); int n = messages.length; for (int i = 0; i < n; ++ i) output.append (messages[i] + "\n"); index += n; Thread.sleep (UPDATE_DELAY); } } catch (InterruptedException ignored) { } catch (RemoteException ex) { input.setVisible (false); frame.validate (); ex.printStackTrace (); }

22 4/7/200522 Method actionPerformed public void actionPerformed (ActionEvent ev) { try { RMIChatServer server = this.server; if (server != null) server.addMessage (ev.getActionCommand ()); input.setText (""); } catch (RemoteException ex) { Thread tmp = updater; updater = null; if (tmp != null) tmp.interrupt (); input.setVisible (false); frame.validate (); ex.printStackTrace (); }

23 4/7/200523 Method main public static void main (String[] args) throws RemoteException, NotBoundException { if (args.length != 1) throw new IllegalArgumentException ("Syntax: RMIChatClient "); RMIChatClient chatClient = new RMIChatClient (args[0]); chatClient.start (); }

24 4/7/200524 Disadvantages of Example Waste of resources during idle periods Late updates during active periods Dynamically increasing delay during idle periods and decreasing delay during active periods is not good enough Alternative solution is callback mechanism

25 4/7/200525 Next Class More examples of RMI in practice Read JNP Ch. 24 Project 5 will be passed out


Download ppt "CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina."

Similar presentations


Ads by Google