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_2 / ServerApp.cpp
blob99a4bb9d5d87d094edce5725acf4fcf4ca821231
1 #include "ServerApp.h"
2 #include "AppHelper.h"
3 #include "OrbRunner.h"
4 #include "AppShutdown.h"
5 #include "TestAppExceptionC.h"
6 #include "Foo_A_ClientEngine.h"
7 #include "ace/Get_Opt.h"
8 // To force static load the service.
9 #include "tao/PI/PI.h"
10 #include "tao/CSD_ThreadPool/CSD_ThreadPool.h"
12 ServerApp::ServerApp()
13 : TestAppBase("TP_Test_2_Server"),
14 ior_filename_prefix_(ACE_TEXT("foo")),
15 num_servants_(1),
16 num_csd_threads_(1),
17 num_orb_threads_(1),
18 num_remote_clients_(1),
19 num_collocated_clients_(0),
20 collocated_client_kind_(0)
25 int
26 ServerApp::run_i(int argc, ACE_TCHAR* argv[])
28 int result = this->init(argc, argv);
29 if (result != 0)
31 return result;
34 this->poa_setup();
35 this->csd_setup();
36 this->servant_setup();
37 this->collocated_setup();
38 this->poa_activate();
39 this->run_collocated_clients();
40 this->run_orb_event_loop();
42 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
43 // that the main thread might exit before all CSD Threads exit.
45 // Wait for all CSD task threads exit.
46 ACE_Thread_Manager::instance ()->wait ();
48 this->cleanup();
49 return this->check_validity () ? 0 : -1;
53 int
54 ServerApp::init(int argc, ACE_TCHAR* argv[])
56 this->orb_ = CORBA::ORB_init(argc, argv);
58 // Parse the command-line args for this application.
59 // * Raises -1 if problems are encountered.
60 // * Returns 1 if the usage statement was explicitly requested.
61 // * Returns 0 otherwise.
62 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 client_id_start = this->num_remote_clients_;
114 for (unsigned i = 0; i < this->num_collocated_clients_; i++)
116 // Dole out the servant object references in a round-robin fashion.
117 unsigned servant_index = i % this->num_servants_;
119 ServantListType::T_stub_var obj = this->servants_.objref(servant_index);
120 ClientEngine_Handle engine = new Foo_A_ClientEngine(obj.in(), ++client_id_start);
121 this->collocated_client_task_.add_engine(engine.in());
126 void
127 ServerApp::poa_activate()
129 PortableServer::POAManager_var poa_manager
130 = this->poa_->the_POAManager();
132 poa_manager->activate();
136 void
137 ServerApp::run_collocated_clients()
139 if (this->num_collocated_clients_ > 0)
141 if (this->collocated_client_task_.open() == -1)
143 throw TestAppException ();
149 void
150 ServerApp::run_orb_event_loop()
152 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
153 orb_runner.run();
154 TheAppShutdown->wait ();
158 void
159 ServerApp::cleanup()
165 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
167 this->exe_name_ = argv[0];
169 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("p:s:n:t:r:c:k:"));
171 int c;
173 while ((c = get_opts()) != -1)
175 int result = 0;
176 switch (c)
178 case 'p':
179 this->ior_filename_prefix_ = get_opts.opt_arg();
180 break;
182 case 's':
183 result = this->set_arg(this->num_servants_,
184 get_opts.opt_arg(),
186 "num_servants",
188 break;
190 case 'n':
191 result = this->set_arg(this->num_csd_threads_,
192 get_opts.opt_arg(),
194 "num_servants",
196 break;
198 case 't':
199 result = this->set_arg(this->num_orb_threads_,
200 get_opts.opt_arg(),
202 "num_orb_threads",
204 break;
206 case 'r':
207 result = this->set_arg(this->num_remote_clients_,
208 get_opts.opt_arg(),
210 "num_remote_clients");
211 break;
213 case 'c':
214 result = this->set_arg(this->num_collocated_clients_,
215 get_opts.opt_arg(),
217 "num_collocated_clients");
218 break;
220 case 'k':
221 result = this->set_arg(this->collocated_client_kind_,
222 get_opts.opt_arg(),
224 "collocated_client_kind");
225 break;
227 case '?':
228 this->usage_statement();
229 return 1;
231 default:
232 this->usage_statement();
233 return -1;
236 if (result != 0)
238 return result;
242 return this->arg_dependency_checks();
245 void
246 ServerApp::usage_statement()
248 ACE_ERROR((LM_ERROR,
249 "Usage: %s [options]\n\n"
250 "OPTIONS:\n\n"
251 "\t[-p <ior_filename_prefix>]\n"
252 "\t[-s <num_servants>]\n"
253 "\t[-n <num_csd_threads>]\n"
254 "\t[-t <num_orb_threads>]\n"
255 "\t[-r <num_remote_clients>]\n"
256 "\t[-c <num_collocated_clients>]\n"
257 "\t[-k <collocated_client_kind>]\n"
258 "\t[-?]\n\n",
259 this->exe_name_.c_str()));
264 ServerApp::arg_dependency_checks()
266 return (this->num_remote_clients_
267 + this->num_collocated_clients_) > 0 ? 0 : -1;
272 ServerApp::set_arg(unsigned& value,
273 const ACE_TCHAR* arg,
274 char opt,
275 const char* name,
276 int min)
278 int tmp = ACE_OS::atoi(arg);
280 if (tmp < min)
282 ACE_ERROR((LM_ERROR,
283 "Error: -%c <%s> must be integer type with a value of, "
284 "at least, %d.\n", opt, name, min));
285 this->usage_statement();
286 return -1;
289 value = tmp;
290 return 0;
294 PortableServer::POA_ptr
295 ServerApp::create_poa(CORBA::ORB_ptr orb,
296 const char* poa_name)
298 // Get the Root POA.
299 PortableServer::POA_var root_poa
300 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
301 "RootPOA");
303 // Get the POAManager from the Root POA.
304 PortableServer::POAManager_var poa_manager
305 = root_poa->the_POAManager();
307 // Create the child POA Policies.
308 CORBA::PolicyList policies(0);
309 policies.length(0);
311 // Create the child POA
312 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
313 root_poa.in(),
314 poa_manager.in(),
315 policies);
317 // Give away the child POA_ptr from the POA_var variable.
318 return poa._retn();
322 bool
323 ServerApp::check_validity ()
325 // Check whether the clients return any errors.
326 if (this->num_collocated_clients_ > 0
327 && this->collocated_client_task_.failure_count () > 0)
329 return false;
332 unsigned num_clients = this->num_remote_clients_ +
333 this->num_collocated_clients_;
335 Foo_A_Statistics stats (num_clients);
337 Foo_A_ClientEngine::expected_results (stats);
339 for (unsigned i = 0; i < this->num_servants_; i++)
341 this->servants_.servant(i)->gather_stats(stats);
344 return stats.actual_vs_expected ();