Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_2247_Regression / Manager.cpp
blob31316978ce2318eca9ec65fdda76a5825d22efbc
1 #include "Manager.h"
2 #include "Client_i.h"
3 #include "testC.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Read_Buffer.h"
6 #include "ace/OS_NS_fcntl.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "tao/IORManipulation/IORManip_Loader.h"
9 #include "tao/PortableServer/PortableServer.h"
10 // Ensure that the PI library is linked in when building statically
11 #include "tao/PI/PI.h"
12 #include "orbsvcs/FaultTolerance/FT_Service_Activate.h"
13 #include "orbsvcs/FaultTolerance/FT_IOGR_Property.h"
14 #include "ace/OS_NS_stdio.h"
16 // Files which have the IOR
17 const ACE_TCHAR *first_ior = 0;
18 const ACE_TCHAR *first_key = 0;
19 const ACE_TCHAR *second_ior = 0;
20 const ACE_TCHAR *second_key = 0;
21 const ACE_TCHAR *ior_output_file = 0;
22 int shutdown_test = 0;
23 int merged_test = 0;
25 // Objects
26 CORBA::Object_var object_primary = 0;
27 CORBA::Object_var object_secondary = 0;
29 // Reference to the IOR manipulator
30 TAO_IOP::TAO_IOR_Manipulation_var iorm = 0;
32 int
33 parse_args (int argc, ACE_TCHAR *argv[])
35 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:k:b:l:c:sm"));
36 int c;
38 while ((c = get_opts ()) != -1)
39 switch (c)
41 case 'a':
42 first_ior = get_opts.opt_arg ();
43 break;
44 case 'k':
45 first_key = get_opts.opt_arg ();
46 break;
47 case 'b':
48 second_ior = get_opts.opt_arg ();
49 break;
50 case 'l':
51 second_key = get_opts.opt_arg ();
52 break;
53 case 'c':
54 ior_output_file = get_opts.opt_arg ();
55 break;
56 case 's':
57 shutdown_test = 1;
58 break;
59 case 'm':
60 merged_test = 1;
61 break;
62 case '?':
63 default:
64 ACE_ERROR_RETURN ((LM_ERROR,
65 "usage: %s "
66 "-a <iorfile1> -k <key1> "
67 "-b <iorfile2> -l <key2> "
68 "-c <output ior file>"
69 "\n",
70 argv [0]),
71 -1);
73 // Indicates successful parsing of the command line
74 return 0;
78 int
79 ACE_TMAIN (int argc,
80 ACE_TCHAR *argv[])
82 Manager manager;
84 try
86 // Initilaize the ORB, POA etc.
87 manager.init (argc, argv);
89 // the command line arguments
90 if (parse_args (argc, argv) == -1)
91 return -1;
93 // Merge the different IORS
94 manager.make_merged_iors ();
96 if (merged_test)
98 // Set properties. This is the most important portion of the
99 // test
100 manager.set_properties ();
103 // Write IOR to file
104 manager.write_to_file ();
106 // Client, who is going to use the merged IOR
107 // Construct that with the managers ORB
108 Client_i client_imp (manager.orb ());
109 return client_imp.init ();
111 catch (const CORBA::Exception& ex)
113 ex._tao_print_exception ("Caught");
114 return -1;
117 return 0;
120 Manager::Manager ()
121 :orb_ (0),
122 merged_set_ (0)
124 //no-op
127 void
128 Manager::init (int argc,
129 ACE_TCHAR *argv[])
131 this->orb_ = CORBA::ORB_init (argc, argv);
133 // Obtain the RootPOA.
134 CORBA::Object_var obj_var =
135 this->orb_->resolve_initial_references ("RootPOA");
137 // Get the POA_var object from Object_var.
138 PortableServer::POA_var root_poa_var =
139 PortableServer::POA::_narrow (obj_var.in ());
141 // Get the POAManager of the RootPOA.
142 PortableServer::POAManager_var poa_manager_var =
143 root_poa_var->the_POAManager ();
145 poa_manager_var->activate ();
149 Manager::make_merged_iors ()
151 // First server
152 object_primary =
153 this->orb_->string_to_object (first_ior);
155 if (merged_test)
157 //Second server
158 object_secondary =
159 this->orb_->string_to_object (second_ior);
161 // Get an object reference for the ORBs IORManipultion object!
162 CORBA::Object_var IORM =
163 this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
166 iorm =
167 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM.in());
170 // Create the list
171 TAO_IOP::TAO_IOR_Manipulation::IORList iors (2);
172 iors.length(2);
173 iors [0] = CORBA::Object::_duplicate (object_primary.in ());
174 iors [1] = CORBA::Object::_duplicate (object_secondary.in ());
176 // Create a merged set 1;
177 merged_set_ =
178 iorm->merge_iors (iors);
180 else
182 merged_set_ = object_primary;
185 return 0;
189 Manager::set_properties ()
191 FT::TagFTGroupTaggedComponent ft_tag_component;
193 // Property values
195 // Major and Minor revision numbers
196 ft_tag_component.component_version.major = (CORBA::Octet) 1;
197 ft_tag_component.component_version.minor = (CORBA::Octet) 0;
199 // Domain id
200 const char *id = "iogr_testing";
201 ft_tag_component.group_domain_id = id;
203 // Object group id
204 ft_tag_component.object_group_id =
205 (CORBA::ULongLong) 10;
207 // Version
208 ft_tag_component.object_group_ref_version =
209 (CORBA::ULong) 5;
211 // Construct the IOGR Property class
212 TAO_FT_IOGR_Property iogr_prop (ft_tag_component);
214 // Set the property
215 CORBA::Boolean retval = iorm->set_primary (&iogr_prop,
216 object_secondary.in (),
217 this->merged_set_.in ());
219 // Set the primary
220 // See we are setting the second ior as the primary
221 if (retval != 0)
223 retval = iorm->set_property (&iogr_prop,
224 this->merged_set_.in ());
227 return 0;
231 Manager::run ()
235 this->orb_->run ();
237 catch (const CORBA::Exception&)
239 ACE_ERROR_RETURN ((LM_DEBUG,
240 "Error in run\n"),
241 -1);
244 return 0;
248 Manager::write_to_file ()
251 CORBA::String_var iorref =
252 this->orb_->object_to_string (this->merged_set_.in ());
254 if (ior_output_file != 0)
256 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
257 if (output_file == 0)
258 ACE_ERROR_RETURN ((LM_ERROR,
259 "Cannot open output file for writing IOR: %s",
260 ior_output_file),
262 ACE_OS::fprintf (output_file, "%s", iorref.in ());
263 ACE_OS::fclose (output_file);
266 return 0;
269 CORBA::ORB_ptr
270 Manager::orb ()
272 return this->orb_.in ();
275 Client_i::Client_i (CORBA::ORB_ptr orb)
276 :orb_ (CORBA::ORB::_duplicate (orb))
280 int run_remote_test (Simple_Server_ptr server, const ACE_TCHAR* execute_key)
282 char expected[1024], received[1024];
283 if (execute_key)
285 ACE_OS::sprintf (expected,
286 "remote_call() completed by %s", ACE_TEXT_ALWAYS_CHAR (execute_key));
288 else if (shutdown_test && !merged_test)
290 ACE_OS::strcpy (expected, "CORBA::COMM_FAILURE");
292 else
294 ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
299 // Make a remote call
300 CORBA::String_var s =
301 server->remote_call ();
303 if (!s.in())
305 ACE_OS::strcpy (received, "(null)");
307 else
309 ACE_OS::strcpy (received, s.in());
312 catch (const CORBA::TRANSIENT&)
314 ACE_OS::strcpy (received, "CORBA::TRANSIENT");
316 catch (const CORBA::COMM_FAILURE&)
318 ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
321 ACE_DEBUG ((LM_DEBUG,
322 ACE_TEXT("CLIENT> REMOTE expected: '%C'"),
323 expected));
325 if (ACE_OS::strcmp (expected, received))
327 ACE_DEBUG ((LM_DEBUG,
328 ACE_TEXT ("... FAIL\n")
329 ACE_TEXT ("CLIENT> received: '%C'\n"),
330 received));
331 return -1;
334 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
335 return 0;
338 int run_abort_test (Simple_Server_ptr server,
339 const ACE_TCHAR* request_key, const ACE_TCHAR* execute_key)
341 char expected[1024], received[1024];
342 if (execute_key)
344 ACE_OS::sprintf (expected,
345 "abort() completed by %s, still_alive=1",
346 ACE_TEXT_ALWAYS_CHAR (execute_key));
348 else if (merged_test)
350 ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
352 else
354 ACE_OS::strcpy (expected, "CORBA::COMM_FAILURE");
359 CORBA::String_var s = server->abort (ACE_TEXT_ALWAYS_CHAR (request_key));
361 if (!s.in())
363 ACE_OS::strcpy (received, "(null)");
365 else
367 ACE_OS::strcpy (received, s.in());
370 catch (const CORBA::TRANSIENT&)
372 ACE_OS::strcpy (received, "CORBA::TRANSIENT");
374 catch (const CORBA::COMM_FAILURE&)
376 ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
379 ACE_DEBUG ((LM_DEBUG,
380 ACE_TEXT("CLIENT> ABORT expected: '%C'"),
381 expected));
382 if (ACE_OS::strcmp (expected, received))
384 ACE_DEBUG ((LM_DEBUG,
385 ACE_TEXT ("... FAIL\n")
386 ACE_TEXT ("CLIENT> received: '%C'\n"),
387 received));
388 return -1;
391 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
392 return 0;
395 int run_shutdown_test (Simple_Server_ptr server,
396 const ACE_TCHAR* request_key, const ACE_TCHAR* execute_key)
398 char expected[1024], received[1024];
399 if (execute_key)
401 ACE_OS::sprintf (expected,
402 "shutdown() completed by %s, still_alive=0",
403 ACE_TEXT_ALWAYS_CHAR (execute_key));
405 else
407 ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
412 CORBA::String_var s = server->shutdown (ACE_TEXT_ALWAYS_CHAR (request_key));
414 if (!s.in())
416 ACE_OS::strcpy (received, "(null)");
418 else
420 ACE_OS::strcpy (received, s.in());
423 catch (const CORBA::TRANSIENT&)
425 ACE_OS::strcpy (received, "CORBA::TRANSIENT");
428 ACE_DEBUG ((LM_DEBUG,
429 ACE_TEXT("CLIENT> SHUTDOWN expected: '%C'"),
430 expected));
431 if (ACE_OS::strcmp (expected, received))
433 ACE_DEBUG ((LM_DEBUG,
434 ACE_TEXT ("... FAIL\n")
435 ACE_TEXT ("CLIENT> received: '%C'\n"),
436 received));
437 return -1;
440 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
441 return 0;
444 int run_full_abort_test (Simple_Server_ptr server)
446 int rv = run_remote_test (server, second_key);
447 if (rv)
449 return rv;
452 rv = run_abort_test (server, second_key, first_key);
453 if (rv)
455 return rv;
458 rv = run_remote_test (server, first_key);
459 if (rv)
461 return rv;
464 rv = run_abort_test (server, first_key, 0);
465 if (rv)
467 return rv;
470 rv = run_remote_test (server, 0);
471 if (rv)
473 return rv;
476 return 0;
479 int run_full_shutdown_test (Simple_Server_ptr server)
481 int rv = run_remote_test (server, second_key);
482 if (rv)
484 return rv;
487 rv = run_shutdown_test (server, second_key, second_key);
488 if (rv)
490 return rv;
493 rv = run_remote_test (server, first_key);
494 if (rv)
496 return rv;
499 rv = run_shutdown_test (server, first_key, first_key);
500 if (rv)
502 return rv;
505 rv = run_remote_test (server, 0);
506 if (rv)
508 return rv;
511 return 0;
515 Client_i::init ()
517 // Open the file for reading.
518 ACE_HANDLE f_handle = ACE_OS::open (ior_output_file,
521 if (f_handle == ACE_INVALID_HANDLE)
522 ACE_ERROR ((LM_ERROR,
523 ACE_TEXT("Unable to open %s for writing (%p)\n"),
524 ACE_TEXT("open"),
525 ior_output_file));
527 ACE_Read_Buffer ior_buffer (f_handle);
529 char *data = ior_buffer.read ();
531 if (data == 0)
532 ACE_ERROR ((LM_ERROR,
533 "Unable to read ior: %p\n"));
536 int argc = 0;
537 ACE_TCHAR **argv = 0;
538 this->orb_ = CORBA::ORB_init (argc, argv);
540 CORBA::Object_var object =
541 this->orb_->string_to_object (data);
543 // Combined IOR stuff
544 Simple_Server_var server =
545 Simple_Server::_narrow (object.in ());
547 if (CORBA::is_nil (server.in ()))
549 ACE_ERROR ((LM_ERROR,
550 "Object reference <%C> is nil\n",
551 data));
554 int rv;
555 if (!merged_test)
557 if (shutdown_test)
559 rv = run_shutdown_test (server.in(), first_key, first_key);
561 else
563 rv = run_abort_test (server.in(), first_key, 0);
566 if (!rv)
568 rv = run_remote_test (server.in(), 0);
571 else if (shutdown_test)
573 rv = run_full_shutdown_test (server.in ());
575 else
577 rv = run_full_abort_test (server.in ());
580 ior_buffer.alloc ()->free (data);
581 ACE_OS::close (f_handle);
582 return rv;