Sie sind auf Seite 1von 4

Network Programming Paradigms

„ Sockets programming: design a protocol


CSCE 515: first, then implement clients and servers
Computer Network that support the protocol.
Programming
------ Remote Method Invocation
„ RMI: Develop an application, then move
reference: Dave Hollinger some objects to remote machines.
Wenyuan Xu
… Notconcerned with the details of the actual
Department of Computer Science and communication between processes –
Engineering everything is just method calls.
University of South Carolina

2
Netprog: Java RMI

Call Semantics Finding Remote Objects


„ Method Call Semantics – what does it mean to „ It would be awkward if we needed to include a
make a call to a method? hostname, port and protocol with every remote
… How many times is the method run? method invocation.
… How do we know the method ran at all? „ RMI provides a Naming Service through the RMI
Registry that simplifies how programs specify
„ RMI does a great job of providing natural call the location of remote objects.
semantics for remote objects/methods. … This
naming service is a JDK utility called
… Simply a few additional Exceptions that you need to rmiregistry that runs at a well known address (by
handle. default).

3 4
Netprog: Java RMI Netprog: Java RMI

RMI Adds a few layers Remote Object References


„ The client acquires a reference to a remote
object.
Client App. Server App. … This part is different from creating a local object.

Stubs Skeleton
„ The client calls methods on the remote object
Remote Reference Remote Reference
… No (syntactic) difference!
Transport Transport … Just need to worry about a few new exceptions.

5 6
Netprog: Java RMI Netprog: Java RMI
Overview of RMI Programming Java Interfaces
„ Similar to Class
„ Define an interface that declares the methods that „ No implementation! All methods are
will be available remotely. abstract (virtual for C++ folks).
„ The server program must include a class that
implements this interface. „ Everything is public.
„ The server program must create a remote object and „ No fields defined, just Methods.
register it with the naming service. „ No constructor
„ an Interface is an API that can be
„ The client program creates a remote object by asking
the naming service for an object reference. implemented by a Class.

7 8
Netprog: Java RMI Netprog: Java RMI

Interfaces and Inheritence Sample Interface


„ In Java a class can only extend a single public interface Shape {
superclass (single inheritence). public getArea();
„ A class can implement any number of public draw();
interfaces. public fill(Color c);
… endresult is very similar to multiple }
inheritence.

9 10
Netprog: Java RMI Netprog: Java RMI

Implementing an Interface Server Details – extending Remote


public class Circle implements Shape {
private double radius;
„ Create an interface the extends the
private Point center;
java.rmi.Remote interface.
… This new interface includes all the public methods
// define a constructor and other that will be available as remote methods.
// methods
import java.rmi.*;
// MUST define the methods: public interface MyRemote extends Remote {
public int foo(int x) throws RemoteException;
// getArea();
public String blah(int y) throws RemoteException;
// draw(); . . .
// public fill(Color c); }
}
11 12
Netprog: Java RMI Netprog: Java RMI
How the interface will be used Server Details – Implementation
Class
Remote Interface Class RemoteServer

provides methods
„ Create a class that implements the
extends extends
needed by
interface.
Your Interface UnicastRemoteObject … The class should also extend
UnicastRemoteObject*
implements
extends „ This class needs a constructor that throws
RemoteException !
Class for your Remote
Object „ This class is now used by rmic to create
the stub and skeleton code.
*It doesn’t have to extend UnicastRemoteObject, there is another way…

13 14
Netprog: Java RMI Netprog: Java RMI

Remote Object Implementation Class Generating stubs and skeleton


public class MyRemoteImpl extends
UnicastRemoteObject implements MyRemote {

„ Compile the remote interface and


public MyRemoteImpl() throws RemoteException
{} implementation:

> javac MyRemote.java MyRemoteImpl.java


public int foo(int x) {
return(x+1);
} „ Use rmic to generate MyRemoteImpl_stub.class,
public String blah(int y) { MyRemoteImpl_skel.class
return(“Your number is “ + y);
> rmic MyRemoteImpl
}
}
15 16
Netprog: Java RMI Netprog: Java RMI

Server Detail – main() Client Details


„ The client needs to ask the naming service for a
„ The server main() needs to: reference to a remote object.
… create a remote object. … The client needs to know the hostname or IP address
… register the object with the Naming service. of the machine running the server.
… The client needs to know the name of the remote
public static void main(String args[]) { object.
try { „ The naming service uses URLs to identify
MyRemoteImpl r = new MyRemoteImpl(); remote objects.
Naming.bind(“joe”,r);
} catch (RemoteException e) {
. . .

17 18
Netprog: Java RMI Netprog: Java RMI
Using The Naming service Getting a Remote Object
„ Naming.lookup() method takes a string try {
parameter that holds a URL indicating the Object o =
remote object to lookup. Naming.lookup(“rmi://localhost/ReMath”);
rmi://hostname/objectname
MyRemote r = (MyRemote) o;
// . . . Use r like any other Java object!
„ Naming.lookup() returns an Object!
} catch (RemoteException re) {
„ Naming.lookup() can throw . . .
… RemoteException } catch (MalformedURLException up) {
… MalformedURLException throw up;
}

19 20
Netprog: Java RMI Netprog: Java RMI

Starting the Server Sample Code


„ First you need to run the Naming service „ There is sample RMI code on the course
server: homepage:
rmiregistry & … RemoteMathImpl: remote integer arithmetic

„ Now run the server:


java ServerMain

21 22
Netprog: Java RMI Netprog: Java RMI

Das könnte Ihnen auch gefallen