=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / RTCORBA / Server_Declared / server.cpp
blobe84e8a3b901c3880ed767695aba2518944b4ead9
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 ();
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 ()
67 this->orb_->shutdown (false);
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:
195 Task (ACE_Thread_Manager &thread_manager,
196 CORBA::ORB_ptr orb);
198 int svc ();
200 CORBA::ORB_var orb_;
203 Task::Task (ACE_Thread_Manager &thread_manager,
204 CORBA::ORB_ptr orb)
205 : ACE_Task_Base (&thread_manager),
206 orb_ (CORBA::ORB::_duplicate (orb))
211 Task::svc ()
215 // RTORB.
216 CORBA::Object_var object =
217 this->orb_->resolve_initial_references ("RTORB");
218 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
219 if (check_for_nil (rt_orb.in (), "RTORB") == -1)
220 return -1;
222 // RootPOA.
223 object =
224 this->orb_->resolve_initial_references("RootPOA");
225 PortableServer::POA_var root_poa =
226 PortableServer::POA::_narrow (object.in ());
227 if (check_for_nil (root_poa.in (), "RootPOA") == -1)
228 return -1;
230 // POAManager.
231 PortableServer::POAManager_var poa_manager =
232 root_poa->the_POAManager ();
234 // Create child POA with SERVER_DECLARED PriorityModelPolicy,
235 // and MULTIPLE_ID id uniqueness policy (so we can use one
236 // servant to create several objects).
237 CORBA::PolicyList poa_policy_list;
238 poa_policy_list.length (2);
239 poa_policy_list[0] =
240 rt_orb->create_priority_model_policy (RTCORBA::SERVER_DECLARED,
241 poa_priority);
243 poa_policy_list[1] =
244 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
246 PortableServer::POA_var child_poa =
247 root_poa->create_POA ("Child_POA",
248 poa_manager.in (),
249 poa_policy_list);
251 RTPortableServer::POA_var rt_poa =
252 RTPortableServer::POA::_narrow (child_poa.in ());
253 if (check_for_nil (rt_poa.in (), "RTPOA") == -1)
254 return -1;
256 // Servant.
257 Test_i server_impl (this->orb_.in ());
259 // Create object 1 (it will inherit POA's priority).
260 int result;
261 ACE_DEBUG ((LM_DEBUG, "\nActivated object one as "));
262 result = create_object (rt_poa.in (), this->orb_.in (), &server_impl,
263 -1, ior_output_file1);
264 if (result == -1)
265 return -1;
267 // Create object 2 (override POA's priority).
268 ACE_DEBUG ((LM_DEBUG, "\nActivated object two as "));
269 result = create_object (rt_poa.in (), this->orb_.in (), &server_impl,
270 object_priority, ior_output_file2);
271 if (result == -1)
272 return -1;
274 // Activate POA manager.
275 poa_manager->activate ();
277 // Start ORB event loop.
278 this->orb_->run ();
280 ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n\n"));
282 catch (const CORBA::Exception& ex)
284 ex._tao_print_exception (
285 "Unexpected exception caught in Server_Declared test server:");
286 return -1;
289 return 0;
293 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
297 // ORB.
298 CORBA::ORB_var orb =
299 CORBA::ORB_init (argc, argv);
301 // Parse arguments.
302 if (parse_args (argc, argv) != 0)
303 return -1;
305 // Make sure we can support multiple priorities that are required
306 // for this test.
307 if (!check_supported_priorities (orb.in ()))
308 return 2;
310 // Thread Manager for managing task.
311 ACE_Thread_Manager thread_manager;
313 // Create task.
314 Task task (thread_manager,
315 orb.in ());
317 // Task activation flags.
318 long flags =
319 THR_NEW_LWP |
320 THR_JOINABLE |
321 orb->orb_core ()->orb_params ()->thread_creation_flags ();
323 // Activate task.
324 int result =
325 task.activate (flags);
326 if (result == -1)
328 if (errno == EPERM)
330 ACE_ERROR_RETURN ((LM_ERROR,
331 "Cannot create thread with scheduling policy %s\n"
332 "because the user does not have the appropriate privileges, terminating program....\n"
333 "Check svc.conf options and/or run as root\n",
334 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
337 else
338 // Unexpected error.
339 ACE_ASSERT (0);
342 // Wait for task to exit.
343 result =
344 thread_manager.wait ();
345 ACE_ASSERT (result != -1);
347 catch (const CORBA::Exception& ex)
349 ex._tao_print_exception ("Exception caught");
350 return -1;
353 return 0;