2 //=============================================================================
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 //=============================================================================
16 #include "ace/Get_Opt.h"
17 #include "ace/SString.h"
18 #include "ace/Auto_Event.h"
19 #include "ace/OS_NS_unistd.h"
24 parse_args (int argc
, ACE_TCHAR
**argv
)
26 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("d:"));
29 while ((c
= get_opts ()) != -1)
33 debug
= ACE_OS::atoi (get_opts
.opt_arg ());
37 ACE_ERROR_RETURN ((LM_ERROR
,
48 class test_i
: public POA_test
51 test_i (ACE_Auto_Event
&event
);
53 void deactivate_self ();
56 ACE_Auto_Event
&event_
;
59 test_i::test_i (ACE_Auto_Event
&event
)
65 test_i::deactivate_self ()
67 PortableServer::POA_var poa
= this->_default_POA ();
69 PortableServer::ObjectId_var id
= poa
->servant_to_id (this);
72 ACE_DEBUG ((LM_DEBUG
, "(%t) Deactivating servant\n"));
74 poa
->deactivate_object (id
.in ());
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
);
86 ACE_DEBUG ((LM_DEBUG
, "(%t) test_i::deactivate_self complete\n"));
89 class Activator
: public ACE_Task_Base
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
);
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
)),
117 poa_ (PortableServer::POA::_duplicate (poa
)),
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
);
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_
);
146 this->poa_
->activate_object_with_id (this->id_
,
151 ACE_DEBUG ((LM_DEBUG
, "(%t) Activation complete\n"));
153 catch (const CORBA::Exception
& ex
)
155 ex
._tao_print_exception ("Activator::svc");
161 class Deactivator
: public ACE_Task_Base
164 Deactivator (test_ptr t
);
171 Deactivator::Deactivator (test_ptr t
)
172 : test_ (test::_duplicate (t
))
181 this->test_
->deactivate_self ();
183 catch (const CORBA::Exception
& ex
)
185 ex
._tao_print_exception ("Deactivator::svc");
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 (),
251 Activator
activator2 (test_object2
.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)
268 int result
= ACE_Thread_Manager::instance ()->wait ();
273 root_poa
->destroy (true, true);
277 catch (const CORBA::Exception
& ex
)
279 ex
._tao_print_exception ("Exception caught");