1 #include "Server_Manager.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
6 : ior_output_file_ (0),
8 servant_activator_impl_ (0),
9 servant_locator_impl_ (0)
15 delete servant_activator_impl_
;
16 delete servant_locator_impl_
;
19 // This method parses the input.
22 Server_i::parse_args (int argc
,
25 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
28 while ((c
= get_opts ()) != -1)
32 ior_output_file_
= get_opts
.opt_arg ();
37 ACE_ERROR_RETURN ((LM_ERROR
,
39 "[-f ior_output_file] "
45 // Indicates successful parsing of command line.
49 // The IORs obtained are written into files for further use.
52 Server_i::write_iors_to_file (const char *first_ior
,
53 const char *second_ior
)
55 // No filename was specified; simply return
56 if (ior_output_file_
== 0)
59 ACE_TCHAR ior_output_file_1
[BUFSIZ
];
60 ACE_TCHAR ior_output_file_2
[BUFSIZ
];
62 ACE_OS::sprintf (ior_output_file_1
,
65 ACE_OS::sprintf (ior_output_file_2
,
69 FILE *output_file_1
= ACE_OS::fopen (ior_output_file_1
,
71 FILE *output_file_2
= ACE_OS::fopen (ior_output_file_2
,
73 if (output_file_1
== 0
74 || output_file_2
== 0)
75 ACE_ERROR_RETURN ((LM_ERROR
,
76 "Cannot open output files for writing IORs: %s, %s\n",
81 int result
= ACE_OS::fprintf (output_file_1
,
84 ACE_OS::fclose (output_file_1
);
86 || static_cast<size_t> (result
) != ACE_OS::strlen (first_ior
))
87 ACE_ERROR_RETURN ((LM_ERROR
,
88 "ACE_OS::fprintf failed while writing %C to %s\n",
93 result
= ACE_OS::fprintf (output_file_2
,
96 ACE_OS::fclose (output_file_2
);
98 || static_cast<size_t> (result
) != ACE_OS::strlen (second_ior
))
99 ACE_ERROR_RETURN ((LM_ERROR
,
100 "ACE_OS::fprintf failed while writing %C to %s\n",
107 // Initialization of the ORB and POA.
109 Server_i::init (int argc
, ACE_TCHAR
**argv
)
113 // Initialize the ORB.
114 orb_
= CORBA::ORB_init (argc
, argv
);
116 int result
= parse_args (argc
, argv
);
120 // Obtain the RootPOA.
121 CORBA::Object_var obj
=
122 orb_
->resolve_initial_references ("RootPOA");
124 // Narrow the Object reference to a POA reference
125 root_poa_
= PortableServer::POA::_narrow (obj
.in ());
127 // Get the POAManager of RootPOA
128 poa_manager_
= root_poa_
->the_POAManager ();
130 catch (const CORBA::Exception
& ex
)
132 ex
._tao_print_exception ("Server_i:init_poa ()");
139 // This method creates an poa with 4 policies of which the servent
140 // retention policy decides whether the Servant Activator or the
141 // Servant Locator would be used by the Servant Manager.
143 PortableServer::POA_ptr
144 Server_i::create_poa (const char *name
,
145 int servant_retention_policy
)
147 PortableServer::POA_ptr my_poa
= 0;
151 policies_
.length (4);
153 // ID Assignment Policy.
155 root_poa_
->create_id_assignment_policy
156 (PortableServer::USER_ID
);
160 root_poa_
->create_lifespan_policy
161 (PortableServer::PERSISTENT
);
163 // Request Processing Policy.
165 root_poa_
->create_request_processing_policy
166 (PortableServer::USE_SERVANT_MANAGER
);
168 // Servant Retention Policy.
169 if (servant_retention_policy
== 1)
171 root_poa_
->create_servant_retention_policy
172 (PortableServer::RETAIN
);
174 if (servant_retention_policy
== 0)
176 root_poa_
->create_servant_retention_policy
177 (PortableServer::NON_RETAIN
);
179 // Create myPOA as the child of RootPOA with the above
180 // policies_. myPOA will use SERVANT_ACTIVATOR or
181 // SERVANT_LOCATOR depending upon the servant retention policy
182 // being RETAIN or NONRETAIN respectively.
183 my_poa
= root_poa_
->create_POA (name
,
187 // Destroy the policy objects as they have been passed to
188 // create_POA and no longer needed.
189 for (CORBA::ULong i
= 0;
190 i
< policies_
.length ();
193 CORBA::Policy_ptr policy
= policies_
[i
];
197 catch (const CORBA::Exception
& ex
)
199 ex
._tao_print_exception ("Server_i:create_poa ()");
206 // The Servant Activator object is created and initialised.
209 Server_i::create_activator (PortableServer::POA_var first_poa
)
213 // An Servant Activator object is created which will activate
214 // the servant on-demand.
215 ACE_NEW_RETURN (servant_activator_impl_
,
216 ServantActivator_i (orb_
.in ()),
219 // Set ServantActivator_i object as the servant_manager of
221 first_poa
->set_servant_manager (servant_activator_impl_
);
222 // For the code above, we're using the CORBA 3.0 servant manager
223 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
224 // use the following code in place of the previous line:
226 // PortableServer::ServantManager_var servant_activator =
227 // servant_activator_impl_->_this ();
229 // first_poa->set_servant_manager (servant_activator.in (),
232 // Create a reference with user created ID in firstPOA which
233 // uses the ServantActivator. The servant dll name as well as
234 // the factory function in the dll are used in creating the
237 PortableServer::ObjectId_var first_test_oid
=
238 servant_activator_impl_
->create_dll_object_id ("Generic_Servant",
241 first_test_
= first_poa
->create_reference_with_id (first_test_oid
.in (),
244 catch (const CORBA::Exception
& ex
)
246 ex
._tao_print_exception ("Server_i:create_activator ()");
253 // The Servant Locator object is created and initialised.
256 Server_i::create_locator (PortableServer::POA_var second_poa
)
260 // An Servant Locator object is created which will activate
261 // the servant on-demand.
262 ACE_NEW_RETURN (servant_locator_impl_
,
263 ServantLocator_i (orb_
.in ()),
266 // Set ServantLocator_i object as the servant Manager of
268 second_poa
->set_servant_manager (servant_locator_impl_
);
269 // For the code above, we're using the CORBA 3.0 servant manager
270 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
271 // use the following code in place of the previous line:
272 // PortableServer::ServantManager_var servant_loactor =
273 // servant_loator_impl_->_this ();
275 // second_poa->set_servant_manager (servant_locator_impl_,
278 // Try to create a reference with user created ID in second_poa
279 // which uses ServantLocator. The servant dll name as well as
280 // the factory function in the dll are used in creating the
282 PortableServer::ObjectId_var second_test_oid
=
283 servant_locator_impl_
->create_dll_object_id
286 second_test_
= second_poa
->create_reference_with_id
287 (second_test_oid
.in (),
290 catch (const CORBA::Exception
& ex
)
292 ex
._tao_print_exception ("Server_i:create_locator ()");
299 // The execution process of the server.
306 // Invoke object_to_string on the references created in firstPOA
309 CORBA::String_var first_test_ior
=
310 orb_
->object_to_string (first_test_
.in ());
312 CORBA::String_var second_test_ior
=
313 orb_
->object_to_string (second_test_
.in ());
315 // Print the ior's of first_test and second_test.
317 ACE_DEBUG ((LM_DEBUG
,"%C\n%C\n",
318 first_test_ior
.in (),
319 second_test_ior
.in ()));
322 this->write_iors_to_file (first_test_ior
.in (),
323 second_test_ior
.in ());
324 if (write_result
!= 0)
327 // Set the poa_manager state to active, ready to process
329 poa_manager_
->activate ();
337 catch (const CORBA::Exception
& ex
)
339 ex
._tao_print_exception ("Server_i:run ()");