CSCE 790: Computer Network Security

Download Report

Transcript CSCE 790: Computer Network Security

CSCE 515:
Computer Network Programming
Chin-Tser Huang
[email protected]
University of South Carolina
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
4/7/2005
2
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/7/2005
3
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
4/7/2005
4
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()
4/7/2005
5
Class UnicastRemoteObject


Subclass of RemoteServer
Superclass of most normal remote objects
4/7/2005
6
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
4/7/2005
7
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
4/7/2005
8
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()
4/7/2005
9
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
4/7/2005
10
Interface RMIClientSocketFactory


Describe a client-side socket factory
Implement a per-object custom socket
factory
Method
Socket createSocket(String host, int port) throws
IOException
4/7/2005
11
Interface RMIServerSocketFactory


Describe a server-side socket factory
Implement a per-object custom socket
factory
Method
Socket createServerSocket(int port) throws
IOException
4/7/2005
12
Interface RMIFailureHandler

A class that attempts to resolve the cause of
RMI socket exception and correct it
Method
boolean failure(Exception ex)
4/7/2005
13
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
4/7/2005
14
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;
}
4/7/2005
15
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
import
import
import
java.rmi.*;
java.util.*;
java.rmi.server.*;
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 …
}
4/7/2005
16
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);
}
4/7/2005
17
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
import
import
import
java.awt.*;
java.rmi.*;
java.awt.event.*;
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 …
}
4/7/2005
18
Constructor RMIChatClient
protected
protected
protected
protected
String host;
Frame frame;
TextField input;
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 ();
4/7/2005
19
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);
}
4/7/2005
20
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 ();
}
}
4/7/2005
21
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 ();
}
}
4/7/2005
22
Method main
public static void main (String[] args)
throws RemoteException, NotBoundException {
if (args.length != 1)
throw new IllegalArgumentException ("Syntax: RMIChatClient <host>");
RMIChatClient chatClient = new RMIChatClient (args[0]);
chatClient.start ();
}
4/7/2005
23
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
4/7/2005
24
Next Class



More examples of RMI in practice
Read JNP Ch. 24
Project 5 will be passed out
4/7/2005
25