Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / CSD_Strategy_Tests / Broken / ClientApp.cpp
blob9890f024599ac56c5cb2dfbee7f18b60f6d99e70
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"
13 ClientApp::ClientApp()
14 : TestAppBase("TP_Test_3_Client"),
15 client_task_ (true), // shutdown orb after client is done.
16 num_servants_ (1),
17 num_csd_threads_ (1),
18 num_orb_threads_ (1),
19 ior_(ACE_TEXT("Not Set")),
20 client_kind_(0),
21 client_id_(0)
26 ClientApp::~ClientApp()
31 int
32 ClientApp::run_i(int argc, ACE_TCHAR* argv[])
34 int result = this->init(argc, argv);
35 if (result != 0)
37 return result;
40 this->poa_setup();
41 this->csd_setup();
42 this->client_setup();
43 this->poa_activate();
44 this->run_clients();
45 this->run_orb_event_loop();
47 // Calling wait on ACE_Thread_Manager singleton to avoid the problem
48 // that the main thread might exit before all CSD Threads exit.
50 // Wait for all CSD task threads exit.
51 ACE_Thread_Manager::instance ()->wait ();
53 this->cleanup();
55 return this->check_validity () ? 0 : -1;
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()
83 this->poa_ = this->create_poa(this->orb_.in(),
84 "ChildPoa");
88 void
89 ClientApp::csd_setup()
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()
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()
124 PortableServer::POAManager_var poa_manager
125 = this->poa_->the_POAManager();
126 poa_manager->activate();
130 void
131 ClientApp::run_clients()
133 if (this->client_task_.open() != 0)
135 throw TestAppException ();
140 void
141 ClientApp::run_orb_event_loop()
143 OrbRunner orb_runner(this->orb_.in(), this->num_orb_threads_);
144 orb_runner.run();
145 TheAppShutdown->wait ();
149 PortableServer::POA_ptr
150 ClientApp::create_poa(CORBA::ORB_ptr orb, const char* poa_name)
152 // Get the Root POA.
153 PortableServer::POA_var root_poa
154 = RefHelper<PortableServer::POA>::resolve_initial_ref(orb,
155 "RootPOA");
157 // Get the POAManager from the Root POA.
158 PortableServer::POAManager_var poa_manager
159 = root_poa->the_POAManager();
161 // Create the child POA Policies.
162 CORBA::PolicyList policies(0);
163 policies.length(0);
165 // Create the child POA
166 PortableServer::POA_var poa = AppHelper::create_poa(poa_name,
167 root_poa.in(),
168 poa_manager.in(),
169 policies);
171 // Give away the child POA_ptr from the POA_var variable.
172 return poa._retn();
176 void
177 ClientApp::cleanup()
183 ClientApp::parse_args(int argc, ACE_TCHAR* argv[])
185 this->exe_name_ = argv[0];
187 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("i:k:n:"));
189 int c;
191 while ((c = get_opts()) != -1)
193 int result = 0;
194 switch (c)
196 case 'i':
197 this->ior_ = get_opts.opt_arg();
198 break;
200 case 'k':
201 result = set_arg(this->client_kind_,
202 get_opts.opt_arg(),
204 "client_kind");
205 break;
207 case 'n':
208 result = set_arg(this->client_id_,
209 get_opts.opt_arg(),
211 "client_id");
212 break;
214 case '?':
215 this->usage_statement();
216 return 1;
218 default:
219 this->usage_statement();
220 return -1;
223 if (result != 0)
225 return result;
229 return this->arg_dependency_checks();
232 void
233 ClientApp::usage_statement()
235 ACE_ERROR((LM_ERROR,
236 "Usage: %s [options]\n\n"
237 "OPTIONS:\n\n"
238 "\t[-i <ior>]\n"
239 "\t[-k <client_kind>]\n"
240 "\t[-n <client_id>]\n"
241 "\t[-?]\n\n",
242 this->exe_name_.c_str()));
247 ClientApp::arg_dependency_checks()
249 if (this->ior_ == ACE_TEXT("Not Set"))
251 ACE_ERROR((LM_ERROR,
252 "Error: Missing required command-line option (-i <ior>).\n"));
253 this->usage_statement();
254 return -1;
256 if (this->client_id_ <= 0)
258 ACE_ERROR((LM_ERROR,
259 "Error: Invalid command-line option (-n <client id>).\n"
260 " The client id should be positive integer.\n"));
261 this->usage_statement();
262 return -1;
265 return 0;
270 ClientApp::set_arg(unsigned& value,
271 const ACE_TCHAR* arg,
272 char opt,
273 const char* name,
274 int min)
276 int tmp = ACE_OS::atoi(arg);
278 if (tmp < min)
280 ACE_ERROR((LM_ERROR,
281 "Error: -%c <%s> must be integer type with a value of, "
282 "at least, %d.\n", opt, name, min));
283 this->usage_statement();
284 return -1;
287 value = tmp;
288 return 0;
292 bool
293 ClientApp::check_validity ()
295 // Check whether the clients return any errors.
296 if (this->client_task_.failure_count () > 0)
298 return false;
301 return true;