Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Messenger / MessengerServer.cpp
blob065531c70e35c975cd6c253266387d25cbb50cc0
1 #include "Messenger_i.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include <iostream>
4 int
5 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
7 try {
8 // Initialize orb
9 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
11 //Get reference to Root POA
12 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
13 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
15 // Activate POA Manager
16 PortableServer::POAManager_var mgr = poa->the_POAManager();
17 mgr->activate();
19 // Find the Naming Service
20 CORBA::Object_var naming_obj =
21 orb->resolve_initial_references( "NameService" );
22 CosNaming::NamingContext_var root =
23 CosNaming::NamingContext::_narrow( naming_obj.in() );
24 if (CORBA::is_nil(root.in())) {
25 std::cerr << "Nil Naming Context reference" << std::endl;
26 return 1;
29 // Bind the example Naming Context, if necessary
30 CosNaming::Name name;
31 name.length( 1 );
32 name[0].id = CORBA::string_dup( "example" );
33 try {
34 CORBA::Object_var dummy = root->resolve( name );
36 catch(const CosNaming::NamingContext::NotFound&) {
37 CosNaming::NamingContext_var dummy = root->bind_new_context( name );
40 // Bind the Messenger object
41 name.length( 2 );
42 name[1].id = CORBA::string_dup( "Messenger" );
44 // Create an object
45 PortableServer::Servant_var<Messenger_i> messenger_servant = new Messenger_i;
46 PortableServer::ObjectId_var oid = poa->activate_object(messenger_servant.in());
47 obj = poa->id_to_reference( oid.in() );
48 root->rebind(name, obj.in());
50 std::cout << "Messenger object bound in Naming Service" << std::endl;
52 // Accept requests
53 orb->run();
54 orb->destroy();
56 catch(const CORBA::Exception& ex) {
57 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
58 return 1;
61 return 0;