Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / TP_Test_3 / ServerApp.cpp
blob66d647e3d4e5dda07fd969baf05c83d41bb346cb
1 #include "ServerApp.h"
2 #include "AppHelper.h"
3 #include "OrbRunner.h"
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")),
17 num_servants_(1),
18 num_csd_threads_(1),
19 num_orb_threads_(1),
20 num_remote_clients_(1),
21 num_collocated_clients_(0),
22 collocated_client_kind_(0)
26 int
27 ServerApp::run_i(int argc, ACE_TCHAR* argv[])
29 int result = this->init(argc, argv);
30 if (result != 0)
32 return result;
35 this->poa_setup();
36 this->csd_setup();
37 this->servant_setup();
38 this->collocated_setup();
39 this->poa_activate();
40 this->run_collocated_clients();
42 ACE_DEBUG((LM_DEBUG,
43 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
45 this->run_orb_event_loop();
47 ACE_DEBUG((LM_DEBUG,
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 ();
56 this->cleanup();
57 result = this->check_validity () ? 0 : -1;
59 ACE_DEBUG((LM_DEBUG,
60 "(%P|%t) ServerApp check_validity returned %d .\n", result));
62 return result;
66 int
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);
76 if (result != 0)
78 return result;
81 unsigned num_clients = this->num_remote_clients_ +
82 this->num_collocated_clients_;
84 TheAppShutdown->init(this->orb_.in(), num_clients);
86 return 0;
90 void
91 ServerApp::poa_setup()
93 this->poa_ = this->create_poa(this->orb_.in(),
94 "ChildPoa");
96 if (this->num_collocated_clients_ > 0)
98 this->cb_poa_ = this->create_poa(this->orb_.in(),
99 "CallbackPoa");
103 void
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()))
110 ACE_ERROR((LM_ERROR,
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()))
123 ACE_ERROR((LM_ERROR,
124 "Failed to apply CSD strategy to callback poa.\n"));
125 throw TestAppException();
131 void
132 ServerApp::servant_setup()
134 this->foo_servants_.create_and_activate(this->num_servants_,
135 this->orb_.in (),
136 this->poa_.in (),
137 this->ior_filename_prefix_.c_str());
141 void
142 ServerApp::collocated_setup()
144 if (this->num_collocated_clients_ == 0)
145 return;
147 this->cb_servants_.create_and_activate(1, // number of callback servants
148 this->cb_poa_.in());
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++)
156 client_id ++;
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());
169 void
170 ServerApp::poa_activate()
172 PortableServer::POAManager_var poa_manager
173 = this->poa_->the_POAManager();
174 poa_manager->activate();
178 void
179 ServerApp::run_collocated_clients()
181 if (this->num_collocated_clients_ > 0)
183 if (this->collocated_client_task_.open() == -1)
185 throw TestAppException ();
191 void
192 ServerApp::run_orb_event_loop()
194 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
195 orb_runner.run();
196 TheAppShutdown->wait ();
200 void
201 ServerApp::cleanup()
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:"));
213 int c;
215 while ((c = get_opts()) != -1)
217 int result = 0;
218 switch (c)
220 case 'p':
221 this->ior_filename_prefix_ = get_opts.opt_arg();
222 break;
224 case 's':
225 result = set_arg(this->num_servants_,
226 get_opts.opt_arg(),
228 "num_servants",
230 break;
232 case 'n':
233 result = set_arg(this->num_csd_threads_,
234 get_opts.opt_arg(),
236 "num_servants",
238 break;
240 case 't':
241 result = set_arg(this->num_orb_threads_,
242 get_opts.opt_arg(),
244 "num_orb_threads",
246 break;
248 case 'r':
249 result = set_arg(this->num_remote_clients_,
250 get_opts.opt_arg(),
252 "num_remote_clients");
253 break;
255 case 'c':
256 result = set_arg(this->num_collocated_clients_,
257 get_opts.opt_arg(),
259 "num_collocated_clients");
260 break;
262 case 'k':
263 result = set_arg(this->collocated_client_kind_,
264 get_opts.opt_arg(),
266 "collocated_client_kind");
267 break;
269 case '?':
270 this->usage_statement();
271 return 1;
273 default:
274 this->usage_statement();
275 return -1;
278 if (result != 0)
280 return result;
284 return this->arg_dependency_checks();
287 void
288 ServerApp::usage_statement()
290 ACE_ERROR((LM_ERROR,
291 "Usage: %s [options]\n\n"
292 "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"
300 "\t[-?]\n\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,
316 char opt,
317 const char* name,
318 int min)
320 int tmp = ACE_OS::atoi(arg);
322 if (tmp < min)
324 ACE_ERROR((LM_ERROR,
325 "Error: -%c <%s> must be integer type with a value of, "
326 "at least, %d.\n", opt, name, min));
327 this->usage_statement();
328 return -1;
331 value = tmp;
332 return 0;
336 PortableServer::POA_ptr
337 ServerApp::create_poa(CORBA::ORB_ptr orb,
338 const char* poa_name)
340 // Get the Root POA.
341 PortableServer::POA_var root_poa
342 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
343 "RootPOA");
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);
351 policies.length(0);
353 // Create the child POA
354 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
355 root_poa.in(),
356 poa_manager.in(),
357 policies);
359 // Give away the child POA_ptr from the POA_var variable.
360 return poa._retn();
364 bool
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)
371 return false;
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 ();