How is CORBA implemented

CORBA implementation of client and server

Transcript

1 CORBA implementation of client and server J. Heinzelreiter WS 2003/04

2 Implementation of the client Initialization and release of the ORB. Mapping of interfaces. Handling of object references. Management of proxies (reference counting). Parameter transfer Rules for memory management Use of smart pointer classes 2 CORBA / Client & Server

3 Initialization and release of the ORB Initialization and transfer of the ORB parameters using namespace CORBA; int main (int argc, char * argv []) {ORB_var orb; // Smart pointer to ORB try {orb = ORB_init (argc, argv); } catch (exception & e) {return 1; } Release orb-> destroy (); } // ~ ORB_var (): release (orb); 3 CORBA / Client & Server

4 Mapping of interfaces IDL interfaces interface X {void f (); }; are mapped to C ++ classes (proxies). typedef X * X_ptr; class X: public virtual object {public: static X_ptr _narrow (object_ptr obj); static X_ptr _duplicate (x_ptr); static X_ptr _nil (); virtual void f (); }; Smart pointer class X_var is also generated. 4 CORBA / Client & Server

5 Object References - Proxies Proxies are instantiated by the ORB: X_ptr ptrx = getx (); Remote method call: ptrx-> f (); ptrx Client ptrx-> f () X (proxy) 1 Reference-Counter f () Server POA_X (skeleton) f () X_impl 5 CORBA / Client & Server

6 Object references Reference counting ORB instantiates reference (counter is set to 1) X_ptr ptr1 = getx (); ptr1 X (proxy) 1 Copy the reference: X_ptr ptr2 = X :: _ duplicate (ptr1); ptr1 ptr2 X (proxy) 2 Release of the references: CORBA :: release (ptr1); CORBA :: release (ptr2); 6 CORBA / Client & Server ptr1 ptr2 X (proxy) 0

7 Smart pointer class for references (1) class X_var {private: X_ptr _ptr; // Pointer to managed reference public: // Initialization with nil reference. X_var (); // Take ownership of the reference. X_var (X_ptr p); // Copy-Konstr .: Copy reference (X :: _ duplicate ()). X_var (const X_var & pv); // Destructor: release reference (CORBA :: release ()). ~ X_var (); // Selection operator: delegation to _ptr. X_ptr & operator -> () const; 7 CORBA / Client & Server

8 Smart pointer class for references (2) class X_var {... // Assignment operators: take ownership or copy. X_var & operator = (X_ptr p); X_var & operator = (const X_var & pv); // conversion operator. operator X_ptr & (); // Functions for direct access to _ptr. For compilers // incorrectly implementing conversion operators. X_ptr in () const; X_ptr & inout (); X_ptr & out (); // Passes ownership to reference to the caller. X_ptr _retn (); } 8 CORBA / Client & Server

9 Use of the _var class Assignment of a reference Object_var obj = orb-> string_to_object (); Check for nil reference (conversion operator) if (is_nil (obj)) {} Downcast: _narrow increments the ref counter X_var ptr1 = X :: _ narrow (obj); Copy the object reference X_var ptr2 = ptr1; Access to function of interface X ptr1-> f (); Ref-Conter is decremented by ~ X_var (). 9 CORBA / Client & Server

10 Exchange of references Conversion into strings interface ORB {string object_to_string (in Object obj); Object string_to_object (in string str); }; Naming Service (in CosNaming module) interface NamingContext {(re) bind (in Name n, in Object o); Object resolve (in name n); (in name); (NameN) (}; Method call interface X {Y gety (); // Y is also an IDL interface}; 10 CORBA / Client & Server

11 Rules for parameter transfer The type of parameter transfer depends on the directional attribute and the parameter type: IDL in inout out return simple simple simple & string const char * char * & X X_ptr X_ptr & sequ const sequ & sequ & struct, fixed const struct & struct & struct, var. const struct & struct & array, fixed const array array_slice * simple & char * & X_ptr & sequ * & struct & struct * & array_slice * simple char * X_ptr sequ * struct struct * array_slice * array, var. const array array_slice * array_slice * & array_slice * Stack or Heap Heap: Release Heap: Allocation and release 11 CORBA / Client & Server

12 parameters with fixed length IDL: interface X {long f (in long lin, inout long linout, out long lout); }; C ++: typedef Long & Long_out; CORBA :: Long f (corba :: long lin, CORBA :: Long & linout, CORBA :: Long_out lout); Usage: X_var x =; // Get reference to object CORBA :: Long in = 5; CORBA :: Long inout = 9; CORBA :: Long out, ret; ret = x-> f (in, inout, out); cout << inout << out << ret << endl; 12 CORBA / Client & Server

13 Transfer of string parameters IDL: string f (in string sin, // interface X inout string sinout, out string sout); C ++: char * f (const char * sin, char * & sinout, String_out sout); Usage: X_var x =; // Get reference to object // inout must be initialized with a dynamically allocated value, String_var inout = string_dup ("yyy"); String_var out, ret; ret = x-> f ("xxx", inout, out); cout << out << inout << ret << endl; // String_out ensures that out is released before the second call. ret = x-> f ("xxx", inout, out); 13 CORBA / Client & Server

14 parameters with variable length IDL: typedef sequence LongSeq; LongSeq f (in LongSeq sin, // interface X inout LongSeq sinout, out LongSeq sout); C ++: typedef LongSeq * & LongSeq_out; LongSeq * f (const LongSeq sin, LongSeq sinout, LongSeq_out sout); Usage: X_var x =; // Get reference to object LongSeq_var in = new LongSeq; LongSeq_var inout = new LongSeq; LongSeq_var out, ret; in-> length (2); in [0] = 111; in [1] = 222; inout-> length (1); inout [0] = 333; ret = x-> f (in, inout, out); 14 CORBA / Client & Server

15 Transfer of object references IDL: interface Article {} Article f (in Article ain, // interface X inout Article ainout, out Article aout); C ++: Article_ptr f (article_ptr ain, Article_ptr & ainout, Article_out aout); Usage: X_var x =; // Get reference to object Article_var ain = findarticlebyid ("a001"); Article_var ainout = findarticlebyid ("a002"); Article_var aret = x-> f (ain, ainout, aout); ainout-> setstock (100); // Remote method call aout-> setprice (1000.0); 15 CORBA / Client & Server

16 Implementation of the server Initialization and release of the ORB. Implementation of servants. Generation of object references. Parameter transfer Rules for memory management Use of smart pointer classes 16 CORBA / Client & Server

17 Initialization and release of the ORB (1) using namespace CORBA; Initialization of the ORB ORB_var orb; orb = ORB_init (argc, argv); Get reference to object adapter (POA) Object_var poaobj = orb-> resolve_initial_references ("rootpoa"); PortableServer :: POA_var rootpoa = PortableServer :: POA :: _ narrow (poaObj); Activation of the POA manager PortableServer :: POAManager_var manager = rootpoa-> the_poamanager (); manager-> activate (); 17 CORBA / Client & Server

18 Initialization and release of the ORB (2) Instantiation and registration of the servants X_impl servantx (rootpoa); X_var refx = servantx._this (); Start the dispatch loop of the ORB orb-> run (); Terminate the ORB orb-> shutdown (wait_for_completion); Release of the ORB orb-> destroy (); 18 CORBA / Client & Server

19 Implementation of the Servant (1) interface X {void f (); }; ServantBase virtual POA_Ptr _default_poa () virtual Boolean _is_a (...) POA_X X_ptr _this () virtual void f () RefCountServantBase virtual void _add_ref () virtual void _remove_ref () X_impl & COA_Ptr _defaultBA () 19

20 Implementation of the servant (2) ServantBase: common functionality of all servants. Contains implementations of methods of CORBA :: Object (e.g. _is_a (), _non_existent ()). _default_poa () returns a reference to RootPOA. RefServantBase: Reference counting for servants. Derived servants must be allocated on the heap. Release with _remove_ref () (and not with delete). POA_X: Skeleton code Generated by the IDL compiler for each IDL interface. Contains a virtual method for each operation of the IDL interface. _this (): Activation of the servant and generation of an object reference. X_impl: Servant implementation of all methods of the skeleton. Overwrite _default_poa () if POA with specific policies is required. 20 CORBA / Client & Server

21 Parameter transfer Rules for parameter transfer are the counterpart to the corresponding rules on the client side. If the client has to release a parameter, the server has to allocate it. Fixed length parameters are passed by value or by reference. in parameters must not be overwritten or released. out parameters and return values ​​with variable length are allocated dynamically by the server. inout parameters with variable length are allocated by the client. The server can overwrite this. Strings can also be released and reallocated. 21 CORBA / Client & Server

22 parameters with fixed length IDL: interface X {long f (in long lin, inout long linout, out long lout); }; Skeleton: CORBA :: Long f (corba :: long lin, CORBA :: Long & linout, CORBA :: Long_out lout) = 0; Implementation: CORBA :: Long X_impl :: f () {cout << lin << endl; linout + = 1; lout = 0; return 1; } 22 CORBA / Client & Server

23 parameters with variable length IDL: typedef sequence LongSeq; LongSeq f (in LongSeq sin, // interface X inout LongSeq sinout, out LongSeq sout); Skeleton: typedef LongSeq * & LongSeq_out; LongSeq * f (const LongSeq sin, LongSeq sinout, LongSeq_out sout) = 0; Implementation: LongSeq * X_impl :: f () {sinout.length (sin.length ()); for (int i = 0; i length (1); sout [0] = 5; LongSeq_var sret = new LongSeq; sret-> length (2); sret [0] = 1; sret [1] = 2; return sret._retn (); } 23 CORBA / Client & Server

24 Transfer of string parameters IDL: string f (in string sin, // interface X inout string sinout, out string sout); Skeleton: char * f (const char * sin, char * & sinout, String_out sout) = 0; Implementation: char * X_impl :: f () {string_free (sinout); sinout = string_dup (sin); sout = string_dup ("outstring"); return string_dup ("returnstring"); } 24 CORBA / Client & Server

25 Transfer of object references IDL: interface Article {} Article f (in Article ain, // interface X inout Article ainout, out Article aout); Skeleton: Article_ptr f (article_ptr ain, Article_ptr & ainout, Article_out aout) = 0; Implementation: Article_ptr X_impl :: f () {if (! CORBA :: is_nil (aIn)) ain-> setprice (500.0); CORBA :: release (aInOut); ainout = Article :: _ duplicate (sIn); sout = _this (); Article_var aret = stock-> findarticlebyid ("a001"); return aret -> _ retn (); } 25 CORBA / Client & Server