Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / Object_Reactivation / Object_Reactivation.cpp
blob30e7e8ea7e8557c21832c4273c2af8d2b964ae1d
2 //=============================================================================
3 /**
4 * @file Object_Reactivation.cpp
6 * This program tests the reactivation of a servant that has been
7 * deactivated but not removed from the Active Object Map yet.
9 * @author Irfan Pyarali
11 //=============================================================================
14 #include "testS.h"
15 #include "ace/Task.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/SString.h"
18 #include "ace/Auto_Event.h"
19 #include "ace/OS_NS_unistd.h"
21 static int debug = 1;
23 static int
24 parse_args (int argc, ACE_TCHAR **argv)
26 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("d:"));
27 int c;
29 while ((c = get_opts ()) != -1)
30 switch (c)
32 case 'd':
33 debug = ACE_OS::atoi (get_opts.opt_arg ());
34 break;
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "-d debug "
40 "\n",
41 argv [0]),
42 -1);
45 return 0;
48 class test_i : public POA_test
50 public:
51 test_i (ACE_Auto_Event &event);
53 void deactivate_self ();
55 private:
56 ACE_Auto_Event &event_;
59 test_i::test_i (ACE_Auto_Event &event)
60 : event_ (event)
64 void
65 test_i::deactivate_self ()
67 PortableServer::POA_var poa = this->_default_POA ();
69 PortableServer::ObjectId_var id = poa->servant_to_id (this);
71 if (debug)
72 ACE_DEBUG ((LM_DEBUG, "(%t) Deactivating servant\n"));
74 poa->deactivate_object (id.in ());
76 if (debug)
77 ACE_DEBUG ((LM_DEBUG, "(%t) Deactivation complete: signaling main thread and going to sleep\n"));
79 int result = this->event_.signal ();
80 ACE_ASSERT (result == 0);
81 ACE_UNUSED_ARG (result);
83 ACE_OS::sleep (3);
85 if (debug)
86 ACE_DEBUG ((LM_DEBUG, "(%t) test_i::deactivate_self complete\n"));
89 class Activator : public ACE_Task_Base
91 public:
92 Activator (test_ptr t,
93 ACE_Auto_Event &event,
94 PortableServer::POA_ptr poa,
95 PortableServer::Servant servant,
96 const ACE_CString &task_id,
97 const PortableServer::ObjectId &id);
98 int svc ();
100 private:
101 test_var test_;
102 ACE_Auto_Event &event_;
103 PortableServer::POA_var poa_;
104 PortableServer::Servant servant_;
105 ACE_CString task_id_;
106 PortableServer::ObjectId id_;
109 Activator::Activator (test_ptr t,
110 ACE_Auto_Event &event,
111 PortableServer::POA_ptr poa,
112 PortableServer::Servant servant,
113 const ACE_CString &task_id,
114 const PortableServer::ObjectId &id)
115 : test_ (test::_duplicate (t)),
116 event_ (event),
117 poa_ (PortableServer::POA::_duplicate (poa)),
118 servant_ (servant),
119 task_id_ (task_id),
120 id_ (id)
125 Activator::svc ()
127 if (debug)
128 ACE_DEBUG ((LM_DEBUG, "(%t) Waiting for deactivation to complete\n"));
130 int result = this->event_.wait ();
131 ACE_ASSERT (result == 0);
132 ACE_UNUSED_ARG (result);
134 if (debug)
135 ACE_DEBUG ((LM_DEBUG, "(%t) Deactivation complete, trying to activate\n"));
139 if (this->task_id_ == "first thread")
141 PortableServer::ObjectId_var id =
142 this->poa_->activate_object (this->servant_);
144 else
146 this->poa_->activate_object_with_id (this->id_,
147 this->servant_);
150 if (debug)
151 ACE_DEBUG ((LM_DEBUG, "(%t) Activation complete\n"));
153 catch (const CORBA::Exception& ex)
155 ex._tao_print_exception ("Activator::svc");
156 return -1;
158 return 0;
161 class Deactivator : public ACE_Task_Base
163 public:
164 Deactivator (test_ptr t);
165 int svc ();
167 private:
168 test_var test_;
171 Deactivator::Deactivator (test_ptr t)
172 : test_ (test::_duplicate (t))
177 Deactivator::svc ()
181 this->test_->deactivate_self ();
183 catch (const CORBA::Exception& ex)
185 ex._tao_print_exception ("Deactivator::svc");
186 return -1;
188 return 0;
192 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
196 // Initialize the ORB first.
197 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
199 int parse_args_result =
200 parse_args (argc, argv);
202 if (parse_args_result != 0)
203 return parse_args_result;
205 // Obtain the RootPOA.
206 CORBA::Object_var obj =
207 orb->resolve_initial_references ("RootPOA");
209 // Get the POA_var object from Object_var.
210 PortableServer::POA_var root_poa =
211 PortableServer::POA::_narrow (obj.in ());
213 // Get the POAManager of the RootPOA.
214 PortableServer::POAManager_var poa_manager =
215 root_poa->the_POAManager ();
217 poa_manager->activate ();
219 ACE_Auto_Event event1;
220 test_i servant1 (event1);
222 ACE_Auto_Event event2;
223 test_i servant2 (event2);
225 PortableServer::ObjectId_var id_act =
226 root_poa->activate_object (&servant1);
228 CORBA::Object_var object_act = root_poa->id_to_reference (id_act.in ());
230 test_var test_object1 = test::_narrow (object_act.in ());
232 id_act = root_poa->activate_object (&servant2);
234 object_act = root_poa->id_to_reference (id_act.in ());
236 test_var test_object2 = test::_narrow (object_act.in ());
238 PortableServer::ObjectId_var id1 =
239 root_poa->reference_to_id (test_object1.in ());
241 PortableServer::ObjectId_var id2 =
242 root_poa->reference_to_id (test_object2.in ());
244 Activator activator1 (test_object1.in (),
245 event1,
246 root_poa.in (),
247 &servant1,
248 "first thread",
249 id1.in ());
251 Activator activator2 (test_object2.in (),
252 event2,
253 root_poa.in (),
254 &servant2,
255 "second thread",
256 id2.in ());
258 Deactivator deactivator1 (test_object1.in ());
260 Deactivator deactivator2 (test_object2.in ());
262 if (activator1.activate () != 0 ||
263 activator2.activate () != 0 ||
264 deactivator1.activate () != 0 ||
265 deactivator2.activate () != 0)
266 return -1;
268 int result = ACE_Thread_Manager::instance ()->wait ();
270 if (result != 0)
271 return result;
273 root_poa->destroy (true, true);
275 orb->destroy ();
277 catch (const CORBA::Exception& ex)
279 ex._tao_print_exception ("Exception caught");
280 return -1;
283 return 0;