lecture 6

Document Sample
lecture 6 Powered By Docstoc
					                     Computer Science
                    Distributed Systems

                   Lecture 9
       Inter-process Communication &
               Distributed Objects

 2002, M. T. Harandi and J. Hou          Lecture 9-1
Middleware Layers


                        RMI, RPC and events
                        Request reply protocol        layers

                     External data representation

                          Operating System

   2002, M. T. Harandi and J. Hou                     Lecture 9-2
Distributed Objects
• Object
   – consists of a set of data and a set of methods.
• Object reference
   – an identifier via which objects can be accessed.
   – In Java a variable that appears to hold an object actually holds a
     reference to that object.
• Interface
   – provides a definition of the signatures of a set of methods (i.e., the
     types of their arguments, return values, and exceptions) without
     specifying their implementation.
• Remote method invocation
   – method invocations between objects in different processes.
• Remote objects
   – objects that can receive remote invocations.
• Remote object reference
   – an identifier that can be used throughout a distributed system to refer
     to a particular unique remote object.
• Remote interface
   – Every remote object has a remote interface that specifies which of its
      methods can be invoked remotely.
   2002, M. T. Harandi and J. Hou                                    Lecture 9-3
Remote and Local Method Invocations

                          remote          local        C
                         invocation       invocation local
                                                   invocation    invocation   F
                                      B        local
                                            invocation     D

   2002, M. T. Harandi and J. Hou                                    Lecture 9-4
A Remote Object and Its Remote Interface


                                     m1   implementation   m4

                              {      m2
                                     m3    of methods

   2002, M. T. Harandi and J. Hou                              Lecture 9-5
RMI Failure Modes

                                correct                        lost
                 Execute                                       request


       Request                                                 Channel
                                                   Execute     fails
                 Execute,       crash                          reply
                  Crash         before     Reply

       Request                                                  Client
                                                     Execute    fails
                              crash                             before
                 Crash        before
                                             Reply              receiving
                              execution                         reply

   2002, M. T. Harandi and J. Hou                               Lecture 9-6
RMI Invocation Semantics

                                                               whether to keep a
    Whether or not to                                          history of result
    retransmit the request           when retransmissions
                                                               messages to enable
    message until either             are used, whether to
                                                               lost results to be
    a reply is received or           fliter out duplicate
                                                               retransmitted without
    the server is assumed            requests at the server.
                                                               re-executing the operations
    to be failed

                        Fault tolerance measures                           Invocation

    Retransmit request        Duplicate            Re-execute procedure
        message               filtering             or retransmit reply
    No                       Not applicable Not applicable                    Maybe
    Yes                      No                    Re-execute procedure At-least-once

    Yes                      Yes                   Retransmit reply        At-most-once

   2002, M. T. Harandi and J. Hou                                             Lecture 9-7
Proxy and Skeleton in Remote Method

             client                                          server
     object A proxy for B                                 skeleton
                                                                       object B
                                    Request            & dispatcher
                                                       for B’s class


      Remote    Communication               Communication Remote reference
  reference module   module                  module          module

  2002, M. T. Harandi and J. Hou                                            Lecture 9-8
Remote Reference Module
  • Is responsible for translating between local and remote
    object references and for creating remote object references.
  • Has a remote object table
       – An entry for each remote object held by the process.
       – An entry for each local proxy.
  • When a remote object is to be passed as argument for the
    first time, the remote reference module creates a remote
    object reference and adds it to the table.
  • When a remote object reference arrives in a request or reply
    message, the remote reference module is asked for the
    corresponding local object reference, which may refer either
    to a proxy or to a remote object.
       – In the case that the remote object reference is not in the table, the RMI
         software creates a new proxy and asks the remote reference module to
         add it to the table.

   2002, M. T. Harandi and J. Hou                                       Lecture 9-9
 • Is responsible of making RMI transparent to
   clients by behaving like a local object to the
      – The proxy implements the methods in the remote interface of
        the remote object it represents.
 • Instead of executing an invocation, the proxy
   forwards it to a remote object.
      – Each method of the proxy marshals (i) a reference to the target
        object, (ii) its own method Id and (iii) its arguments into a
        request message and sends it to the target, awaits the reply
        message, un-marshals it and returns the results to the invoker.

  2002, M. T. Harandi and J. Hou                              Lecture 9-10
Marshalling & Unmarshalling
  External data representation: an agreed standard
    for the representation of data structures and
    primitive values.
      CORBA Common Data Representation (CDR)
      Java’s object serialization.
  Marshalling: the process of taking a collection of
    data items and assembling them into a form
    suitable for transmission.
      Translation of non-portable representations.
       Assembly of data items into an external data rep.

   Unmarshalling: the process of disassembling
    transmitted data at the destination.
       generation of primitive values
       reconstruction of structured data

   2002, M. T. Harandi and J. Hou                          Lecture 9-11
Dispatcher and Skeleton
 • A server has one dispatcher and skeleton for each
   class representing a remote object.
 • The dispatcher receives the request message
   from the communicating module.
     – It uses the method Id to select the appropriate method in the
       skeleton, passing on the request message.
 • Skeleton implements the methods in the remote
     – A skeleton method un-marshals the arguments in the request
       message and invokes the corresponding method in the remote
     – It waits for the invocation to complete and marshals the result,
       together with any exceptions, in a reply message.

   2002, M. T. Harandi and J. Hou                              Lecture 9-12
Generation of Proxies, Dispatchers and
 • In CORBA, interfaces of remote objects are
   defined in CORBA IDL, and the interface compiler
   is used to generate the classes for proxies,
   dispatchers and skeletons.
 • In Java RMI
      – the set of methods offered by a remote object is defined as a
        Java interface that is implemented in the remote object.
      – The Java RMI compiler generates the proxy, dispatcher and
        skeleton classes from the class of the remote object.

  2002, M. T. Harandi and J. Hou                              Lecture 9-13
Java Remote Interfaces Shape and ShapeList

  import java.rmi.*;
  import java.util.Vector;
  public interface Shape extends Remote {
    int getVersion() throws RemoteException;
    GraphicalObject getAllState() throws RemoteException;     1
  public interface ShapeList extends Remote {
    Shape newShape(GraphicalObject g) throws RemoteException; 2
    Vector allShapes() throws RemoteException;
    int getVersion() throws RemoteException;

   2002, M. T. Harandi and J. Hou                Lecture 9-14
Remote Method Invocation (RMI)
     CLIENT            Proxy                     Proxy object is a hollow
                       Object                    container of Method
                         B                       names.
       Object A                      Remote
                                     Reference   Remote Reference
                                     Module      Module translates
                       Comm.                     between local and
                       Module                    remote object

                                                 Dispatcher sends the
                        Comm.         SERVER     request to Skeleton
                        Module                   Object
                                                 Skeleton unmarshals
      Dispatcher                                 parameters, sends it
                        Skeleton       Object    to the object, &
                         for B’s         B
                                                 marshals the results
                          Class                  for return

   2002, M. T. Harandi and J. Hou                               Lecture 9-15
Binder and Activator
 • A separate service that maintains a table containing
   mappings from textual names to remote object references.
     – Used by servers to register their remote objects by name and by
       clients to look them up.
 • Activation of remote objects
     – A remote object is active when it is available for invocation within a
       running process.
     – A passive object consists of (i) implementation of its methods; and (ii)
       its state in the marshalled form.
     – Activation creates a new instance of the class of a passive object and
       initializing its instance variables.
     – An activator is responsible for
          » Registering passive objects (recording the names of the servers
             against the URLs of the passive objects)
          » Starting named server processes and activating remote objects in

   2002, M. T. Harandi and J. Hou                                      Lecture 9-16
The Naming Class of Java RMIregistry
  void rebind (String name, Remote obj)
       This method is used by a server to register the identifier of a remote
       object by name, as shown in Figure 5.13, line 3.
  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, as
       shown in Figure 5.15 line 1. A remote object reference is returned.
  String [] list()
       This method returns an array of Strings containing the names bound in
       the registry.

   2002, M. T. Harandi and J. Hou                                  Lecture 9-17
Remote Procedure Call (RPC)
   Uniform, reusable, user-friendly, and
     action based.
        Provide a familiar interface for the application developer
        Implement the request-reply primitive
        Format of the message is standard
        Supports code reuse

   Client calls for invocation of a procedure at
     the server machine.
        Semantics are similar to procedure calls in local programs
        Standard interface, independent of applications

   A library of reusable procedures,
            distributed over all sites.
    2002, M. T. Harandi and J. Hou                         Lecture 9-18
Client and Server Stub Procedures in RPC

          client process                                    server process


            client stub                                         server stub
            procedure                                           procedure
  client                                                                       service
  program        Communication               Communication                    procedure
                     module                   module     dispatcher

   2002, M. T. Harandi and J. Hou                                             Lecture 9-19
   Stubs are generated automatically
    from interface specifications.
   Stubs hide details of (un)marshalling
    from application programmer & library
    code developer.
   Client Stubs perform marshalling
   Server Stubs perform unmarshalling
   Stubs also take care of
    communication & invocation

   2002, M. T. Harandi and J. Hou   Lecture 9-20
The Stub Generation Process
                                            Compiler / Linker

                                         Server         Server
                                          Stub          Source

    Interface               Stub      Common          RPC
   Specification          Generator    Header       LIBRARY

                                         Client          Client
                                          Stub          Source

                                            Compiler / Linker

   2002, M. T. Harandi and J. Hou                              Lecture 9-21
Files Interface in Sun XDR
          const MAX = 1000;
          typedef int FileIdentifier;
          typedef int FilePointer;
          typedef int Length;           struct readargs {
          struct Data {                     FileIdentifier f;
              int length;                   FilePointer position;
              char buffer[MAX];             Length length;
          };                            };
          struct writeargs {
              FileIdentifier f;         program FILEREADWRITE {
              FilePointer position;       version VERSION {
              Data data;                    void WRITE(writeargs)=1;        1
          };                                Data READ(readargs)=2;          2
                                        } = 9999;

   2002, M. T. Harandi and J. Hou                           Lecture 9-22
Finding RPCs
     CLIENT                          Finding An RPC:
                          Client     RPCs live on specific hosts
                           Stub      at specific ports.
                         Comm.       Port mapper on the host
                         Module      maps from RPC name to
                                     When a server is initialized,
                                     it registers its RPCs with
                                     the port mapper (handle)
         Comm.              SERVER
         Module                      A client first connects to
                                     port mapper to to get this
       Dispatcher                    handle
                         procedure   The call to RPC is then
          Server                     made by connecting to the
                                     corresponding port

   2002, M. T. Harandi and J. Hou                          Lecture 9-23
Dealing Room System
           Dealer’s computer                            External                  Dealer’s computer

                 Dealer        Notification                        Notification         Dealer

                               Notification        Information
                                                       provider Notification

         Dealer’s computer                                                         Dealer’s computer
             Dealer                                                Notification

   2002, M. T. Harandi and J. Hou                                                               Lecture 9-24
Architecture for Distributed Event Notification

                                            Event service
                            object of interest                             subscriber

                             1.                             notification

                       object of interest        observer                  subscriber

                       2.          notification             notification

  object of interest              observer                                 subscriber

  3.                                                        notification

    2002, M. T. Harandi and J. Hou                                           Lecture 9-25
Java Class ShapeListServer with main Method

  import java.rmi.*;
  public class ShapeListServer{
     public static void main(String args[]){
          System.setSecurityManager(new RMISecurityManager());
              ShapeList aShapeList = new ShapeListServant();                      1
                  Naming.rebind("Shape List", aShapeList );                       2
              System.out.println("ShapeList server ready");
              }catch(Exception e) {
              System.out.println("ShapeList server main " + e.getMessage());}

   2002, M. T. Harandi and J. Hou                                 Lecture 9-26
Java class ShapeListServant Implements
Interface ShapeList
  import java.rmi.*;
  import java.rmi.server.UnicastRemoteObject;
  import java.util.Vector;
  public class ShapeListServant extends UnicastRemoteObject implements ShapeList
      private Vector theList;         // contains the list of Shapes         1
      private int version;
     public ShapeListServant()throws RemoteException{...}
     public Shape newShape(GraphicalObject g) throws RemoteException {       2
              Shape s = new ShapeServant( g, version);                       3
              return s;
     public Vector allShapes()throws RemoteException{...}
     public int getVersion() throws RemoteException { ... }
    2002, M. T. Harandi and J. Hou                              Lecture 9-27
Java Client of ShapeList

    import java.rmi.*;
    import java.rmi.server.*;
    import java.util.Vector;
    public class ShapeListClient{
      public static void main(String args[]){
        System.setSecurityManager(new RMISecurityManager());
        ShapeList aShapeList = null;
             aShapeList = (ShapeList) Naming.lookup("//bruno.ShapeList") ;
             Vector sList = aShapeList.allShapes();
        } catch(RemoteException e) {System.out.println(e.getMessage());
        }catch(Exception e) {System.out.println("Client: " + e.getMessage());}

   2002, M. T. Harandi and J. Hou                                 Lecture 9-28
Classes Supporting Java RMI



              Activatable               UnicastRemoteObject

                                                    <servant class>

   2002, M. T. Harandi and J. Hou                            Lecture 9-29

Shared By:
xiaohuicaicai xiaohuicaicai