Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / tests / Redundant_Naming / client.cpp
blob6d6c95ac71772b122dd4e871211c3ecf7ffee937
1 // ============================================================================
2 //
3 // = DESCRIPTION
4 // This class implements a CORBA client for a redundant CosNaming
5 // Service using stubs generated by the TAO ORB IDL compiler.
6 //
7 // = AUTHORS
8 // Rich Seibel <seibel_r@ociweb.com>
9 // ============================================================================
11 #include "test_objectS.h"
12 #include "orbsvcs/CosNamingC.h"
13 #include "orbsvcs/Naming/Naming_Server.h"
14 #include "tao/debug.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/High_Res_Timer.h"
19 #if defined (_MSC_VER)
20 # pragma warning (disable : 4250)
21 #endif /* _MSC_VER */
23 class My_Test_Object :
24 public virtual POA_Test_Object
26 public:
27 My_Test_Object (CORBA::Short id = 0);
28 // Constructor.
30 ~My_Test_Object (void);
31 // Destructor.
33 // = Interface implementation accessor methods.
35 void id (CORBA::Short id);
36 // Sets id.
38 CORBA::Short id (void);
39 // Gets id.
41 private:
42 short id_;
45 My_Test_Object::My_Test_Object (CORBA::Short id)
46 : id_ (id)
50 My_Test_Object::~My_Test_Object (void)
54 CORBA::Short
55 My_Test_Object::id (void)
57 return id_;
60 void
61 My_Test_Object::id (CORBA::Short id)
63 id_ = id;
67 // This function runs the test.
69 int
70 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
72 int c_breath = 4;
73 int c_depth = 4;
74 int o_breath = 4;
75 ACE_TCHAR *ns1ref = 0;
76 ACE_TCHAR *ns2ref = 0;
78 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("b:d:o:p:q:"));
79 int c;
80 int i;
82 while ((c = get_opts ()) != -1)
83 switch (c)
85 case 'b':
86 i = ACE_OS::atoi(get_opts.opt_arg ());
87 if (i<2)
89 ACE_ERROR((LM_ERROR,
90 ACE_TEXT ("Invalid breath, must be 2 or more\n")));
91 ACE_OS::exit(1);
93 c_breath = i;
94 break;
95 case 'd':
96 i = ACE_OS::atoi(get_opts.opt_arg ());
97 if (i<2)
99 ACE_ERROR((LM_ERROR,
100 ACE_TEXT ("Invalid depth, must be 2 or more\n")));
101 ACE_OS::exit(1);
103 c_depth = i;
104 break;
105 case 'o':
106 i = ACE_OS::atoi(get_opts.opt_arg ());
107 if (i<2)
109 ACE_ERROR((LM_ERROR,
110 ACE_TEXT ("Invalid breath, must be 2 or more\n")));
111 ACE_OS::exit(1);
113 o_breath = i;
114 break;
115 case 'p':
116 ns1ref = get_opts.opt_arg ();
117 break;
118 case 'q':
119 ns2ref = get_opts.opt_arg ();
120 break;
121 default:
122 ACE_ERROR_RETURN ((LM_ERROR,
123 ACE_TEXT ("Argument %c \n usage: %s")
124 ACE_TEXT (" [-b <breath of context tree>]")
125 ACE_TEXT (" [-d <depth of context tree>]")
126 ACE_TEXT (" [-o <breath of object tree>]")
127 ACE_TEXT (" -p <ior of first name server>")
128 ACE_TEXT (" -q <ior of second name server>")
129 ACE_TEXT ("\n")),
130 -1);
133 CosNaming::NamingContext_var root_context_1;
134 CosNaming::NamingContext_var root_context_2;
138 // Initialize orb
139 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
141 // ior's are specified for the name servers through a commandline
142 // option or a file.
144 // Resolve the first name server
146 CORBA::Object_var ns1obj = orb->string_to_object (
147 ACE_TEXT_ALWAYS_CHAR (ns1ref));
149 if (CORBA::is_nil (ns1obj.in ()))
150 ACE_ERROR_RETURN ((LM_ERROR,
151 ACE_TEXT ("invalid ior <%s>\n"),
152 ns1ref),
153 -1);
154 root_context_1 = CosNaming::NamingContext::_narrow (ns1obj.in ());
156 // Resolve the second name server
158 CORBA::Object_var ns2obj = orb->string_to_object (
159 ACE_TEXT_ALWAYS_CHAR (ns2ref));
161 if (CORBA::is_nil (ns2obj.in ()))
162 ACE_ERROR_RETURN ((LM_ERROR,
163 ACE_TEXT ("invalid ior <%s>\n"),
164 ns2ref),
165 -1);
166 root_context_2 = CosNaming::NamingContext::_narrow (ns2obj.in ());
169 catch (const CORBA::Exception& ex)
171 ex._tao_print_exception (ACE_TEXT ("Unable to resolve name servers"));
172 return -1;
175 // Create a bunch of objects in one context
176 // Note: strings to the naming service must be char, not wchar
179 // Bind one context level under root.
180 CosNaming::Name level1;
181 level1.length (1);
182 level1[0].id = CORBA::string_dup ("level1_context");
183 CosNaming::NamingContext_var level1_context;
184 level1_context = root_context_1->bind_new_context (level1);
186 for (i=0; i<o_breath; i++)
188 // Instantiate a dummy object and bind it under the new context.
189 My_Test_Object *impl1 = new My_Test_Object (i+1);
190 Test_Object_var obj1 = impl1->_this ();
191 impl1->_remove_ref ();
193 level1.length (2);
194 char wide_name[16];
195 ACE_OS::sprintf(wide_name, "obj_%d", i);
196 level1[1].id = CORBA::string_dup (wide_name);
197 root_context_1->bind (level1, obj1.in ());
201 catch (const CORBA::Exception& ex)
203 ex._tao_print_exception (ACE_TEXT ("Unable to create a lot of objects"));
204 return -1;
207 // Create a deep context tree
210 CosNaming::NamingContext_var next_context = root_context_1;
211 for (i=0; i<c_depth; i++)
213 // Bind level1 context under root.
214 CosNaming::Name deep;
215 deep.length (1);
216 char deep_name[16];
217 ACE_OS::sprintf(deep_name, "deep_%d", i);
218 deep[0].id = CORBA::string_dup (deep_name);
219 CosNaming::NamingContext_var deep_context;
220 deep_context = next_context->bind_new_context (deep);
221 next_context = deep_context;
224 catch (const CORBA::Exception& ex)
226 ex._tao_print_exception (ACE_TEXT ("Unable to create deep context"));
227 return -1;
230 // Create a wide context tree
233 for (i=0; i<c_breath; i++)
235 // Bind all level of context under root.
236 CosNaming::Name wide;
237 wide.length (1);
238 char wide_name[16];
239 ACE_OS::sprintf(wide_name, "wide_%d", i);
240 wide[0].id = CORBA::string_dup (wide_name);
241 CosNaming::NamingContext_var wide_context;
242 wide_context = root_context_1->bind_new_context (wide);
245 catch (const CORBA::Exception& ex)
247 ex._tao_print_exception (ACE_TEXT ("Unable to create wide context"));
248 return -1;
251 // Delete three selected things, one from each tree
254 // Remove the second to last object from the Naming Context
255 CosNaming::Name wide1;
256 wide1.length (2);
257 wide1[0].id = CORBA::string_dup ("level1_context");
258 char wide_name[16];
259 ACE_OS::sprintf(wide_name, "obj_%d", o_breath-2);
260 wide1[1].id = CORBA::string_dup (wide_name);
261 root_context_1->unbind (wide1);
263 // Remove the second to last context from the wide root Naming Context
264 CosNaming::Name wide2;
265 wide2.length (1);
266 ACE_OS::sprintf(wide_name, "wide_%d", c_breath-2);
267 wide2[0].id = CORBA::string_dup (wide_name);
268 CORBA::Object_var result_obj_ref = root_context_1->resolve (wide2);
269 CosNaming::NamingContext_var result_object =
270 CosNaming::NamingContext::_narrow (result_obj_ref.in ());
271 if (CORBA::is_nil (result_object.in ()))
272 ACE_ERROR_RETURN ((LM_ERROR,
273 ACE_TEXT ("Problems with resolving wide context ")
274 ACE_TEXT ("- nil object ref.\n")),
275 -1);
276 result_object->destroy();
277 root_context_1->unbind (wide2);
279 // Remove the last context from the deep Naming Context
280 CosNaming::Name deep;
281 deep.length (c_depth);
282 char deep_name[16];
283 for (i=0; i<c_depth; i++)
285 ACE_OS::sprintf(deep_name, "deep_%d", i);
286 deep[i].id = CORBA::string_dup (deep_name);
288 result_obj_ref = root_context_1->resolve (deep);
289 result_object =
290 CosNaming::NamingContext::_narrow (result_obj_ref.in ());
291 if (CORBA::is_nil (result_object.in ()))
292 ACE_ERROR_RETURN ((LM_ERROR,
293 ACE_TEXT ("Problems with resolving deep context ")
294 ACE_TEXT ("- nil object ref.\n")),
295 -1);
296 result_object->destroy();
297 root_context_1->unbind (deep);
300 catch (const CORBA::Exception& ex)
302 ex._tao_print_exception (ACE_TEXT ("Unable to delete objects"));
303 return -1;
306 // Now use the other name server to access 3 objects next to the
307 // deleted objects and the 3 deleted objects
310 // Access the last object from the Naming Context
311 CosNaming::Name wide;
312 wide.length (2);
313 wide[0].id = CORBA::string_dup ("level1_context");
314 char wide_name[16];
315 ACE_OS::sprintf(wide_name, "obj_%d", o_breath-1);
316 wide[1].id = CORBA::string_dup (wide_name);
317 CORBA::Object_var result_obj_ref = root_context_2->resolve (wide);
318 Test_Object_var result_object = Test_Object::_narrow (result_obj_ref.in ());
319 if (CORBA::is_nil (result_object.in ()))
320 ACE_ERROR_RETURN ((LM_ERROR,
321 ACE_TEXT ("Problems with resolving object from ")
322 ACE_TEXT ("redundant server - nil object ref.\n")),
323 -1);
325 catch (const CORBA::Exception& ex)
327 ex._tao_print_exception (
328 ACE_TEXT (
329 "Unable to resolve object from redundant server"));
330 return -1;
335 // Access the deleted second to last object from the Naming Context
336 CosNaming::Name wide;
337 wide.length (2);
338 wide[0].id = CORBA::string_dup ("level1_context");
339 char wide_name[16];
340 ACE_OS::sprintf(wide_name, "obj_%d", o_breath-2);
341 wide[1].id = CORBA::string_dup (wide_name);
342 CORBA::Object_var result_obj_ref = root_context_2->resolve (wide);
343 ACE_ERROR_RETURN ((LM_ERROR,
344 ACE_TEXT ("Problems with resolving object from ")
345 ACE_TEXT ("redundant server - deleted object found.\n")),
346 -1);
348 catch (const CORBA::Exception&)
350 //expect exception since the context was deleted
355 // Access the last context from the wide Naming Context
356 CosNaming::Name wide;
357 wide.length (1);
358 char wide_name[16];
359 ACE_OS::sprintf(wide_name, "wide_%d", c_breath-1);
360 wide[0].id = CORBA::string_dup (wide_name);
361 CORBA::Object_var result_obj_ref = root_context_2->resolve (wide);
362 CosNaming::NamingContext_var result_object =
363 CosNaming::NamingContext::_narrow (result_obj_ref.in ());
364 if (CORBA::is_nil (result_object.in ()))
365 ACE_ERROR_RETURN ((LM_ERROR,
366 ACE_TEXT ("Problems with resolving wide context from ")
367 ACE_TEXT ("redundant server - nil object ref.\n")),
368 -1);
370 catch (const CORBA::Exception& ex)
372 ex._tao_print_exception (
373 ACE_TEXT (
374 "Unable to resolve wide context from redundant server"));
375 return -1;
380 // Access the deleted second to last object from the Naming Context
381 CosNaming::Name wide;
382 wide.length (2);
383 char wide_name[16];
384 ACE_OS::sprintf(wide_name, "wide_%d", c_breath-2);
385 wide[0].id = CORBA::string_dup (wide_name);
386 CORBA::Object_var result_obj_ref = root_context_2->resolve (wide);
387 ACE_ERROR_RETURN ((LM_ERROR,
388 ACE_TEXT ("Problems with resolving wide context from ")
389 ACE_TEXT ("redundant server - deleted object found.\n")),
390 -1);
392 catch (const CORBA::Exception&)
394 //expect exception since the context was deleted
399 // Access the deleted last context from the deep Naming Context
400 CosNaming::Name deep;
401 deep.length (c_depth);
402 char deep_name[16];
403 for (i=0; i<c_depth; i++)
405 ACE_OS::sprintf(deep_name, "deep_%d", i);
406 deep[i].id = CORBA::string_dup (deep_name);
408 CORBA::Object_var result_obj_ref = root_context_1->resolve (deep);
409 ACE_ERROR_RETURN ((LM_ERROR,
410 ACE_TEXT ("Problems with resolving deep context from ")
411 ACE_TEXT ("redundant server - deleted object found.\n")),
412 -1);
414 catch (const CORBA::Exception&)
416 //expect exception since the context was deleted
421 // Access the second to last object from the Naming Context
422 CosNaming::Name deep;
423 deep.length (c_depth-1);
424 char deep_name[16];
425 for (i=0; i<c_depth-1; i++)
427 ACE_OS::sprintf(deep_name, "deep_%d", i);
428 deep[i].id = CORBA::string_dup (deep_name);
430 CORBA::Object_var result_obj_ref = root_context_1->resolve (deep);
431 CosNaming::NamingContext_var result_object =
432 CosNaming::NamingContext::_narrow (result_obj_ref.in ());
433 if (CORBA::is_nil (result_object.in ()))
434 ACE_ERROR_RETURN ((LM_ERROR,
435 ACE_TEXT ("Problems with resolving deep context from ")
436 ACE_TEXT ("redundant server - nil object ref.\n")),
437 -1);
441 catch (const CORBA::Exception& ex)
443 ex._tao_print_exception (
444 ACE_TEXT (
445 "Unable to resolve deep context from redundant server"));
446 return -1;
449 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Redundancy test OK\n")));
451 // Test performance of binding a bunch of objects in one context
454 // Bind one context level under root.
455 CosNaming::Name level1;
456 level1.length (1);
457 level1[0].id = CORBA::string_dup ("perf_context");
458 CosNaming::NamingContext_var perf_context;
459 perf_context = root_context_1->bind_new_context (level1);
461 // Instantiate a dummy object and bind it under the new context.
462 My_Test_Object *impl1 = new My_Test_Object (i+1);
463 Test_Object_var obj1 = impl1->_this ();
464 impl1->_remove_ref ();
466 int test_runs = 100;
467 ACE_High_Res_Timer::global_scale_factor_type gsf =
468 ACE_High_Res_Timer::global_scale_factor ();
470 ACE_hrtime_t start = ACE_OS::gethrtime ();
472 // Test how long it takes to bind
473 for (i=0; i<test_runs; i++)
475 level1.length (1);
476 char wide_name[16];
477 ACE_OS::sprintf(wide_name, "obj_%d", i);
478 level1[0].id = CORBA::string_dup (wide_name);
479 perf_context->bind (level1, obj1.in ());
482 ACE_hrtime_t elapsed_time = ACE_OS::gethrtime () - start;
483 // convert to microseconds
484 ACE_UINT32 usecs = ACE_UINT32(elapsed_time / gsf);
485 double secs = usecs / 1000000.0;
487 ACE_DEBUG ((LM_DEBUG,
488 "Bound %i objects in %.2f secs\n",
489 test_runs, secs));
491 // Test how long it takes to resolve
492 start = ACE_OS::gethrtime ();
493 for (i=0; i<test_runs; i++)
495 level1.length (1);
496 char wide_name[16];
497 ACE_OS::sprintf(wide_name, "obj_%d", i);
498 level1[0].id = CORBA::string_dup (wide_name);
499 CORBA::Object_var result_obj_ref = perf_context->resolve (level1);
502 elapsed_time = ACE_OS::gethrtime () - start;
503 // convert to microseconds
504 usecs = ACE_UINT32(elapsed_time / gsf);
505 secs = ((ACE_INT32) usecs) / 1000000.0;
507 ACE_DEBUG ((LM_DEBUG,
508 "Resolved %i objects in %.2f secs\n",
509 test_runs, secs));
511 // Test how long it takes to unbind
512 start = ACE_OS::gethrtime ();
513 for (i=0; i<test_runs; i++)
515 level1.length (1);
516 char wide_name[16];
517 ACE_OS::sprintf(wide_name, "obj_%d", i);
518 level1[0].id = CORBA::string_dup (wide_name);
519 perf_context->unbind (level1);
522 elapsed_time = ACE_OS::gethrtime () - start;
523 // convert to microseconds
524 usecs = ACE_UINT32(elapsed_time / gsf);
525 secs = ((ACE_INT32) usecs) / 1000000.0;
527 ACE_DEBUG ((LM_DEBUG,
528 "Unbound %i objects in %.2f secs\n",
529 test_runs, secs));
531 catch (const CORBA::Exception& ex)
533 ex._tao_print_exception (ACE_TEXT ("ERROR: Exception during performance test.\n"));
534 return -1;
537 // All tests have passed up to this point
538 return 0;