Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / Bug_2248_Regression / client.cpp
blob99e5e9ab7393baa11564faed6987e47c03a2d7df
1 #include "client.h"
2 #include "Client_i.h"
3 #include "testC.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Read_Buffer.h"
6 #include "tao/IORManipulation/IORManip_Loader.h"
7 #include "tao/PortableServer/PortableServer.h"
8 // Ensure that the PI library is linked in when building statically
9 #include "tao/PI/PI.h"
10 #include "orbsvcs/FaultTolerance/FT_Service_Activate.h"
11 #include "orbsvcs/FaultTolerance/FT_IOGR_Property.h"
12 #include "ace/OS_NS_stdio.h"
13 #include "ace/OS_NS_unistd.h"
15 // Files which have the IOR
16 const ACE_TCHAR *first_ior = 0;
17 const ACE_TCHAR *first_key = 0;
18 const ACE_TCHAR *second_ior = 0;
19 const ACE_TCHAR *second_key = 0;
20 const ACE_TCHAR *first_iogr_file = 0;
21 const ACE_TCHAR *second_iogr_file = 0;
23 int sleep_delay = 15; //seconds
25 // Objects
26 CORBA::Object_var object_primary = 0;
27 CORBA::Object_var object_secondary = 0;
29 // Reference to the IOR manipulator
31 int
32 parse_args (int argc, ACE_TCHAR *argv[])
34 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:k:b:l:g:h:t:"));
35 int c;
37 while ((c = get_opts ()) != -1)
38 switch (c)
40 case 'a':
41 first_ior = get_opts.opt_arg ();
42 break;
43 case 'k':
44 first_key = get_opts.opt_arg ();
45 break;
46 case 'b':
47 second_ior = get_opts.opt_arg ();
48 break;
49 case 'l':
50 second_key = get_opts.opt_arg ();
51 break;
52 case 'g':
53 first_iogr_file = get_opts.opt_arg ();
54 break;
55 case 'h':
56 second_iogr_file = get_opts.opt_arg ();
57 break;
58 case 't':
59 sleep_delay = ACE_OS::atoi(get_opts.opt_arg ());
60 break;
61 case '?':
62 default:
63 ACE_ERROR_RETURN ((LM_ERROR,
64 "usage: %s "
65 "-a <iorfile1> -k <key1> "
66 "-b <iorfile2> -l <key2> "
67 "-g <iogr v10 output file> "
68 "-h <iogr v11 output 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, ACE_TCHAR *argv[])
81 Manager manager;
83 try
85 // Initilaize the ORB, POA etc.
86 manager.init (argc, argv);
88 // the command line arguments
89 if (parse_args (argc, argv) == -1)
90 return -1;
92 // Merge the different IORS
93 manager.make_merged_iors ();
95 // Write IOR to file
96 if (manager.write_to_files ())
97 return -1;
99 ACE_OS::sleep (sleep_delay);
101 // Client, who is going to use the merged IOR
102 // Construct that with the managers ORB
103 Client_i client_imp (manager.orb ());
104 return client_imp.init ();
106 catch (const CORBA::Exception& ex)
108 ex._tao_print_exception ("Caught");
109 return -1;
112 return 0;
115 Manager::Manager ()
117 //no-op
120 void
121 Manager::init (int& argc,
122 ACE_TCHAR *argv[])
124 this->orb_ = CORBA::ORB_init (argc, argv);
126 // Obtain the RootPOA.
127 CORBA::Object_var obj_var =
128 this->orb_->resolve_initial_references ("RootPOA");
130 // Get the POA_var object from Object_var.
131 PortableServer::POA_var root_poa_var =
132 PortableServer::POA::_narrow (obj_var.in ());
134 // Get the POAManager of the RootPOA.
135 PortableServer::POAManager_var poa_manager_var =
136 root_poa_var->the_POAManager ();
138 poa_manager_var->activate ();
142 Manager::make_merged_iors ()
144 // Get an object reference for the ORBs IORManipultion object!
145 CORBA::Object_var IORM =
146 this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION, 0);
148 if (CORBA::is_nil (IORM.in()))
149 return -1;
151 TAO_IOP::TAO_IOR_Manipulation_var iorm =
152 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM.in());
154 if (CORBA::is_nil (iorm.in()))
155 return -1;
157 CORBA::Object_var first = orb_->string_to_object (first_ior);
159 if (CORBA::is_nil (first.in()))
160 return -1;
162 // Create the list
163 TAO_IOP::TAO_IOR_Manipulation::IORList iors (1);
164 iors.length(1);
165 iors [0] = first;
167 merged_set_10_ = iorm->merge_iors (iors);
169 if (CORBA::is_nil (merged_set_10_.in()))
171 return -1;
174 int result = set_properties (iorm.in(), merged_set_10_.in(),
175 first.in(), 10);
177 if (-1 == result)
179 return -1;
182 first = orb_->string_to_object (first_ior);
183 if (CORBA::is_nil (first.in()))
185 return -1;
188 CORBA::Object_var second = orb_->string_to_object (second_ior);
190 if (CORBA::is_nil (second.in()))
192 return -1;
195 // Create the list
197 iors.length(2);
198 iors [0] = first;
199 iors [1] = second;
201 merged_set_11_ = iorm->merge_iors (iors);
203 if (CORBA::is_nil (merged_set_11_.in()))
205 return -1;
208 result = set_properties (iorm.in(), merged_set_11_.in(),
209 first.in(), 11);
211 if (-1 == result)
213 return -1;
216 return 0;
220 Manager::set_properties (TAO_IOP::TAO_IOR_Manipulation_ptr iorm,
221 CORBA::Object_ptr merged_set,
222 CORBA::Object_ptr primary,
223 CORBA::ULong version)
225 FT::TagFTGroupTaggedComponent ft_tag_component;
227 // Property values
229 // Major and Minor revision numbers
230 ft_tag_component.component_version.major = (CORBA::Octet) 1;
231 ft_tag_component.component_version.minor = (CORBA::Octet) 0;
233 // Domain id
234 const char *id = "iogr_testing";
235 ft_tag_component.group_domain_id = id;
237 // Object group id
238 ft_tag_component.object_group_id = 10;
240 // Version
241 ft_tag_component.object_group_ref_version = version;
243 // Construct the IOGR Property class
244 TAO_FT_IOGR_Property iogr_prop (ft_tag_component);
246 // Set the property
247 CORBA::Boolean retval = iorm->set_primary (&iogr_prop,
248 primary,
249 merged_set);
251 // Set the primary
252 // See we are setting the second ior as the primary
253 if (retval != 0)
255 retval = iorm->set_property (&iogr_prop,
256 merged_set);
259 return 0;
263 Manager::run ()
267 this->orb_->run ();
269 catch (const CORBA::Exception&)
271 ACE_ERROR_RETURN ((LM_DEBUG,
272 "Error in run\n"),
273 -1);
276 return 0;
279 int Manager::write_to_files ()
281 if (write_to_file (merged_set_10_.in(), first_iogr_file) ||
282 write_to_file (merged_set_11_.in(), second_iogr_file))
284 return -1;
286 return 0;
290 Manager::write_to_file (CORBA::Object_ptr ior, const ACE_TCHAR *ior_output_file)
292 CORBA::String_var iorref =
293 this->orb_->object_to_string (ior);
295 if (ior_output_file != 0)
297 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
298 if (output_file == 0)
299 ACE_ERROR_RETURN ((LM_ERROR,
300 "Cannot open output file for writing IOR: %s",
301 ior_output_file),
303 ACE_OS::fprintf (output_file, "%s", iorref.in ());
304 ACE_OS::fclose (output_file);
307 return 0;
310 CORBA::ORB_ptr
311 Manager::orb ()
313 return this->orb_.in ();
316 Client_i::Client_i (CORBA::ORB_ptr orb)
317 :orb_ (CORBA::ORB::_duplicate (orb))
321 int run_remote_test (Simple_Server_ptr server, const ACE_TCHAR* execute_key)
323 char expected[1024], received[1024];
324 if (execute_key)
326 ACE_OS::sprintf (expected,
327 "remote_call() completed by %s", ACE_TEXT_ALWAYS_CHAR (execute_key));
329 else
331 ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
336 // Make a remote call
337 CORBA::String_var s =
338 server->remote_call ();
340 if (!s.in())
342 ACE_OS::strcpy (received, "(null)");
344 else
346 ACE_OS::strcpy (received, s.in());
349 catch (const CORBA::TRANSIENT&)
351 ACE_OS::strcpy (received, "CORBA::TRANSIENT");
353 catch (const CORBA::COMM_FAILURE&)
355 ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
358 ACE_DEBUG ((LM_DEBUG,
359 ACE_TEXT("CLIENT> REMOTE expected: '%C'"),
360 expected));
361 if (ACE_OS::strcmp (expected, received))
363 ACE_DEBUG ((LM_DEBUG,
364 ACE_TEXT ("... FAIL\n")
365 ACE_TEXT ("CLIENT> received: '%C'\n"),
366 received));
367 return -1;
370 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
371 return 0;
374 int run_abort_test (Simple_Server_ptr server,
375 const ACE_TCHAR* request_key,
376 const ACE_TCHAR* execute_key)
378 char expected[1024], received[1024];
379 if (execute_key)
381 ACE_OS::sprintf (expected,
382 "abort() completed by %s, still_alive=1",
383 ACE_TEXT_ALWAYS_CHAR (execute_key));
385 else
387 ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
392 CORBA::String_var s = server->abort (ACE_TEXT_ALWAYS_CHAR (request_key));
394 if (!s.in())
396 ACE_OS::strcpy (received, "(null)");
398 else
400 ACE_OS::strcpy (received, s.in());
403 catch (const CORBA::TRANSIENT&)
405 ACE_OS::strcpy (received, "CORBA::TRANSIENT");
407 catch (const CORBA::COMM_FAILURE&)
409 ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
412 ACE_DEBUG ((LM_DEBUG,
413 ACE_TEXT("CLIENT> ABORT expected: '%C'"),
414 expected));
416 if (ACE_OS::strcmp (expected, received))
418 ACE_DEBUG ((LM_DEBUG,
419 ACE_TEXT ("... FAIL\n")
420 ACE_TEXT ("CLIENT> received: '%C'\n"),
421 received));
422 return -1;
425 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
426 return 0;
429 int run_test (Simple_Server_ptr server)
431 int rv = run_abort_test (server, first_key, second_key);
433 if (rv)
435 return rv;
438 rv = run_remote_test (server, second_key);
440 if (rv)
442 return rv;
445 rv = run_abort_test (server, second_key, 0);
447 return rv;
451 Client_i::init ()
453 char iogrstr[1024];
454 ACE_OS::sprintf (iogrstr, "file://%s", ACE_TEXT_ALWAYS_CHAR (first_iogr_file));
456 int argc = 0;
457 ACE_TCHAR **argv = 0;
458 this->orb_ = CORBA::ORB_init (argc, argv);
460 CORBA::Object_var object =
461 this->orb_->string_to_object (iogrstr);
464 // Combined IOR stuff
465 Simple_Server_var server =
466 Simple_Server::_narrow (object.in ());
468 if (CORBA::is_nil (server.in ()))
470 ACE_ERROR ((LM_ERROR,
471 "Object reference <%C> is nil\n",
472 iogrstr));
475 // run tests
476 int result = run_test (server.in());
478 return result;