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