ACE+TAO-7_0_8
[ACE_TAO.git] / TAO / tests / RTCORBA / Profile_And_Endpoint_Selection / server.cpp
blob644a7c9ea5b7381d9f219bb0c5757fd3e38df899
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 (void);
33 //FUZZ: disable check_for_lack_ACE_OS
34 void shutdown (void);
35 //FUZZ: enable check_for_lack_ACE_OS
37 PortableServer::POA_ptr _default_POA (void);
39 private:
41 CORBA::ORB_var orb_;
42 PortableServer::POA_var poa_;
45 test_i::test_i (CORBA::ORB_ptr orb,
46 PortableServer::POA_ptr poa)
47 : orb_ (CORBA::ORB::_duplicate (orb)),
48 poa_ (PortableServer::POA::_duplicate (poa))
52 void
53 test_i::method (void)
55 // Get the ORB_Core's TSS resources.
56 TAO_ORB_Core_TSS_Resources *tss =
57 this->orb_->orb_core ()->get_tss_resources ();
59 /// Get the lane attribute in TSS.
60 TAO_Thread_Lane *lane =
61 (TAO_Thread_Lane *) tss->lane_;
63 if (debug)
65 if (lane)
66 ACE_DEBUG ((LM_DEBUG,
67 "test_i::method request in thread %t (pool id = %d; lane id = %d)\n",
68 lane->pool ().id (),
69 lane->id ()));
70 else
71 ACE_DEBUG ((LM_DEBUG,
72 "test_i::method request in thread %t (default thread pool)\n"));
76 void
77 test_i::shutdown (void)
79 if (debug)
80 ACE_DEBUG ((LM_DEBUG,
81 "test_i::shutdown\n"));
83 this->orb_->shutdown (false);
86 PortableServer::POA_ptr
87 test_i::_default_POA (void)
89 return PortableServer::POA::_duplicate (this->poa_.in ());
92 static int
93 parse_args (int argc, ACE_TCHAR **argv)
95 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("b:d:l:o:"));
96 int c;
98 while ((c = get_opts ()) != -1)
99 switch (c)
101 case 'b':
102 bands_file = get_opts.opt_arg ();
103 break;
105 case 'd':
106 debug = ACE_OS::atoi (get_opts.opt_arg ());
107 break;
109 case 'l':
110 lanes_file = get_opts.opt_arg ();
111 break;
113 case 'o':
114 ior_file_base = get_opts.opt_arg ();
115 break;
117 case '?':
118 default:
119 ACE_ERROR_RETURN ((LM_ERROR,
120 "%s usage:\n"
121 "\t-b <bands file> (defaults to %s)\n"
122 "\t-b <lanes file> (defaults to %s)\n"
123 "\t-o <ior file base> (defaults to %s)\n"
124 "\n",
125 argv[0],
126 bands_file,
127 lanes_file,
128 ior_file_base),
129 -1);
132 return 0;
135 static void
136 write_iors_to_file (CORBA::Object_ptr object,
137 CORBA::ORB_ptr orb)
139 char filename[BUFSIZ];
140 ACE_OS::sprintf (filename,
141 "%s_%d",
142 ACE_TEXT_ALWAYS_CHAR (ior_file_base),
143 ior_file_count);
144 ior_file_count++;
146 FILE *file =
147 ACE_OS::fopen (filename, "w");
148 ACE_ASSERT (file != 0);
150 CORBA::String_var ior =
151 orb->object_to_string (object);
153 u_int result = 0;
154 result =
155 ACE_OS::fprintf (file,
156 "%s",
157 ior.in ());
159 ACE_ASSERT (result == ACE_OS::strlen (ior.in ()));
160 ACE_UNUSED_ARG (result);
162 ACE_OS::fclose (file);
165 void
166 vanilla_poa (CORBA::ORB_ptr orb,
167 PortableServer::POA_ptr root_poa,
168 PortableServer::POAManager_ptr poa_manager)
170 CORBA::PolicyList policies;
172 CORBA::Policy_var implicit_activation_policy =
173 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
175 policies.length (policies.length () + 1);
176 policies[policies.length () - 1] =
177 implicit_activation_policy;
179 PortableServer::POA_var poa =
180 root_poa->create_POA ("child",
181 poa_manager,
182 policies);
184 test_i *servant = 0;
185 ACE_NEW_THROW_EX (servant,
186 test_i (orb,
187 poa.in ()),
188 CORBA::NO_MEMORY ());
190 PortableServer::ServantBase_var safe_servant (servant);
192 PortableServer::ObjectId_var id_act =
193 poa->activate_object (servant);
195 CORBA::Object_var object = poa->id_to_reference (id_act.in ());
197 test_var test =
198 test::_narrow (object.in ());
200 write_iors_to_file (test.in (),
201 orb);
204 void
205 rt_poa (CORBA::ORB_ptr orb,
206 RTCORBA::RTORB_ptr rt_orb,
207 PortableServer::POA_ptr root_poa,
208 PortableServer::POAManager_ptr poa_manager)
210 CORBA::PolicyList policies;
212 int result =
213 get_priority_bands ("server",
214 bands_file,
215 rt_orb,
216 policies,
217 debug);
218 if (result != 0)
220 ACE_ERROR ((LM_ERROR,
221 "Error in parsing bands data file: %s\n",
222 bands_file));
223 return;
226 result =
227 get_priority_lanes ("server",
228 lanes_file,
229 rt_orb,
230 stacksize,
231 static_threads,
232 dynamic_threads,
233 allow_request_buffering,
234 max_buffered_requests,
235 max_request_buffer_size,
236 allow_borrowing,
237 policies,
238 debug);
239 if (result != 0)
241 ACE_ERROR ((LM_ERROR,
242 "Error in parsing lanes data file: %s\n",
243 lanes_file));
244 return;
247 CORBA::Policy_var priority_model_policy =
248 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
251 // Implicit_activation policy.
252 CORBA::Policy_var implicit_activation_policy =
253 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
255 policies.length (policies.length () + 1);
256 policies[policies.length () - 1] =
257 priority_model_policy;
259 policies.length (policies.length () + 1);
260 policies[policies.length () - 1] =
261 implicit_activation_policy;
263 PortableServer::POA_var poa =
264 root_poa->create_POA ("rt_poa",
265 poa_manager,
266 policies);
268 test_i *servant = 0;
269 ACE_NEW_THROW_EX (servant,
270 test_i (orb,
271 poa.in ()),
272 CORBA::NO_MEMORY ());
274 PortableServer::ServantBase_var safe_servant (servant);
276 PortableServer::ObjectId_var id_act =
277 poa->activate_object (servant);
279 CORBA::Object_var object = poa->id_to_reference (id_act.in ());
281 test_var test =
282 test::_narrow (object.in ());
284 write_iors_to_file (test.in (),
285 orb);
288 class Task : public ACE_Task_Base
290 public:
292 Task (ACE_Thread_Manager &thread_manager,
293 CORBA::ORB_ptr orb);
295 int svc (void);
297 CORBA::ORB_var orb_;
301 Task::Task (ACE_Thread_Manager &thread_manager,
302 CORBA::ORB_ptr orb)
303 : ACE_Task_Base (&thread_manager),
304 orb_ (CORBA::ORB::_duplicate (orb))
309 Task::svc (void)
313 CORBA::Object_var object =
314 this->orb_->resolve_initial_references ("RTORB");
316 RTCORBA::RTORB_var rt_orb =
317 RTCORBA::RTORB::_narrow (object.in ());
319 object =
320 this->orb_->resolve_initial_references ("RootPOA");
322 PortableServer::POA_var root_poa =
323 PortableServer::POA::_narrow (object.in ());
325 PortableServer::POAManager_var poa_manager =
326 root_poa->the_POAManager ();
328 vanilla_poa (this->orb_.in (),
329 root_poa.in (),
330 poa_manager.in ());
332 rt_poa (this->orb_.in (),
333 rt_orb.in (),
334 root_poa.in (),
335 poa_manager.in ());
337 poa_manager->activate ();
339 this->orb_->run ();
341 this->orb_->destroy ();
343 catch (const CORBA::Exception& ex)
345 ex._tao_print_exception ("Exception caught");
346 return -1;
349 return 0;
353 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
357 CORBA::ORB_var orb =
358 CORBA::ORB_init (argc, argv);
360 int result =
361 parse_args (argc, argv);
362 if (result != 0)
363 return result;
365 // Make sure we can support multiple priorities that are required
366 // for this test.
367 if (!check_supported_priorities (orb.in ()))
368 return 2;
370 // Thread Manager for managing task.
371 ACE_Thread_Manager thread_manager;
373 // Create task.
374 Task task (thread_manager,
375 orb.in ());
377 // Task activation flags.
378 long flags =
379 THR_NEW_LWP |
380 THR_JOINABLE |
381 orb->orb_core ()->orb_params ()->thread_creation_flags ();
383 // Activate task.
384 result =
385 task.activate (flags);
386 if (result == -1)
388 if (errno == EPERM)
390 ACE_ERROR_RETURN ((LM_ERROR,
391 "Cannot create thread with scheduling policy %C\n"
392 "because the user does not have the appropriate privileges, terminating program....\n"
393 "Check svc.conf options and/or run as root\n",
394 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
397 else
398 // Unexpected error.
399 ACE_ASSERT (0);
402 // Wait for task to exit.
403 result =
404 thread_manager.wait ();
405 ACE_ASSERT (result != -1);
407 catch (const CORBA::Exception& ex)
409 ex._tao_print_exception ("Exception caught");
410 return -1;
413 return 0;