Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / Loader / Server_Manager.cpp
blobd349f30162cd925f5940118e34c67e23ce3b1371
1 #include "Server_Manager.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
5 Server_i::Server_i ()
6 : ior_output_file_ (0),
7 policies_ (4)
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.
20 int
21 Server_i::parse_args (int argc, ACE_TCHAR **argv)
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
24 int c;
26 while ((c = get_opts ()) != -1)
27 switch (c)
29 case 'f':
30 ior_output_file_ = get_opts.opt_arg ();
31 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s "
37 "[-f ior_output_file] "
38 "\n",
39 argv [0]),
40 -1);
43 // Indicates successful parsing of command line.
44 return 0;
47 // The IORs obtained are written into files for further use.
49 int
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
55 return 0;
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,
64 "w");
65 FILE *output_file_2 = ACE_OS::fopen (ior_output_file_2,
66 "w");
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",
71 ior_output_file_1,
72 ior_output_file_2),
73 -1);
75 int result = ACE_OS::fprintf (output_file_1,
76 "%s",
77 first_ior);
78 if (result <= 0
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",
82 first_ior,
83 ior_output_file_1),
84 -1);
86 result = ACE_OS::fprintf (output_file_2,
87 "%s",
88 second_ior);
89 if (result <= 0
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",
93 second_ior,
94 ior_output_file_2),
95 -1);
96 ACE_OS::fclose (output_file_1);
97 ACE_OS::fclose (output_file_2);
98 return 0;
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);
112 if (result != 0)
113 return result;
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 ()");
128 return 1;
131 return 0;
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.
150 policies_[0] =
151 root_poa_->create_id_assignment_policy
152 (PortableServer::USER_ID);
154 // Lifespan Policy.
155 policies_[1] =
156 root_poa_->create_lifespan_policy
157 (PortableServer::PERSISTENT);
159 // Request Processing Policy.
160 policies_[2] =
161 root_poa_->create_request_processing_policy
162 (PortableServer::USE_SERVANT_MANAGER);
164 // Servant Retention Policy.
165 if (servant_retention_policy == 1)
167 policies_[3] =
168 root_poa_->create_servant_retention_policy
169 (PortableServer::RETAIN);
172 if (servant_retention_policy == 0)
174 policies_[3] =
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,
184 poa_manager_.in (),
185 policies_);
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 ();
191 ++i)
193 CORBA::Policy_ptr policy = policies_[i];
194 policy->destroy ();
197 catch (const CORBA::Exception& ex)
199 ex._tao_print_exception ("Server_i:create_poa ()");
200 return 0;
203 return my_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
224 // firstPOA.
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 ()
234 //);
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 (),
242 "IDL:test:1.0");
244 catch (const CORBA::Exception& ex)
246 ex._tao_print_exception ("Server_i:create_activator ()");
247 return 1;
250 return 0;
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
270 // secondPOA.
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
280 // secondPOA.
281 // second_poa->set_servant_manager (this->servant_locator_.in ()
282 //);
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");
289 second_test_ =
290 second_poa->create_reference_with_id (second_test_oid.in (),
291 "IDL:test:1.0");
293 catch (const CORBA::Exception& ex)
295 ex._tao_print_exception ("Server_i:create_locator ()");
296 return 1;
299 return 0;
302 // The execution process of the server.
305 Server_i::run ()
309 // Invoke object_to_string on the references created in firstPOA
310 // and secondPOA.
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 ()));
324 int write_result =
325 this->write_iors_to_file (first_test_ior.in (),
326 second_test_ior.in ());
327 if (write_result != 0)
328 return write_result;
330 // Set the poa_manager state to active, ready to process
331 // requests.
332 poa_manager_->activate ();
335 // Run the ORB.
336 orb_->run ();
338 catch (const CORBA::Exception& ex)
340 ex._tao_print_exception ("Server_i:run ()");
341 return 1;
344 return 0;