Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / TP_Test_3 / ClientApp.cpp
blob96b4f181f3eff061cdca4f8735ea7f0b76f51fa9
1 #include "ClientApp.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 "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 ClientApp::ClientApp()
13 : TestAppBase("TP_Test_3_Client"),
14 client_task_ (true), // shutdown orb after client is done.
15 num_servants_ (1),
16 num_csd_threads_ (1),
17 num_orb_threads_ (1),
18 ior_(ACE_TEXT("Not Set")),
19 client_kind_(0),
20 client_id_(0)
25 ClientApp::~ClientApp()
30 int
31 ClientApp::run_i(int argc, ACE_TCHAR* argv[])
33 int result = this->init(argc, argv);
34 if (result != 0)
36 return result;
39 this->poa_setup();
40 this->csd_setup();
41 this->client_setup();
42 this->poa_activate();
43 this->run_clients();
44 this->run_orb_event_loop();
46 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
47 // that the main thread might exit before all CSD Threads exit.
49 // Wait for all CSD task threads exit.
50 ACE_Thread_Manager::instance ()->wait ();
52 result = this->check_validity () ? 0 : -1;
53 this->cleanup();
55 return result;
59 int
60 ClientApp::init(int argc, ACE_TCHAR* argv[])
62 this->orb_ = CORBA::ORB_init(argc, argv);
64 // Parse the command-line args for this application.
65 // * Raises -1 if problems are encountered.
66 // * Returns 1 if the usage statement was explicitly requested.
67 // * Returns 0 otherwise.
68 int result = this->parse_args(argc, argv);
69 if (result != 0)
71 return result;
74 TheAppShutdown->init(this->orb_.in(), num_servants_);
76 return 0;
80 void
81 ClientApp::poa_setup(void)
83 this->poa_ = this->create_poa(this->orb_.in(),
84 "ChildPoa");
88 void
89 ClientApp::csd_setup(void)
91 this->tp_strategy_ = new TAO::CSD::TP_Strategy(this->num_csd_threads_);
93 if (!this->tp_strategy_->apply_to(this->poa_.in()))
95 ACE_ERROR((LM_ERROR,
96 "Failed to apply CSD strategy to poa.\n"));
97 throw TestAppException();
102 void
103 ClientApp::client_setup(void)
105 // Turn the ior_ into a Foo_B obj ref.
106 Foo_B_var foo = RefHelper<Foo_B>::string_to_ref(this->orb_.in(),
107 this->ior_.c_str());
109 this->servants_.create_and_activate(1, // number of callback servants
110 this->poa_.in());
111 ServantListType::T_stub_var cb = this->servants_.objref(0);
113 // Create the ClientEngine object, and give it the Foo_B and Callback object
114 // references.
115 ClientEngine_Handle engine
116 = new Foo_B_ClientEngine(foo.in(), cb.in (), this->client_id_);
117 this->client_task_.add_engine(engine.in());
121 void
122 ClientApp::poa_activate(void)
124 PortableServer::POAManager_var poa_manager
125 = this->poa_->the_POAManager();
126 poa_manager->activate();
130 void
131 ClientApp::run_clients()
133 this->client_task_.open();
137 void
138 ClientApp::run_orb_event_loop(void)
140 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
141 orb_runner.run();
142 TheAppShutdown->wait ();
146 PortableServer::POA_ptr
147 ClientApp::create_poa(CORBA::ORB_ptr orb,
148 const char* poa_name)
150 // Get the Root POA.
151 PortableServer::POA_var root_poa
152 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
153 "RootPOA");
155 // Get the POAManager from the Root POA.
156 PortableServer::POAManager_var poa_manager
157 = root_poa->the_POAManager();
159 // Create the child POA Policies.
160 CORBA::PolicyList policies(0);
161 policies.length(0);
163 // Create the child POA
164 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
165 root_poa.in(),
166 poa_manager.in(),
167 policies);
169 // Give away the child POA_ptr from the POA_var variable.
170 return poa._retn();
174 void
175 ClientApp::cleanup()
180 ClientApp::parse_args(int argc, ACE_TCHAR* argv[])
182 this->exe_name_ = argv[0];
184 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("i:k:n:"));
186 int c;
188 while ((c = get_opts()) != -1)
190 int result = 0;
191 switch (c)
193 case 'i':
194 this->ior_ = get_opts.opt_arg();
195 break;
197 case 'k':
198 result = set_arg(this->client_kind_,
199 get_opts.opt_arg(),
201 "client_kind");
202 break;
204 case 'n':
205 result = set_arg(this->client_id_,
206 get_opts.opt_arg(),
208 "client_id");
209 break;
211 case '?':
212 this->usage_statement();
213 return 1;
215 default:
216 this->usage_statement();
217 return -1;
220 if (result != 0)
222 return result;
226 return this->arg_dependency_checks();
229 void
230 ClientApp::usage_statement()
232 ACE_ERROR((LM_ERROR,
233 "Usage: %s [options]\n\n"
234 "OPTIONS:\n\n"
235 "\t[-i <ior>]\n"
236 "\t[-k <client_kind>]\n"
237 "\t[-n <client_id>]\n"
238 "\t[-?]\n\n",
239 this->exe_name_.c_str()));
244 ClientApp::arg_dependency_checks()
246 if (this->ior_ == ACE_TEXT("Not Set"))
248 ACE_ERROR((LM_ERROR,
249 "Error: Missing required command-line option (-i <ior>).\n"));
250 this->usage_statement();
251 return -1;
253 if (this->client_id_ <= 0)
255 ACE_ERROR((LM_ERROR,
256 "Error: Invalid command-line option (-n <client id>).\n"
257 " The client id should be positive integer.\n"));
258 this->usage_statement();
259 return -1;
262 return 0;
267 ClientApp::set_arg(unsigned& value,
268 const ACE_TCHAR* arg,
269 char opt,
270 const char* name,
271 int min)
273 int tmp = ACE_OS::atoi(arg);
275 if (tmp < min)
277 ACE_ERROR((LM_ERROR,
278 "Error: -%c <%s> must be integer type with a value of, "
279 "at least, %d.\n", opt, name, min));
280 this->usage_statement();
281 return -1;
284 value = tmp;
285 return 0;
289 bool
290 ClientApp::check_validity ()
292 // Check whether the clients return any errors.
293 if (this->client_task_.failure_count () > 0)
295 return false;
298 return true;