Object-Oriented Middleware (OOMI) Locating Distributed Objects Emmerich – Chapter
Slide 2 of 41 © Ingeniørhøjskolen i Århus Outline 1.Quick recap on object references 2. Object Naming –Principles –CORBA Naming Service 3. Object Trading (not part of Course Curriculum)
Slide 3 of 41 © Ingeniørhøjskolen i Århus Recap on IOR – so far IOR: Interoperable Object Reference –Includes info on: Repository ID (standard), Endpoint Info (standard) - including IP and port number, Object Key (proprietary) Can be written into a file & read –as we have seen the last two lectures Not really nice with a file-based reference – or what? File-based may be necessary due to firewall problems Possible to use a HTTP or FTP server for distributing the references IOR: c3 a48656c6c6f f48656c6c6 f3a312e c e e e312e b afabcb a80a a
Slide 4 of 41 © Ingeniørhøjskolen i Århus #include int run(CORBA::ORB_ptr); int main(int argc, char* argv[]) {int status = EXIT_SUCCESS; CORBA::ORB_var orb; try { orb = CORBA::ORB_init(argc, argv); status = run(orb); } catch(const CORBA::Exception&) { status = EXIT_FAILURE; } if(!CORBA::is_nil(orb)) { try { orb -> destroy(); } catch(const CORBA::Exception&) {status = EXIT_FAILURE; } } return status; } HelloCorba C++ Client with file-based IOR Part 1 Init ORB Destroy ORB
Slide 5 of 41 © Ingeniørhøjskolen i Århus … int run(CORBA::ORB_ptr orb) { const char* refFile = "object.ref"; ifstream in(refFile); char s[2048]; in >> s; CORBA::Object_var obj = orb -> string_to_object(s); HelloApp::Hello_var hello = HelloApp::Hello::_narrow(obj); cout sayHello() << endl; return 0; } HelloCorba C++ Client with file-based IOR Part 1 Narrow the call (CORBA type cast) Object reference Read from file Object reference Read from file Call via Proxy and print
Slide 6 of 41 © Ingeniørhøjskolen i Århus Motivation Avoid using physical locations for locating components! Why? Naming: –Locating components by external names –Similar to white pages –Java RMI Registry, CORBA Naming Service Trading: Locating components by service characteristics –Similar to yellow pages –Web services UDDI
Slide 7 of 41 © Ingeniørhøjskolen i Århus 1. Naming - Common Principles (1) Object-oriented middleware uses object references to address server objects We need to find a way to get hold of these object references without assuming physical locations A name is a sequence of character strings that can be bound to an object reference A name binding can be resolved to obtain the object reference
Slide 8 of 41 © Ingeniørhøjskolen i Århus Common Principles (2) There may be many server objects in a distributed object system Server objects may have several names Leads to large number of name bindings Name space has to be arranged in a hierarchy to avoid –Name clashes –Poor performance when binding/resolving names Hierarchy achieved by naming contexts
Slide 9 of 41 © Ingeniørhøjskolen i Århus Cup Winners 1.FC Kaiserslautern Premier First Man United Chelsea QPR South End United England Germany 1. Liga 2. Liga BVB Bayern Bochum Lautern UEFA Manchester United Common Principles: Naming Contexts Servant objects Naming Context objects
Slide 10 of 41 © Ingeniørhøjskolen i Århus Common Principles: Composite Names Names are composed of possibly more than one component Used to describe traversals across several naming contexts Examples: ("UEFA","England","Premier", “Manchester United") ("UEFA",“Cup Winners", “Manchester United") HOWEVER: Often only a flat naming structure is used!
Slide 11 of 41 © Ingeniørhøjskolen i Århus Common Principles: Name Server Behavior Name bindings are managed by Name Servers Not every server object needs a name Server objects may have several names (aliases) Name servers must store bindings persistently Name servers should be tuned towards efficiently resolving name bindings Name server may itself be distributed
Slide 12 of 41 © Ingeniørhøjskolen i Århus Stock Quoter Example with NameServer CPH: Stock Exchange Server Stock Quoter client 1 TCP/IP Network Stock Quoter client 2 :Name Server LON: Stock Exchange Server Broker Architecture Pattern
Slide 13 of 41 © Ingeniørhøjskolen i Århus CORBA Naming Service Application Objects CORBA Facilities CORBA Services Naming Service Domain Interfaces Object Request Broker
Slide 14 of 41 © Ingeniørhøjskolen i Århus CORBA Naming Service Supports bindings of names to CORBA object references Consists of two elements: –Standardized CORBA Interfaces –Vendor dependent server implementation Names are scoped in naming contexts Multiple names can be defined for object references Not all object references need names –Factory objects have names
Slide 15 of 41 © Ingeniørhøjskolen i Århus Client and Server using a NameServer Stock Quoter client 1 :Name Server CPH: Stock Exchange Server 1. bind(StockFactoryName,objref) 2. objref= resolve(StockFactoryName) 3. call operations on StockFactory object
Slide 16 of 41 © Ingeniørhøjskolen i Århus CORBA Names Names are composed of simple names Simple names are value-kind pairs Value attribute is used for resolving names Kind attribute is used to provide information about the role of the object
Slide 17 of 41 © Ingeniørhøjskolen i Århus IDL Types for Names module CosNaming { typedef string Istring; struct NameComponent { Istring id ; Istring kind ; }; typedef sequence Name ;... };
Slide 18 of 41 © Ingeniørhøjskolen i Århus Example of a bind() operation Registration of a simple Name binding at a Name Server: CosNaming::Name name; name.length(1); name[0].id=CORBA::string_dup(“MyFactory"); name[0].kind= CORBA::string_dup(""); // bind name with my_factory servant at NameServer naming_context->bind(name,my_factory.in()); “MyFactory” MyFactoryImp NameServer ior
Slide 19 of 41 © Ingeniørhøjskolen i Århus The IDL Interfaces Naming Service is specified by two IDL interfaces: –NamingContext defines operations to bind objects to names and resolve name bindings –BindingInterator defines operations to iterate over a set of names defined in a naming context
Slide 20 of 41 © Ingeniørhøjskolen i Århus Excerpt of NamingContext Interface interface NamingContext { void bind (in Name n,in Object obj) raises (NotFound,...); Object resolve (in Name n) raises(NotFound,CannotProceed,...); void unbind (in Name n) raises(NotFound,CannotProceed...); void list (in unsigned long how_many, out BindingList bl, out BindingIterator bi);... };
Slide 21 of 41 © Ingeniørhøjskolen i Århus Naming Scenario: Binding Promote Bielefeld to German '1. Liga' and relegate Frankfurt root: Naming Contextc:Client 1L=resolve("UEFA","Germany","1. Liga") 1L:NamingContext bind("Arm. Bielefeld", bielefeld) unbind("Eintr. Frankfurt")
Slide 22 of 41 © Ingeniørhøjskolen i Århus Bootstrapping Naming Service How to get the Root Naming Context? ORB Interface provides for initialization: module CORBA { interface ORB { typedef string ObjectId; typedef sequence ObjectIdList; exception InvalidName{}; ObjectIdList list_initial_services (); Object resolve_initial_references ( in ObjectId identifier) raises(InvalidName); }
Slide 23 of 41 © Ingeniørhøjskolen i Århus root: Naming Context c:Client Naming Scenario: Resolving Print squad of Borussia Dortmund D=resolve("UEFA","Germany") D:Naming D:NamingContext do:Team do=resolve("1. Liga","BVB") print()
Slide 24 of 41 © Ingeniørhøjskolen i Århus Implementation details Consists of two elements: –Standardized CORBA Interfaces –Vendor dependent server implementation Need a server proces running We look at Orbacus & Java J2SE SDK
Slide 25 of 41 © Ingeniørhøjskolen i Århus Vendor specific Naming Servers Orbacus C++ –nameserv OR ntnameservice (native Windows NT service) Orbacus Java –com.ooc.CosNaming.Server Java J2SE (prior to 1.4.1) –tnameserv Java J2SE (after 1.4.1) –orbd (ORB Daemon – which includes a naming server) –servertool (if you want to use a persistent naming service) Example: start orbd –ORBIntialPort 2500 –will start a naming service listening on port 2500
Slide 26 of 41 © Ingeniørhøjskolen i Århus Example implementation HelloWorld using the ORBD Nameservice We need: –Hello.idl –HelloServer.java (pos. HelloServant.java) –HelloClient.java Then we need –the IDLJ tool (with –fall) –the ORBD daemon – as the nameserver
Slide 27 of 41 © Ingeniørhøjskolen i Århus module HelloApp { interface Hello { string sayHello(); oneway void shutdown(); }; Hello.idl Setting up the module, interface & the methods needed for our example
Slide 28 of 41 © Ingeniørhøjskolen i Århus import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA; import java.util.Properties; class HelloImpl extends HelloPOA { private ORB orb; public void setORB(ORB orb_val) { orb = orb_val; } // implement sayHello() method public String sayHello() { return "\nHello world !!\n"; } // implement shutdown() method public void shutdown() { orb.shutdown(false); } (continued on next slide) … HelloWorld Server 1 of 3 The Servant part – our implementation of the IDL interface -nothing new here
Slide 29 of 41 © Ingeniørhøjskolen i Århus … (continued from last slide) public class HelloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get reference to rootpoa & activate the POAManager POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // create servant and register it with the ORB HelloImpl helloImpl = new HelloImpl(); helloImpl.setORB(orb); // get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl); Hello href = HelloHelper.narrow(ref); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt which is part of the Interoperable // Naming Service (INS) specification. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); (continued on next slide) … HelloWorld Server 2 of 3 The Server part – getting the Nameservice context
Slide 30 of 41 © Ingeniørhøjskolen i Århus … (continued from last slide) // bind the Object Reference in Naming String name = "Hello"; NameComponent path[] = ncRef.to_name( name ); ncRef.rebind(path, href); System.out.println("HelloServer ready and waiting..."); // wait for invocations from clients orb.run(); } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("HelloServer Exiting..."); } HelloWorld Server 3 of 3 Binding the HelloApp object reference to the Nameserver context using the name ”Hello”
Slide 31 of 41 © Ingeniørhøjskolen i Århus import HelloApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; public class HelloClient { static Hello helloImpl; public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context – getting hold of the Name Service (ORBD) org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt instead of NamingContext. This is // part of the Interoperable naming Service – meaning that you may com with // other ORBs NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // resolve the Object Reference in Naming – using ncRef’s resolve operation String name = "Hello"; helloImpl = HelloHelper.narrow(ncRef.resolve_str(name)); System.out.println("Obtained a handle on server object: " + helloImpl); System.out.println(helloImpl.sayHello()); helloImpl.shutdown(); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } }} HelloWorld Client Using same nameservice to get the name – now using the resolve operation
Slide 32 of 41 © Ingeniørhøjskolen i Århus Commands used To compile static stubs –idlj -fall Hello.idl Compile all Java files –using javac To start-up the Naming Service –orbd -ORBInitialPort 1050 Starting up the server: –start java HelloServer -ORBInitialPort ORBInitialHost localhost Starting up the client: –java HelloClient -ORBInitialPort ORBInitialHost localhost
Slide 33 of 41 © Ingeniørhøjskolen i Århus Now with a C++ client We want to contact the ORBD Nameserver from a C++ Client Using the Orbacus C++ ORB Needed: –Hello.idl –HelloCORBAClient.cpp –Orbbacus idl compiler
Slide 34 of 41 © Ingeniørhøjskolen i Århus #include int run(CORBA::ORB_ptr); int main(int argc, char* argv[]) { int status = EXIT_SUCCESS; CORBA::ORB_var orb; try { //Connect to the ORB with supplied arguments orb = CORBA::ORB_init(argc, argv); status = run(orb); } catch(const CORBA::Exception&){ status = EXIT_FAILURE;} if(!CORBA::is_nil(orb)) { try { //remember to inform the orb that we no longer need it orb -> destroy(); } catch(const CORBA::Exception&){ status = EXIT_FAILURE; } } return status; } … (continued on next) … HelloWorld C++ Client Part 1 of 2 Do the usual initialization to connect to ORB
Slide 35 of 41 © Ingeniørhøjskolen i Århus … (continued from last slide) int run(CORBA::ORB_ptr orb) { //OBTAINING REFERENCE TO Name Server CORBA::Object_var obj = orb->resolve_initial_references("NameService"); //Narrow the Call – we need to turn the Name server obj into a NamingContext CosNaming::NamingContext_var naming_context; naming_context = CosNaming::NamingContext::_narrow(obj); //Now setup the CosNaming name that we want to find (Hello) CosNaming::Name name; name.length(1); name[0].id= CORBA::string_dup("Hello"); name[0].kind= CORBA::string_dup(""); //Using the resolve operation on the root naming_context - get the Hello Servant stub CORBA::Object_var tempObj = naming_context->resolve(name); //Call Narrow now to transform the generic CORBA Obj ínto an HelloApp::Hello class HelloApp::Hello_var hello = HelloApp::Hello::_narrow(tempObj); cout sayHello() << endl; return 0; } HelloWorld C++ Client Part 2 of 2 Using the ORBD name- service to get the IOR to the Java Servant
Slide 36 of 41 © Ingeniørhøjskolen i Århus … int run(CORBA::ORB_ptr orb) { CORBA::Object_var tempObj; // OBTAINING REFERENCE TO Name Server CORBA::Object_var obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContext_var naming_context; try { naming_context = CosNaming::NamingContext::_narrow(obj);} catch (const CORBA::Exception & e) { cerr << " Naming_Context narrow error " << endl; throw 0; } if (CORBA::is_nil(naming_context)) { cerr << " No naming context found" << endl; throw 0;} //Now setup the CosNaming name that we want to find (Hello) CosNaming::Name name; name.length(1); name[0].id= CORBA::string_dup("Hello"); name[0].kind= CORBA::string_dup(""); try { //Using the resolve operation on the root naming_context tempObj = naming_context->resolve(name); } catch (const CosNaming::NamingContext::NotFound&) { cerr << " Hello Object not found " << endl;} //Call Narrow using HelloApp::Hello_var hello = HelloApp::Hello::_narrow(tempObj); cout sayHello() << endl; return 0; } HelloWorld C++ Client Part 2 with error hand. Same code – but this time including error handling
Slide 37 of 41 © Ingeniørhøjskolen i Århus Commands used To compile static stubs –\ooc\bin\idl Hello.idl Compile all files Remember to start-up ORBD and the Java Servant on port 1050 Starting up the client: –C:\OOC_DEV\HelloCorbaCppNaming\Debug>HelloCorba -ORBInitRef NameService=corbaloc::localhost:1050/NameService
Slide 38 of 41 © Ingeniørhøjskolen i Århus Transient or Persistent Naming Transient Naming Service –object references will not survive termination of nameserver –servants need to reregister –clients need to resolve again Persistent Naming Service –Servants are activated automatically –Object references are persisted –Need to register the HelloWorld Server with the servertool
Slide 39 of 41 © Ingeniørhøjskolen i Århus Tutorials How to implement a Transient Name binding using ”orbd” – l/tutorial/GSserver.htmlhttp://java.sun.com/j2se/1.4.1/docs/guide/id l/tutorial/GSserver.html How to implement a Persistent Name binding using ”orbd & servertool” – l/jidlExample2.htmlhttp://java.sun.com/j2se/1.4.1/docs/guide/id l/jidlExample2.html
Slide 40 of 41 © Ingeniørhøjskolen i Århus Limitations of Naming Limitation of Naming in all approaches: Client always has to identify the server by name Inappropriate if client just wants to use a service at a certain quality but does not know from who: –Automatic cinema ticketing –Video on demand –Electronic commerce
Slide 41 of 41 © Ingeniørhøjskolen i Århus CORBA Trading Service Application Objects CORBA Facilities CORBA Services Object Trader Domain Interfaces Object Request Broker