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::~ServerApp()
33 ServerApp::run_i(int argc
, ACE_TCHAR
* argv
[])
35 int result
= this->init(argc
, argv
);
43 this->servant_setup();
44 this->collocated_setup();
46 this->run_collocated_clients();
49 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
51 this->run_orb_event_loop();
54 "(%P|%t) ServerApp ORB event loop has completed.\n"));
56 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
57 // that the main thread might exit before all CSD Threads exit.
59 // Wait for all CSD task threads exit.
60 ACE_Thread_Manager::instance ()->wait ();
63 result
= this->check_validity () ? 0 : -1;
66 "(%P|%t) ServerApp check_validity returned %d .\n", result
));
73 ServerApp::init(int argc
, ACE_TCHAR
* argv
[])
75 this->orb_
= CORBA::ORB_init(argc
, argv
);
77 // Parse the command-line args for this application.
78 // * Raises -1 if problems are encountered.
79 // * Returns 1 if the usage statement was explicitly requested.
80 // * Returns 0 otherwise.
81 int result
= this->parse_args(argc
, argv
);
87 unsigned num_clients
= this->num_remote_clients_
+
88 this->num_collocated_clients_
;
90 TheAppShutdown
->init(this->orb_
.in(), num_clients
);
97 ServerApp::poa_setup(void)
99 this->poa_
= this->create_poa(this->orb_
.in(),
102 if (this->num_collocated_clients_
> 0)
104 this->cb_poa_
= this->create_poa(this->orb_
.in(),
110 ServerApp::csd_setup(void)
112 this->tp_strategy_
= new TAO::CSD::TP_Strategy(this->num_csd_threads_
);
114 if (!this->tp_strategy_
->apply_to(this->poa_
.in()))
117 "Failed to apply CSD strategy to poa.\n"));
118 throw TestAppException();
121 // Use another poa and strategy for callbacks. This would resolve
122 // the deadlock situation that happens when having number of csd
123 // threads less than number of collocated clients.
124 if (this->num_collocated_clients_
> 0)
126 this->cb_tp_strategy_
= new TAO::CSD::TP_Strategy();
127 if (!this->cb_tp_strategy_
->apply_to(this->cb_poa_
.in()))
130 "Failed to apply CSD strategy to callback poa.\n"));
131 throw TestAppException();
138 ServerApp::servant_setup(void)
140 this->foo_servants_
.create_and_activate(this->num_servants_
,
143 this->ior_filename_prefix_
.c_str());
148 ServerApp::collocated_setup(void)
150 if (this->num_collocated_clients_
== 0)
153 this->cb_servants_
.create_and_activate(1, // number of callback servants
156 CallbackServantListType::T_stub_var cb
= this->cb_servants_
.objref(0);
158 unsigned client_id
= this->num_remote_clients_
;
160 for (unsigned i
= 0; i
< this->num_collocated_clients_
; i
++)
163 // Dole out the servant object references in a round-robin fashion.
164 unsigned servant_index
= i
% this->num_servants_
;
166 FooServantListType::T_stub_var foo
167 = this->foo_servants_
.objref(servant_index
);
168 ClientEngine_Handle engine
169 = new Foo_B_ClientEngine(foo
.in(), cb
.in (), client_id
, true);
170 this->collocated_client_task_
.add_engine(engine
.in());
176 ServerApp::poa_activate(void)
178 PortableServer::POAManager_var poa_manager
179 = this->poa_
->the_POAManager();
180 poa_manager
->activate();
185 ServerApp::run_collocated_clients(void)
187 if (this->num_collocated_clients_
> 0)
189 if (this->collocated_client_task_
.open() == -1)
191 throw TestAppException ();
198 ServerApp::run_orb_event_loop(void)
200 OrbRunner
orb_runner(this->orb_
.in(), this->num_orb_threads_
);
202 TheAppShutdown
->wait ();
213 ServerApp::parse_args(int argc
, ACE_TCHAR
* argv
[])
215 this->exe_name_
= argv
[0];
217 ACE_Get_Opt
get_opts(argc
, argv
, ACE_TEXT("p:s:n:t:r:c:k:"));
221 while ((c
= get_opts()) != -1)
227 this->ior_filename_prefix_
= get_opts
.opt_arg();
231 result
= set_arg(this->num_servants_
,
239 result
= set_arg(this->num_csd_threads_
,
247 result
= set_arg(this->num_orb_threads_
,
255 result
= set_arg(this->num_remote_clients_
,
258 "num_remote_clients");
262 result
= set_arg(this->num_collocated_clients_
,
265 "num_collocated_clients");
269 result
= set_arg(this->collocated_client_kind_
,
272 "collocated_client_kind");
276 this->usage_statement();
280 this->usage_statement();
290 return this->arg_dependency_checks();
294 ServerApp::usage_statement()
297 "Usage: %s [options]\n\n"
299 "\t[-p <ior_filename_prefix>]\n"
300 "\t[-s <num_servants>]\n"
301 "\t[-n <num_csd_threads>]\n"
302 "\t[-t <num_orb_threads>]\n"
303 "\t[-r <num_remote_clients>]\n"
304 "\t[-c <num_collocated_clients>]\n"
305 "\t[-k <collocated_client_kind>]\n"
307 this->exe_name_
.c_str()));
312 ServerApp::arg_dependency_checks()
314 return (this->num_remote_clients_
315 + this->num_collocated_clients_
) > 0 ? 0 : -1;
320 ServerApp::set_arg(unsigned& value
,
321 const ACE_TCHAR
* arg
,
326 int tmp
= ACE_OS::atoi(arg
);
331 "Error: -%c <%s> must be integer type with a value of, "
332 "at least, %d.\n", opt
, name
, min
));
333 this->usage_statement();
342 PortableServer::POA_ptr
343 ServerApp::create_poa(CORBA::ORB_ptr orb
,
344 const char* poa_name
)
347 PortableServer::POA_var root_poa
348 = RefHelper
<PortableServer::POA
>::resolve_initial_ref(orb
,
351 // Get the POAManager from the Root POA.
352 PortableServer::POAManager_var poa_manager
353 = root_poa
->the_POAManager();
355 // Create the child POA Policies.
356 CORBA::PolicyList
policies(0);
359 // Create the child POA
360 PortableServer::POA_var poa
= AppHelper::create_poa(poa_name
,
365 // Give away the child POA_ptr from the POA_var variable.
371 ServerApp::check_validity ()
373 // Check whether the clients return any errors.
374 if (this->num_collocated_clients_
> 0
375 && this->collocated_client_task_
.failure_count () > 0)
380 Foo_B_Statistics
stats (this->num_remote_clients_
,
381 this->num_collocated_clients_
);
383 Foo_B_ClientEngine::expected_results (stats
);
385 for (unsigned i
= 0; i
< this->num_servants_
; i
++)
387 this->foo_servants_
.servant(i
)->gather_stats (stats
);
390 if (this->num_collocated_clients_
> 0)
392 this->cb_servants_
.servant (0)->gather_stats (stats
);
395 return stats
.actual_vs_expected ();