Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / TP_Test_3 / ServerApp.cpp
blob9d43cef70aab103cf7205a516a72bb370f5dd515
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)
27 ServerApp::~ServerApp()
32 int
33 ServerApp::run_i(int argc, ACE_TCHAR* argv[])
35 int result = this->init(argc, argv);
36 if (result != 0)
38 return result;
41 this->poa_setup();
42 this->csd_setup();
43 this->servant_setup();
44 this->collocated_setup();
45 this->poa_activate();
46 this->run_collocated_clients();
48 ACE_DEBUG((LM_DEBUG,
49 "(%P|%t) ServerApp is ready. Running the ORB event loop.\n"));
51 this->run_orb_event_loop();
53 ACE_DEBUG((LM_DEBUG,
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 ();
62 this->cleanup();
63 result = this->check_validity () ? 0 : -1;
65 ACE_DEBUG((LM_DEBUG,
66 "(%P|%t) ServerApp check_validity returned %d .\n", result));
68 return result;
72 int
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);
82 if (result != 0)
84 return result;
87 unsigned num_clients = this->num_remote_clients_ +
88 this->num_collocated_clients_;
90 TheAppShutdown->init(this->orb_.in(), num_clients);
92 return 0;
96 void
97 ServerApp::poa_setup(void)
99 this->poa_ = this->create_poa(this->orb_.in(),
100 "ChildPoa");
102 if (this->num_collocated_clients_ > 0)
104 this->cb_poa_ = this->create_poa(this->orb_.in(),
105 "CallbackPoa");
109 void
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()))
116 ACE_ERROR((LM_ERROR,
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()))
129 ACE_ERROR((LM_ERROR,
130 "Failed to apply CSD strategy to callback poa.\n"));
131 throw TestAppException();
137 void
138 ServerApp::servant_setup(void)
140 this->foo_servants_.create_and_activate(this->num_servants_,
141 this->orb_.in (),
142 this->poa_.in (),
143 this->ior_filename_prefix_.c_str());
147 void
148 ServerApp::collocated_setup(void)
150 if (this->num_collocated_clients_ == 0)
151 return;
153 this->cb_servants_.create_and_activate(1, // number of callback servants
154 this->cb_poa_.in());
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++)
162 client_id ++;
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());
175 void
176 ServerApp::poa_activate(void)
178 PortableServer::POAManager_var poa_manager
179 = this->poa_->the_POAManager();
180 poa_manager->activate();
184 void
185 ServerApp::run_collocated_clients(void)
187 if (this->num_collocated_clients_ > 0)
189 if (this->collocated_client_task_.open() == -1)
191 throw TestAppException ();
197 void
198 ServerApp::run_orb_event_loop(void)
200 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
201 orb_runner.run();
202 TheAppShutdown->wait ();
206 void
207 ServerApp::cleanup()
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:"));
219 int c;
221 while ((c = get_opts()) != -1)
223 int result = 0;
224 switch (c)
226 case 'p':
227 this->ior_filename_prefix_ = get_opts.opt_arg();
228 break;
230 case 's':
231 result = set_arg(this->num_servants_,
232 get_opts.opt_arg(),
234 "num_servants",
236 break;
238 case 'n':
239 result = set_arg(this->num_csd_threads_,
240 get_opts.opt_arg(),
242 "num_servants",
244 break;
246 case 't':
247 result = set_arg(this->num_orb_threads_,
248 get_opts.opt_arg(),
250 "num_orb_threads",
252 break;
254 case 'r':
255 result = set_arg(this->num_remote_clients_,
256 get_opts.opt_arg(),
258 "num_remote_clients");
259 break;
261 case 'c':
262 result = set_arg(this->num_collocated_clients_,
263 get_opts.opt_arg(),
265 "num_collocated_clients");
266 break;
268 case 'k':
269 result = set_arg(this->collocated_client_kind_,
270 get_opts.opt_arg(),
272 "collocated_client_kind");
273 break;
275 case '?':
276 this->usage_statement();
277 return 1;
279 default:
280 this->usage_statement();
281 return -1;
284 if (result != 0)
286 return result;
290 return this->arg_dependency_checks();
293 void
294 ServerApp::usage_statement()
296 ACE_ERROR((LM_ERROR,
297 "Usage: %s [options]\n\n"
298 "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"
306 "\t[-?]\n\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,
322 char opt,
323 const char* name,
324 int min)
326 int tmp = ACE_OS::atoi(arg);
328 if (tmp < min)
330 ACE_ERROR((LM_ERROR,
331 "Error: -%c <%s> must be integer type with a value of, "
332 "at least, %d.\n", opt, name, min));
333 this->usage_statement();
334 return -1;
337 value = tmp;
338 return 0;
342 PortableServer::POA_ptr
343 ServerApp::create_poa(CORBA::ORB_ptr orb,
344 const char* poa_name)
346 // Get the Root POA.
347 PortableServer::POA_var root_poa
348 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
349 "RootPOA");
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);
357 policies.length(0);
359 // Create the child POA
360 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
361 root_poa.in(),
362 poa_manager.in(),
363 policies);
365 // Give away the child POA_ptr from the POA_var variable.
366 return poa._retn();
370 bool
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)
377 return false;
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 ();