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_4 / ServerApp.cpp
blob8c94b0eea4c6db4d7b2a337ee005e5ad11c9b57d
1 #include "ServerApp.h"
2 #include "AppHelper.h"
3 #include "OrbRunner.h"
4 #include "AppShutdown.h"
5 #include "TestAppExceptionC.h"
6 #include "Foo_C_Custom_ClientEngine.h"
7 #include "Foo_C_ClientEngine.h"
8 #include "Foo_C_Statistics.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_4_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();
41 this->run_orb_event_loop();
43 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
44 // that the main thread might exit before all CSD Threads exit.
46 // Wait for all CSD task threads exit.
47 ACE_Thread_Manager::instance ()->wait ();
49 this->cleanup();
50 return this->check_validity () ? 0 : -1;
54 int
55 ServerApp::init(int argc, ACE_TCHAR* argv[])
57 this->orb_ = CORBA::ORB_init(argc, argv);
59 // Parse the command-line args for this application.
60 // * Raises -1 if problems are encountered.
61 // * Returns 1 if the usage statement was explicitly requested.
62 // * Returns 0 otherwise.
63 int result = this->parse_args(argc, argv);
64 if (result != 0)
66 return result;
69 unsigned num_clients = this->num_remote_clients_ +
70 this->num_collocated_clients_;
72 TheAppShutdown->init(this->orb_.in(), num_clients);
74 return 0;
78 void
79 ServerApp::poa_setup()
81 this->poa_ = this->create_poa(this->orb_.in(),
82 "ChildPoa");
86 void
87 ServerApp::csd_setup()
89 this->tp_strategy_ = new TAO::CSD::TP_Strategy(this->num_csd_threads_);
91 if (!this->tp_strategy_->apply_to(this->poa_.in()))
93 ACE_ERROR((LM_ERROR,
94 "Failed to apply CSD strategy to poa.\n"));
95 throw TestAppException();
100 void
101 ServerApp::servant_setup()
103 this->servants_.create_and_activate(this->num_servants_,
104 this->orb_.in (),
105 this->poa_.in (),
106 this->ior_filename_prefix_.c_str());
110 void
111 ServerApp::collocated_setup()
113 int custom_client_id_start = this->num_remote_clients_;
115 unsigned servant_index = 0;
117 for (unsigned i = 0; i < this->num_collocated_clients_; i++)
119 if (i > 0)
121 // Dole out the servant object references in a round-robin fashion.
122 servant_index = (servant_index + 1) % this->num_servants_;
125 ServantListType::T_stub_var obj = this->servants_.objref(servant_index);
127 ClientEngine_Handle engine =
128 new Foo_C_Custom_ClientEngine(this->servants_.servant(servant_index),
129 obj.in(),
130 this->tp_strategy_.in(),
131 ++ custom_client_id_start);
132 this->collocated_client_task_.add_engine(engine.in());
137 void
138 ServerApp::poa_activate()
140 PortableServer::POAManager_var poa_manager
141 = this->poa_->the_POAManager();
143 poa_manager->activate();
147 void
148 ServerApp::run_collocated_clients()
150 if (this->num_collocated_clients_ > 0)
152 if (this->collocated_client_task_.open() == -1)
154 throw TestAppException ();
160 void
161 ServerApp::run_orb_event_loop()
163 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
164 orb_runner.run();
165 TheAppShutdown->wait ();
169 void
170 ServerApp::cleanup()
172 for (unsigned i = 0; i < this->num_servants_; i++)
174 this->servants_.servant(i)->dump();
177 // Wait for all of the collocated client task threads to finish.
178 if (this->num_collocated_clients_ > 0)
180 this->collocated_client_task_.wait();
186 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
188 this->exe_name_ = argv[0];
190 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("p:s:n:t:r:c:k:"));
192 int c;
194 while ((c = get_opts()) != -1)
196 int result = 0;
197 switch (c)
199 case 'p':
200 this->ior_filename_prefix_ = get_opts.opt_arg();
201 break;
203 case 's':
204 result = this->set_arg(this->num_servants_,
205 get_opts.opt_arg(),
207 "num_servants",
209 break;
211 case 'n':
212 result = this->set_arg(this->num_csd_threads_,
213 get_opts.opt_arg(),
215 "num_servants",
217 break;
219 case 't':
220 result = this->set_arg(this->num_orb_threads_,
221 get_opts.opt_arg(),
223 "num_orb_threads",
225 break;
227 case 'r':
228 result = this->set_arg(this->num_remote_clients_,
229 get_opts.opt_arg(),
231 "num_remote_clients");
232 break;
234 case 'c':
235 result = this->set_arg(this->num_collocated_clients_,
236 get_opts.opt_arg(),
238 "num_collocated_clients");
239 break;
241 case 'k':
242 result = this->set_arg(this->collocated_client_kind_,
243 get_opts.opt_arg(),
245 "collocated_client_kind");
246 break;
248 case '?':
249 this->usage_statement();
250 return 1;
252 default:
253 this->usage_statement();
254 return -1;
257 if (result != 0)
259 return result;
263 return this->arg_dependency_checks();
266 void
267 ServerApp::usage_statement()
269 ACE_ERROR((LM_ERROR,
270 "Usage: %s [options]\n\n"
271 "OPTIONS:\n\n"
272 "\t[-p <ior_filename_prefix>]\n"
273 "\t[-s <num_servants>]\n"
274 "\t[-n <num_csd_threads>]\n"
275 "\t[-t <num_orb_threads>]\n"
276 "\t[-r <num_remote_clients>]\n"
277 "\t[-c <num_collocated_clients>]\n"
278 "\t[-k <collocated_client_kind>]\n"
279 "\t[-?]\n\n",
280 this->exe_name_.c_str()));
285 ServerApp::arg_dependency_checks()
287 return (this->num_remote_clients_
288 + this->num_collocated_clients_) > 0 ? 0 : -1;
293 ServerApp::set_arg(unsigned& value,
294 const ACE_TCHAR* arg,
295 char opt,
296 const char* name,
297 int min)
299 int tmp = ACE_OS::atoi(arg);
301 if (tmp < min)
303 ACE_ERROR((LM_ERROR,
304 "Error: -%c <%s> must be integer type with a value of, "
305 "at least, %d.\n", opt, name, min));
306 this->usage_statement();
307 return -1;
310 value = tmp;
312 return 0;
316 PortableServer::POA_ptr
317 ServerApp::create_poa(CORBA::ORB_ptr orb,
318 const char* poa_name)
320 // Get the Root POA.
321 PortableServer::POA_var root_poa
322 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
323 "RootPOA");
325 // Get the POAManager from the Root POA.
326 PortableServer::POAManager_var poa_manager
327 = root_poa->the_POAManager();
329 // Create the child POA Policies.
330 CORBA::PolicyList policies(0);
331 policies.length(0);
333 // Create the child POA
334 PortableServer::POA_var poa
335 = AppHelper::create_poa(poa_name,
336 root_poa.in(),
337 poa_manager.in(),
338 policies);
340 // Give away the child POA_ptr from the POA_var variable.
341 return poa._retn();
345 bool
346 ServerApp::check_validity ()
348 Foo_C_Statistics stats (this->num_remote_clients_,
349 this->num_collocated_clients_);
351 Foo_C_ClientEngine::expected_results (stats);
352 Foo_C_Custom_ClientEngine::expected_results (stats);
354 for (unsigned i = 0; i < this->num_servants_; i++)
356 this->servants_.servant(i)->gather_stats(stats);
359 return stats.actual_vs_expected ();