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
Remote Method Invocation






Provide a framework for Java objects to communicate
via their methods, regardless of their location
Networking details disappear under the guise of
standard method calls
Define a remote interface that declares methods that
can be called remotely
Use stub and skeleton classes to translate parameters
and return values
Register remote object with a naming service
Client connects to naming service to get a remote
reference
4/5/2005
2
Stub and Skeleton Classes



Generated by rmic tool in JDK
Stub class automatically translates remote
method calls into network communication
setup and parameter passing
Skeleton class accepts network connections
and translate them into actual method calls
on actual object
4/5/2005
3
RMI Architecture
Client
Server
JVM 1
JVM 2
Remote
method()
Stub
4/5/2005
RemoteObject
Internet
method()
Skeleton
4
Object Transmission


To transmit an object over network, need to make
the object serializable
Three stages of object transmission



Marshaling: pack up data into a form that can be sent over
some communication channel
Delivery: over the network
Unmarshaling: reconstruct object into original form at the
other end
Delivery
Marshaling
object
4/5/2005
bytes
Internet
Unmarshaling
bytes
object
5
Object Transmission in RMI




Marshaling performed by a customized
ObjectOutputStream
Delivery performed over TCP/IP socket
connection
Unmarshaling performed by a customized
ObjectInputStream
Autogenerated stub and skeleton classes
contain the code so is transparent to
programmers
4/5/2005
6
RemoteException



Superclass of all exceptions that can
occur in RMI run time
Thrown whenever a remote method
invocation fails
All method in a remote interface can
throw RemoteException
4/5/2005
7
Steps to Create RMI Application



Define remote interface
Implement remote interface
Generate stub and skeleton classes using
rmic




Write a client that locates server in naming
registry and then calls remote methods
Start naming registry using rmiregistry
Start server
Run client
4/5/2005
8
An RMI Date Server Example




A simple date server that allows clients to
determine date and time at server using
remote method calls
Define remote interface DateServer
Implement remote interface with class
DateServerImpl
Client class DateClient
4/5/2005
9
Interface DateServer
/* 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.Remote;
import java.rmi.RemoteException;
import java.util.Date;
public interface DateServer extends Remote {
public Date getDate () throws RemoteException;
}
4/5/2005
10
Class DateServerImpl
/* 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.rmi.server.*;
import java.util.Date;
public class DateServerImpl extends UnicastRemoteObject implements DateServer {
public DateServerImpl () throws RemoteException {
}
public Date getDate () {
return new Date ();
}
}
public static void main (String[] args) throws Exception {
DateServerImpl dateServer = new DateServerImpl ();
Naming.bind ("DateServer", dateServer);
}
4/5/2005
11
Class DateClient
/* 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.Naming;
import java.util.Date;
public class DateClient {
public static void main (String[] args) throws Exception {
if (args.length != 1)
throw new IllegalArgumentException ("Syntax: DateClient <hostname>");
DateServer dateServer = (DateServer) Naming.lookup
("rmi://" + args[0] + "/DateServer");
Date when = dateServer.getDate ();
System.out.println (when);
}
}
4/5/2005
12
RMI-Related Packages

Five main packages in RMI framework





4/5/2005
java.rmi: classes related to client side of RMI
java.rmi.server: classes related to server side of
RMI
java.rmi.registry: classes related to RMI naming
registry
java.rmi.dgc: classes supporting distributed
garbage collection
java.rmi.activation: classes supporting JDK 1.2
activation mechanism
13
Interface Remote




Superinterface for all remote interfaces
Serve to identify all remote interfaces, so
declare no methods
A remote interface that extends Remote
describes the only methods that a remote
object supports
Clearly delineate API of a remote object, so
separate remote object’s implementation
from its publicly-exposed interface
4/5/2005
14
Remote Method Call Semantics



Major difference between remote method call
and direct method call is parameters are
passed by value in RMI
Remote method gets copy of field values of
Object, so client will not see changes
reflected in its local copy of Object
To modify an Object inside a remote method,
need to have remote method return modified
Object as result of remote method call
4/5/2005
15
Class Naming
Methods
Remote lookup(String address) throws MalformedURLException,
RemoteException, NotBoundException
void bind(String address, Remote object) throws
MalformedURLException, RemoteException, AlreadyBoundException
void rebind(String address, Remote object) throws
MalformedURLException, RemoteException
void unbind(String address) throws MalformedURLException,
RemoteException, NotBoundException
String[] list(String address) throws MalformedURLException,
RemoteException
4/5/2005
16
Class Naming
Exceptions
MalformedURLException
RemoteException
UnknownHostException
NotBoundException
AlreadyBoundException
4/5/2005
17
Examples of Using Naming
String[] services = Naming.list (“//accounts.my.bank/”);
for (int i = 0; i < services.length; ++i)
System.out.println (services[i]);
BankAccount firstAccount = (BankAccount) Naming.lookup
(services[0]);
Naming.bind (“First Account”, firstAccount);
account = new PersonalBankAccountImpl (“jim”);
Naming.rebind (“personal/jim”, account);
4/5/2005
18
Class LocateRegistry
Static methods
Registry getRegistry() throws RemoteException
Registry getRegistry(int port) throws RemoteException
Registry getRegistry(String host) throws RemoteException
Registry getRegistry(String host, int port) throws
RemoteException
Registry getRegistry(String host, int port,
RMIClientSocketFactory clients) throws RemoteException
Registry createRegistry(int port) throws RemoteException
Registry createRegistry(int port, RMIServerSocketFactory
servers, RMIClientSocketFactory clients) throws
RemoteException
4/5/2005
19
Class LocateRegistry
Exceptions
RemoteException
UnknownHostException
4/5/2005
20
Interface Registry
Static variable
int REGISTRY_PORT
Methods
Remote lookup(String name) throws RemoteException,
NotBoundException
void bind(String name, Remote object) throws
RemoteException, AlreadyBoundException
void rebind(String name, Remote object) throws
RemoteException
void unbind(String name) throws RemoteException,
NotBoundException
String[] list() throws RemoteException
4/5/2005
21
Interface Registry
Exceptions
RemoteException
NotBoundException
AlreadyBoundException
AccessException
4/5/2005
22
Examples of Using Registry
Registry registry = LocateRegistry.getRegistry (“host”, 1234);
registry.rebind (“Service”, service);
Registry registry = LocateRegistry.createRegistry (1234);
registry.bind (“Service”, service);
4/5/2005
23
Next Class



Remote method invocation (RMI)
Examples of RMI in practice
Read JNP Ch. 23, 24
4/5/2005
24