Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Quoter / client.cpp
blobfe45683efc41af056f38aca45618feeeec4e7aab
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 (void)
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 (void)
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 (void)
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 // 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 (void)
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));
129 catch (const CORBA::Exception& ex)
131 ACE_ERROR ((LM_ERROR, "Quoter_Client::run - %C\n", exception_message));
132 ex._tao_print_exception ("Quoter_Client::run");
133 return -1;
136 return 0;
139 Quoter_Client::~Quoter_Client (void)
141 // Free resources
142 // Close the ior files
143 if (this->quoter_factory_key_ != 0)
144 ACE_OS::free (this->quoter_factory_key_);
145 if (this->quoter_key_ != 0)
146 ACE_OS::free (this->quoter_key_);
150 Quoter_Client::init_naming_service (void)
152 const char *exception_message = "Null Message";
156 // Resolve the Naming Service
157 CORBA::Object_var naming_obj =
158 orb_->resolve_initial_references ("NameService");
160 if (CORBA::is_nil (naming_obj.in ()))
161 ACE_ERROR_RETURN ((LM_ERROR,
162 "Unable to resolve the Name Service.\n"),
163 -1);
165 exception_message = "While narrowing the naming context";
166 CosNaming::NamingContext_var naming_context =
167 CosNaming::NamingContext::_narrow (naming_obj.in ());
169 if (this->debug_level_ >= 2)
170 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Naming Service.\n"));
172 CosNaming::Name quoterFactoryFinderName (2);
173 quoterFactoryFinderName.length (2);
174 quoterFactoryFinderName[0].id = CORBA::string_dup ("IDL_Quoter");
175 quoterFactoryFinderName[1].id = CORBA::string_dup ("Quoter_Factory_Finder");
177 if (this->debug_level_ >= 2)
178 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Trying to resolve the Quoter Factory Finder!\n"));
180 exception_message = "While resolving the factory finder";
181 CORBA::Object_var factory_obj =
182 naming_context->resolve (quoterFactoryFinderName);
184 if (this->debug_level_ >= 2)
185 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Resolved the Quoter Factory Finder!\n"));
187 exception_message = "While narrowing the factory finder";
188 factory_Finder_var_ =
189 Stock::Quoter_Factory_Finder::_narrow (factory_obj.in ());
191 if (CORBA::is_nil (factory_Finder_var_.in ()))
192 ACE_ERROR_RETURN ((LM_ERROR,
193 " could not resolve quoter factory in Naming service\n"),
194 -1);
196 if (this->debug_level_ >= 2)
197 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Quoter Factory Finder.\n"));
199 // The name of the Quoter Generic Factory
200 CosLifeCycle::Key factoryName (2); // max = 2
202 if (this->useLifeCycleService_ == 1)
204 // use the LifeCycle Service
205 factoryName.length(1);
206 factoryName[0].id = CORBA::string_dup ("Life_Cycle_Service");
208 else
210 // use a Generic Factory
211 factoryName.length(2);
212 factoryName[0].id = CORBA::string_dup ("IDL_Quoter");
213 factoryName[1].id = CORBA::string_dup ("Quoter_Generic_Factory");
216 if (this->debug_level_ >= 2)
217 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Trying to get a reference of a factory.\n"));
219 // Find an appropriate factory over there.
220 exception_message = "While finding factories";
221 CosLifeCycle::Factories *factories_ptr =
222 factory_Finder_var_->find_factories (factoryName);
224 if (factories_ptr == 0)
225 ACE_ERROR_RETURN ((LM_ERROR,
226 "Did not get a Generic Quoter Factory.\n"),
227 -1);
229 if (this->debug_level_ >= 2)
230 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Got a proper reference of a factory.\n"));
233 // Get the first object reference to a factory.
234 CORBA::Object_var quoter_FactoryObj_var;
236 if (factories_ptr->length () >= 1)
238 // everything is ok, at least one factory is there
239 CosLifeCycle::Factory_ptr factory = (*factories_ptr)[0];
240 quoter_FactoryObj_var = factory;
242 else
243 ACE_ERROR_RETURN ((LM_ERROR,
244 "No Factory received.\n"),
245 -1);
247 // Narrow it to a Quoter Generic Factory
248 exception_message = "While narrowing the factory";
249 generic_Factory_var_ =
250 CosLifeCycle::GenericFactory::_narrow (quoter_FactoryObj_var.in ());
252 if (CORBA::is_nil (this->generic_Factory_var_.in ()))
253 ACE_ERROR_RETURN ((LM_ERROR,
254 "Factory received is not valid.\n"),
255 -1);
257 if (this->debug_level_ >= 2)
258 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Have a proper reference to the Quoter Factory.\n"));
260 catch (const CosLifeCycle::NoFactory& excpt)
262 ACE_ERROR ((LM_ERROR, "Quoter_Client::run - %C\n", exception_message));
263 excpt._tao_print_exception (
264 "Quoter::init_naming_service: No Factory available!");
266 catch (const CORBA::Exception& ex)
268 ACE_ERROR ((LM_ERROR, "Quoter_Client::init_naming_service - %C\n", exception_message));
269 ex._tao_print_exception ("Quoter::init_naming_service");
270 return -1;
273 return 0;
277 Quoter_Client::init (int argc, ACE_TCHAR **argv)
279 this->argc_ = argc;
280 int i;
282 // Make a copy of argv since ORB_init will change it.
283 this->argv_ = new ACE_TCHAR *[argc];
285 for (i = 0; i < argc; i++)
286 this->argv_[i] = argv[i];
290 // Retrieve the ORB.
291 this->orb_ = CORBA::ORB_init (this->argc_,
292 this->argv_,
293 "internet");
295 // Parse command line and verify parameters.
296 if (this->parse_args () == -1)
297 return -1;
299 int naming_result = this->init_naming_service ();
300 if (naming_result == -1)
301 return naming_result;
303 if (this->debug_level_ >= 2)
304 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Factory received OK\n"));
306 // using the Quoter Generic Factory
307 CosLifeCycle::Key genericFactoryName (1); // max = 1
308 genericFactoryName.length(1);
309 genericFactoryName[0].id = CORBA::string_dup ("Quoter_Factory");
311 // The final factory
313 CosLifeCycle::Criteria criteria(1);
314 criteria.length (1);
315 criteria[0].name = CORBA::string_dup ("filter");
316 criteria[0].value <<= CORBA::string_dup ("name=='Quoter_Generic_Factory'");
317 // used to find the last generic factory in the chain
319 CORBA::Object_var quoterObject_var =
320 this->generic_Factory_var_->create_object (genericFactoryName,
321 criteria);
323 this->quoter_var_ = Stock::Quoter::_narrow (quoterObject_var.in());
325 if (this->debug_level_ >= 2)
326 ACE_DEBUG ((LM_DEBUG, "Quoter Client: Quoter Created\n"));
328 if (CORBA::is_nil (this->quoter_var_.in()))
330 ACE_ERROR_RETURN ((LM_ERROR,
331 "null quoter objref returned by factory\n"),
332 -1);
335 catch (const CORBA::Exception& ex)
337 ex._tao_print_exception ("Quoter::init");
338 return -1;
341 return 0;
345 // This function runs the test.
348 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
350 ACE_Thread_Manager thr_mgr;
352 int i;
353 int threads = 1;
355 for (i = 0; i < argc; i++)
356 if (ACE_OS::strcmp (argv[i], ACE_TEXT("-n")) == 0)
357 threads = ACE_OS::atoi(argv[i + 1]);
359 Quoter_Task **clients = new Quoter_Task*[threads];
361 for (i = 0; i < threads; i++)
362 clients[i] = new Quoter_Task (argc, argv);
365 for (i = 0; i < threads; i++)
366 clients[i]->activate (THR_BOUND | THR_SCHED_FIFO, 1, 0,
367 ACE_DEFAULT_THREAD_PRIORITY);
369 int result = ACE_Thread_Manager::instance ()->wait ();
371 for (i = 0; i < threads; i++)
372 delete clients[i];
374 delete [] clients;
376 return result;