3 #include "Callback_i.h"
4 #include "ClientTask.h"
5 #include "OrbShutdownTask.h"
6 #include "ace/Get_Opt.h"
7 #include "tao/CSD_ThreadPool/CSD_TP_Strategy.h"
8 #include "tao/Intrusive_Ref_Count_Handle_T.h"
9 #include "tao/Messaging/Messaging.h"
10 #include "tao/AnyTypeCode/Any.h"
11 // To force static load the service.
12 #include "tao/PI/PI.h"
13 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
16 ServerApp::ServerApp()
17 : synch_with_server_ (0)
22 ServerApp::~ServerApp()
28 ServerApp::run (int argc
, ACE_TCHAR
* argv
[])
30 this->orb_
= CORBA::ORB_init (argc
, argv
);
32 // Parse the command-line args for this application.
33 // * Raises -1 if problems are encountered.
34 // * Returns 1 if the usage statement was explicitly requested.
35 // * Returns 0 otherwise.
36 int result
= this->parse_args (argc
, argv
);
42 TheOrbShutdownTask::instance()->orb (this->orb_
.in ());
44 if (synch_with_server_
)
46 CORBA::Object_var manager_object
=
47 orb_
->resolve_initial_references("ORBPolicyManager");
49 CORBA::PolicyManager_var policy_manager
50 = CORBA::PolicyManager::_narrow(manager_object
.in());
52 if (CORBA::is_nil (policy_manager
.in ()))
55 " (%P|%t) Panic: nil PolicyManager\n"));
56 throw TestException();
59 CORBA::Any policy_value
;
60 policy_value
<<= Messaging::SYNC_WITH_SERVER
;
61 CORBA::PolicyList
policies(1);
65 orb_
->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE
,
68 policy_manager
->set_policy_overrides (policies
,
71 policies
[0]->destroy ();
75 = orb_
->resolve_initial_references("RootPOA");
77 if (CORBA::is_nil(obj
.in()))
80 "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n"));
81 throw TestException();
84 PortableServer::POA_var root_poa
85 = PortableServer::POA::_narrow(obj
.in());
87 if (CORBA::is_nil(root_poa
.in()))
90 "(%P|%t) Failed to narrow obj ref to POA interface.\n"));
91 throw TestException();
94 PortableServer::POAManager_var poa_manager
95 = root_poa
->the_POAManager();
97 // Create the child POA.
98 CORBA::PolicyList
policies(1);
101 policies
[0] = root_poa
->create_id_assignment_policy(PortableServer::USER_ID
);
103 PortableServer::POA_var child_poa_1
= root_poa
->create_POA("ChildPoa_1",
107 if (CORBA::is_nil(child_poa_1
.in()))
109 ACE_ERROR((LM_ERROR
, "(%P|%t) ERROR [ServerApp::run()]: "
110 "Failed to create the ChildPoa_1.\n"));
111 throw TestException();
114 PortableServer::POA_var child_poa_2
= root_poa
->create_POA("ChildPoa_2",
118 if (CORBA::is_nil(child_poa_2
.in()))
120 ACE_ERROR((LM_ERROR
, "(%P|%t) ERROR [ServerApp::run()]: "
121 "Failed to create the ChildPoa_2.\n"));
122 throw TestException();
125 policies
[0]->destroy ();
127 // Create the thread pool servant dispatching strategy object, and
128 // hold it in a (local) smart pointer variable.
129 TAO_Intrusive_Ref_Count_Handle
<TAO::CSD::TP_Strategy
> csd_tp_strategy
=
130 new TAO::CSD::TP_Strategy();
132 // We need create multiple working threads otherwise it would deadlock
133 // with the callback test.
134 csd_tp_strategy
->set_num_threads(2);
136 // Tell the strategy to apply itself to the child poa.
137 if (csd_tp_strategy
->apply_to(child_poa_1
.in()) == false)
139 ACE_ERROR((LM_ERROR
, "(%P|%t) ERROR [ServerApp::run()]: "
140 "Failed to apply custom dispatching strategy to child poa 1.\n"));
141 throw TestException();
144 Foo_var foo1
= this->create_foo(child_poa_1
.in(),
145 "foo_applied_strategy");
147 Foo_var foo2
= this->create_foo(child_poa_2
.in(),
148 "foo_not_applied_strategy");
150 Callback_var callback1
151 = this->create_callback(child_poa_1
.in(),
152 "callback_applied_strategy");
154 Callback_var callback2
155 = this->create_callback(child_poa_2
.in(),
156 "callback_not_applied_strategy");
158 // Activate the POA Manager
159 poa_manager
->activate();
162 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
164 // Wait for the custom stretegy launch the dispatching threads.
167 ClientTask
client1 (foo1
.in (), callback1
.in ());
168 if (client1
.open() != 0)
171 "(%P|%t) Failed to open the collocated client1.\n"));
172 throw TestException();
175 ClientTask
client2 (foo2
.in (), callback2
.in ());
176 if (client2
.open() != 0)
179 "(%P|%t) Failed to open the collocated client2.\n"));
180 throw TestException();
183 // Run the ORB event loop.
187 "(%P|%t) ServerApp ORB has stopped running.\n"));
190 "(%P|%t) Wait for the OrbShutdownTask.\n"));
192 TheOrbShutdownTask::instance()->wait ();
195 "(%P|%t) Wait for the collocated client task.\n"));
201 "(%P|%t) Stop the CSD strategy.\n"));
204 "(%P|%t) ServerApp is destroying the Root POA.\n"));
206 // Tear-down the root poa and orb_.
207 root_poa
->destroy(1, 1);
210 "(%P|%t) ServerApp is destroying the ORB.\n"));
215 "(%P|%t) ServerApp has completed running successfully.\n"));
222 ServerApp::parse_args(int argc
, ACE_TCHAR
* argv
[])
224 this->exe_name_
= argv
[0];
226 ACE_Get_Opt
get_opts(argc
, argv
, ACE_TEXT("s"));
230 while ((c
= get_opts()) != -1)
235 this->synch_with_server_
= 1;
239 this->usage_statement();
243 this->usage_statement();
253 ServerApp::create_foo (
254 PortableServer::POA_ptr poa
,
255 const char* servant_name
)
257 PortableServer::ServantBase_var servant
258 = new Foo_i(servant_name
);
260 PortableServer::ObjectId_var id
=
261 PortableServer::string_to_ObjectId(servant_name
);
263 poa
->activate_object_with_id(id
.in(), servant
.in());
265 CORBA::Object_var obj
= poa
->id_to_reference(id
.in());
267 if (CORBA::is_nil(obj
.in()))
270 "(%P|%t) Failed to activate servant (%s).\n",
272 throw TestException();
275 Foo_var foo
= Foo::_narrow (obj
.in ());
281 ServerApp::create_callback(
282 PortableServer::POA_ptr poa
,
283 const char* servant_name
)
285 PortableServer::ServantBase_var servant
288 PortableServer::ObjectId_var id
=
289 PortableServer::string_to_ObjectId("callback");
291 poa
->activate_object_with_id(id
.in(), servant
.in());
293 CORBA::Object_var obj
= poa
->id_to_reference(id
.in());
295 if (CORBA::is_nil(obj
.in()))
298 "(%P|%t) Failed to activate servant (%s).\n",
300 throw TestException();
303 Callback_var callback
= Callback::_narrow (obj
.in ());
305 return callback
._retn ();
309 ServerApp::usage_statement()
312 "Usage: %s [options]\n\n"
314 "\t[-s <synch_with_server>]\n"
316 this->exe_name_
.c_str()));