Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / POA / On_Demand_Loading / Server_Manager.cpp
blob45f5412c753c1f8a5e800c4d5499705d0d776a90
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),
8 servant_activator_impl_ (0),
9 servant_locator_impl_ (0)
13 Server_i::~Server_i()
15 delete servant_activator_impl_;
16 delete servant_locator_impl_;
19 // This method parses the input.
21 int
22 Server_i::parse_args (int argc,
23 ACE_TCHAR **argv)
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'f':
32 ior_output_file_ = get_opts.opt_arg ();
33 break;
35 case '?':
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "[-f ior_output_file] "
40 "\n",
41 argv [0]),
42 -1);
45 // Indicates successful parsing of command line.
46 return 0;
49 // The IORs obtained are written into files for further use.
51 int
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)
57 return 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,
63 ACE_TEXT("%s_1"),
64 ior_output_file_);
65 ACE_OS::sprintf (ior_output_file_2,
66 ACE_TEXT("%s_2"),
67 ior_output_file_);
69 FILE *output_file_1 = ACE_OS::fopen (ior_output_file_1,
70 "w");
71 FILE *output_file_2 = ACE_OS::fopen (ior_output_file_2,
72 "w");
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",
77 ior_output_file_1,
78 ior_output_file_2),
79 -1);
81 int result = ACE_OS::fprintf (output_file_1,
82 "%s",
83 first_ior);
84 ACE_OS::fclose (output_file_1);
85 if (result <= 0
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",
89 first_ior,
90 ior_output_file_1),
91 -1);
93 result = ACE_OS::fprintf (output_file_2,
94 "%s",
95 second_ior);
96 ACE_OS::fclose (output_file_2);
97 if (result <= 0
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",
101 second_ior,
102 ior_output_file_2),
103 -1);
104 return 0;
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);
117 if (result != 0)
118 return result;
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 ()");
133 return 1;
136 return 0;
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.
154 policies_[0] =
155 root_poa_->create_id_assignment_policy
156 (PortableServer::USER_ID);
158 // Lifespan Policy.
159 policies_[1] =
160 root_poa_->create_lifespan_policy
161 (PortableServer::PERSISTENT);
163 // Request Processing Policy.
164 policies_[2] =
165 root_poa_->create_request_processing_policy
166 (PortableServer::USE_SERVANT_MANAGER);
168 // Servant Retention Policy.
169 if (servant_retention_policy == 1)
170 policies_[3] =
171 root_poa_->create_servant_retention_policy
172 (PortableServer::RETAIN);
174 if (servant_retention_policy == 0)
175 policies_[3] =
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,
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 ACE_NEW_RETURN (servant_activator_impl_,
216 ServantActivator_i (orb_.in ()),
219 // Set ServantActivator_i object as the servant_manager of
220 // firstPOA.
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 (),
230 //);
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
235 // objectId.
237 PortableServer::ObjectId_var first_test_oid =
238 servant_activator_impl_->create_dll_object_id ("Generic_Servant",
239 "create_test_i");
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 ACE_NEW_RETURN (servant_locator_impl_,
263 ServantLocator_i (orb_.in ()),
266 // Set ServantLocator_i object as the servant Manager of
267 // secondPOA.
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_,
276 //);
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
281 // objectId.
282 PortableServer::ObjectId_var second_test_oid =
283 servant_locator_impl_->create_dll_object_id
284 ("Generic_Servant",
285 "create_test_i");
286 second_test_ = second_poa->create_reference_with_id
287 (second_test_oid.in (),
288 "IDL:test:1.0");
290 catch (const CORBA::Exception& ex)
292 ex._tao_print_exception ("Server_i:create_locator ()");
293 return 1;
296 return 0;
299 // The execution process of the server.
302 Server_i::run ()
306 // Invoke object_to_string on the references created in firstPOA
307 // and secondPOA.
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 ()));
321 int write_result =
322 this->write_iors_to_file (first_test_ior.in (),
323 second_test_ior.in ());
324 if (write_result != 0)
325 return write_result;
327 // Set the poa_manager state to active, ready to process
328 // requests.
329 poa_manager_->activate ();
332 // Run the ORB.
333 orb_->run ();
335 orb_->destroy ();
337 catch (const CORBA::Exception& ex)
339 ex._tao_print_exception ("Server_i:run ()");
340 return 1;
342 return 0;