Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Collocation_Opportunities / Collocation_Opportunities.cpp
blob0f8e79e67e254edb15c1ce32c402241e7f8b7a81
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:
12 test_i (CORBA::ORB_ptr orb,
13 PortableServer::POA_ptr poa);
15 void set_other (test_ptr test);
17 void method (void);
19 PortableServer::POA_ptr _default_POA (void);
21 CORBA::ORB_var orb_;
23 PortableServer::POA_var poa_;
25 test_var other_;
27 PortableServer::Current_var poa_current_;
30 test_i::test_i (CORBA::ORB_ptr orb,
31 PortableServer::POA_ptr poa)
32 : orb_ (CORBA::ORB::_duplicate (orb)),
33 poa_ (PortableServer::POA::_duplicate (poa))
35 CORBA::Object_var object;
37 object =
38 this->orb_->resolve_initial_references ("POACurrent");
40 this->poa_current_ =
41 PortableServer::Current::_narrow (object.in ());
44 void
45 test_i::set_other (test_ptr test)
47 this->other_ = test::_duplicate (test);
50 void
51 test_i::method (void)
53 if (this->other_.in () != test::_nil ())
55 this->other_->method ();
57 else
59 TAO::Portable_Server::POA_Current &tao_poa_current =
60 dynamic_cast <TAO::Portable_Server::POA_Current &>
61 (*(this->poa_current_.in ()));
63 TAO::Portable_Server::POA_Current_Impl &tao_poa_current_implementation =
64 *tao_poa_current.implementation ();
66 const char *upcall = 0;
67 if (tao_poa_current_implementation.previous ())
68 upcall = "collocated";
69 else
70 upcall = "remote";
72 PortableServer::ObjectId_var id =
73 this->poa_current_->get_object_id ();
75 CORBA::String_var id_string =
76 PortableServer::ObjectId_to_string (id.in ());
78 ACE_DEBUG ((LM_DEBUG,
79 "%C method() invoked for <%C> servant in thread %t\n",
80 upcall,
81 id_string.in ()));
85 PortableServer::POA_ptr
86 test_i::_default_POA (void)
88 return PortableServer::POA::_duplicate (this->poa_.in ());
91 class Task : public ACE_Task_Base
93 public:
95 Task (CORBA::ORB_ptr orb);
97 int svc (void);
99 CORBA::ORB_var orb_;
103 Task::Task (CORBA::ORB_ptr orb)
104 : orb_ (CORBA::ORB::_duplicate (orb))
109 Task::svc (void)
113 this->orb_->run ();
115 catch (const CORBA::Exception&)
119 return 0;
123 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
127 CORBA::ORB_var orb =
128 CORBA::ORB_init (argc, argv);
130 CORBA::Object_var object =
131 orb->resolve_initial_references ("RootPOA");
133 PortableServer::POA_var root_poa =
134 PortableServer::POA::_narrow (object.in ());
136 PortableServer::POAManager_var poa_manager =
137 root_poa->the_POAManager ();
139 poa_manager->activate ();
141 Task task (orb.in ());
143 int result =
144 task.activate ();
145 ACE_ASSERT (result != -1);
146 ACE_UNUSED_ARG (result);
148 CORBA::PolicyList policies (1);
149 policies.length (1);
151 policies[0] =
152 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
154 PortableServer::POA_var child_poa =
155 root_poa->create_POA ("child_poa",
156 poa_manager.in (),
157 policies);
159 test_i *base_servant =
160 new test_i (orb.in (),
161 child_poa.in ());
163 PortableServer::ServantBase_var safe_base_servant (base_servant);
165 PortableServer::ObjectId_var base_oid =
166 PortableServer::string_to_ObjectId ("base");
168 child_poa->activate_object_with_id (base_oid.in (),
169 base_servant);
171 PortableServer::ObjectId_var id_act =
172 root_poa->activate_object (base_servant);
174 CORBA::Object_var object_act = root_poa->id_to_reference (id_act.in ());
176 test_var base_test =
177 test::_narrow (object_act.in ());
179 test_i *first_servant =
180 new test_i (orb.in (),
181 child_poa.in ());
183 PortableServer::ServantBase_var safe_first_servant (first_servant);
185 PortableServer::ObjectId_var first_oid =
186 PortableServer::string_to_ObjectId ("first");
188 child_poa->activate_object_with_id (first_oid.in (),
189 first_servant);
191 PortableServer::ObjectId_var id_actu =
192 root_poa->activate_object (first_servant);
194 object_act = root_poa->id_to_reference (id_actu.in ());
196 test_var first_test =
197 test::_narrow (object_act.in ());
199 base_servant->set_other (first_test.in ());
201 base_test->method ();
203 PortableServer::ObjectId_var second_oid =
204 PortableServer::string_to_ObjectId ("second");
206 object =
207 child_poa->create_reference_with_id (second_oid.in (),
208 "IDL:test:1.0");
210 test_var second_test =
211 test::_narrow (object.in ());
213 test_i *second_servant =
214 new test_i (orb.in (),
215 child_poa.in ());
217 PortableServer::ServantBase_var safe_second_servant (second_servant);
219 child_poa->activate_object_with_id (second_oid.in (),
220 second_servant);
222 base_servant->set_other (second_test.in ());
224 base_test->method ();
226 PortableServer::ObjectId_var third_oid =
227 PortableServer::string_to_ObjectId ("third");
229 object =
230 child_poa->create_reference_with_id (third_oid.in (),
231 "IDL:test:1.0");
233 CORBA::String_var third_ior =
234 orb->object_to_string (object.in ());
236 object =
237 orb->string_to_object (third_ior.in ());
239 test_var third_test =
240 test::_narrow (object.in ());
242 test_i *third_servant =
243 new test_i (orb.in (),
244 child_poa.in ());
246 PortableServer::ServantBase_var safe_third_servant (third_servant);
248 child_poa->activate_object_with_id (third_oid.in (),
249 third_servant);
251 base_servant->set_other (third_test.in ());
253 base_test->method ();
255 orb->shutdown (1);
257 result = task.wait ();
258 ACE_ASSERT (result != -1);
259 ACE_UNUSED_ARG (result);
261 catch (const CORBA::Exception& ex)
263 ex._tao_print_exception ("Caught exception:");
264 return -1;
267 return 0;