=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / examples / Quoter / client.cpp
blobe5eb55e14077fb621b0a7682711111a781da4a89
1 #include "ace/Read_Buffer.h"
3 #include "orbsvcs/CosNamingC.h"
5 #include "client.h"
7 Quoter_Task::Quoter_Task (int argc, ACE_TCHAR **argv)
8 : argc_ (argc), argv_ (argv)
10 // Nothing
13 int
14 Quoter_Task::svc ()
16 if (this->quoter_client.init (this->argc_, this->argv_) == -1)
17 return 1;
18 else
19 return this->quoter_client.run ();
22 // Constructor.
23 Quoter_Client::Quoter_Client ()
24 : quoter_factory_key_ (0),
25 quoter_key_ (ACE_OS::strdup ("key0")),
26 shutdown_ (0),
27 quoter_var_ (Stock::Quoter::_nil ()),
28 useLifeCycleService_(0), // use the Generic Factory
29 debug_level_ (1)
31 // Nothing
34 // Parses the command line arguments and returns an error status.
36 int
37 Quoter_Client::parse_args ()
39 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("n:d:lx"));
40 int opt;
41 int exit_code = 0;
43 while ((opt = get_opts ()) != -1)
44 switch (opt)
46 case 'n': // multiple threads
47 // ignore it, it was handled already
48 break;
49 case 'd': // debug flag.
50 this->debug_level_ = ACE_OS::atoi (get_opts.opt_arg ());
51 break;
52 case 'l':
53 this->useLifeCycleService_ = 1;
54 break;
55 case 'x':
56 this->shutdown_ = 1;
57 break;
58 default:
59 exit_code = 1;
60 ACE_ERROR ((LM_ERROR,
61 "%s: unknown arg, -%c\n",
62 this->argv_[0], char(opt)));
63 ACE_FALLTHROUGH;
64 case '?':
65 ACE_DEBUG ((LM_DEBUG,
66 "usage: %s"
67 " [-m]"
68 " [-d] <debug level> - Set the debug level\n"
69 " [-l] - use the lifecycle service instead of the generic factory"
70 " [-x]"
71 "\n",
72 this->argv_ [0]));
73 ACE_OS::exit (exit_code);
76 // Indicates successful parsing of command line.
77 return 0;
80 int
81 Quoter_Client::run ()
83 if (this->debug_level_ >= 1)
84 ACE_DEBUG ((LM_DEBUG,
85 "\nQuoter Example: Quoter_Client is running\n"));
87 const char *exception_message = "Null Message";
88 try
90 exception_message = "While using get_quote ()";
91 CORBA::Long q = this->quoter_var_->get_quote ("ACE Hardware");
93 if (this->debug_level_ >= 1)
94 ACE_DEBUG ((LM_DEBUG, "Quoter Client: ACE Hardware = %i\n", q));
96 // Copy the Quoter
98 CosLifeCycle::Criteria criteria;
99 exception_message = "While copying the quoter";
100 CORBA::Object_var quoterObj_var =
101 this->quoter_var_->copy (factory_Finder_var_.in (),
102 criteria);
104 if (CORBA::is_nil (quoterObj_var.in()))
105 ACE_ERROR_RETURN ((LM_ERROR,
106 "Quoter_Client::run: Copied Object pointer is nil!"),
107 -1);
109 // Narrow it to the actual Quoter interface
110 exception_message = "While narrowing the quoter";
111 Stock::Quoter_var copied_quoter_var =
112 Stock::Quoter::_narrow (quoterObj_var.in ());
114 if (CORBA::is_nil (copied_quoter_var.in()))
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "Quoter_Client::run: Copied Quoter is nil!"),
117 -1);
119 if (this->debug_level_ >= 2)
120 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Copied object.\n"));
122 exception_message = "While using get_quote () on copied object";
123 q = copied_quoter_var->get_quote ("ACE Hardware");
125 if (this->debug_level_ >= 1)
126 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Copied object: ACE Hardware = %i\n", q));
128 catch (const CORBA::Exception& ex)
130 ACE_ERROR ((LM_ERROR, "Quoter_Client::run - %C\n", exception_message));
131 ex._tao_print_exception ("Quoter_Client::run");
132 return -1;
135 return 0;
138 Quoter_Client::~Quoter_Client ()
140 // Free resources
141 // Close the ior files
142 if (this->quoter_factory_key_ != 0)
143 ACE_OS::free (this->quoter_factory_key_);
144 if (this->quoter_key_ != 0)
145 ACE_OS::free (this->quoter_key_);
149 Quoter_Client::init_naming_service ()
151 const char *exception_message = "Null Message";
155 // Resolve the Naming Service
156 CORBA::Object_var naming_obj =
157 orb_->resolve_initial_references ("NameService");
159 if (CORBA::is_nil (naming_obj.in ()))
160 ACE_ERROR_RETURN ((LM_ERROR,
161 "Unable to resolve the Name Service.\n"),
162 -1);
164 exception_message = "While narrowing the naming context";
165 CosNaming::NamingContext_var naming_context =
166 CosNaming::NamingContext::_narrow (naming_obj.in ());
168 if (this->debug_level_ >= 2)
169 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Naming Service.\n"));
171 CosNaming::Name quoterFactoryFinderName (2);
172 quoterFactoryFinderName.length (2);
173 quoterFactoryFinderName[0].id = CORBA::string_dup ("IDL_Quoter");
174 quoterFactoryFinderName[1].id = CORBA::string_dup ("Quoter_Factory_Finder");
176 if (this->debug_level_ >= 2)
177 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Trying to resolve the Quoter Factory Finder!\n"));
179 exception_message = "While resolving the factory finder";
180 CORBA::Object_var factory_obj =
181 naming_context->resolve (quoterFactoryFinderName);
183 if (this->debug_level_ >= 2)
184 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Resolved the Quoter Factory Finder!\n"));
186 exception_message = "While narrowing the factory finder";
187 factory_Finder_var_ =
188 Stock::Quoter_Factory_Finder::_narrow (factory_obj.in ());
190 if (CORBA::is_nil (factory_Finder_var_.in ()))
191 ACE_ERROR_RETURN ((LM_ERROR,
192 " could not resolve quoter factory in Naming service\n"),
193 -1);
195 if (this->debug_level_ >= 2)
196 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Quoter Factory Finder.\n"));
198 // The name of the Quoter Generic Factory
199 CosLifeCycle::Key factoryName (2); // max = 2
201 if (this->useLifeCycleService_ == 1)
203 // use the LifeCycle Service
204 factoryName.length(1);
205 factoryName[0].id = CORBA::string_dup ("Life_Cycle_Service");
207 else
209 // use a Generic Factory
210 factoryName.length(2);
211 factoryName[0].id = CORBA::string_dup ("IDL_Quoter");
212 factoryName[1].id = CORBA::string_dup ("Quoter_Generic_Factory");
215 if (this->debug_level_ >= 2)
216 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Trying to get a reference of a factory.\n"));
218 // Find an appropriate factory over there.
219 exception_message = "While finding factories";
220 CosLifeCycle::Factories *factories_ptr =
221 factory_Finder_var_->find_factories (factoryName);
223 if (factories_ptr == 0)
224 ACE_ERROR_RETURN ((LM_ERROR,
225 "Did not get a Generic Quoter Factory.\n"),
226 -1);
228 if (this->debug_level_ >= 2)
229 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Got a proper reference of a factory.\n"));
232 // Get the first object reference to a factory.
233 CORBA::Object_var quoter_FactoryObj_var;
235 if (factories_ptr->length () >= 1)
237 // everything is ok, at least one factory is there
238 CosLifeCycle::Factory_ptr factory = (*factories_ptr)[0];
239 quoter_FactoryObj_var = factory;
241 else
242 ACE_ERROR_RETURN ((LM_ERROR,
243 "No Factory received.\n"),
244 -1);
246 // Narrow it to a Quoter Generic Factory
247 exception_message = "While narrowing the factory";
248 generic_Factory_var_ =
249 CosLifeCycle::GenericFactory::_narrow (quoter_FactoryObj_var.in ());
251 if (CORBA::is_nil (this->generic_Factory_var_.in ()))
252 ACE_ERROR_RETURN ((LM_ERROR,
253 "Factory received is not valid.\n"),
254 -1);
256 if (this->debug_level_ >= 2)
257 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Quoter Factory.\n"));
259 catch (const CosLifeCycle::NoFactory& excpt)
261 ACE_ERROR ((LM_ERROR, "Quoter_Client::run - %C\n", exception_message));
262 excpt._tao_print_exception (
263 "Quoter::init_naming_service: No Factory available!");
265 catch (const CORBA::Exception& ex)
267 ACE_ERROR ((LM_ERROR, "Quoter_Client::init_naming_service - %C\n", exception_message));
268 ex._tao_print_exception ("Quoter::init_naming_service");
269 return -1;
272 return 0;
276 Quoter_Client::init (int argc, ACE_TCHAR **argv)
278 this->argc_ = argc;
279 int i;
281 // Make a copy of argv since ORB_init will change it.
282 this->argv_ = new ACE_TCHAR *[argc];
284 for (i = 0; i < argc; i++)
285 this->argv_[i] = argv[i];
289 // Retrieve the ORB.
290 this->orb_ = CORBA::ORB_init (this->argc_,
291 this->argv_,
292 "internet");
294 // Parse command line and verify parameters.
295 if (this->parse_args () == -1)
296 return -1;
298 int naming_result = this->init_naming_service ();
299 if (naming_result == -1)
300 return naming_result;
302 if (this->debug_level_ >= 2)
303 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Factory received OK\n"));
305 // using the Quoter Generic Factory
306 CosLifeCycle::Key genericFactoryName (1); // max = 1
307 genericFactoryName.length(1);
308 genericFactoryName[0].id = CORBA::string_dup ("Quoter_Factory");
310 // The final factory
312 CosLifeCycle::Criteria criteria(1);
313 criteria.length (1);
314 criteria[0].name = CORBA::string_dup ("filter");
315 criteria[0].value <<= CORBA::string_dup ("name=='Quoter_Generic_Factory'");
316 // used to find the last generic factory in the chain
318 CORBA::Object_var quoterObject_var =
319 this->generic_Factory_var_->create_object (genericFactoryName,
320 criteria);
322 this->quoter_var_ = Stock::Quoter::_narrow (quoterObject_var.in());
324 if (this->debug_level_ >= 2)
325 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Quoter Created\n"));
327 if (CORBA::is_nil (this->quoter_var_.in()))
329 ACE_ERROR_RETURN ((LM_ERROR,
330 "null quoter objref returned by factory\n"),
331 -1);
334 catch (const CORBA::Exception& ex)
336 ex._tao_print_exception ("Quoter::init");
337 return -1;
340 return 0;
344 // This function runs the test.
347 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
349 ACE_Thread_Manager thr_mgr;
351 int i;
352 int threads = 1;
354 for (i = 0; i < argc; i++)
355 if (ACE_OS::strcmp (argv[i], ACE_TEXT("-n")) == 0)
356 threads = ACE_OS::atoi(argv[i + 1]);
358 Quoter_Task **clients = new Quoter_Task*[threads];
360 for (i = 0; i < threads; i++)
361 clients[i] = new Quoter_Task (argc, argv);
364 for (i = 0; i < threads; i++)
365 clients[i]->activate (THR_BOUND | THR_SCHED_FIFO, 1, 0,
366 ACE_DEFAULT_THREAD_PRIORITY);
368 int result = ACE_Thread_Manager::instance ()->wait ();
370 for (i = 0; i < threads; i++)
371 delete clients[i];
373 delete [] clients;
375 return result;