Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Profile_And_Endpoint_Selection / server.cpp
bloba54df58297ebce05f9d21d267ef23d3542c48b13
1 #include "ace/Get_Opt.h"
2 #include "tao/ORB_Core.h"
3 #include "tao/RTCORBA/Thread_Pool.h"
4 #include "tao/RTPortableServer/RTPortableServer.h"
5 #include "tao/Strategies/advanced_resource.h"
6 #include "../check_supported_priorities.cpp"
7 #include "../common_args.cpp"
8 #include "testS.h"
10 static CORBA::ULong stacksize = 0;
11 static CORBA::ULong static_threads = 2;
12 static CORBA::ULong dynamic_threads = 0;
13 static CORBA::Boolean allow_request_buffering = 0;
14 static CORBA::ULong max_buffered_requests = 0;
15 static CORBA::ULong max_request_buffer_size = 0;
16 static CORBA::Boolean allow_borrowing = 0;
18 static int debug = 1;
19 static int ior_file_count = 1;
20 static const ACE_TCHAR *ior_file_base = ACE_TEXT ("ior");
21 static const ACE_TCHAR *bands_file = ACE_TEXT ("empty_file");
22 static const ACE_TCHAR *lanes_file = ACE_TEXT ("empty_file");
24 class test_i :
25 public POA_test
27 public:
28 test_i (CORBA::ORB_ptr orb,
29 PortableServer::POA_ptr poa);
31 void method ();
33 //FUZZ: disable check_for_lack_ACE_OS
34 void shutdown ();
35 //FUZZ: enable check_for_lack_ACE_OS
37 PortableServer::POA_ptr _default_POA ();
39 private:
40 CORBA::ORB_var orb_;
41 PortableServer::POA_var poa_;
44 test_i::test_i (CORBA::ORB_ptr orb,
45 PortableServer::POA_ptr poa)
46 : orb_ (CORBA::ORB::_duplicate (orb)),
47 poa_ (PortableServer::POA::_duplicate (poa))
51 void
52 test_i::method ()
54 // Get the ORB_Core's TSS resources.
55 TAO_ORB_Core_TSS_Resources *tss =
56 this->orb_->orb_core ()->get_tss_resources ();
58 /// Get the lane attribute in TSS.
59 TAO_Thread_Lane *lane =
60 (TAO_Thread_Lane *) tss->lane_;
62 if (debug)
64 if (lane)
65 ACE_DEBUG ((LM_DEBUG,
66 "test_i::method request in thread %t (pool id = %d; lane id = %d)\n",
67 lane->pool ().id (),
68 lane->id ()));
69 else
70 ACE_DEBUG ((LM_DEBUG,
71 "test_i::method request in thread %t (default thread pool)\n"));
75 void
76 test_i::shutdown ()
78 if (debug)
79 ACE_DEBUG ((LM_DEBUG,
80 "test_i::shutdown\n"));
82 this->orb_->shutdown (false);
85 PortableServer::POA_ptr
86 test_i::_default_POA ()
88 return PortableServer::POA::_duplicate (this->poa_.in ());
91 static int
92 parse_args (int argc, ACE_TCHAR **argv)
94 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("b:d:l:o:"));
95 int c;
97 while ((c = get_opts ()) != -1)
98 switch (c)
100 case 'b':
101 bands_file = get_opts.opt_arg ();
102 break;
104 case 'd':
105 debug = ACE_OS::atoi (get_opts.opt_arg ());
106 break;
108 case 'l':
109 lanes_file = get_opts.opt_arg ();
110 break;
112 case 'o':
113 ior_file_base = get_opts.opt_arg ();
114 break;
116 case '?':
117 default:
118 ACE_ERROR_RETURN ((LM_ERROR,
119 "%s usage:\n"
120 "\t-b <bands file> (defaults to %s)\n"
121 "\t-b <lanes file> (defaults to %s)\n"
122 "\t-o <ior file base> (defaults to %s)\n"
123 "\n",
124 argv[0],
125 bands_file,
126 lanes_file,
127 ior_file_base),
128 -1);
131 return 0;
134 static void
135 write_iors_to_file (CORBA::Object_ptr object,
136 CORBA::ORB_ptr orb)
138 char filename[BUFSIZ];
139 ACE_OS::sprintf (filename,
140 "%s_%d",
141 ACE_TEXT_ALWAYS_CHAR (ior_file_base),
142 ior_file_count);
143 ior_file_count++;
145 FILE *file =
146 ACE_OS::fopen (filename, "w");
147 ACE_ASSERT (file != 0);
149 CORBA::String_var ior =
150 orb->object_to_string (object);
152 u_int result = 0;
153 result =
154 ACE_OS::fprintf (file,
155 "%s",
156 ior.in ());
158 ACE_ASSERT (result == ACE_OS::strlen (ior.in ()));
159 ACE_UNUSED_ARG (result);
161 ACE_OS::fclose (file);
164 void
165 vanilla_poa (CORBA::ORB_ptr orb,
166 PortableServer::POA_ptr root_poa,
167 PortableServer::POAManager_ptr poa_manager)
169 CORBA::PolicyList policies;
171 CORBA::Policy_var implicit_activation_policy =
172 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
174 policies.length (policies.length () + 1);
175 policies[policies.length () - 1] =
176 implicit_activation_policy;
178 PortableServer::POA_var poa =
179 root_poa->create_POA ("child",
180 poa_manager,
181 policies);
183 test_i *servant = 0;
184 ACE_NEW_THROW_EX (servant,
185 test_i (orb,
186 poa.in ()),
187 CORBA::NO_MEMORY ());
189 PortableServer::ServantBase_var safe_servant (servant);
191 PortableServer::ObjectId_var id_act =
192 poa->activate_object (servant);
194 CORBA::Object_var object = poa->id_to_reference (id_act.in ());
196 test_var test =
197 test::_narrow (object.in ());
199 write_iors_to_file (test.in (),
200 orb);
203 void
204 rt_poa (CORBA::ORB_ptr orb,
205 RTCORBA::RTORB_ptr rt_orb,
206 PortableServer::POA_ptr root_poa,
207 PortableServer::POAManager_ptr poa_manager)
209 CORBA::PolicyList policies;
211 int result =
212 get_priority_bands ("server",
213 bands_file,
214 rt_orb,
215 policies,
216 debug);
217 if (result != 0)
219 ACE_ERROR ((LM_ERROR,
220 "Error in parsing bands data file: %s\n",
221 bands_file));
222 return;
225 result =
226 get_priority_lanes ("server",
227 lanes_file,
228 rt_orb,
229 stacksize,
230 static_threads,
231 dynamic_threads,
232 allow_request_buffering,
233 max_buffered_requests,
234 max_request_buffer_size,
235 allow_borrowing,
236 policies,
237 debug);
238 if (result != 0)
240 ACE_ERROR ((LM_ERROR,
241 "Error in parsing lanes data file: %s\n",
242 lanes_file));
243 return;
246 CORBA::Policy_var priority_model_policy =
247 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
250 // Implicit_activation policy.
251 CORBA::Policy_var implicit_activation_policy =
252 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
254 policies.length (policies.length () + 1);
255 policies[policies.length () - 1] =
256 priority_model_policy;
258 policies.length (policies.length () + 1);
259 policies[policies.length () - 1] =
260 implicit_activation_policy;
262 PortableServer::POA_var poa =
263 root_poa->create_POA ("rt_poa",
264 poa_manager,
265 policies);
267 test_i *servant = 0;
268 ACE_NEW_THROW_EX (servant,
269 test_i (orb,
270 poa.in ()),
271 CORBA::NO_MEMORY ());
273 PortableServer::ServantBase_var safe_servant (servant);
275 PortableServer::ObjectId_var id_act =
276 poa->activate_object (servant);
278 CORBA::Object_var object = poa->id_to_reference (id_act.in ());
280 test_var test =
281 test::_narrow (object.in ());
283 write_iors_to_file (test.in (),
284 orb);
287 class Task : public ACE_Task_Base
289 public:
290 Task (ACE_Thread_Manager &thread_manager,
291 CORBA::ORB_ptr orb);
293 int svc ();
295 CORBA::ORB_var orb_;
298 Task::Task (ACE_Thread_Manager &thread_manager,
299 CORBA::ORB_ptr orb)
300 : ACE_Task_Base (&thread_manager),
301 orb_ (CORBA::ORB::_duplicate (orb))
306 Task::svc ()
310 CORBA::Object_var object =
311 this->orb_->resolve_initial_references ("RTORB");
313 RTCORBA::RTORB_var rt_orb =
314 RTCORBA::RTORB::_narrow (object.in ());
316 object =
317 this->orb_->resolve_initial_references ("RootPOA");
319 PortableServer::POA_var root_poa =
320 PortableServer::POA::_narrow (object.in ());
322 PortableServer::POAManager_var poa_manager =
323 root_poa->the_POAManager ();
325 vanilla_poa (this->orb_.in (),
326 root_poa.in (),
327 poa_manager.in ());
329 rt_poa (this->orb_.in (),
330 rt_orb.in (),
331 root_poa.in (),
332 poa_manager.in ());
334 poa_manager->activate ();
336 this->orb_->run ();
338 this->orb_->destroy ();
340 catch (const CORBA::Exception& ex)
342 ex._tao_print_exception ("Exception caught");
343 return -1;
346 return 0;
350 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
354 CORBA::ORB_var orb =
355 CORBA::ORB_init (argc, argv);
357 int result =
358 parse_args (argc, argv);
359 if (result != 0)
360 return result;
362 // Make sure we can support multiple priorities that are required
363 // for this test.
364 if (!check_supported_priorities (orb.in ()))
365 return 2;
367 // Thread Manager for managing task.
368 ACE_Thread_Manager thread_manager;
370 // Create task.
371 Task task (thread_manager,
372 orb.in ());
374 // Task activation flags.
375 long flags =
376 THR_NEW_LWP |
377 THR_JOINABLE |
378 orb->orb_core ()->orb_params ()->thread_creation_flags ();
380 // Activate task.
381 result =
382 task.activate (flags);
383 if (result == -1)
385 if (errno == EPERM)
387 ACE_ERROR_RETURN ((LM_ERROR,
388 "Cannot create thread with scheduling policy %C\n"
389 "because the user does not have the appropriate privileges, terminating program....\n"
390 "Check svc.conf options and/or run as root\n",
391 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
394 else
395 // Unexpected error.
396 ACE_ASSERT (0);
399 // Wait for task to exit.
400 result =
401 thread_manager.wait ();
402 ACE_ASSERT (result != -1);
404 catch (const CORBA::Exception& ex)
406 ex._tao_print_exception ("Exception caught");
407 return -1;
410 return 0;