Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NamingService / Naming_Server / MessengerTask.cpp
blob57bbf13048a3fa21933a6921b8f56770167c80b2
1 #include "MessengerTask.h"
2 #include "Messenger_i.h"
3 #include "orbsvcs/CosNamingC.h"
4 #include <iostream>
6 MessengerTask::MessengerTask()
8 // cast away constness to make Sun CC family of compilers happy.
9 char* argv[] = {const_cast<char *>("Messenger"), 0 };
10 int argc = 1;
11 orb_ = CORBA::ORB_init(argc, argv, "ServerORB");
14 void MessengerTask::end()
16 orb_->shutdown(0);
17 this->wait();
20 int MessengerTask::svc()
22 try {
23 // Get reference to Root POA
24 CORBA::Object_var obj = orb_->resolve_initial_references("RootPOA");
25 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());
27 // Activate POA Manager
28 PortableServer::POAManager_var mgr = poa->the_POAManager();
29 mgr->activate();
31 // Find the Naming Service
32 obj = orb_->resolve_initial_references("NameService");
33 CosNaming::NamingContext_var root =
34 CosNaming::NamingContext::_narrow(obj.in());
36 if (CORBA::is_nil(root.in())) {
37 std::cerr << "Nil Naming Context reference" << std::endl;
38 return 1;
40 // Bind the example Naming Context, if necessary
41 CosNaming::Name name;
42 name.length(1);
43 name[0].id = CORBA::string_dup("example");
44 try {
45 root->resolve(name);
47 catch(const CosNaming::NamingContext::NotFound&) {
48 root->bind_new_context(name);
51 // Bind the Messenger object
52 name.length(2);
53 name[1].id = CORBA::string_dup("Messenger");
55 // Create an object
56 PortableServer::Servant_var<Messenger_i> servant = new Messenger_i;
57 PortableServer::ObjectId_var oid = poa->activate_object(servant.in());
58 obj = poa->id_to_reference(oid.in());
59 root->rebind(name, obj.in());
61 std::cout << "Messenger object bound in Naming Service" << std::endl;
63 // Normally we run the orb and the orb is shutdown by
64 // calling MessengerTask::end(). To simplify the coordination
65 // between the main thread and this Messenger thread, specify
66 // the time period to let the Messenger thread finish by itself.
67 // Accept requests
68 ACE_Time_Value tv(1);
69 orb_->run(tv);
70 orb_->destroy();
72 return 0;
74 catch(const CORBA::Exception& ex) {
75 std::cerr << "CORBA exception: " << ex << std::endl;
78 return -1;