Presentation is loading. Please wait.

Presentation is loading. Please wait.

Remote method invocation (RMI)

Similar presentations


Presentation on theme: "Remote method invocation (RMI)"— Presentation transcript:

1 Remote method invocation (RMI)

2 معایب فراخوانی رویه راه دور:
فضای کمی برای کد عدم استقلال از زبان روشی نقطه به نقطه مدل مشتری و سرور و نه همتا به همتا راه حل : استفاده از اشیاء توزیع شده

3 سه مدل اشیاء توزیع شده معروف:
CORBA : معماری عمومی مستقل از زبان و سیستم عامل DCOM : از مایکروسافت و وابسته به ویندوز ولی مستقل از زبان RMI : مستقل از سیستم عامل ولی وابسته به زبان جاوا

4 Remote method invocation (RMI)
Related to RPC but extended into the world of distributed objects In RMI, a calling object can invoke a method in a remote object Underlying details are hidden from the user Commonalities (RMI, RPC) are: They both support programming with interfaces They both typically constructed on top of request-reply protocols and can offer a range of call semantics They both offer a similar level of transparency

5 Remote and local method invocations (RMI)
B C D E F

6 Fundamental concepts are heart of the distributed object:
Remote object references: Objects can invoke the methods of a remote object, access to its remote object reference Remote interfaces: Remote object has remote interface, specifies its methods can invoked remotely Action: is initiated by an object invoking a method in another object, invocation of a method can have three effects: State of the receiver be changed New object may be instantiated Further invocations on methods in other objects

7 RMI A remote object and its remote interface
Data implementation object { of methods reference

8 Implementation of RMI The role of proxy and skeleton in remote method invocation

9 RMI (modules) Communication module:
Do request-reply protocol (transmits request and reply messages) Specified invocation semantics, for example at-most-once In the server selects the dispatcher Remote reference module: Translating between local and remote object references For creating remote object references Remote object table that records the correspondence between local and remote object references, table includes: an entry for all remote objects held by the process in table at the server, an entry for each local proxy in the table at the client

10 RMI (modules) Servants:
An instance of a class that provides the body of a remote object Handles the remote requests passed on by the corresponding skeleton Live within a server Created when remote objects are instantiated and remain in use until No longer needed being deleted

11 RMI Software RMI software consists of a layer of software between the application objects and communication and remote reference modules, roles of it are as follows (Proxy, Dispatcher, Skeleton) Proxy: Make remote method invocation transparent to clients by behaving like a local object Dispatcher: One dispatcher and one skeleton for each class representing a remote object, Uses operationId to select the appropriate method in the skeleton

12 Skeleton: Each class of a remote object has a skeleton
Implemented quite differently from the methods in the servant Skeleton unmarshals the arguments in the request message and invokes the corresponding method in the servant Marshals the result, together with any exceptions, in reply message

13 Classes for the proxy, dispatcher and skeleton used in RMI are generated automatically by an interface Compiler: In CORBA, interfaces of remote objects are defined in CORBA IDL, and the interface compiler can be used to generate the classes proxies, dispatchers and skeletons in C++ or Java In Java RMI, the set of methods offered by a remote object is defined as a Java interface. Java RMI compiler generates the proxy, dispatcher and skeleton classes

14 Binder: Client require, obtaining a remote object reference for remote objects in server Binder is separate service that maintains a table mappings from names to remote object references By servers to register their remote objects by name By clients to look them up CORBA Naming Service Java binder, RMIregistry

15 Java Remote interfaces Shape and ShapeList
Remote interfaces are defined by extending an interface called Remote provided in the java.rmi package. import java.rmi.*; import java.util.Vector; public interface Shape extends Remote { int getVersion() throws RemoteException; GraphicalObject getAllState() throws RemoteException; } public interface ShapeList extends Remote { Shape newShape(GraphicalObject g) throws RemoteException; 2 Vector allShapes() throws RemoteException;

16 RMIregistry RMIregistry is the binder for Java RMI
An instance of RMIregistry should normally run on every server computer It maintains a table mapping, URL-style names to references to remote objects hosted on computer Is accessed by methods of the Naming class, whose methods take as an argument a URL-formatted string of the form: //computerName:port/objectName

17 Java RMIregistry The methods of the Naming class
void rebind (String name, Remote obj) This method is used by a server to register the identifier of a remote object by name void bind (String name, Remote obj) This method can alternatively be used by a server to register a remote object by name, but if the name is already bound to a remote object reference an exception is thrown. void unbind (String name, Remote obj) This method removes a binding. Remote lookup(String name) This method is used by clients to look up a remote object by name. A remote object reference is returned. String [] list() This method returns an array of Strings containing the names bound in the registry.

18   مثال: اینترفیس سرور import java.rmi.*; public interface NameCollections extends Remote //Remote Interface { public String getName (int index) throws RemoteException; //Remotely Accessible Method\ }

19 کلاسی که این اینترفیس را در سرور پیاده‌سازی میکند
  کلاسی که این اینترفیس را در سرور پیاده‌سازی میکند import java.rmi.*; import java.util.*; import java.rmi.server.*; class NameStorage extends UnicastRemoteObject implements NameCollections { //A Class That Implements The Remote Interface private Map name; public NameStorage () throws RemoteException{ name=new HashMap(); name.put(1,"iman"); name.put(2,"reihanian"); } public String getName (int index)throws RemoteException{ String str=name.get(new Integer(index)); return str;

20 در ادامه پیاده سازی RMI در سرور
import java.rmi.*; import javax.naming.*; class NameServer { public static void main (String args[]) throws RemoteException,NamingException { Context ictxt=new InitialContext(); NameStorage store=new NameStorage(); ictxt.bind("rmi:Storage",store); //Binding The Remote Object(store) With A Name(storage) System.out.println("Clients can invoke the methods...."); }

21 قسمت کلاینت import java.rmi.*; import java.util.*; import javax.naming.*; class NameClient { public static void main (String args[]) throws NamingException,RemoteException { Context itxt=new InitialContext(); //Searching For The Remote Object In The RMI registry NameCollections coll= (NameCollections) itxt.lookup("rmi://localhost/Storage"); String n1=coll.getName(2);// Invoking The Remote Method System.out.println("Name Obtained from the server:" + n1); }


Download ppt "Remote method invocation (RMI)"

Similar presentations


Ads by Google