idl by huangyuarong

VIEWS: 3 PAGES: 32

									                      Distributed Systems
                           UQC003H3
                         OMG CORBA
                     Interface Definitions
                          Using IDL
Nigel.Baker@ecid.cig.mot.com           Distributed Systems
                             ORB Interfaces
                               IDL Interface                                 Implementation
                                Definitions                                    Installation



                 Interface                                                       Implementation
                Repository                     Client       Implementation         Repository
                                               Stubs          Skeletons

             Accesses               Includes               Includes          Describes

                                Client                            Object Implementation




                All objects are defined in IDL by specifying their interfaces.
                Object definitions (interfaces) are manifest as objects in the Interface Repository,
                 as client stubs, and as implementation skeletons.
                Descriptions of object implementations are maintained as objects in the
                 Implementation Repository.


Nigel.Baker@ecid.cig.mot.com                                                      Distributed Systems
                    Role of OMG IDL

                         Client Side                               Object
                                                               Implementation
                                                   C                Side           COBOL

                                     C++                                                          C
                                                   IDL                                IDL


                                             IDL                                            IDL
                           COBOL                                                                         Ada
                                        IDL                                                   IDL
                                                         ORB                    ORB
                                        IDL                                                   IDL

                               Ada                                                                      Small
                                             IDL                                            IDL          talk

                                                   IDL                                IDL
                                     Small
                                      talk                                                        C++

                                               More                                   More




Nigel.Baker@ecid.cig.mot.com                                                    Distributed Systems
                       IDL Variable Types
       PRIMITIVE:                    CONSTRUCTED:
        Integer:                     Structs
              short, long,
                                      Unions
              signed & unsigned
                                      Enums
           Floating point:
              float, double,         Sequences
               long double, fixed     Strings, wstrings
           char, wchar, boolean, octetArrays
                                     
           Any
           CORBA Object Reference

Nigel.Baker@ecid.cig.mot.com                   Distributed Systems
                  Basic IDL Data Types
                                                 typically
           IDL                  CORBA            typedef’d to

        short                  CORBA::Short     short
        long                   CORBA::Long      long
        unsigned short         CORBA::UShort    unsigned      short
        unsigned long          CORBA::ULong     unsigned      long
        float                  CORBA::Float     float
        double                 CORBA::Double    double
        char                   CORBA::Char      char
        boolean                CORBA::Boolean   unsigned      char
        octet                  CORBA::Octet     unsigned      char
        enum                   enum             enum




Nigel.Baker@ecid.cig.mot.com                       Distributed Systems
          Struct Declaration Example

            struct StoreTotals {
             float StoreTotal;
             float StoreTaxTotal;
            };
            readonly attribute
             StoreTotals Totals;


Nigel.Baker@ecid.cig.mot.com        Distributed Systems
          More Declaration Examples
       enum          ItemTypes {food, clothes, other};

       struct POSinfo {
         POS::POSid   id;
         StoreAccess StoreAccessReference;
         float        TotalSales;
         float        TotalTaxes;
       };
       typedef sequence <POSinfo> POSlist;


Nigel.Baker@ecid.cig.mot.com                  Distributed Systems
          Interfaces & Operations
               Interface:
                  a group of operations
                  the unit of inheritance
                  an object implements one or more
                   interfaces
               Operation:
                  required: return value, operation name, list
                   of parameters
                  optional: exceptions, oneway declaration




Nigel.Baker@ecid.cig.mot.com                         Distributed Systems
                               So, An IDL Interface:
           Module Lecture1 {
             typedef string mystringtype;
             interface example1 {
                 typedef short myshorttype;
                 long op1 (in long arg1,
                   in myshorttype myshort,
                   out mystringtype mystring)
             };
           };


Nigel.Baker@ecid.cig.mot.com                   Distributed Systems
                                 Attributes

            Just like any OMG IDL variable except -
                 IDL automatically generates set and get
                  operations
            Unless you declare readonly -
                 then just the get operation; no set
            A good place to keep state information
                 but not the only place !




Nigel.Baker@ecid.cig.mot.com                            Distributed Systems
                               Inheritance
         Derive a new interface from one or more existing
          interfaces
         Add new elements -
            constants, types, attributes, operations
           interface example1 {
                   long op1 (in long arg1)
           };
           interface example2:example1 {
                   void op2 (in short arg2)
           };

Nigel.Baker@ecid.cig.mot.com                     Distributed Systems
                          Language Mapping

                Maps from OMG IDL constructs to
                 programming language constructs
                IDL compiler uses to generate stubs and
                 skeletons
                Also a template file for your object
                 implementation




Nigel.Baker@ecid.cig.mot.com                            Distributed Systems
   Mapping Interfaces & Operations
             C:
                interfaces don't map
                operations map to functions
             C++ :
                Modules map to Namespaces
                interfaces map to classes
                operations map to member functions
             Smalltalk :
                operations map to Smalltalk messages
             Java:
                Modules map to Packages
                interfaces map to public Java interfaces plus
                 “Helper” and “Holder” Java classes
                operations map to methods
Nigel.Baker@ecid.cig.mot.com                        Distributed Systems
                 C Mapping Example

              This Basic IDL Interface Declaration
              interface example1 {
                    long op1 (in long arg1);
                    }
              Generates the following C declarations:
              typedef Object example1;               /* in C     */
              extern long example1_op1 (example1 o,
              Environment *ev, long arg1);




Nigel.Baker@ecid.cig.mot.com                             Distributed Systems
                C++ Mapping Example
                       The same IDL Declaration:
                       interface example1 {
                               long op1 (in long arg1);
                               }


                       is invoked as follows in C++:
                       // C++
                       // Declare object reference:
                                   example1_var           myex1;
                                   CORBA::long            mylongin, mylongout;
                       // code to retrieve a reference to an
                       // example1 object and bind it to myex1                . . .
                                   mylongout = myex1->op1 ( mylongin );


Nigel.Baker@ecid.cig.mot.com                                             Distributed Systems
                        Inheritance Example - C

                     This IDL Interface Declaration, inheriting from example1:
                     interface example2:example1 {
                          void op2 (in long arg2, out long arg3);
                          }
                     Generates the following C declarations:
                     typedef Object example2;                /* in C */
                     extern long example2_op1 (example2 o, Environment *ev,
                         long arg1);
                     extern void example2_op2(
                         example2 o,
                         Environment *ev,
                         long arg2,
                         long *arg3
                     );



Nigel.Baker@ecid.cig.mot.com                                              Distributed Systems
                       Inheritance Example - C++
            This IDL Interface Declaration, inheriting from example1:
            interface example2:example1 {
                 void op2 (in long arg2, out long arg3);
                 }
            is used as follows in C++:
            // C++
            // Declare object reference:
                      example2_var     myex2;
                      CORBA::long      mylongin, mylongout;
            // code to retrieve a reference to an
            // example2 object and bind it to myex2 . . .

            // now we can invoke not only . . .
                   void = myex2->op2 ( mylongin, mylongout );

            // but also, through inheritance,
                   mylongout = myex2->op1 ( mylongin );


Nigel.Baker@ecid.cig.mot.com                                            Distributed Systems
       CORBA Programming Steps
          Define the IDL Interfaces and compile
          Implement the IDL interfaces ( using Java, C++, …)
          Write a server main function which creates instances of the
           classes and register the server with the ORB
          Write a client application to find and use the server’s objects.

          Compile and link generated code to produce client and server
          Run the distributed application




Nigel.Baker@ecid.cig.mot.com                                 Distributed Systems
         Simple Java-CORBA Example
            Based on example given in Orfali and Harkey’s Clent Server Programming with Java & CORBA



         The IDL Interface                                          Description
                                                          The interface count has a single
                                                          method called increment which
          // File: Count.idl                              increments by one the value of a
                                                          variable sum which is returned to
          module Counter                                  the client.
                                                          sum is defined to be a CORBA
          {                                               read/write attribute. Clients can use
            interface Count                               built-in set and get CORBA
            { attribute long sum;                         accesssor functions.
                                                          A module is the equivalent of a
             long increment();                            package in Java and provides a
            };                                            naming context for a set of related
          };                                              interfaces.


Nigel.Baker@ecid.cig.mot.com                                                    Distributed Systems
      The counter Implementation
                               Simple Java-CORBA Example cont...


                                                                        Description
 //   File: CountImpl.java:                                        This is the implementation
                                                                   of the count IDL interface.
 class CountImpl extends Counter._CountImplBase
 {                                                                 This server implementation
   private int sum;                                                class inherits (extends)
                                                                   _xxxxImplBase class the
   // Constructors                                                 skeleton generated by the
   CountImpl(String name)                                          IDL compiler. Another way
   { super(name);                                                  to associate it with the
     System.out.println("Count Object Created");                   skeleton is by delegation
     sum = 0;                                                      (the Tie method).
   }
                                                                   We also have to implement
                                                                   a constructor.


Nigel.Baker@ecid.cig.mot.com                                                Distributed Systems
     The counter Implementationcont...
                               Simple Java-CORBA Example cont...



                                                          Description
        // get sum
          public int sum()
          { return sum;
                                                The constructor calls is super class
          }
                                                which in this case is the skeleton to
          // set sum
                                                create a named implementation object.
          public void sum(int val)
                                                This means that each named instance of
          { sum = val;
                                                CountImpl will have a persistent name.
          }
                                                If super is called without and argument
         // increment method
                                                then a transient object will be created.
          public int increment()
          { sum++;
            return sum;
          }
        }



Nigel.Baker@ecid.cig.mot.com                                           Distributed Systems
           Server Interaction Diagram
                                               ORB   BOA
        CountServer              CountImpl

                       ORB.init
                               BOA_init
                           new

                                Obj_is_ready


                               impl_is_ready


Nigel.Baker@ecid.cig.mot.com                         Distributed Systems
          The Main Server Programcont...
                               Simple Java-CORBA Example cont...


  // CountServer.java: The Count Server main program

  class CountServer
  { static public void main(String[ ] args)
   { try
     { // Initialize the ORB
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

       // Initialize the BOA ( in future calls will be to the POA )
       org.omg.CORBA.BOA boa = orb.BOA_init();

       // Create the Count object
       CountImpl count = new CountImpl("My Count");



Nigel.Baker@ecid.cig.mot.com                                       Distributed Systems
           The Main Server Programcont...
                               Simple Java-CORBA Example cont...


  // CountServer.java: The Count Server main program continued ……..

          // Export to the ORB the newly created object
          boa.obj_is_ready(count);

          // Ready to service requests
          boa.impl_is_ready();
          }
          catch(org.omg.CORBA.SystemException e)
          { System.err.println(e);
          }
      }
  }



Nigel.Baker@ecid.cig.mot.com                                       Distributed Systems
            Client Interaction Diagram
         Client                ORB    CountHelper   Count “proxy”       CountImpl



           Orb.init

                                 bind


                                     Sum(0)
                                                             Sum(0)
                                increment
                                                            increment

Nigel.Baker@ecid.cig.mot.com                                 Distributed Systems
                        The Client Program
                               Simple Java-CORBA Example cont...


   // CountClient.java Static Client, VisiBroker for Java

   class CountClient
   { public static void main(String args[])
    { try
      { // Initialize the ORB
       System.out.println("Initializing the ORB");
       org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

        // Bind to the Count Object
        System.out.println("Binding to Count Object");
        Counter.Count counter = Counter.CountHelper.bind(orb, "My Count");

        // Set sum to initial value of 0
        System.out.println("Setting sum to 0");
        counter.sum((int)0);


Nigel.Baker@ecid.cig.mot.com                                       Distributed Systems
                   The Client Programcont...
                               Simple Java-CORBA Example cont...

              // Calculate Start time
                    long startTime = System.currentTimeMillis();
                    // Increment 1000 times
                    System.out.println("Incrementing");
                    for (int i = 0 ; i < 1000 ; i++ )
                    { counter.increment();
                    }
                   // Calculate stop time; print out statistics
                    long stopTime = System.currentTimeMillis();
                    System.out.println("Avg Ping = "
                                + ((stopTime - startTime)/1000f) + " msecs");
                    System.out.println("Sum = " + counter.sum());
                  } catch(org.omg.CORBA.SystemException e)
                  { System.err.println("System Exception");
                    System.err.println(e);
                  }
                }
              }
Nigel.Baker@ecid.cig.mot.com                                           Distributed Systems
     Class & Interface Generation
                                    Simple Java-CORBA Example cont...


                                       javac -d CountClient.java
      CountClient.java                                                          CountClient

                                                       _st-Count
                                                     CountHelper

                                                     CountHolder
        count.idl              idl2java count.idl

                                                    _CountImplBase
                                                         Count

      CountImpl.java                                _example_Count

                                      javac -d CountImpl.java
                                                                               CountServer
     CountServer.java                 javac -d CountServer.java



Nigel.Baker@ecid.cig.mot.com                                            Distributed Systems
                IDL Generated Classes
        _CountImplBase - Class implements server side skeleton
        Count -         maps Count interface to corresponding Java Interface

        -example_Count - an example class for the Count object
         implementation

        _st_Count - client stub class
        CountHelper - class that provides useful helper functions
        CountHolder - class that handles OUT & INOUT parameters




Nigel.Baker@ecid.cig.mot.com                                  Distributed Systems
       Java Helper & Holder Classes
          associated <>Helper class:
             “Narrow” method allows clients to cast object
              references class
             non-standard bind method
             any insertion/extraction
             get repository ID
             get typecode
          associated <>Holder class:
             supports stream for portable stubs & skeletons
             enable out and inout parameters


Nigel.Baker@ecid.cig.mot.com                        Distributed Systems
                  Java Mapping Example
        IDL Declaration to show parameter passing:
        interface example1 {
              long op1 (       in long arg1,
                               out long arg2,
                               inout long arg3);
              }
        produces the following Java:
        public interface example1 {
             int op1 (         int arg1,
                               IntHolder arg2,
                               IntHolder arg3)
             }

Nigel.Baker@ecid.cig.mot.com                         Distributed Systems
           IDL Operation Parameters
        The IN parameter is passed from client to server and it is
         expected that the server will not modify it. Pass by value
         semantics.
        The OUT parameter is passed from server to client which will
         be available to the client to modify. Pass by reference semantics.
        The INOUT parameter is passed from the client to the server.
         The server modifies the parameter which is then available to the
         client. Pass by reference semantics.
        A request may also return a single return value. Pass by value
         semantics. There can be any number of IN, OUT, INOUT
         parameters.

Nigel.Baker@ecid.cig.mot.com                               Distributed Systems

								
To top