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;
31 const ACE_TCHAR
*form_arg
;
33 HostNameForm host_form
= from_hostname
;
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)
59 else if (ACE_OS::strcasecmp (argv
[c
], ACE_TEXT ("-h")) == 0)
63 ACE_ERROR_RETURN ((LM_ERROR
,
64 "host form option requires an argument\n"),-1);
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
;
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
])
82 ACE_ERROR_RETURN ((LM_ERROR
,
93 // Indicates successful parsing of the command line
98 make_ior (CORBA::ORB_ptr orb
,
99 PortableServer::POA_ptr poa
,
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",
122 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
123 ACE_OS::fclose (output_file
);
130 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
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)
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];
148 ACE_OS::hostname (hostname
, sizeof(hostname
) / sizeof (ACE_TCHAR
));
152 ACE_OS::strcpy (hostname
, ACE_TEXT ("localhost"));
156 hostname
[0] = ACE_TEXT ('\0');
160 hostname
[0] = ACE_TEXT ('\0');
161 e2_format
= ACE_TEXT ("diop://");
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],
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],
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
];
190 for (i
= 0; i
< argc
; 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"));
206 CORBA::ORB_init (argc
, largv
, "EndpointPolicy");
209 orb
->resolve_initial_references("RootPOA");
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"),
220 = root_poa
->the_POAManagerFactory ();
222 catch (const CORBA::Exception
&ex
)
224 ex
._tao_print_exception("initialization error ");
228 for (i
= 0; i
< num_extra
; i
++)
229 ACE::strdelete (extra
[i
]);
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
;
242 EndpointPolicy::EndpointList list
;
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
,
252 good_pm
= poa_manager_factory
->create_POAManager ("goodPOAManager",
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");
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
,
286 bad_pm
= poa_manager_factory
->create_POAManager ("badPOAManager",
289 catch (const CORBA::Exception
&ex
)
291 ex
._tao_print_exception ("Failed to create unreachable POA manager");
298 ACE_NEW_RETURN (hello_impl
,
301 PortableServer::ServantBase_var
owner_transfer(hello_impl
);
303 // Create poas assiciated with the each the good poa manager and the
306 PortableServer::POA_var good_poa
=
307 root_poa
->create_POA ("goodPOA",
312 result
= make_ior (orb
.in(), root_poa
.in(), hello_impl
, root_ior_file
);
316 result
= make_ior (orb
.in(), good_poa
.in(), hello_impl
, good_ior_file
);
320 good_pm
->activate ();
322 PortableServer::POA_var bad_poa
;
324 if (host_form
!= multi_protocol
)
327 root_poa
->create_POA ("badPOA",
330 result
= make_ior (orb
.in(), bad_poa
.in(), hello_impl
, bad_ior_file
);
337 if (host_form
== from_hostname
|| host_form
== use_localhost
)
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);
353 catch (const CORBA::Exception
&ex
)
355 ex
._tao_print_exception ("CORBA exception during shutdown");