Merge pull request #2258 from likema/log-msg-reset-ostream
[ACE_TAO.git] / TAO / orbsvcs / tests / FaultTolerance / GroupRef_Manipulation / server.cpp
blob8e21a491314e5c1c18ea981c3ab4eacaabf29386
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "test_i.h"
4 #include "ace/OS_NS_stdio.h"
6 #if TAO_HAS_INTERCEPTORS
8 #include "Server_ORBInitializer.h"
9 #include "Server_Request_Interceptor.h"
10 #include "tao/IORManipulation/IORManipulation.h"
11 #include "tao/ORBInitializer_Registry.h"
12 #include "orbsvcs/FaultTolerance/FT_Service_Activate.h"
13 #include "orbsvcs/FaultTolerance/FT_IOGR_Property.h"
14 #include "orbsvcs/FT_CORBA_ORBC.h"
16 const ACE_TCHAR *ior_file = 0;
18 int
19 parse_args (int argc, ACE_TCHAR *argv[])
21 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
22 int c;
24 while ((c = get_opts ()) != -1)
25 switch (c)
27 case 'o':
28 ior_file = get_opts.opt_arg ();
29 break;
30 default:
31 ACE_ERROR_RETURN ((LM_ERROR,
32 "Usage: %s "
33 "-o IOR\n",
34 argv[0]),
35 -1);
38 return 0;
41 void
42 add_ft_prop (CORBA::ORB_ptr o,
43 CORBA::Object_ptr obj1,
44 CORBA::Object_ptr obj2)
46 // Get an object reference for the ORBs IORManipultion object!
47 CORBA::Object_var IORM =
48 o->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
49 0);
51 TAO_IOP::TAO_IOR_Manipulation_var iorm =
52 TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM.in());
54 FT::TagFTGroupTaggedComponent ft_tag_component;
56 // Property values
57 // Major and Minor revision numbers
58 ft_tag_component.component_version.major = (CORBA::Octet) 1;
59 ft_tag_component.component_version.minor = (CORBA::Octet) 0;
61 // Domain id
62 const char *id = "version_testing";
63 ft_tag_component.group_domain_id = id;
65 // Object group id
66 ft_tag_component.object_group_id =
67 (CORBA::ULongLong) 10;
69 // Version
70 ft_tag_component.object_group_ref_version =
71 (CORBA::ULong) 1;
73 // Construct the IOGR Property class
74 TAO_FT_IOGR_Property iogr_prop (ft_tag_component);
76 // Set the property for object 1
77 CORBA::Boolean retval = iorm->set_property (&iogr_prop,
78 obj1);
80 if (retval != 0)
82 retval = iorm->set_primary (&iogr_prop,
83 obj1,
84 obj1);
88 // Set the property for object 2
89 // Change the version. That is the point of this test
90 // Version
91 ft_tag_component.object_group_ref_version = (CORBA::ULong) 5;
93 retval = iorm->set_property (&iogr_prop,
94 obj2);
96 if (retval != 0)
98 retval = iorm->set_primary (&iogr_prop,
99 obj2,
100 obj2);
104 void test_colocal (ForwardRequestTest::test_ptr server)
106 CORBA::ULong number = 0;
107 for (int i = 1; i <= 25; ++i)
109 ACE_DEBUG ((LM_INFO,
110 "CLIENT: Issuing colocal request %d.\n",
111 i));
112 CORBA::ULong retval =
113 server->number ();
114 if (retval != 1 && retval != 317)
116 // Not a valid return value, forwarding went wrong and
117 // retval is in udefined state
118 ACE_ERROR ((LM_ERROR,
119 "(%P|%t) ERROR: colocal invocation returned invalid"
120 " value, location-forwarding must have failed.\n"));
121 ACE_OS::abort ();
124 number += retval;
127 ACE_DEBUG ((LM_INFO,
128 "CLIENT: Number %d .\n",
129 number));
132 if (number < 250)
134 ACE_ERROR ((LM_ERROR,
135 "(%P|%t) ERROR: Did not forward to new location\n"));
136 ACE_OS::abort ();
142 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
146 Server_ORBInitializer *temp_initializer = 0;
147 ACE_NEW_RETURN (temp_initializer,
148 Server_ORBInitializer,
149 -1); // No exceptions yet!
150 PortableInterceptor::ORBInitializer_var orb_initializer =
151 temp_initializer;
153 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
155 CORBA::ORB_var orb =
156 CORBA::ORB_init (argc, argv, "Server ORB");
158 CORBA::Object_var poa_object =
159 orb->resolve_initial_references ("RootPOA");
161 if (CORBA::is_nil (poa_object.in ()))
162 ACE_ERROR_RETURN ((LM_ERROR,
163 " (%P|%t) Unable to initialize the POA.\n"),
166 PortableServer::POA_var root_poa =
167 PortableServer::POA::_narrow (poa_object.in ());
169 PortableServer::POAManager_var poa_manager =
170 root_poa->the_POAManager ();
172 if (::parse_args (argc, argv) != 0)
173 return -1;
175 CORBA::PolicyList policies; // Empty policy list.
177 // Servant 1
178 test_i servant1 (1, orb.in ());
179 test_i servant2 (317, orb.in ());
181 PortableServer::POA_var first_poa =
182 root_poa->create_POA ("first POA",
183 poa_manager.in (),
184 policies);
187 PortableServer::ObjectId_var oid1 =
188 first_poa->activate_object (&servant1);
190 PortableServer::ObjectId_var oid2 =
191 first_poa->activate_object (&servant2);
193 CORBA::Object_var obj1 =
194 first_poa->servant_to_reference (&servant1);
196 CORBA::Object_var obj2 =
197 first_poa->servant_to_reference (&servant2);
199 (void) add_ft_prop (orb.in (),
200 obj1.in (),
201 obj2.in ());
203 CORBA::String_var ior =
204 orb->object_to_string (obj1.in ());
206 ACE_DEBUG ((LM_DEBUG,
207 "ForwardRequestTest::test servant 1: <%s>\n",
208 ior.in ()));
210 poa_manager->activate ();
212 // Set the forward references in the server request interceptor.
213 PortableInterceptor::ServerRequestInterceptor_var
214 server_interceptor = temp_initializer->server_interceptor ();
216 ForwardRequestTest::ServerRequestInterceptor_var interceptor =
217 ForwardRequestTest::ServerRequestInterceptor::_narrow (
218 server_interceptor.in ());
220 if (CORBA::is_nil (interceptor.in ()))
221 ACE_ERROR_RETURN ((LM_ERROR,
222 "(%P|%t) Could not obtain reference to "
223 "server request interceptor.\n"),
224 -1);
226 interceptor->forward_references (obj1.in (),
227 obj2.in ());
229 // Run co-local test
231 ForwardRequestTest::test_var server =
232 ForwardRequestTest::test::_narrow (obj1.in ());
233 test_colocal (server.in());
235 // Reset interceptor for remote tests
236 interceptor->reset ();
239 // Write each IOR to a file.
241 // IOR 1
242 FILE *output_file= ACE_OS::fopen (ior_file, "w");
243 if (output_file == 0)
244 ACE_ERROR_RETURN ((LM_ERROR,
245 "Cannot open output file <%s> for writing "
246 "IOR: %s",
247 ior.in ()),
249 ACE_OS::fprintf (output_file, "%s", ior.in ());
250 ACE_OS::fclose (output_file);
252 // Run the ORB event loop.
253 orb->run ();
255 root_poa->destroy (true, true);
257 orb->destroy ();
259 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
261 catch (const CORBA::Exception& ex)
263 ex._tao_print_exception ("Caught exception:");
264 return -1;
267 return 0;
270 #else
273 ACE_TMAIN(int, ACE_TCHAR *[])
275 return 0;
278 #endif /* TAO_HAS_INTERCEPTORS */