Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3643_Regression / server.cpp
blob3d347af46b3799c31d3ed10e7d41f626b4a4551b
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_unistd.h"
4 #include "tao/ORB_Core.h"
5 #include "ace/Task.h"
6 #include "tao/RTPortableServer/RTPortableServer.h"
7 #include "../check_supported_priorities.cpp"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
10 int ior_count = 1;
11 CORBA::ULong static_threads = 1;
12 CORBA::ULong dynamic_threads = 25;
13 long nap_time = 1000;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:d:t:h"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'o':
25 ior_output_file = get_opts.opt_arg ();
26 break;
28 case 's':
29 static_threads = ACE_OS::atoi (get_opts.opt_arg ());
30 break;
32 case 'd':
33 dynamic_threads = ACE_OS::atoi (get_opts.opt_arg ());
34 break;
36 case 't':
37 nap_time = ACE_OS::atoi (get_opts.opt_arg ());
38 break;
40 case 'h':
41 ACE_ERROR_RETURN ((LM_ERROR,
42 "usage: %s "
43 "-o <iorfile> "
44 "-s <static_threads> "
45 "-d <dynamic_threads> "
46 "-t <nap_time> "
47 "\n",
48 argv [0]),
49 -1);
52 // Indicates successful parsing of the command line
53 return 0;
56 int
57 write_ior_to_file (CORBA::ORB_ptr orb,
58 test_ptr test)
60 CORBA::String_var ior =
61 orb->object_to_string (test);
63 char filename[BUFSIZ];
64 ACE_OS::sprintf (filename,
65 "%s_%d",
66 ACE_TEXT_ALWAYS_CHAR (ior_output_file),
67 ior_count++);
69 FILE *output_file =
70 ACE_OS::fopen (filename,
71 "w");
73 if (output_file == 0)
74 ACE_ERROR_RETURN ((LM_ERROR,
75 "Cannot open output file for writing IOR: %s",
76 filename),
77 -1);
79 ACE_OS::fprintf (output_file,
80 "%s",
81 ior.in ());
83 ACE_OS::fclose (output_file);
85 return 0;
88 int
89 create_POA_and_register_servant (CORBA::Policy_ptr threadpool_policy,
90 const char *poa_name,
91 PortableServer::POAManager_ptr poa_manager,
92 PortableServer::POA_ptr root_poa,
93 CORBA::ORB_ptr orb,
94 RTCORBA::RTORB_ptr rt_orb)
96 // Policies for the firstPOA to be created.
97 CORBA::PolicyList policies (3); policies.length (3);
99 // Implicit_activation policy.
100 policies[0] =
101 root_poa->create_implicit_activation_policy
102 (PortableServer::IMPLICIT_ACTIVATION);
104 // Thread pool policy.
105 policies[1] =
106 CORBA::Policy::_duplicate (threadpool_policy);
108 // Priority Model policy.
109 policies[2] =
110 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
113 // Create the POA under the RootPOA.
114 PortableServer::POA_var poa =
115 root_poa->create_POA (poa_name,
116 poa_manager,
117 policies);
119 // Creation of POAs is over. Destroy the Policy objects.
120 for (CORBA::ULong i = 0;
121 i < policies.length ();
122 ++i)
124 policies[i]->destroy ();
127 test_i *servant =
128 new test_i (orb,
129 poa.in (),
130 nap_time);
132 PortableServer::ServantBase_var safe_servant (servant);
133 ACE_UNUSED_ARG (safe_servant);
135 PortableServer::ObjectId_var id =
136 poa->activate_object (servant);
138 CORBA::Object_var object = poa->id_to_reference (id.in ());
140 test_var test =
141 test::_narrow (object.in ());
143 int result =
144 write_ior_to_file (orb,
145 test.in ());
147 return result;
150 class Task : public ACE_Task_Base
152 public:
153 Task (ACE_Thread_Manager &thread_manager,
154 CORBA::ORB_ptr orb);
156 int svc ();
158 CORBA::ORB_var orb_;
161 Task::Task (ACE_Thread_Manager &thread_manager,
162 CORBA::ORB_ptr orb)
163 : ACE_Task_Base (&thread_manager),
164 orb_ (CORBA::ORB::_duplicate (orb))
169 Task::svc ()
173 CORBA::Object_var object =
174 this->orb_->resolve_initial_references ("RootPOA");
176 PortableServer::POA_var root_poa =
177 PortableServer::POA::_narrow (object.in ());
179 PortableServer::POAManager_var poa_manager =
180 root_poa->the_POAManager ();
182 object =
183 this->orb_->resolve_initial_references ("RTORB");
185 RTCORBA::RTORB_var rt_orb =
186 RTCORBA::RTORB::_narrow (object.in ());
188 object =
189 this->orb_->resolve_initial_references ("RTCurrent");
191 RTCORBA::Current_var current =
192 RTCORBA::Current::_narrow (object.in ());
194 RTCORBA::Priority default_thread_priority =
195 get_implicit_thread_CORBA_priority (this->orb_.in ());
197 test_i servant (this->orb_.in (),
198 root_poa.in (),
199 nap_time);
200 PortableServer::ObjectId_var id =
201 root_poa->activate_object (&servant);
203 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
205 test_var test =
206 test::_narrow (object_act.in ());
208 int result =
209 write_ior_to_file (this->orb_.in (),
210 test.in ());
212 if (result != 0)
213 return result;
215 poa_manager->activate ();
217 CORBA::ULong stacksize = 0;
218 CORBA::Boolean allow_request_buffering = 0;
219 CORBA::ULong max_buffered_requests = 0;
220 CORBA::ULong max_request_buffer_size = 0;
222 RTCORBA::ThreadpoolId threadpool_id_1 =
223 rt_orb->create_threadpool (stacksize,
224 static_threads,
225 dynamic_threads,
226 default_thread_priority,
227 allow_request_buffering,
228 max_buffered_requests,
229 max_request_buffer_size);
231 CORBA::Policy_var threadpool_policy_1 =
232 rt_orb->create_threadpool_policy (threadpool_id_1);
234 CORBA::Boolean allow_borrowing = 0;
235 RTCORBA::ThreadpoolLanes lanes (1);
236 lanes.length (1);
238 lanes[0].lane_priority = default_thread_priority;
239 lanes[0].static_threads = static_threads;
240 lanes[0].dynamic_threads = dynamic_threads;
242 RTCORBA::ThreadpoolId threadpool_id_2 =
243 rt_orb->create_threadpool_with_lanes (stacksize,
244 lanes,
245 allow_borrowing,
246 allow_request_buffering,
247 max_buffered_requests,
248 max_request_buffer_size);
250 CORBA::Policy_var threadpool_policy_2 =
251 rt_orb->create_threadpool_policy (threadpool_id_2);
253 result =
254 create_POA_and_register_servant (threadpool_policy_1.in (),
255 "first_poa",
256 poa_manager.in (),
257 root_poa.in (),
258 this->orb_.in (),
259 rt_orb.in ());
260 if (result != 0)
261 return result;
263 result =
264 create_POA_and_register_servant (threadpool_policy_2.in (),
265 "second_poa",
266 poa_manager.in (),
267 root_poa.in (),
268 this->orb_.in (),
269 rt_orb.in ());
270 if (result != 0)
271 return result;
273 this->orb_->run ();
275 this->orb_->destroy ();
277 catch (const CORBA::Exception& ex)
279 ex._tao_print_exception ("Exception caught:");
280 return -1;
283 return 0;
287 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
291 CORBA::ORB_var orb =
292 CORBA::ORB_init (argc, argv);
294 int result =
295 parse_args (argc, argv);
296 if (result != 0)
297 return result;
299 // Make sure we can support multiple priorities that are required
300 // for this test.
301 if (!check_supported_priorities (orb.in ()))
302 return 2;
304 // Thread Manager for managing task.
305 ACE_Thread_Manager thread_manager;
307 // Create task.
308 Task task (thread_manager,
309 orb.in ());
311 // Task activation flags.
312 long flags =
313 THR_NEW_LWP |
314 THR_JOINABLE |
315 orb->orb_core ()->orb_params ()->thread_creation_flags ();
317 // Activate task.
318 result =
319 task.activate (flags);
320 if (result == -1)
322 if (errno == EPERM)
324 ACE_ERROR_RETURN ((LM_ERROR,
325 "Cannot create thread with scheduling policy %s\n"
326 "because the user does not have the appropriate privileges, terminating program....\n"
327 "Check svc.conf options and/or run as root\n",
328 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
331 else
332 // Unexpected error.
333 ACE_ASSERT (0);
336 // Wait for task to exit.
337 result =
338 thread_manager.wait ();
339 ACE_ASSERT (result != -1);
341 catch (const CORBA::Exception& ex)
343 ex._tao_print_exception ("Exception caught");
344 return -1;
347 return 0;