Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Linear_Priority / server.cpp
blob65afe698d1f13b2de132af196e53f314a2673a6e
1 #include "ace/Get_Opt.h"
2 #include "ace/Task.h"
3 #include "tao/ORB_Core.h"
4 #include "testS.h"
5 #include "tao/RTPortableServer/RTPortableServer.h"
6 #include "../check_supported_priorities.cpp"
7 #include "../common_args.cpp"
9 class test_i :
10 public POA_test
12 public:
13 test_i (CORBA::ORB_ptr orb,
14 PortableServer::POA_ptr poa);
16 void method ();
18 //FUZZ: disable check_for_lack_ACE_OS
19 void shutdown ();
20 //FUZZ: enable check_for_lack_ACE_OS
22 PortableServer::POA_ptr _default_POA ();
24 private:
25 CORBA::ORB_var orb_;
26 PortableServer::POA_var poa_;
29 test_i::test_i (CORBA::ORB_ptr orb,
30 PortableServer::POA_ptr poa)
31 : orb_ (CORBA::ORB::_duplicate (orb)),
32 poa_ (PortableServer::POA::_duplicate (poa))
36 void
37 test_i::method ()
39 ACE_DEBUG ((LM_DEBUG,
40 "test_i::method\n"));
43 void
44 test_i::shutdown ()
46 ACE_DEBUG ((LM_DEBUG,
47 "test_i::shutdown\n"));
49 this->orb_->shutdown (false);
52 PortableServer::POA_ptr
53 test_i::_default_POA ()
55 return PortableServer::POA::_duplicate (this->poa_.in ());
58 static CORBA::ULong stacksize = 0;
59 static CORBA::ULong static_threads = 2;
60 static CORBA::ULong dynamic_threads = 0;
61 static CORBA::Boolean allow_request_buffering = 0;
62 static CORBA::ULong max_buffered_requests = 0;
63 static CORBA::ULong max_request_buffer_size = 0;
64 static CORBA::Boolean allow_borrowing = 0;
66 static const ACE_TCHAR *ior = ACE_TEXT("ior");
67 static int debug = 1;
69 static const ACE_TCHAR *bands_file = ACE_TEXT ("bands");
70 static const ACE_TCHAR *lanes_file = ACE_TEXT ("lanes");
72 static int
73 parse_args (int argc, ACE_TCHAR **argv)
75 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:b:d:l:"));
76 int c;
78 while ((c = get_opts ()) != -1)
79 switch (c)
81 case 'o':
82 ior = get_opts.opt_arg ();
83 break;
85 case 'b':
86 bands_file = get_opts.opt_arg ();
87 break;
89 case 'd':
90 debug = ACE_OS::atoi (get_opts.opt_arg ());
91 break;
93 case 'l':
94 lanes_file = get_opts.opt_arg ();
95 break;
97 case '?':
98 default:
99 ACE_ERROR_RETURN ((LM_ERROR,
100 "usage: %s "
101 "-b <bands_file> "
102 "-d <debug> "
103 "-l <lanes_file> "
104 "-o <ior file> "
105 "\n",
106 argv [0]),
107 -1);
110 return 0;
113 static void
114 write_iors_to_file (CORBA::Object_ptr object,
115 CORBA::ORB_ptr orb,
116 const ACE_TCHAR *filename)
118 FILE *file =
119 ACE_OS::fopen (filename, "w");
120 ACE_ASSERT (file != 0);
122 CORBA::String_var ior =
123 orb->object_to_string (object);
125 u_int result = 0;
126 result =
127 ACE_OS::fprintf (file,
128 "%s",
129 ior.in ());
131 ACE_ASSERT (result == ACE_OS::strlen (ior.in ()));
132 ACE_UNUSED_ARG (result);
134 ACE_OS::fclose (file);
137 class Task : public ACE_Task_Base
139 public:
140 Task (ACE_Thread_Manager &thread_manager,
141 CORBA::ORB_ptr orb);
143 int svc ();
145 CORBA::ORB_var orb_;
148 Task::Task (ACE_Thread_Manager &thread_manager,
149 CORBA::ORB_ptr orb)
150 : ACE_Task_Base (&thread_manager),
151 orb_ (CORBA::ORB::_duplicate (orb))
156 Task::svc ()
160 CORBA::Object_var object =
161 this->orb_->resolve_initial_references ("RTORB");
163 RTCORBA::RTORB_var rt_orb =
164 RTCORBA::RTORB::_narrow (object.in ());
166 object =
167 this->orb_->resolve_initial_references ("RootPOA");
169 PortableServer::POA_var root_poa =
170 PortableServer::POA::_narrow (object.in ());
172 PortableServer::POAManager_var poa_manager =
173 root_poa->the_POAManager ();
175 CORBA::PolicyList policies;
177 int result =
178 get_priority_bands ("server",
179 bands_file,
180 rt_orb.in (),
181 policies,
182 debug);
183 if (result != 0)
184 return result;
186 result =
187 get_priority_lanes ("server",
188 lanes_file,
189 rt_orb.in (),
190 stacksize,
191 static_threads,
192 dynamic_threads,
193 allow_request_buffering,
194 max_buffered_requests,
195 max_request_buffer_size,
196 allow_borrowing,
197 policies,
198 debug);
199 if (result != 0)
200 return result;
202 CORBA::Policy_var priority_model_policy =
203 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
206 // Implicit_activation policy.
207 CORBA::Policy_var implicit_activation_policy =
208 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
210 policies.length (policies.length () + 1);
211 policies[policies.length () - 1] =
212 priority_model_policy;
214 policies.length (policies.length () + 1);
215 policies[policies.length () - 1] =
216 implicit_activation_policy;
218 PortableServer::POA_var poa =
219 root_poa->create_POA ("child",
220 poa_manager.in (),
221 policies);
223 test_i *servant = 0;
224 ACE_NEW_THROW_EX (servant,
225 test_i (this->orb_.in (),
226 poa.in ()),
227 CORBA::NO_MEMORY ());
229 PortableServer::ServantBase_var safe_servant (servant);
231 PortableServer::ObjectId_var id =
232 poa->activate_object (servant);
234 CORBA::Object_var object_act = poa->id_to_reference (id.in ());
236 test_var test =
237 test::_narrow (object_act.in ());
239 write_iors_to_file (test.in (),
240 this->orb_.in (),
241 ior);
243 poa_manager->activate ();
245 this->orb_->run ();
247 this->orb_->destroy ();
249 catch (const CORBA::Exception& ex)
251 ex._tao_print_exception ("Exception caught");
252 return -1;
255 return 0;
259 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
263 CORBA::ORB_var orb =
264 CORBA::ORB_init (argc, argv);
266 int result =
267 parse_args (argc, argv);
268 if (result != 0)
269 return result;
271 // Make sure we can support multiple priorities that are required
272 // for this test.
273 if (!check_supported_priorities (orb.in ()))
274 return 2;
276 // Thread Manager for managing task.
277 ACE_Thread_Manager thread_manager;
279 // Create task.
280 Task task (thread_manager,
281 orb.in ());
283 // Task activation flags.
284 long flags =
285 THR_NEW_LWP |
286 THR_JOINABLE |
287 orb->orb_core ()->orb_params ()->thread_creation_flags ();
289 // Activate task.
290 result =
291 task.activate (flags);
292 if (result == -1)
294 if (errno == EPERM)
296 ACE_ERROR_RETURN ((LM_ERROR,
297 "Cannot create thread with scheduling policy %C\n"
298 "because the user does not have the appropriate privileges, terminating program....\n"
299 "Check svc.conf options and/or run as root\n",
300 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
303 else
304 // Unexpected error.
305 ACE_ASSERT (0);
308 // Wait for task to exit.
309 result =
310 thread_manager.wait ();
311 ACE_ASSERT (result != -1);
313 catch (const CORBA::Exception& ex)
315 ex._tao_print_exception ("Exception caught");
316 return -1;
319 return 0;