Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / Broken / ServerApp.cpp
blob97724f5a5e9ff834257ab68f0e09b844eb563bfe
1 #include "ServerApp.h"
2 #include "AppHelper.h"
3 #include "OrbRunner.h"
4 #include "AppShutdown.h"
5 #include "TestAppExceptionC.h"
6 #include "Foo_B_SimpleClientEngine.h"
7 #include "Callback_i.h"
8 #include "ace/Get_Opt.h"
9 // To force static load the service.
10 #include "tao/PI/PI.h"
11 #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();
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");
84 if (this->num_collocated_clients_ > 0)
86 this->cb_poa_ = this->create_poa(this->orb_.in(),
87 "CallbackPoa");
91 void
92 ServerApp::csd_setup()
94 this->tp_strategy_ = new TAO::CSD::TP_Strategy(this->num_csd_threads_);
96 // We don't apply the strategy for this test to show that the bug isn't
97 // part of the CSD ThreadPool Strategy code.
98 #if 0
99 if (!this->tp_strategy_->apply_to(this->poa_.in()))
101 ACE_ERROR((LM_ERROR,
102 "Failed to apply CSD strategy to poa.\n"));
103 throw TestAppException();
105 #endif
107 // Use another poa and strategy for callbacks. This would resolve
108 // the deadlock situation that happens when having number of csd
109 // threads less than number of collocated clients.
110 if (this->num_collocated_clients_ > 0)
112 this->cb_tp_strategy_ = new TAO::CSD::TP_Strategy();
113 // We don't apply the strategy for this test to show that the bug isn't
114 // part of the CSD ThreadPool Strategy code.
115 #if 0
116 if (!this->cb_tp_strategy_->apply_to(this->cb_poa_.in()))
118 ACE_ERROR((LM_ERROR,
119 "Failed to apply CSD strategy to callback poa.\n"));
120 throw TestAppException();
122 #endif
127 void
128 ServerApp::servant_setup()
130 this->foo_servants_.create_and_activate(this->num_servants_,
131 this->orb_.in (),
132 this->poa_.in (),
133 this->ior_filename_prefix_.c_str());
137 void
138 ServerApp::collocated_setup()
140 if (this->num_collocated_clients_ == 0)
141 return;
143 this->cb_servants_.create_and_activate(1, // number of callback servants
144 this->cb_poa_.in());
146 CallbackServantListType::T_stub_var cb = this->cb_servants_.objref(0);
148 unsigned client_id = this->num_remote_clients_;
150 for (unsigned i = 0; i < this->num_collocated_clients_; i++)
152 client_id ++;
153 // Dole out the servant object references in a round-robin fashion.
154 unsigned servant_index = i % this->num_servants_;
156 FooServantListType::T_stub_var foo
157 = this->foo_servants_.objref(servant_index);
158 ClientEngine_Handle engine
159 = new Foo_B_SimpleClientEngine(foo.in(), cb.in (), client_id);
160 this->collocated_client_task_.add_engine(engine.in());
165 void
166 ServerApp::poa_activate()
168 PortableServer::POAManager_var poa_manager
169 = this->poa_->the_POAManager();
170 poa_manager->activate();
174 void
175 ServerApp::run_collocated_clients()
177 if (this->num_collocated_clients_ > 0)
179 if (this->collocated_client_task_.open() == -1)
181 throw TestAppException ();
187 void
188 ServerApp::run_orb_event_loop()
190 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
191 orb_runner.run();
192 TheAppShutdown->wait ();
196 void
197 ServerApp::cleanup()
203 ServerApp::parse_args(int argc, ACE_TCHAR* argv[])
205 this->exe_name_ = argv[0];
207 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("p:s:n:t:r:c:k:"));
209 int c;
211 while ((c = get_opts()) != -1)
213 int result = 0;
214 switch (c)
216 case 'p':
217 this->ior_filename_prefix_ = get_opts.opt_arg();
218 break;
220 case 's':
221 result = set_arg(this->num_servants_,
222 get_opts.opt_arg(),
224 "num_servants",
226 break;
228 case 'n':
229 result = set_arg(this->num_csd_threads_,
230 get_opts.opt_arg(),
232 "num_servants",
234 break;
236 case 't':
237 result = set_arg(this->num_orb_threads_,
238 get_opts.opt_arg(),
240 "num_orb_threads",
242 break;
244 case 'r':
245 result = set_arg(this->num_remote_clients_,
246 get_opts.opt_arg(),
248 "num_remote_clients");
249 break;
251 case 'c':
252 result = set_arg(this->num_collocated_clients_,
253 get_opts.opt_arg(),
255 "num_collocated_clients");
256 break;
258 case 'k':
259 result = set_arg(this->collocated_client_kind_,
260 get_opts.opt_arg(),
262 "collocated_client_kind");
263 break;
265 case '?':
266 this->usage_statement();
267 return 1;
269 default:
270 this->usage_statement();
271 return -1;
274 if (result != 0)
276 return result;
280 return this->arg_dependency_checks();
283 void
284 ServerApp::usage_statement()
286 ACE_ERROR((LM_ERROR,
287 "Usage: %s [options]\n\n"
288 "OPTIONS:\n\n"
289 "\t[-p <ior_filename_prefix>]\n"
290 "\t[-s <num_servants>]\n"
291 "\t[-n <num_csd_threads>]\n"
292 "\t[-t <num_orb_threads>]\n"
293 "\t[-r <num_remote_clients>]\n"
294 "\t[-c <num_collocated_clients>]\n"
295 "\t[-k <collocated_client_kind>]\n"
296 "\t[-?]\n\n",
297 this->exe_name_.c_str()));
302 ServerApp::arg_dependency_checks()
304 return (this->num_remote_clients_
305 + this->num_collocated_clients_) > 0 ? 0 : -1;
310 ServerApp::set_arg(unsigned& value,
311 const ACE_TCHAR* arg,
312 char opt,
313 const char* name,
314 int min)
316 int tmp = ACE_OS::atoi(arg);
318 if (tmp < min)
320 ACE_ERROR((LM_ERROR,
321 "Error: -%c <%s> must be integer type with a value of, "
322 "at least, %d.\n", opt, name, min));
323 this->usage_statement();
324 return -1;
327 value = tmp;
328 return 0;
332 PortableServer::POA_ptr
333 ServerApp::create_poa(CORBA::ORB_ptr orb,
334 const char* poa_name)
336 // Get the Root POA.
337 PortableServer::POA_var root_poa
338 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
339 "RootPOA");
341 // Get the POAManager from the Root POA.
342 PortableServer::POAManager_var poa_manager
343 = root_poa->the_POAManager();
345 // Create the child POA Policies.
346 CORBA::PolicyList policies(0);
347 policies.length(0);
349 // Create the child POA
350 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
351 root_poa.in(),
352 poa_manager.in(),
353 policies);
355 // Give away the child POA_ptr from the POA_var variable.
356 return poa._retn();
360 bool
361 ServerApp::check_validity ()
363 return true;
364 #if 0
365 // Check whether the clients return any errors.
366 if (this->num_collocated_clients_ > 0
367 && this->collocated_client_task_.failure_count () > 0)
369 return false;
372 Foo_B_Statistics stats (this->num_remote_clients_,
373 this->num_collocated_clients_);
375 Foo_B_SimpleClientEngine::expected_results (stats);
377 for (unsigned i = 0; i < this->num_servants_; i++)
379 this->foo_servants_.servant(i)->gather_stats (stats);
382 if (this->num_collocated_clients_ > 0)
384 this->cb_servants_.servant (0)->gather_stats (stats);
387 return stats.actual_vs_expected ();
388 #endif