Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / EndpointPolicy / server.cpp
blob6854f0cb1a1c3ff2a422b1323cbe58252b863a71
1 #include "Hello.h"
2 #include "tao/PolicyC.h"
3 #include "tao/EndpointPolicy/EndpointPolicy.h"
4 #include "tao/EndpointPolicy/IIOPEndpointValue_i.h"
5 #include "tao/Strategies/advanced_resource.h"
6 #include "tao/PI_Server/PI_Server.h"
7 #include "tao/ORB_Constants.h"
8 #include "ace/OS_NS_strings.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_unistd.h"
12 const ACE_TCHAR *good_ior_file = ACE_TEXT ("good.ior");
13 const ACE_TCHAR *bad_ior_file = ACE_TEXT ("bad.ior");
14 const ACE_TCHAR *root_ior_file = ACE_TEXT("root.ior");
15 const ACE_TCHAR *svc_conf_file = ACE_TEXT("multi_prot.conf");
17 int load_advanced_resources =
18 ACE_Service_Config::process_directive (ace_svc_desc_TAO_Advanced_Resource_Factory);
20 CORBA::Short endpoint_port = 12345;
21 int verbose = 0;
23 enum HostNameForm
25 from_hostname,
26 use_localhost,
27 use_defaulted,
28 multi_protocol
31 const ACE_TCHAR *form_arg;
33 HostNameForm host_form = from_hostname;
35 int
36 parse_args (int argc, ACE_TCHAR *argv[])
38 for (int c = 1; c < argc; c++) {
39 if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-g")) == 0)
41 good_ior_file = argv[++c];
43 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-b")) == 0)
45 bad_ior_file = argv[++c];
47 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-c")) == 0)
49 svc_conf_file = argv[++c];
51 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-p")) == 0)
53 endpoint_port = ACE_OS::atoi (argv[++c]);
55 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-v")) == 0)
57 verbose = 1;
59 else if (ACE_OS::strcasecmp (argv[c], ACE_TEXT ("-h")) == 0)
61 ++c;
62 if (c == argc)
63 ACE_ERROR_RETURN ((LM_ERROR,
64 "host form option requires an argument\n"),-1);
65 form_arg = argv[c];
66 if (ACE_OS::strcasecmp (form_arg, ACE_TEXT ("local")) == 0)
67 host_form = use_localhost;
68 else if (ACE_OS::strcasecmp (form_arg, ACE_TEXT ("default")) == 0)
69 host_form = use_defaulted;
70 else if (ACE_OS::strcasecmp (form_arg, ACE_TEXT ("multi")) == 0)
71 host_form = multi_protocol;
72 else
73 ACE_ERROR_RETURN ((LM_ERROR,
74 "invalid host form arg, '%s'\n", form_arg), -1);
76 else if (ACE_OS::strstr (argv[c], ACE_TEXT ("-ORB")) == argv[c])
78 c++;
79 continue;
81 else
82 ACE_ERROR_RETURN ((LM_ERROR,
83 "usage: %s "
84 "-g <goodiorfile> "
85 "-b <badiorfile> "
86 "-c <svc_conf_file>"
87 "-p <port> "
88 "-v "
89 "\n",
90 argv [0]),
91 -1);
93 // Indicates successful parsing of the command line
94 return 0;
97 int
98 make_ior (CORBA::ORB_ptr orb,
99 PortableServer::POA_ptr poa,
100 Hello * servant,
101 const ACE_TCHAR *ior_file)
103 CORBA::String_var poa_name = poa->the_name();
104 ACE_DEBUG ((LM_DEBUG, "Creating IOR from %C\n", poa_name.in()));
106 PortableServer::ObjectId_var oid = poa->activate_object (servant);
107 CORBA::Object_var o = poa->id_to_reference (oid.in ());
110 if (host_form == from_hostname || host_form == use_localhost)
112 CORBA::String_var ior =
113 orb->object_to_string (o.in ());
115 FILE *output_file= ACE_OS::fopen (ior_file, "w");
116 if (output_file == 0)
117 ACE_ERROR_RETURN ((LM_ERROR,
118 "Cannot open output file %s for writing IOR: %C",
119 ior_file,
120 ior.in ()),
122 ACE_OS::fprintf (output_file, "%s", ior.in ());
123 ACE_OS::fclose (output_file);
125 return 0;
130 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
132 CORBA::ORB_var orb;
133 CORBA::Object_var obj;
134 PortableServer::POA_var root_poa;
135 PortableServer::POAManagerFactory_var poa_manager_factory;
137 if (parse_args (argc, argv) != 0)
138 return 1;
140 const ACE_TCHAR *e1_format= ACE_TEXT ("iiop://%s:%d");
141 const ACE_TCHAR *e2_format= ACE_TEXT ("iiop://%s:%d/hostname_in_ior=unreachable");
142 ACE_TCHAR hostname[256];
143 int num_extra = 4;
145 switch (host_form)
147 case from_hostname:
148 ACE_OS::hostname (hostname, sizeof(hostname) / sizeof (ACE_TCHAR));
149 break;
151 case use_localhost:
152 ACE_OS::strcpy (hostname, ACE_TEXT ("localhost"));
153 break;
155 case use_defaulted:
156 hostname[0] = ACE_TEXT ('\0');
157 break;
159 case multi_protocol:
160 hostname[0] = ACE_TEXT ('\0');
161 e2_format = ACE_TEXT ("diop://");
162 num_extra = 6;
163 break;
166 size_t hostname_len = ACE_OS::strlen (hostname);
167 size_t e1_len = ACE_OS::strlen (e1_format) + 5; // 5 for the port#
168 size_t e2_len = ACE_OS::strlen (e2_format) + 5;
169 ACE_TCHAR **extra = 0;
170 ACE_NEW_RETURN (extra, ACE_TCHAR *[num_extra], -1);
172 extra[0] = ACE::strnew (ACE_TEXT ("-ORBEndpoint"));
173 ACE_NEW_RETURN (extra[1],
174 ACE_TCHAR[e1_len + hostname_len + 1],
175 -1);
176 ACE_OS::sprintf (extra[1], e1_format, hostname, endpoint_port);
177 extra[2] = ACE::strnew (ACE_TEXT ("-ORBEndpoint"));
178 ACE_NEW_RETURN (extra[3],
179 ACE_TCHAR[e2_len + hostname_len + 1],
180 -1);
181 ACE_OS::sprintf (extra[3], e2_format, hostname, endpoint_port+1);
182 if (host_form == multi_protocol)
184 extra[4] = ACE::strnew (ACE_TEXT ("-ORBSvcConf"));
185 extra[5] = ACE::strnew (svc_conf_file);
188 ACE_TCHAR **largv = new ACE_TCHAR *[argc+num_extra];
189 int i = 0;
190 for (i = 0; i < argc; i++)
191 largv[i] = argv[i];
193 ACE_DEBUG ((LM_DEBUG,"server adding args: "));
194 for (i = 0; i < num_extra; i++)
196 ACE_DEBUG ((LM_DEBUG, "%s ", extra[i]));
197 largv[argc+i] = extra[i];
199 ACE_DEBUG ((LM_DEBUG, "\n"));
201 argc += num_extra;
205 orb =
206 CORBA::ORB_init (argc, largv, "EndpointPolicy");
208 obj =
209 orb->resolve_initial_references("RootPOA");
211 root_poa =
212 PortableServer::POA::_narrow (obj.in ());
214 if (CORBA::is_nil (root_poa.in ()))
215 ACE_ERROR_RETURN ((LM_ERROR,
216 " (%P|%t) Panic: nil RootPOA\n"),
219 poa_manager_factory
220 = root_poa->the_POAManagerFactory ();
222 catch (const CORBA::Exception &ex)
224 ex._tao_print_exception("initialization error ");
225 return 1;
228 for (i = 0; i < num_extra; i++)
229 ACE::strdelete (extra[i]);
230 delete [] extra;
231 delete [] largv;
233 //-----------------------------------------------------------------------
235 // Create two valid endpoint policies. One to match each of the generated
236 // endpoint arguments supplied to ORB_init().
237 PortableServer::POAManager_var good_pm;
238 PortableServer::POAManager_var bad_pm;
239 CORBA::PolicyList policies;
240 policies.length (1);
242 EndpointPolicy::EndpointList list;
243 list.length (1);
244 list[0] = new IIOPEndpointValue_i (ACE_TEXT_ALWAYS_CHAR (hostname), endpoint_port);
248 CORBA::Any policy_value;
249 policy_value <<= list;
250 policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
251 policy_value);
252 good_pm = poa_manager_factory->create_POAManager ("goodPOAManager",
253 policies);
255 if (host_form == use_defaulted)
257 ACE_ERROR_RETURN ((LM_DEBUG,
258 "ERROR: Expected to catch policy error with "
259 "defaulted hostname, but didn't.\n"),1);
262 catch (const CORBA::PolicyError &)
264 if (host_form == use_defaulted)
266 ACE_ERROR_RETURN ((LM_DEBUG,
267 "Defaulted hostname properly rejected\n"), 0);
269 ACE_ERROR_RETURN ((LM_DEBUG,
270 "ERROR: Unexpectedly caught PolicyError "
271 "exception host_form = %s\n", form_arg), 1);
273 catch (const CORBA::Exception &ex)
275 ex._tao_print_exception ("Failed to create reachable POA manager");
276 return 1;
279 list[0] = new IIOPEndpointValue_i("unreachable", endpoint_port+1);
282 CORBA::Any policy_value;
283 policy_value <<= list;
284 policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
285 policy_value);
286 bad_pm = poa_manager_factory->create_POAManager ("badPOAManager",
287 policies);
289 catch (const CORBA::Exception &ex)
291 ex._tao_print_exception ("Failed to create unreachable POA manager");
292 return 1;
297 Hello *hello_impl;
298 ACE_NEW_RETURN (hello_impl,
299 Hello (orb.in ()),
301 PortableServer::ServantBase_var owner_transfer(hello_impl);
303 // Create poas assiciated with the each the good poa manager and the
304 // bad poa manager.
305 policies.length(0);
306 PortableServer::POA_var good_poa =
307 root_poa->create_POA ("goodPOA",
308 good_pm.in (),
309 policies);
311 int result = 0;
312 result = make_ior (orb.in(), root_poa.in(), hello_impl, root_ior_file);
313 if (result != 0)
314 return result;
316 result = make_ior (orb.in(), good_poa.in(), hello_impl, good_ior_file);
317 if (result != 0)
318 return result;
320 good_pm->activate ();
322 PortableServer::POA_var bad_poa;
324 if (host_form != multi_protocol)
326 bad_poa =
327 root_poa->create_POA ("badPOA",
328 bad_pm.in (),
329 policies);
330 result = make_ior (orb.in(), bad_poa.in(), hello_impl, bad_ior_file);
331 if (result != 0)
332 return result;
334 bad_pm->activate ();
337 if (host_form == from_hostname || host_form == use_localhost)
339 orb->run ();
340 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
343 catch (const CORBA::Exception &ex)
345 ex._tao_print_exception ("cannot run server");
349 root_poa->destroy (true, true);
351 orb->destroy ();
353 catch (const CORBA::Exception &ex)
355 ex._tao_print_exception ("CORBA exception during shutdown");
357 return 0;