Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / orbsvcs / examples / ImR / Combined_Service / dynserver.cpp
blob6fcabe26717e6fe9de145f3006d28ffcb86d755f
1 #include "dynserver.h"
3 #include "tao/IORTable/IORTable.h"
4 #include "tao/PortableServer/Root_POA.h"
6 #include "ace/Dynamic_Service.h"
7 #include "ace/Task.h"
9 using namespace CORBA;
10 using namespace PortableServer;
12 DynServer::DynServer()
13 : n_(0)
17 DynServer::~DynServer() {
20 Long DynServer::get()
22 ACE_DEBUG((LM_DEBUG, "dynserver: get() %d\n", ++n_));
23 return n_;
26 namespace {
27 POA_ptr createPersistPOA(const char* name, POA_ptr root_poa, POAManager_ptr poaman)
29 PolicyList policies (2);
30 policies.length (2);
31 policies[0] = root_poa->create_id_assignment_policy(USER_ID);
32 policies[1] = root_poa->create_lifespan_policy(PERSISTENT);
33 POA_var poa = root_poa->create_POA(name, poaman, policies);
34 policies[0]->destroy();
35 policies[1]->destroy();
36 return poa._retn();
40 class DynServer_ORB_Runner : public ACE_Task_Base
42 ORB_var orb_;
43 public:
44 DynServer_ORB_Runner(ORB_ptr orb)
45 : orb_(ORB::_duplicate(orb))
48 void end() {
49 if (! is_nil(orb_.in())) {
50 orb_->shutdown(1);
51 this->wait();
54 virtual int svc()
56 orb_->run();
57 orb_ = ORB::_nil();
58 return 0;
62 DynServer_Loader::DynServer_Loader()
66 int
67 DynServer_Loader::init (int argc, ACE_TCHAR* argv[])
69 try {
70 orb_ = ORB_init(argc, argv, "DynServer");
72 Object_var obj = orb_->resolve_initial_references("RootPOA");
73 root_poa_ = POA::_narrow(obj.in());
74 POAManager_var poaman = root_poa_->the_POAManager();
75 obj = this->orb_->resolve_initial_references ("IORTable");
76 IORTable::Table_var ior_table = IORTable::Table::_narrow (obj.in());
77 ACE_ASSERT(! is_nil(ior_table.in()));
79 ACE_DEBUG((LM_DEBUG, "dynserver: creating poas. (Registers with ImR)\n"));
81 POA_var poa1 = createPersistPOA("DynObject1", root_poa_.in(), poaman.in());
82 POA_var poa2 = createPersistPOA("DynObject2", root_poa_.in(), poaman.in());
84 ACE_DEBUG((LM_DEBUG, "dynserver: activating objects.\n"));
86 DynServer* svt1 = new DynServer;
87 ServantBase_var scoped_svt1(svt1);
88 DynServer* svt2 = new DynServer;
89 ServantBase_var scoped_svt2(svt2);
91 ObjectId_var id = string_to_ObjectId("myobject");
93 poa1->activate_object_with_id(id.in(), svt1);
94 poa2->activate_object_with_id(id.in(), svt2);
96 TAO_Root_POA* tmp_poa = dynamic_cast<TAO_Root_POA*>(poa1.in());
97 obj = tmp_poa->id_to_reference_i (id.in(), false);
98 String_var ior = orb_->object_to_string(obj.in());
99 ior_table->bind ("DynObject1", ior.in());
101 tmp_poa = dynamic_cast<TAO_Root_POA*>(poa2.in());
102 obj = tmp_poa->id_to_reference_i (id.in(), false);
103 ior = orb_->object_to_string(obj.in());
104 ior_table->bind ("DynObject2", ior.in());
106 poaman->activate();
108 runner_.reset(new DynServer_ORB_Runner(orb_.in()));
109 runner_->activate();
111 ACE_DEBUG((LM_DEBUG, "dynserver: running.\n"));
112 } catch (Exception& e) {
113 e._tao_print_exception ("DynServer::init()");
115 return 0;
119 DynServer_Loader::fini ()
121 ACE_ASSERT(runner_.get() != 0);
122 try {
123 ACE_DEBUG((LM_DEBUG, "dynserver: shutting down.\n"));
125 runner_->end();
126 runner_.reset(0);
128 root_poa_->destroy(1, 1);
129 orb_->destroy();
131 ACE_DEBUG((LM_DEBUG, "dynserver: shut down successfully.\n"));
133 return 0;
134 } catch (Exception& e) {
135 e._tao_print_exception ("DynServer::fini()");
137 return -1;
140 Object_ptr
141 DynServer_Loader::create_object (ORB_ptr,
142 int,
143 ACE_TCHAR **)
145 throw NO_IMPLEMENT();
148 ACE_FACTORY_DEFINE (DynServer, DynServer_Loader)