Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Server_Declared / server.cpp
blobb35389e6690c41927088c1606f9913bec81b1b4f
1 #include "testS.h"
2 #include "ace/Get_Opt.h"
3 #include "tao/ORB_Core.h"
4 #include "ace/Task.h"
5 #include "tao/RTCORBA/RTCORBA.h"
6 #include "tao/RTPortableServer/RTPortableServer.h"
7 #include "../check_supported_priorities.cpp"
9 class Test_i : public POA_Test
11 // = TITLE
12 // An implementation for the Test interface in test.idl
14 public:
15 Test_i (CORBA::ORB_ptr orb);
16 // ctor
18 void test_method (CORBA::Short priority);
20 //FUZZ: disable check_for_lack_ACE_OS
21 void shutdown (void);
22 //FUZZ: enable check_for_lack_ACE_OS
24 private:
25 CORBA::ORB_var orb_;
26 // The ORB
29 Test_i::Test_i (CORBA::ORB_ptr orb)
30 : orb_ (CORBA::ORB::_duplicate (orb))
34 void
35 Test_i::test_method (CORBA::Short priority)
37 // Use RTCurrent to find out the CORBA priority of the current
38 // thread.
40 CORBA::Object_var obj =
41 this->orb_->resolve_initial_references ("RTCurrent");
43 RTCORBA::Current_var current =
44 RTCORBA::Current::_narrow (obj.in ());
46 if (CORBA::is_nil (obj.in ()))
47 throw CORBA::INTERNAL ();
49 CORBA::Short servant_thread_priority =
50 current->the_priority ();
52 // Print out the info.
53 if (servant_thread_priority != priority)
54 ACE_DEBUG ((LM_DEBUG,
55 "ERROR: servant thread priority is not equal"
56 "to method argument.\n"));
58 ACE_DEBUG ((LM_DEBUG,
59 "Server_Declared priority: %d "
60 "Servant thread priority: %d\n",
61 priority, servant_thread_priority));
64 void
65 Test_i::shutdown (void)
67 this->orb_->shutdown (0);
70 //*************************************************************************
72 const ACE_TCHAR *ior_output_file1 = ACE_TEXT("test1.ior");
73 const ACE_TCHAR *ior_output_file2 = ACE_TEXT("test2.ior");
74 CORBA::Short poa_priority = -1;
75 CORBA::Short object_priority = -1;
77 // Parse command-line arguments.
78 int
79 parse_args (int argc, ACE_TCHAR *argv[])
81 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:o:a:b:"));
82 int c, result;
84 while ((c = get_opts ()) != -1)
85 switch (c)
87 case 'p':
88 ior_output_file1 = get_opts.opt_arg ();
89 break;
91 case 'o':
92 ior_output_file2 = get_opts.opt_arg ();
93 break;
95 case 'a':
96 result = ::sscanf (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()),
97 "%hd",
98 &poa_priority);
99 if (result == 0 || result == EOF)
100 ACE_ERROR_RETURN ((LM_ERROR,
101 "Unable to process <-a> option"),
102 -1);
103 break;
105 case 'b':
106 result = ::sscanf (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()),
107 "%hd",
108 &object_priority);
109 if (result == 0 || result == EOF)
110 ACE_ERROR_RETURN ((LM_ERROR,
111 "Unable to process <-b> option"),
112 -1);
113 break;
115 case '?':
116 default:
117 ACE_ERROR_RETURN ((LM_ERROR,
118 "usage: %s "
119 "-p <iorfile1> "
120 "-o <iorfile2> "
121 "-a <poa_priority> "
122 "-b <object_priority> "
123 "\n",
124 argv [0]),
125 -1);
128 if (poa_priority < 0
129 || object_priority < 0)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "Valid poa and object priorities must be"
132 " specified.\nSee README file for more info\n"),
133 -1);
135 return 0;
139 check_for_nil (CORBA::Object_ptr obj, const char *msg)
141 if (CORBA::is_nil (obj))
142 ACE_ERROR_RETURN ((LM_ERROR,
143 "ERROR: Object reference <%C> is nil\n",
144 msg),
145 -1);
146 else
147 return 0;
151 create_object (RTPortableServer::POA_ptr poa,
152 CORBA::ORB_ptr orb,
153 Test_i *server_impl,
154 CORBA::Short priority,
155 const ACE_TCHAR *filename)
157 // Register with poa.
158 PortableServer::ObjectId_var id;
160 if (priority > -1)
161 id = poa->activate_object_with_priority (server_impl,
162 priority);
163 else
164 id = poa->activate_object (server_impl);
167 CORBA::Object_var server =
168 poa->id_to_reference (id.in ());
170 // Print out the IOR.
171 CORBA::String_var ior =
172 orb->object_to_string (server.in ());
174 ACE_DEBUG ((LM_DEBUG, "<%C>\n\n", ior.in ()));
176 // Print ior to the file.
177 if (filename != 0)
179 FILE *output_file= ACE_OS::fopen (filename, "w");
180 if (output_file == 0)
181 ACE_ERROR_RETURN ((LM_ERROR,
182 "Cannot open output file for writing IOR: %s",
183 filename),
184 -1);
185 ACE_OS::fprintf (output_file, "%s", ior.in ());
186 ACE_OS::fclose (output_file);
189 return 0;
192 class Task : public ACE_Task_Base
194 public:
196 Task (ACE_Thread_Manager &thread_manager,
197 CORBA::ORB_ptr orb);
199 int svc (void);
201 CORBA::ORB_var orb_;
205 Task::Task (ACE_Thread_Manager &thread_manager,
206 CORBA::ORB_ptr orb)
207 : ACE_Task_Base (&thread_manager),
208 orb_ (CORBA::ORB::_duplicate (orb))
213 Task::svc (void)
217 // RTORB.
218 CORBA::Object_var object =
219 this->orb_->resolve_initial_references ("RTORB");
220 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
221 if (check_for_nil (rt_orb.in (), "RTORB") == -1)
222 return -1;
224 // RootPOA.
225 object =
226 this->orb_->resolve_initial_references("RootPOA");
227 PortableServer::POA_var root_poa =
228 PortableServer::POA::_narrow (object.in ());
229 if (check_for_nil (root_poa.in (), "RootPOA") == -1)
230 return -1;
232 // POAManager.
233 PortableServer::POAManager_var poa_manager =
234 root_poa->the_POAManager ();
236 // Create child POA with SERVER_DECLARED PriorityModelPolicy,
237 // and MULTIPLE_ID id uniqueness policy (so we can use one
238 // servant to create several objects).
239 CORBA::PolicyList poa_policy_list;
240 poa_policy_list.length (2);
241 poa_policy_list[0] =
242 rt_orb->create_priority_model_policy (RTCORBA::SERVER_DECLARED,
243 poa_priority);
245 poa_policy_list[1] =
246 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
248 PortableServer::POA_var child_poa =
249 root_poa->create_POA ("Child_POA",
250 poa_manager.in (),
251 poa_policy_list);
253 RTPortableServer::POA_var rt_poa =
254 RTPortableServer::POA::_narrow (child_poa.in ());
255 if (check_for_nil (rt_poa.in (), "RTPOA") == -1)
256 return -1;
258 // Servant.
259 Test_i server_impl (this->orb_.in ());
261 // Create object 1 (it will inherit POA's priority).
262 int result;
263 ACE_DEBUG ((LM_DEBUG, "\nActivated object one as "));
264 result = create_object (rt_poa.in (), this->orb_.in (), &server_impl,
265 -1, ior_output_file1);
266 if (result == -1)
267 return -1;
269 // Create object 2 (override POA's priority).
270 ACE_DEBUG ((LM_DEBUG, "\nActivated object two as "));
271 result = create_object (rt_poa.in (), this->orb_.in (), &server_impl,
272 object_priority, ior_output_file2);
273 if (result == -1)
274 return -1;
276 // Activate POA manager.
277 poa_manager->activate ();
279 // Start ORB event loop.
280 this->orb_->run ();
282 ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n\n"));
284 catch (const CORBA::Exception& ex)
286 ex._tao_print_exception (
287 "Unexpected exception caught in Server_Declared test server:");
288 return -1;
291 return 0;
295 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
299 // ORB.
300 CORBA::ORB_var orb =
301 CORBA::ORB_init (argc, argv);
303 // Parse arguments.
304 if (parse_args (argc, argv) != 0)
305 return -1;
307 // Make sure we can support multiple priorities that are required
308 // for this test.
309 if (!check_supported_priorities (orb.in ()))
310 return 2;
312 // Thread Manager for managing task.
313 ACE_Thread_Manager thread_manager;
315 // Create task.
316 Task task (thread_manager,
317 orb.in ());
319 // Task activation flags.
320 long flags =
321 THR_NEW_LWP |
322 THR_JOINABLE |
323 orb->orb_core ()->orb_params ()->thread_creation_flags ();
325 // Activate task.
326 int result =
327 task.activate (flags);
328 if (result == -1)
330 if (errno == EPERM)
332 ACE_ERROR_RETURN ((LM_ERROR,
333 "Cannot create thread with scheduling policy %s\n"
334 "because the user does not have the appropriate privileges, terminating program....\n"
335 "Check svc.conf options and/or run as root\n",
336 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
339 else
340 // Unexpected error.
341 ACE_ASSERT (0);
344 // Wait for task to exit.
345 result =
346 thread_manager.wait ();
347 ACE_ASSERT (result != -1);
349 catch (const CORBA::Exception& ex)
351 ex._tao_print_exception ("Exception caught");
352 return -1;
355 return 0;