Distributed Object Systems
– 3 –
CORBA/IDL
Piet van OostrumSep 11, 2008
DOS-3
Corba
I Common Object Request Broker Architecture
I Middleware for communicating objects
I Context
I Object Management Group (OMG)
I Consortium of computer companies (Sun, HP, IBM, ...) I Defines standards (interfaces) for support of (distributed) OO
applications
I Also for example UML
I Based on concrete available technology, propositions by
consortium members
I Status
I Many services defined
I But not all implemented by all distributions yet I Many commercial and open source implementations I New services invented occasionally (e.g. Corba Components)
Piet van Oostrum DOS-3 1
DOS-3
Corba
Distributed Object Systems 03/02/98 © Atze Dijkstra
Java RMI 3 Dept. of Comp.Science, UU
Corba
•
The (very) global picture
Business Objects Object Services
"System Frameworks"
Object Bus CommonFacilities
"Application Frameworks"
Collaboration Interoperability
The (very) global picture
Piet van Oostrum DOS-3 2
DOS-3
OMG Object Management Architecture
Distributed Object Systems 03/02/98 © Atze Dijkstra
Java RMI 6 Dept. of Comp.Science, UU
Corba services
•
To make collaboration go well
Information Management Systems Management Task Management User Interface
Vertical Common Facilities
Horizontal Common Facilities
Common Object Services Common Facilities
Application Objects
Object Request Broker (ORB)
Naming Events Persistence Life Cycle Transactions Externalization Query Properties Concurrency Time Collections Relationships Trader Security Licensing Change Management
-
OMG Object Management Architecture
-
Not all services yet defined
Not all services yet defined
Piet van Oostrum DOS-3 3
DOS-3
Services
I Naming I Referencing by name I Naming schemes I Events I Sending eventsI Registering interest in events
I Persistence
I Storing objects
I Life Cycle
I Creating, copying, moving, deleting components on the bus
I Transactions
I Two-phase commit, rollback
Many more . . .
Piet van Oostrum DOS-3 4
DOS-3
Features
I Static and dynamic method invocation
I Compile time or runtime definition of method invocation
I High-level language bindings
I Interface Definition Language (IDL)
I Independent of language (C, C++, Smalltalk, Java, Python) I Translates to language code
I Self-describing system
I Interface Repository
I Contains information describing interfaces, methods (+
parameters), etc. a server provides
I Can be interrogated at runtime
Piet van Oostrum DOS-3 5
DOS-3
Example Quoter
I Looking like normal use of service I Notation used: C++
int main( void ) {
// Use a factory to bind to any quoter
Quoter_var quoter = find_quoter_service("Quoter") ; const char *stock_name = "ACME ORB, Inc." ;
long value = quoter->get_quote(stock_name) ; cout << stock_name << " = " << value << endl ; return 0 ;
}
Piet van Oostrum DOS-3 6
DOS-3
Quoter interface
I Functionality defined in OMG IDL
interface Quoter {
exception Invalid_Stock{} ;
long get_quote( in string stock_name ) raises (Invalid_Stock) ; };
I Interface Description Language I Used by both clients and servers I Language independent
I With compilers to specific languages I Location transparent
Piet van Oostrum DOS-3 7
DOS-3
CORBA structure
Dynamic Invocation Interface Repository Skeleton InterfaceORB Object Adapter DynamicInterface identical for all ORB implementations May be multiple object adaptors
ORB dependent interface
Stubs and a skeleton for each object type Static IDL Impl Repository Object ORB Core Skeleton Client Implementation StubsIDL Requests
Piet van Oostrum DOS-3 8
DOS-3
CORBA core components
I An Object Request Broker (ORB)
I Basic or Portable Object Adapter (BOA/POA) I An Interface Definition Language (IDL) I A Static Invocation Interface (SII) I A Dynamic Invocation Interface (DII) I A Dynamic Skeleton Interface (DSI) I Interface and Implementation Repositories I Programming language mappings I An Interoperability Spec (GIOP, IIOP)
GIOP (General Inter-ORB Protocol) = protocol between local ORBs
IIOP (Internet Inter-ORB Protocol) = GIOP through Internet (TCP/IP)
Piet van Oostrum DOS-3 9
DOS-3
Core components
I Object Request Broker (ORB)
I Central component
I Object location, creation, activation I Message exchange
I Has its own interface for local services I Local ORBs + G/IIOP = object bus
I NB. ORB is a distributed thing, but each local part is also
called ORB
I Usually implemented as a library inside the process
(e.g. DLL or Java package)
I Static Invocation Interface (SII)
I Client stubs (proxy pattern) I Static interface to object services
I Dynamic Invocation Interface (DII) is runtime variant,
interface repository is used for construction of calls
Piet van Oostrum DOS-3 10
DOS-3
Core components
I Static skeleton
I Framework through which calls are passed to object service I Dynamic Skeleton Interface (DSI) is dynamic variant
I Object adapter
I Provides mapping of object identifiers to implementation
instances
I Uses implementation repository
I Object identifiers
I ORB translates local identifier to Interoperable Object
References (IOR) and vice-versa
I IOR contains all information to locate the remote object
Piet van Oostrum DOS-3 11
DOS-3
Object Adapter
Tasks of an object adapter
Distributed Object Systems 03/02/98 © Atze Dijkstra
Java RMI 19 Dept. of Comp.Science, UU
Object Adaptor
•
Tasks of an object adaptor
Object Implementation Up-call to method Register new classes Instantiate new objects Manage object references Handle incoming requests Broadcast services ID ID ID Object Adapter Interface A skeleton Interface B skeleton Impl. Repository
Object adapter maps object ID’s to object instances Skeleton maps methods names to method implementations
Piet van Oostrum DOS-3 12
DOS-3
Basic Object Adapter (BOA)
I Basic service
I Barebones ORB I Variety of server kinds
I Server kinds
I Single process accepting calls, blocking while handling I Server per object starts at first call on object I Server per method starts for each call I Persistent server already exists
I BOA is deprecated
Piet van Oostrum DOS-3 13
DOS-3
Portable Object Adapter (POA)
I BOA is too restricted
I E.g. no support for moving objects or objects that are ‘sleeping’ in a database.
I BOA is underspecified
I Vendors made their own extensions→non-portable I POA solves this
I Many different ways to create/revive objects I Will be treated in a later lecture.
Piet van Oostrum DOS-3 14
DOS-3
Corba implementations
I There are many Corba implementations:
I Several commercial implementations
I Open source implementations (see selection below)
I C++:
I TAO (The Ace ORB) I OmniORB
I Orbit (used in Linux/Gnome)
I Java:
I built-in implementation in Java (called Java IDL) is not very
good
I JacORB is a better implementation I OpenORB (a bit antique)
I Python:
I Fnorb (written in Python) I OmniORB
Piet van Oostrum DOS-3 15
DOS-3
Using IDL
Using IDL
Client IDL stubs Object Impl's Server IDL stubs Client IDL stubs Object Impl's Server IDL stubs Object Adapter Server Client Client IDL stubs Object Impl's Server IDL stubs Instantiate Compile SkeletonsAdd server impl code Create IDL definitions
Interface Repository Impl. Repository Precompiler 6 7 4 3 5 2 1
Piet van Oostrum DOS-3 16
DOS-3
Using IDL
I From IDL to runtime info
I Define object classes using IDL
I Precompile IDL file, creates stubs and skeletons I Add implementation code to skeletons I Compile code, creating misc interfaces and info I Bind class definitions to interface repository (optional) I Instantiate objects on server
I Register run-time objects with the implementation repository
(optional)
I Calling
I Static interface as method invocation
Uses normalobject.methodsyntax
I Dynamic interface using special calls to construct message
method invocation is constructed at run-time.
Piet van Oostrum DOS-3 17
DOS-3
IDL compiler
srv.cpp x_skel.cpp x.cpp x_skel.h Client x.idl Client Executable app.java IIOP IDL Developer IDL-to-C++ Compiler IDL-to-Java Compiler x.java Java ORBRun-Time Server C++ ORBRun-Time
Developer Developer Executable Server x.h generated file (source: Iona)
Piet van Oostrum DOS-3 18
DOS-3
IDL
I We use a subset of Corba IDL
I IDL version 2 defines ‘simple’ interfaces I IDL version 3 defines ‘components’
I IDL can be translated to C, C++, Java, Python, etc. I Translation generates interface definitions, stubs, skeletons,
etc.
I Other IDL’s (e.g. Microsoft IDL) are similar.
Piet van Oostrum DOS-3 19
DOS-3
IDL concepts – 1
I Module: namespace, like package in Java
I Interface: Basic thing, between Java interface and class
I Abstract interface (no objects)
I Local interface (only for non-remote objects) I ‘Unconstrained’ interface
I Inheritance: Interface can inherit from one or more other interfaces (multiple inheritance):
I interface Dog: Pet, Animal
I Inherits all variables, methods, exceptions, . . .
I Unconstrained interface may not inherit from local interface
I All unconstrained interfaces inherit fromCORBA::Object(this is Corba Object, not Java Object).
I Constants: const double pi=3.14159; I Operations (methods)
Piet van Oostrum DOS-3 20
DOS-3
IDL concepts – 2
I Attributes (instance variables)
I Implemented by get/set methods I Can be declared readonly (no set)
I Type declarations: give a name to a type typedef struct complex {float re, im;}; typedef sequence<complex> vector; I Exceptions: can have data members
I System exceptions: can be raised even when not specified I User exceptions: must be declared withraises
I Standard exceptions: are defined in OMG documentation
(both system and user)
Piet van Oostrum DOS-3 21
DOS-3
IDL concepts – 3
I Names (identifiers) arecase insensitive, so User, USER and user are the same.
But you must useonly one form
I Scopes: Names can be qualified by the module and interface in which they are defined.
e.g. Stock::Quoter::get_quote
If no module is used:::Quoter::get_quote
I Members of structs, unions, etc. can also be qualified with the struct, etc. name
I Inside the same module the module part can be omitted, inside the same interface the interface part can be omitted, etc.
Piet van Oostrum DOS-3 22
DOS-3
IDL types
I short – 16 bit integer I long – 32 bit integer I long long – 64 bit integer
I signed and unsigned
I boolean
I float, double and long double I fixed – fixed point number I char (8 bits character – iso-8859-1)
I wchar (wide characters – implementation dependent)
Piet van Oostrum DOS-3 23
DOS-3
IDL types
I string – strings consisting of chars I wstring – strings consisting of wchars I octet (8 bits – unchanged)
I struct (like in C, C++, Pascal record)
I union (can contain different types, chosen with a switch construction)
I enum (enumeration constant) I array – multidimensional, fixed bounds
I sequence – array like, one-dimensional; no fixed length (max may be specified)
I any – can contain any other type.
Piet van Oostrum DOS-3 24
DOS-3
IDL Example
e
(in short ho lon raises g explanation}; /* interface Dog:Pet, ni { } /*
interface Cat: Animal { void Eat(); w_ } ; ; } ; unsigned long attribut def of Cat */ def of Dog */ age; module MyAnimals { ( i ; ) NotInterested) A mal
except on NotInterested {string
void Bark y mng ci ontext d o meth
CORBA c ass with e c n a t r e h n ) e l p it u m ( l i i l e p t : s e m a a and d rection n p r e a i t r
nst ar
.
v
s
, ..
.
i
Piet van Oostrum DOS-3 25
DOS-3
IDL Hello
module HelloApp { interface Hello { string sayHello(); }; interface HelloFactory {Hello create(in string message); };
};
Piet van Oostrum DOS-3 26
DOS-3
IDL Quoter
module Stock {
// An exception is a combination of // an event and maybe some data. exception Invalid_Stock {} ; exception Invald_Quoter {} ;
// Interface like C++ class, Java interface interface Quoter {
long get_quote(in string stock_name) raises (Invalid_Stock) ; };
Piet van Oostrum DOS-3 27
DOS-3
IDL Quoter – continued
// Manage the lifecycle of a Quoter object interface Quoter_Factory {
// Returns a new Quoter selected by name // e.g. "MSFT"
Quoter create_quoter(in string name) raises (Invalid_Quoter); void destroy_quoter(in Quoter quoter) ; };
};
I Similiar to Java constructs
I in, out, inout specify direction of parameters
Piet van Oostrum DOS-3 28
DOS-3
Complex types in IDL
I Complex types should be given aname
I enum I struct I union I sequence I array I fixed I typedef typedef long int; typedef string<30> name;
typedef some_complex_type mytype; I Note: the name to be defined comes last!!
Piet van Oostrum DOS-3 29
DOS-3
Enum
I enumdefines a series of named constants: enum Color { Red, Green, Blue }; const Color favorite_color = Blue; module Mod {
enum Size { small, medium, large }; };
const Mod::Size MYSIZE = Mod::medium;
Error:
const Color another = Mod::medium; // is an error
Piet van Oostrum DOS-3 30
DOS-3
Struct
I structdefines a type with a collection of fields I Like a Java class without methods
struct Point2D { float x; float y; }; struct Address { string<25> street; short number; string<20> city; }
Piet van Oostrum DOS-3 31
DOS-3
Struct
I Better to use names for the complex types: I In some cases this is obligatory
typedef string<25> streetname; typedef string<20> cityname; struct Address {
streetname street; short number; cityname city; };
Piet van Oostrum DOS-3 32
DOS-3
Sequence and array
I Arrays have fixed size and can be multidimensional: I Define matrix as an array of 10*20 floats
typedef double Point3D[3]; typedef float matrix[10][20];
I First the type of the elements, then the name of the new type, then the dimensions!!!
I Sequences are onedimensional and are flexible in length I They may have an upper bound
typedef sequence<int> vector;
typedef sequence<int, 10> bounded_vector;
Piet van Oostrum DOS-3 33
DOS-3
union
I unioncan contain any of a number of other types I The choice is indicated by adiscriminator I This must be some integer, char, bool or enum.
union message switch (char) { case ’e’: string english; case ’c’: wstring chinese; };
Piet van Oostrum DOS-3 34
DOS-3
union
enum message_type {SMS, Email}; struct sms_message { long telephone_number; string<160> contents; }; struct email_message { string From; string To; string Subject; string contents; };
Piet van Oostrum DOS-3 35
DOS-3
union
union message switch (message_type) { case SMS: sms_message sms; case Email: email_message email; };Piet van Oostrum DOS-3 36
DOS-3
IDL to Java mapping
I Language mapping
I Most constructs map easily to Java I Done by IDL compiler
I Example constant, array
module Example {
const long aLong = -12345; const long ArrayBound = 42; struct ArrayContainer {
long array[ArrayBound]; };
};
I gives
Piet van Oostrum DOS-3 37
DOS-3
IDL to Java mapping
package Example;
public final class aLong {
public static final int value = (int) (-12345L); }
public final class ArrayContainer { // instance variables
public int[] array; // constructors
public ArrayContainer() { }
public ArrayContainer(int[] __array) {...} }
Piet van Oostrum DOS-3 38
DOS-3
Another Example
module StatMod { interface Stat {
void init();
void addItem(in double item); double getAvg ();
long getNum ();
void getAll (out double avg, out long num); };
};
Piet van Oostrum DOS-3 39
DOS-3
Python crash course
Python is an elegantdynamicprogramming language: I No static type declarations
I Statements can be entered and executed directly in the interpreter
I High level datatypes: lists, tuples, dictionaries, classes, . . . I Each python file is a module (namespace) and can be
imported in other modules I Block structure by indentation
Piet van Oostrum DOS-3 40
DOS-3
Python example
class Test:
#constructor, ’self’ is like ’this’ in Java def __init__(self, value):
self.value = value def getval(self, x):
return self.value, x # return a tuple x = Test(5)
print x.getval(3) # should print (5, 3)
I instance variables/methods can only be referenced with dot notation (obj.var/obj.meth()
I without dot only local and global variables
Piet van Oostrum DOS-3 41
DOS-3
Python server part 1
import sys
from Fnorb.orb import BOA, CORBA import StatMod_skel class StatServer(StatMod_skel.Stat_skel): def init(self): self.count = 0 self.sum = 0.0 def addItem(self, x): self.sum += x self.count += 1 def getNum(self): return self.count
Piet van Oostrum DOS-3 42
DOS-3
Python server part 2
def getAvg(self): try: return self.sum/self.count except: return 0 def getAll(self): return self.getAvg(),self.getNum()
Piet van Oostrum DOS-3 43
DOS-3
Python server part 3
# Initialise the ORB.
orb = CORBA.ORB_init(argv, CORBA.ORB_ID) # Initialise the BOA.
boa = BOA.BOA_init(argv, BOA.BOA_ID)
# Create an object reference (’fred’ is the object key). obj = boa.create(’fred’, StatServer._FNORB_ID)
# Create an instance of the implementation class. impl = StatServer()
boa.obj_is_ready(obj, impl)
Piet van Oostrum DOS-3 44
DOS-3
Python server part 3
# Write the stringified object reference to a file f = open(’Server.ref’, ’w’)
f.write(orb.object_to_string(obj)) f.close()
# Start the event loop. boa._fnorb_mainloop()
In this example we don’t use a Naming service, just write the object reference to a file.
Piet van Oostrum DOS-3 45
DOS-3
Python client
import sys
from Fnorb.orb import CORBA import StatMod
# Initialise the ORB.
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID) # Read the server’s stringified IOR from a file stringified_ior = open(’Server.ref’).read()
# Convert the stringified IOR into an active object reference. server = orb.string_to_object(stringified_ior)
Piet van Oostrum DOS-3 46
DOS-3
Python client
# Make sure that the server is not a ’nil object reference’ # (represented in Python by the value None).
if server is None:
raise ’Nil object reference!’ # Call the server!
server.init() # server.addItem etc.
Piet van Oostrum DOS-3 47
DOS-3
Java client
import org.omg.CORBA.*; import java.util.*; import java.io.*; import StatMod.*; public class StatClient {public static void main(String args[]) { Stat stat;
String ior; try {
ORB orb = ORB.init(args, null); BufferedReader ref = new BufferedReader
(new FileReader("Server.ref")); ior = ref.readLine();
stat = StatHelper.narrow
(orb.string_to_object(ior));
Piet van Oostrum DOS-3 48
DOS-3
Java client
stat.init(); stat.addItem(7); stat.addItem(4); System.out.println(stat.getAvg()); } catch (Throwable e) { System.err.println(e); e.printStackTrace(System.err); } } }Piet van Oostrum DOS-3 49
DOS-3
Translation to Java
package StatMod;
public interface StatOperations {
void init ();
void addItem (double item); double getAvg ();
int getNum ();
void getAll (org.omg.CORBA.DoubleHolder avg, org.omg.CORBA.IntHolder num); }
Piet van Oostrum DOS-3 50
DOS-3
Java out parameters
IntHolder count = new IntHolder(); DoubleHolder avg = new DoubleHolder(); ...
stat.init(); stat.addItem(7); stat.addItem(4); // Use out parameters stat.getAll(avg, count);
System.out.println(avg.value+" "+count.value); Holder classes have avalueattribute that contains the actual value of the (in)out parameter.
Piet van Oostrum DOS-3 51