1 #include "Server_Manager.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
6 : ior_output_file_ (0),
11 Server_i::~Server_i ()
13 delete this->servant_activator_
;
14 delete this->servant_locator_
;
15 this->orb_
->destroy ();
18 // This method parses the input.
21 Server_i::parse_args (int argc
, ACE_TCHAR
**argv
)
23 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("f:"));
26 while ((c
= get_opts ()) != -1)
30 ior_output_file_
= get_opts
.opt_arg ();
35 ACE_ERROR_RETURN ((LM_ERROR
,
37 "[-f ior_output_file] "
43 // Indicates successful parsing of command line.
47 // The IORs obtained are written into files for further use.
50 Server_i::write_iors_to_file (const char *first_ior
,
51 const char *second_ior
)
53 if (ior_output_file_
== 0)
54 // No filename was specified; simply return
57 char ior_output_file_1
[BUFSIZ
];
58 char ior_output_file_2
[BUFSIZ
];
60 ACE_OS::sprintf (ior_output_file_1
, "%s_1", ACE_TEXT_ALWAYS_CHAR(ior_output_file_
));
61 ACE_OS::sprintf (ior_output_file_2
, "%s_2", ACE_TEXT_ALWAYS_CHAR(ior_output_file_
));
63 FILE *output_file_1
= ACE_OS::fopen (ior_output_file_1
,
65 FILE *output_file_2
= ACE_OS::fopen (ior_output_file_2
,
67 if (output_file_1
== 0
68 || output_file_2
== 0)
69 ACE_ERROR_RETURN ((LM_ERROR
,
70 "Cannot open output files for writing IORs: %C, %C\n",
75 int result
= ACE_OS::fprintf (output_file_1
,
79 || static_cast<size_t> (result
) != ACE_OS::strlen (first_ior
))
80 ACE_ERROR_RETURN ((LM_ERROR
,
81 "ACE_OS::fprintf failed while writing %C to %C\n",
86 result
= ACE_OS::fprintf (output_file_2
,
90 || static_cast<size_t> (result
) != ACE_OS::strlen (second_ior
))
91 ACE_ERROR_RETURN ((LM_ERROR
,
92 "ACE_OS::fprintf failed while writing %C to %C\n",
96 ACE_OS::fclose (output_file_1
);
97 ACE_OS::fclose (output_file_2
);
101 // Initialisation of the ORB and POA.
104 Server_i::init (int argc
, ACE_TCHAR
**argv
)
108 // Initialize the ORB.
109 orb_
= CORBA::ORB_init (argc
, argv
);
111 int result
= parse_args (argc
, argv
);
115 // Obtain the RootPOA.
116 CORBA::Object_var obj
=
117 orb_
->resolve_initial_references ("RootPOA");
119 // Narrow the Object reference to a POA reference
120 root_poa_
= PortableServer::POA::_narrow (obj
.in ());
122 // Get the POAManager of RootPOA
123 poa_manager_
= root_poa_
->the_POAManager ();
125 catch (const CORBA::Exception
& ex
)
127 ex
._tao_print_exception ("Server_i:init_poa ()");
134 // This method creates an poa with 4 policies of which the servent
135 // retention policy decides whether the Servant Activator or the
136 // Servant Locator would be used by the Servant Manager.
138 PortableServer::POA_ptr
139 Server_i::create_poa (const char *name
,
140 int servant_retention_policy
)
142 PortableServer::POA_ptr my_poa
= 0;
147 policies_
.length (4);
149 // ID Assignment Policy.
151 root_poa_
->create_id_assignment_policy
152 (PortableServer::USER_ID
);
156 root_poa_
->create_lifespan_policy
157 (PortableServer::PERSISTENT
);
159 // Request Processing Policy.
161 root_poa_
->create_request_processing_policy
162 (PortableServer::USE_SERVANT_MANAGER
);
164 // Servant Retention Policy.
165 if (servant_retention_policy
== 1)
168 root_poa_
->create_servant_retention_policy
169 (PortableServer::RETAIN
);
172 if (servant_retention_policy
== 0)
175 root_poa_
->create_servant_retention_policy
176 (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 ServantActivator
*temp_servant_activator
;
216 ACE_NEW_RETURN (temp_servant_activator
,
217 ServantActivator (orb_
.in (),
218 ACE_TEXT("Generic_Servant"),
219 ACE_TEXT("supply_servant"),
220 ACE_TEXT("destroy_servant")),
223 // Set ServantActivator object as the servant_manager of
225 this->servant_activator_
= temp_servant_activator
;
226 first_poa
->set_servant_manager (this->servant_activator_
);
227 // For the code above, we're using the CORBA 3.0 servant manager
228 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
229 // use the following code in place of the previous lines:
231 // this->servant_activator_ = temp_servant_activator->_this ();
233 // first_poa->set_servant_manager (this->servant_activator_.in ()
236 // Create a reference with user created ID in firstPOA which
237 // uses the ServantActivator.
238 PortableServer::ObjectId_var first_test_oid
=
239 PortableServer::string_to_ObjectId ("first test");
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 ServantLocator
*temp_servant_locator
= 0;
263 ACE_NEW_RETURN (temp_servant_locator
,
264 ServantLocator (orb_
.in (),
265 ACE_TEXT ("Generic_Servant"),
266 ACE_TEXT ("supply_servant"),
267 ACE_TEXT ("destroy_servant")),
269 // Set ServantLocator object as the servant Manager of
271 this->servant_locator_
= temp_servant_locator
;
272 second_poa
->set_servant_manager (this->servant_locator_
);
273 // For the code above, we're using the CORBA 3.0 servant manager
274 // semantics supported by TAO. For CORBA 2.x ORBs you'd need to
275 // use the following code in place of the previous lines:
277 // this->servant_locator_ = temp_servant_locator->_this ();
279 // Set ServantLocator object as the servant Manager of
281 // second_poa->set_servant_manager (this->servant_locator_.in ()
284 // Try to create a reference with user created ID in second_poa
285 // which uses ServantLocator.
286 PortableServer::ObjectId_var second_test_oid
=
287 PortableServer::string_to_ObjectId ("second test");
290 second_poa
->create_reference_with_id (second_test_oid
.in (),
293 catch (const CORBA::Exception
& ex
)
295 ex
._tao_print_exception ("Server_i:create_locator ()");
302 // The execution process of the server.
309 // Invoke object_to_string on the references created in firstPOA
312 CORBA::String_var first_test_ior
=
313 orb_
->object_to_string (first_test_
.in ());
315 CORBA::String_var second_test_ior
=
316 orb_
->object_to_string (second_test_
.in ());
318 // Print the ior's of first_test and second_test.
320 ACE_DEBUG ((LM_DEBUG
,"%C\n%C\n",
321 first_test_ior
.in (),
322 second_test_ior
.in ()));
325 this->write_iors_to_file (first_test_ior
.in (),
326 second_test_ior
.in ());
327 if (write_result
!= 0)
330 // Set the poa_manager state to active, ready to process
332 poa_manager_
->activate ();
338 catch (const CORBA::Exception
& ex
)
340 ex
._tao_print_exception ("Server_i:run ()");