Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / PortableInterceptors / PICurrent_NameService / MessengerServer.cpp
blobf920ab4198acef722e442d44591498ed4fd9f46a
1 #include "Messenger_i.h"
2 #include "MessengerS.h"
3 #include "ServerInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
6 // Ensure that the PI_Server library is linked in when building statically
7 #include "tao/PI_Server/PI_Server.h"
8 #include "orbsvcs/CosNamingC.h"
9 #include <iostream>
11 int
12 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
14 try
16 ServerInitializer *temp_initializer = 0;
17 temp_initializer = new ServerInitializer;
19 PortableInterceptor::ORBInitializer_var orb_initializer =
20 temp_initializer;
22 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
24 CORBA::ORB_var orb =
25 CORBA::ORB_init (argc, argv, "Server ORB");
27 //Get reference to Root POA
28 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
29 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
31 // Activate POA Manager
32 PortableServer::POAManager_var mgr = poa->the_POAManager();
33 mgr->activate();
35 // Create an object
36 PortableServer::Servant_var<Messenger_i> messenger_servant =
37 new Messenger_i;
39 // Find the Naming Service
40 CORBA::Object_var naming_obj =
41 orb->resolve_initial_references( "NameService" );
42 CosNaming::NamingContext_var root =
43 CosNaming::NamingContext::_narrow( naming_obj.in() );
44 if( CORBA::is_nil( root.in() ) ) {
45 std::cerr << "Nil Naming Context reference" << std::endl;
46 return 1;
49 // Bind Messenger
50 CosNaming::Name name;
51 name.length( 1 );
52 name[0].id = CORBA::string_dup( "Messenger" );
54 PortableServer::ObjectId_var oid =
55 poa->activate_object( messenger_servant.in() );
56 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
58 root->rebind(name, messenger_obj.in());
60 std::cout << "Messenger bound in Naming Service" << std::endl;
62 // Accept requests
63 orb->run();
64 orb->destroy();
67 catch(const CORBA::Exception& ex)
69 std::cerr << "Caught CORBA exception: " << ex << std::endl;
70 return 1;
73 return 0;