Remote Object Invocation Tran, Van Hoai Department of Systems & Networking Faculty of Computer...
-
Upload
angelica-dixon -
Category
Documents
-
view
215 -
download
0
Transcript of Remote Object Invocation Tran, Van Hoai Department of Systems & Networking Faculty of Computer...
Remote Object Invocation
Tran, Van HoaiDepartment of Systems & Networking
Faculty of Computer Science & EngineeringHCMC University of Technology
2009-2010 2
Outline
• Distributed objects– Binding client to object– Static vs Dynamic– Parameter passing
• Examples– Java RMI
2009-2010 3
Why object-oriented ?
OO has strong features information hidingdata abstractionencapsulationmodularitypolymorphismand inheritance
OOP software engineering• Modelling• Design• Language• Database• Scripting• …
Enhancement of RPC
• RPC– simple– effective– standard for communication in distributed system
development
Object Oriented + Remote Procedure Call = Remote Object Invocation
Distributed object model (1)
Client OS
Client
ServerOS
Server
Clientinvokes a method
Skeleton invokes same method at object
Client machine Server machine
Proxy
Network
Proxysame interface as object
Marshalled invocation passed across network
Skeleton
interface
object
state
method
Distributed object model (2)
• When a client binds to a distributed object, load the interface (“proxy”) into client address space– Proxy analogous to stubs
• Server stub is referred to as a skeleton
Characteristics for distributed objects (1)
• Compile time vs runtime objects– compile time objects: related to language-level
objects• Object = instance of a class• Interfaces compiled into client, server’s stubs• Drawback: depend strongly on particular programming
language
– runtime objects: how object implemented left open• need an adapter to bind dynamically invocation to
implementation
Characteristics for distributed objects (2)
• Persistent vs transient objects– persistent objects: not depend on server process• object can be stored in secondary storage if server exits• and restored when new server started
– transient objects: exists as long as server managing it
Binding client to object
• Distributed object model provides systemwide reference– can be passed as parameters
• Binding needed before invoking methods– In practice, binding temporarily creates proxy– 2 approaches• Implicit binding• Explicit binding
Implicit vs Explicit bindings
Implicit
Distr_object *obj_ref;obj_ref = ...;obj_ref->do_something();
Explicit
Distr_object *obj_ref;Local_object *obj_ptr;obj_ref = ...;obj_ptr = bind( obj_ref );obj_ptr->do_something();Explicit bind and get a pointer to proxy
Invoke a method on local proxy
Object reference implementation issuses (1)
• Goal: to allow client binding to an object• Simple approach– reference = (network address, server ID, object ID)– server ID = port (in practice)
• Drawbacks– what happens if server machine crashes, and the
server has a new server ID ?• Client can contact with a service to provide mapping
between server ID and server• The server must register with the service
Object reference implementation issuses (2)
• Drawbacks– what happens if the server moved to other
machine ?• a server to provide a mapping, called location server• reference = (location server address, systemwide server
ID)
Remote Method Invocation
• Similar to RPC, but has an advantages of systemwide object reference
• static invocation:– predefined interface definition– interface known when client being developedfobject.append(int)
• dynamic invocation:– select at runtime which method to be invoked at
remote objectinvoke( fobject, id(append), int)
Passing object references(local & remote)
local object O1 remote object O2
copy of O1 copy of R1 to O2
server code(method implementation)
remote reference R1
new local reference
remote invocation with L1 and R1 as parameters
client code with RMI to server at C
local reference L1
machine A machine B
machine C
DCE Distributed object model
Dynamic (private) object
Dynamic (private) object
Dynamic (private) object
client #1 client #2 client #3
named (shared) object
remote reference
server machine server machine
Distributed dynamic objects Distributed named objects
• object created for individual client• a function “create()” to new an object
• object not for a single client• client looks up named object at directory service
Java RMI
• Java RMI allows programmer to execute remote function class using the same semantics as local functions calls
Local Machine (Client)
SampleServer remoteObject;int s;…
s = remoteObject.sum(1,2);
System.out.println(s);
Remote Machine (Server)
public int sum(int a,int b) { return a + b;}
1,2
3
Java RMI architecture
• The server must first bind its name to the registry
• The client lookup the server name in the registry to establish remote references.
• The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub.
RMI Server
skeleton
stub
RMI Client
Registry
bind
lookupreturn call
Local Machine
Remote Machine
Naming service (1)
• Directory that associates names to remote objects (bind)
server machine
RemoteObject
C
RemoteObject
A
RemoteObject
B
Naming
“X”
“Y”
“Z”
Naming service (2)
• Client use Naming Service to find a particular Server object (lookup)
client machine server machine
ObjectClient
RemoteObjectServer
Naming
“X”
“Y”
“Z”
lookup(“Y”)
Remote reference to Server
Steps for Developing an RMI System
1. Define the remote interface2. Develop the remote object by implementing the
remote interface.3. Develop the client program.4. Compile the Java source files.5. Generate the client stubs and server skeletons.6. Start the RMI registry.7. Start the remote server objects.8. Run the client
Server/* SampleServerImpl.java */ public static void main(String args[]) { try { System.setSecurityManager(new RMISecurityManager()); //set the security manager
//create a local instance of the object SampleServerImpl Server = new SampleServerImpl();
//put the local instance in the registry Naming.rebind("SAMPLE-SERVER" , Server);
System.out.println("Server waiting....."); } catch (java.net.MalformedURLException me) { System.out.println("Malformed URL: " + me.toString()); } catch (RemoteException re) { System.out.println("Remote exception: " + re.toString()); } }
Clientimport java.rmi.*;import java.rmi.server.*;public class SampleClient { public static void main(String[] args) { // set the security manager for the client System.setSecurityManager(new RMISecurityManager()); //get the remote object from the registry try { System.out.println("Security Manager loaded"); String url = "//localhost/SAMPLE-SERVER"; SampleServer remoteObject = (SampleServer)Naming.lookup(url); System.out.println("Got remote object"); System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) ); } catch (RemoteException exc) { System.out.println("Error in lookup: " + exc.toString()); } catch (java.net.MalformedURLException exc) { System.out.println("Malformed URL: " + exc.toString()); } catch (java.rmi.NotBoundException exc) { System.out.println("NotBound: " + exc.toString()); } }}