4 #include "AppShutdown.h"
5 #include "TestAppExceptionC.h"
6 #include "Foo_B_ClientEngine.h"
7 #include "Foo_B_ClientEngine.h"
8 #include "Callback_i.h"
9 #include "ace/Get_Opt.h"
10 // To force static load the service.
11 #include "tao/PI/PI.h"
12 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
14 ServerApp::ServerApp()
15 : TestAppBase("TP_Test_3_Server"),
16 ior_filename_prefix_(ACE_TEXT("foo")),
20 num_remote_clients_(1),
21 num_collocated_clients_(0),
22 collocated_client_kind_(0)
27 ServerApp::run_i(int argc
, ACE_TCHAR
* argv
[])
29 int result
= this->init(argc
, argv
);
37 this->servant_setup();
38 this->collocated_setup();
40 this->run_collocated_clients();
43 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
45 this->run_orb_event_loop();
48 "(%P|%t) ServerApp ORB event loop has completed.\n"));
50 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
51 // that the main thread might exit before all CSD Threads exit.
53 // Wait for all CSD task threads exit.
54 ACE_Thread_Manager::instance ()->wait ();
57 result
= this->check_validity () ? 0 : -1;
60 "(%P|%t) ServerApp check_validity returned %d .\n", result
));
67 ServerApp::init(int argc
, ACE_TCHAR
* argv
[])
69 this->orb_
= CORBA::ORB_init(argc
, argv
);
71 // Parse the command-line args for this application.
72 // * Raises -1 if problems are encountered.
73 // * Returns 1 if the usage statement was explicitly requested.
74 // * Returns 0 otherwise.
75 int result
= this->parse_args(argc
, argv
);
81 unsigned num_clients
= this->num_remote_clients_
+
82 this->num_collocated_clients_
;
84 TheAppShutdown
->init(this->orb_
.in(), num_clients
);
91 ServerApp::poa_setup()
93 this->poa_
= this->create_poa(this->orb_
.in(),
96 if (this->num_collocated_clients_
> 0)
98 this->cb_poa_
= this->create_poa(this->orb_
.in(),
104 ServerApp::csd_setup()
106 this->tp_strategy_
= new TAO::CSD::TP_Strategy(this->num_csd_threads_
);
108 if (!this->tp_strategy_
->apply_to(this->poa_
.in()))
111 "Failed to apply CSD strategy to poa.\n"));
112 throw TestAppException();
115 // Use another poa and strategy for callbacks. This would resolve
116 // the deadlock situation that happens when having number of csd
117 // threads less than number of collocated clients.
118 if (this->num_collocated_clients_
> 0)
120 this->cb_tp_strategy_
= new TAO::CSD::TP_Strategy();
121 if (!this->cb_tp_strategy_
->apply_to(this->cb_poa_
.in()))
124 "Failed to apply CSD strategy to callback poa.\n"));
125 throw TestAppException();
132 ServerApp::servant_setup()
134 this->foo_servants_
.create_and_activate(this->num_servants_
,
137 this->ior_filename_prefix_
.c_str());
142 ServerApp::collocated_setup()
144 if (this->num_collocated_clients_
== 0)
147 this->cb_servants_
.create_and_activate(1, // number of callback servants
150 CallbackServantListType::T_stub_var cb
= this->cb_servants_
.objref(0);
152 unsigned client_id
= this->num_remote_clients_
;
154 for (unsigned i
= 0; i
< this->num_collocated_clients_
; i
++)
157 // Dole out the servant object references in a round-robin fashion.
158 unsigned servant_index
= i
% this->num_servants_
;
160 FooServantListType::T_stub_var foo
161 = this->foo_servants_
.objref(servant_index
);
162 ClientEngine_Handle engine
163 = new Foo_B_ClientEngine(foo
.in(), cb
.in (), client_id
);
164 this->collocated_client_task_
.add_engine(engine
.in());
170 ServerApp::poa_activate()
172 PortableServer::POAManager_var poa_manager
173 = this->poa_
->the_POAManager();
174 poa_manager
->activate();
179 ServerApp::run_collocated_clients()
181 if (this->num_collocated_clients_
> 0)
183 if (this->collocated_client_task_
.open() == -1)
185 throw TestAppException ();
192 ServerApp::run_orb_event_loop()
194 OrbRunner
orb_runner(this->orb_
.in(), this->num_orb_threads_
);
196 TheAppShutdown
->wait ();
207 ServerApp::parse_args(int argc
, ACE_TCHAR
* argv
[])
209 this->exe_name_
= argv
[0];
211 ACE_Get_Opt
get_opts(argc
, argv
, ACE_TEXT("p:s:n:t:r:c:k:"));
215 while ((c
= get_opts()) != -1)
221 this->ior_filename_prefix_
= get_opts
.opt_arg();
225 result
= set_arg(this->num_servants_
,
233 result
= set_arg(this->num_csd_threads_
,
241 result
= set_arg(this->num_orb_threads_
,
249 result
= set_arg(this->num_remote_clients_
,
252 "num_remote_clients");
256 result
= set_arg(this->num_collocated_clients_
,
259 "num_collocated_clients");
263 result
= set_arg(this->collocated_client_kind_
,
266 "collocated_client_kind");
270 this->usage_statement();
274 this->usage_statement();
284 return this->arg_dependency_checks();
288 ServerApp::usage_statement()
291 "Usage: %s [options]\n\n"
293 "\t[-p <ior_filename_prefix>]\n"
294 "\t[-s <num_servants>]\n"
295 "\t[-n <num_csd_threads>]\n"
296 "\t[-t <num_orb_threads>]\n"
297 "\t[-r <num_remote_clients>]\n"
298 "\t[-c <num_collocated_clients>]\n"
299 "\t[-k <collocated_client_kind>]\n"
301 this->exe_name_
.c_str()));
306 ServerApp::arg_dependency_checks()
308 return (this->num_remote_clients_
309 + this->num_collocated_clients_
) > 0 ? 0 : -1;
314 ServerApp::set_arg(unsigned& value
,
315 const ACE_TCHAR
* arg
,
320 int tmp
= ACE_OS::atoi(arg
);
325 "Error: -%c <%s> must be integer type with a value of, "
326 "at least, %d.\n", opt
, name
, min
));
327 this->usage_statement();
336 PortableServer::POA_ptr
337 ServerApp::create_poa(CORBA::ORB_ptr orb
,
338 const char* poa_name
)
341 PortableServer::POA_var root_poa
342 = RefHelper
<PortableServer::POA
>::resolve_initial_ref(orb
,
345 // Get the POAManager from the Root POA.
346 PortableServer::POAManager_var poa_manager
347 = root_poa
->the_POAManager();
349 // Create the child POA Policies.
350 CORBA::PolicyList
policies(0);
353 // Create the child POA
354 PortableServer::POA_var poa
= AppHelper::create_poa(poa_name
,
359 // Give away the child POA_ptr from the POA_var variable.
365 ServerApp::check_validity ()
367 // Check whether the clients return any errors.
368 if (this->num_collocated_clients_
> 0
369 && this->collocated_client_task_
.failure_count () > 0)
374 Foo_B_Statistics
stats (this->num_remote_clients_
,
375 this->num_collocated_clients_
);
377 Foo_B_ClientEngine::expected_results (stats
);
379 for (unsigned i
= 0; i
< this->num_servants_
; i
++)
381 this->foo_servants_
.servant(i
)->gather_stats (stats
);
384 if (this->num_collocated_clients_
> 0)
386 this->cb_servants_
.servant (0)->gather_stats (stats
);
389 return stats
.actual_vs_expected ();