Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / FaultTolerance / GroupRef_Manipulation / server.cpp
blob53c65dd74e96d771a1ad933fb2f64655f54bb681
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);
103 return;
106 void test_colocal (ForwardRequestTest::test_ptr server)
108 CORBA::ULong number = 0;
109 for (int i = 1; i <= 25; ++i)
111 ACE_DEBUG ((LM_INFO,
112 "CLIENT: Issuing colocal request %d.\n",
113 i));
114 CORBA::ULong retval =
115 server->number ();
116 if (retval != 1 && retval != 317)
118 // Not a valid return value, forwarding went wrong and
119 // retval is in udefined state
120 ACE_ERROR ((LM_ERROR,
121 "(%P|%t) ERROR: colocal invocation returned invalid"
122 " value, location-forwarding must have failed.\n"));
123 ACE_OS::abort ();
126 number += retval;
129 ACE_DEBUG ((LM_INFO,
130 "CLIENT: Number %d .\n",
131 number));
134 if (number < 250)
136 ACE_ERROR ((LM_ERROR,
137 "(%P|%t) ERROR: Did not forward to new location\n"));
138 ACE_OS::abort ();
144 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
148 Server_ORBInitializer *temp_initializer = 0;
149 ACE_NEW_RETURN (temp_initializer,
150 Server_ORBInitializer,
151 -1); // No exceptions yet!
152 PortableInterceptor::ORBInitializer_var orb_initializer =
153 temp_initializer;
155 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
157 CORBA::ORB_var orb =
158 CORBA::ORB_init (argc, argv, "Server ORB");
160 CORBA::Object_var poa_object =
161 orb->resolve_initial_references ("RootPOA");
163 if (CORBA::is_nil (poa_object.in ()))
164 ACE_ERROR_RETURN ((LM_ERROR,
165 " (%P|%t) Unable to initialize the POA.\n"),
168 PortableServer::POA_var root_poa =
169 PortableServer::POA::_narrow (poa_object.in ());
171 PortableServer::POAManager_var poa_manager =
172 root_poa->the_POAManager ();
174 if (::parse_args (argc, argv) != 0)
175 return -1;
177 CORBA::PolicyList policies; // Empty policy list.
179 // Servant 1
180 test_i servant1 (1, orb.in ());
181 test_i servant2 (317, orb.in ());
183 PortableServer::POA_var first_poa =
184 root_poa->create_POA ("first POA",
185 poa_manager.in (),
186 policies);
190 PortableServer::ObjectId_var oid1 =
191 first_poa->activate_object (&servant1);
193 PortableServer::ObjectId_var oid2 =
194 first_poa->activate_object (&servant2);
196 CORBA::Object_var obj1 =
197 first_poa->servant_to_reference (&servant1);
199 CORBA::Object_var obj2 =
200 first_poa->servant_to_reference (&servant2);
202 (void) add_ft_prop (orb.in (),
203 obj1.in (),
204 obj2.in ());
206 CORBA::String_var ior =
207 orb->object_to_string (obj1.in ());
209 ACE_DEBUG ((LM_DEBUG,
210 "ForwardRequestTest::test servant 1: <%s>\n",
211 ior.in ()));
213 poa_manager->activate ();
215 // Set the forward references in the server request interceptor.
216 PortableInterceptor::ServerRequestInterceptor_var
217 server_interceptor = temp_initializer->server_interceptor ();
219 ForwardRequestTest::ServerRequestInterceptor_var interceptor =
220 ForwardRequestTest::ServerRequestInterceptor::_narrow (
221 server_interceptor.in ());
223 if (CORBA::is_nil (interceptor.in ()))
224 ACE_ERROR_RETURN ((LM_ERROR,
225 "(%P|%t) Could not obtain reference to "
226 "server request interceptor.\n"),
227 -1);
229 interceptor->forward_references (obj1.in (),
230 obj2.in ());
232 // Run co-local test
234 ForwardRequestTest::test_var server =
235 ForwardRequestTest::test::_narrow (obj1.in ());
236 test_colocal (server.in());
238 // Reset interceptor for remote tests
239 interceptor->reset ();
242 // Write each IOR to a file.
244 // IOR 1
245 FILE *output_file= ACE_OS::fopen (ior_file, "w");
246 if (output_file == 0)
247 ACE_ERROR_RETURN ((LM_ERROR,
248 "Cannot open output file <%s> for writing "
249 "IOR: %s",
250 ior.in ()),
252 ACE_OS::fprintf (output_file, "%s", ior.in ());
253 ACE_OS::fclose (output_file);
255 // Run the ORB event loop.
256 orb->run ();
258 root_poa->destroy (1, 1);
260 orb->destroy ();
262 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
264 catch (const CORBA::Exception& ex)
266 ex._tao_print_exception ("Caught exception:");
267 return -1;
270 return 0;
273 #else
276 ACE_TMAIN(int, ACE_TCHAR *[])
278 return 0;
281 #endif /* TAO_HAS_INTERCEPTORS */