Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Collocation_Opportunities / Collocation_Opportunities.cpp
blob06721d060afb13ebd5705b40f1f6310fd36ccdfe
1 #include "testS.h"
2 #include "ace/Task.h"
3 #include "tao/PortableServer/Object_Adapter.h"
4 #include "tao/PortableServer/POA_Current.h"
5 #include "tao/PortableServer/POA_Current_Impl.h"
7 class test_i :
8 public POA_test
10 public:
11 test_i (CORBA::ORB_ptr orb,
12 PortableServer::POA_ptr poa);
14 void set_other (test_ptr test);
16 void method ();
18 PortableServer::POA_ptr _default_POA ();
20 CORBA::ORB_var orb_;
22 PortableServer::POA_var poa_;
24 test_var other_;
26 PortableServer::Current_var poa_current_;
29 test_i::test_i (CORBA::ORB_ptr orb,
30 PortableServer::POA_ptr poa)
31 : orb_ (CORBA::ORB::_duplicate (orb)),
32 poa_ (PortableServer::POA::_duplicate (poa))
34 CORBA::Object_var object;
36 object =
37 this->orb_->resolve_initial_references ("POACurrent");
39 this->poa_current_ =
40 PortableServer::Current::_narrow (object.in ());
43 void
44 test_i::set_other (test_ptr test)
46 this->other_ = test::_duplicate (test);
49 void
50 test_i::method ()
52 if (this->other_.in () != test::_nil ())
54 this->other_->method ();
56 else
58 TAO::Portable_Server::POA_Current &tao_poa_current =
59 dynamic_cast <TAO::Portable_Server::POA_Current &>
60 (*(this->poa_current_.in ()));
62 TAO::Portable_Server::POA_Current_Impl &tao_poa_current_implementation =
63 *tao_poa_current.implementation ();
65 const char *upcall = 0;
66 if (tao_poa_current_implementation.previous ())
67 upcall = "collocated";
68 else
69 upcall = "remote";
71 PortableServer::ObjectId_var id =
72 this->poa_current_->get_object_id ();
74 CORBA::String_var id_string =
75 PortableServer::ObjectId_to_string (id.in ());
77 ACE_DEBUG ((LM_DEBUG,
78 "%C method() invoked for <%C> servant in thread %t\n",
79 upcall,
80 id_string.in ()));
84 PortableServer::POA_ptr
85 test_i::_default_POA ()
87 return PortableServer::POA::_duplicate (this->poa_.in ());
90 class Task : public ACE_Task_Base
92 public:
93 Task (CORBA::ORB_ptr orb);
95 int svc ();
97 CORBA::ORB_var orb_;
100 Task::Task (CORBA::ORB_ptr orb)
101 : orb_ (CORBA::ORB::_duplicate (orb))
106 Task::svc ()
110 this->orb_->run ();
112 catch (const CORBA::Exception&)
116 return 0;
120 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
124 CORBA::ORB_var orb =
125 CORBA::ORB_init (argc, argv);
127 CORBA::Object_var object =
128 orb->resolve_initial_references ("RootPOA");
130 PortableServer::POA_var root_poa =
131 PortableServer::POA::_narrow (object.in ());
133 PortableServer::POAManager_var poa_manager =
134 root_poa->the_POAManager ();
136 poa_manager->activate ();
138 Task task (orb.in ());
140 int result =
141 task.activate ();
142 ACE_ASSERT (result != -1);
143 ACE_UNUSED_ARG (result);
145 CORBA::PolicyList policies (1);
146 policies.length (1);
148 policies[0] =
149 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
151 PortableServer::POA_var child_poa =
152 root_poa->create_POA ("child_poa",
153 poa_manager.in (),
154 policies);
156 test_i *base_servant =
157 new test_i (orb.in (),
158 child_poa.in ());
160 PortableServer::ServantBase_var safe_base_servant (base_servant);
162 PortableServer::ObjectId_var base_oid =
163 PortableServer::string_to_ObjectId ("base");
165 child_poa->activate_object_with_id (base_oid.in (),
166 base_servant);
168 PortableServer::ObjectId_var id_act =
169 root_poa->activate_object (base_servant);
171 CORBA::Object_var object_act = root_poa->id_to_reference (id_act.in ());
173 test_var base_test =
174 test::_narrow (object_act.in ());
176 test_i *first_servant =
177 new test_i (orb.in (),
178 child_poa.in ());
180 PortableServer::ServantBase_var safe_first_servant (first_servant);
182 PortableServer::ObjectId_var first_oid =
183 PortableServer::string_to_ObjectId ("first");
185 child_poa->activate_object_with_id (first_oid.in (),
186 first_servant);
188 PortableServer::ObjectId_var id_actu =
189 root_poa->activate_object (first_servant);
191 object_act = root_poa->id_to_reference (id_actu.in ());
193 test_var first_test =
194 test::_narrow (object_act.in ());
196 base_servant->set_other (first_test.in ());
198 base_test->method ();
200 PortableServer::ObjectId_var second_oid =
201 PortableServer::string_to_ObjectId ("second");
203 object =
204 child_poa->create_reference_with_id (second_oid.in (),
205 "IDL:test:1.0");
207 test_var second_test =
208 test::_narrow (object.in ());
210 test_i *second_servant =
211 new test_i (orb.in (),
212 child_poa.in ());
214 PortableServer::ServantBase_var safe_second_servant (second_servant);
216 child_poa->activate_object_with_id (second_oid.in (),
217 second_servant);
219 base_servant->set_other (second_test.in ());
221 base_test->method ();
223 PortableServer::ObjectId_var third_oid =
224 PortableServer::string_to_ObjectId ("third");
226 object =
227 child_poa->create_reference_with_id (third_oid.in (),
228 "IDL:test:1.0");
230 CORBA::String_var third_ior =
231 orb->object_to_string (object.in ());
233 object =
234 orb->string_to_object (third_ior.in ());
236 test_var third_test =
237 test::_narrow (object.in ());
239 test_i *third_servant =
240 new test_i (orb.in (),
241 child_poa.in ());
243 PortableServer::ServantBase_var safe_third_servant (third_servant);
245 child_poa->activate_object_with_id (third_oid.in (),
246 third_servant);
248 base_servant->set_other (third_test.in ());
250 base_test->method ();
252 orb->shutdown (true);
254 result = task.wait ();
255 ACE_ASSERT (result != -1);
256 ACE_UNUSED_ARG (result);
258 catch (const CORBA::Exception& ex)
260 ex._tao_print_exception ("Caught exception:");
261 return -1;
264 return 0;