Merge pull request #2306 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / FaultTolerance / IOGR / Manager.cpp
blobedf57e5b4f44f449ac6aaedc4f2119c2af08de12
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 "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"
14 #include "ace/OS_NS_fcntl.h"
16 // Files which have the IOR
17 const ACE_TCHAR *first_ior = 0;
18 const ACE_TCHAR *second_ior = 0;
19 const ACE_TCHAR *ior_output_file = 0;
21 // Reference to the IOR manipulator
22 TAO_IOP::TAO_IOR_Manipulation_var iorm = 0;
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:b:c:"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'a':
34 first_ior = get_opts.opt_arg ();
35 break;
36 case 'b':
37 second_ior = get_opts.opt_arg ();
38 break;
39 case 'c':
40 ior_output_file = get_opts.opt_arg ();
41 break;
42 case '?':
43 default:
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "usage: %s "
46 "-a <iorfile>"
47 "-b <iorfile>"
48 "-c <output ior file>"
49 "\n",
50 argv [0]),
51 -1);
53 // Indicates successful parsing of the command line
54 return 0;
57 int
58 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
60 Manager manager;
62 try
64 // Initilaize the ORB, POA etc.
65 manager.init (argc, argv);
67 // the command line arguments
68 if (parse_args (argc, argv) == -1)
69 return -1;
71 // Merge the different IORS
72 manager.make_merged_iors ();
74 // Set properties. This is the most important portion of the
75 // test
76 manager.set_properties ();
78 // Write IOR to file
79 manager.write_to_file ();
81 // Client, who is going to use the merged IOR
82 // Construct that with the managers ORB
83 Client_i client_imp (manager.orb ());
84 client_imp.init ();
86 catch (const CORBA::Exception& ex)
88 ex._tao_print_exception ("Caught");
89 return -1;
92 return 0;
95 void
96 Manager::init (int argc, ACE_TCHAR *argv[])
98 this->orb_ = CORBA::ORB_init (argc, argv);
100 // Obtain the RootPOA.
101 CORBA::Object_var obj_var =
102 this->orb_->resolve_initial_references ("RootPOA");
104 // Get the POA_var object from Object_var.
105 PortableServer::POA_var root_poa_var =
106 PortableServer::POA::_narrow (obj_var.in ());
108 // Get the POAManager of the RootPOA.
109 PortableServer::POAManager_var poa_manager_var =
110 root_poa_var->the_POAManager ();
112 poa_manager_var->activate ();
116 Manager::make_merged_iors ()
118 // First server
119 this->object_primary_ =
120 this->orb_->string_to_object (first_ior);
122 //Second server
123 this->object_secondary_ =
124 this->orb_->string_to_object (second_ior);
126 // Get an object reference for the ORBs IORManipultion object!
127 CORBA::Object_var IORM =
128 this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
131 iorm =
132 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM.in());
135 // Create the list
136 TAO_IOP::TAO_IOR_Manipulation::IORList iors (2);
137 iors.length(2);
138 iors [0] = CORBA::Object::_duplicate (this->object_primary_.in ());
139 iors [1] = CORBA::Object::_duplicate (this->object_secondary_.in ());
141 // Create a merged set 1;
142 merged_set_ =
143 iorm->merge_iors (iors);
145 return 0;
149 Manager::set_properties ()
151 FT::TagFTGroupTaggedComponent ft_tag_component;
153 // Property values
155 // Major and Minor revision numbers
156 ft_tag_component.component_version.major = (CORBA::Octet) 1;
157 ft_tag_component.component_version.minor = (CORBA::Octet) 0;
159 // Domain id
160 const char *id = "iogr_testing";
161 ft_tag_component.group_domain_id = id;
163 // Object group id
164 ft_tag_component.object_group_id =
165 (CORBA::ULongLong) 10;
167 // Version
168 ft_tag_component.object_group_ref_version =
169 (CORBA::ULong) 5;
171 // Construct the IOGR Property class
172 TAO_FT_IOGR_Property iogr_prop (ft_tag_component);
174 // Set the property
175 CORBA::Boolean retval = iorm->set_property (&iogr_prop,
176 this->merged_set_.in ());
178 // Set the primary
179 // See we are setting the second ior as the primary
180 if (retval != 0)
182 retval = iorm->set_primary (&iogr_prop,
183 this->object_secondary_.in (),
184 this->merged_set_.in ());
187 return 0;
191 Manager::run ()
195 this->orb_->run ();
197 catch (const CORBA::Exception&)
199 ACE_ERROR_RETURN ((LM_DEBUG,
200 "Error in run\n"),
201 -1);
204 return 0;
208 Manager::write_to_file ()
211 CORBA::String_var iorref =
212 this->orb_->object_to_string (this->merged_set_.in ());
214 if (ior_output_file != 0)
216 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
217 if (output_file == 0)
218 ACE_ERROR_RETURN ((LM_ERROR,
219 "Cannot open output file for writing IOR: %s",
220 ior_output_file),
222 ACE_OS::fprintf (output_file, "%s", iorref.in ());
223 ACE_OS::fclose (output_file);
226 return 0;
229 CORBA::ORB_ptr
230 Manager::orb ()
232 return this->orb_.in ();
235 Client_i::Client_i (CORBA::ORB_ptr orb)
236 :orb_ (CORBA::ORB::_duplicate (orb))
240 void
241 run_test (Simple_Server_ptr server);
243 void
244 Client_i::init ()
246 // Open the file for reading.
247 ACE_HANDLE f_handle = ACE_OS::open (ior_output_file,
250 if (f_handle == ACE_INVALID_HANDLE)
251 ACE_ERROR ((LM_ERROR,
252 "Unable to open %s for writing: %p\n",
253 ior_output_file));
255 ACE_Read_Buffer ior_buffer (f_handle);
257 char *data = ior_buffer.read ();
259 if (data == 0)
260 ACE_ERROR ((LM_ERROR,
261 "Unable to read ior: %p\n"));
264 int argc = 0;
265 ACE_TCHAR **argv = 0;
266 this->orb_ = CORBA::ORB_init (argc, argv);
268 CORBA::Object_var object =
269 this->orb_->string_to_object (data);
271 // Combined IOR stuff
272 Simple_Server_var server =
273 Simple_Server::_narrow (object.in ());
275 if (CORBA::is_nil (server.in ()))
277 ACE_ERROR ((LM_ERROR,
278 "Object reference <%C> is nil\n",
279 data));
282 run_test (server.in ());
284 ior_buffer.alloc ()->free (data);
285 ACE_OS::close (f_handle);
289 void run_test (Simple_Server_ptr server)
291 // We do this twice as we know that there are only two servers.
292 for (CORBA::ULong i = 0;
293 i < 2;
294 i++)
298 for (CORBA::ULong j = 0;
299 j < 10;
300 j++)
302 // Make a remote call
303 server->remote_call ();
306 ACE_DEBUG ((LM_DEBUG,
307 ACE_TEXT ("*********************************\n")));
308 ACE_DEBUG ((LM_DEBUG,
309 ACE_TEXT ("I am going to shutdown the server\n")));
310 server->shutdown ();
311 ACE_OS::sleep (2);
313 catch (const CORBA::TRANSIENT& t)
315 if (t.completed () != CORBA::COMPLETED_NO)
317 t._tao_print_exception ("Unexpected kind of TRANSIENT");
319 else
321 ACE_DEBUG ((LM_DEBUG,
322 ACE_TEXT ("The completed status %d\n"), t.completed ()));
323 ACE_DEBUG ((LM_DEBUG,
324 ACE_TEXT ("Automagically re-issuing request on TRANSIENT\n")));
325 ACE_OS::sleep (1);
328 catch (const CORBA::COMM_FAILURE& f)
330 f._tao_print_exception ("A (sort of) expected COMM_FAILURE");
331 ACE_DEBUG ((LM_DEBUG,
332 ACE_TEXT ("Automagically re-issuing request on COMM_FAILURE\n")));
334 catch (const CORBA::Exception& ex)
336 ex._tao_print_exception ("Unexpected exception");
337 throw;