Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Naming_Client / MessengerServer.cpp
blob12c13e002312a19de79c05b561ffc100df4e4b06
1 #include "Messenger_i.h"
2 #include "orbsvcs/CosNamingC.h"
3 #include "orbsvcs/Naming/Naming_Client.h"
4 #include <iostream>
5 int
6 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
8 try {
9 // Initialize orb
10 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
12 //Get reference to Root POA
13 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
14 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
16 // Activate POA Manager
17 PortableServer::POAManager_var mgr = poa->the_POAManager();
18 mgr->activate();
20 // Find the Naming Service
21 TAO_Naming_Client naming_client;
23 if(naming_client.init(orb.in()) != 0){
24 std::cerr << "Could not initialize naming client." << std::endl;
25 return 1;
28 // Bind the example Naming Context, if necessary
29 CosNaming::Name name;
30 name.length(1);
31 name[0].id = CORBA::string_dup("example");
32 try {
33 CORBA::Object_var dummy = naming_client->resolve(name);
35 catch(const CosNaming::NamingContext::NotFound&) {
36 CosNaming::NamingContext_var dummy = naming_client->bind_new_context( name );
39 // Bind the Messenger object
40 name.length( 2 );
41 name[1].id = CORBA::string_dup( "Messenger" );
43 // Create an object
44 PortableServer::Servant_var<Messenger_i> servant = new Messenger_i;
45 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
46 obj = poa->id_to_reference(oid.in());
47 naming_client->rebind(name, obj.in());
49 std::cout << "Messenger object bound in Naming Service" << std::endl;
51 // Accept requests
52 orb->run();
53 orb->destroy();
55 catch(const CORBA::Exception& ex) {
56 std::cerr << "Caught a CORBA exception: " << ex << std::endl;
57 return 1;
60 return 0;