Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3382_Regression / server.cpp
blob253f91cd2f6ef5e6bf27671a3898437957958775
1 #include "testS.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "tao/ORB_Core.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 // = The Test methods.
19 void test_method (CORBA::Short priority);
21 void shutdown ();
23 private:
24 CORBA::ORB_var orb_;
25 // The ORB
28 Test_i::Test_i (CORBA::ORB_ptr orb)
29 : orb_ (CORBA::ORB::_duplicate (orb))
33 void
34 Test_i::test_method (CORBA::Short priority)
36 // Use RTCurrent to find out the CORBA priority of the current
37 // thread.
39 CORBA::Object_var obj =
40 this->orb_->resolve_initial_references ("RTCurrent");
42 RTCORBA::Current_var current =
43 RTCORBA::Current::_narrow (obj.in ());
45 if (CORBA::is_nil (obj.in ()))
46 throw CORBA::INTERNAL ();
48 CORBA::Short servant_thread_priority =
49 current->the_priority ();
51 // Print out the info.
52 if (servant_thread_priority != priority)
53 ACE_DEBUG ((LM_DEBUG,
54 "ERROR: servant thread priority is not equal "
55 "to method argument.\n"));
57 ACE_DEBUG ((LM_DEBUG,
58 "Client priority: %d "
59 "Servant thread priority: %d\n",
60 priority, servant_thread_priority));
63 void
64 Test_i::shutdown ()
66 this->orb_->shutdown (0);
69 //*************************************************************************
71 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
73 // Parse command-line arguments.
74 int
75 parse_args (int argc, ACE_TCHAR *argv[])
77 ACE_Get_Opt get_opts (argc, argv, "o:");
78 int c;
80 while ((c = get_opts ()) != -1)
81 switch (c)
83 case 'o':
84 ior_output_file = get_opts.opt_arg ();
85 break;
87 case '?':
88 default:
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "usage: %s "
91 "-o <iorfile>"
92 "\n",
93 argv [0]),
94 -1);
97 return 0;
100 class Task : public ACE_Task_Base
102 public:
104 Task (ACE_Thread_Manager &thread_manager,
105 CORBA::ORB_ptr orb);
107 int svc (void);
109 CORBA::ORB_var orb_;
113 Task::Task (ACE_Thread_Manager &thread_manager,
114 CORBA::ORB_ptr orb)
115 : ACE_Task_Base (&thread_manager),
116 orb_ (CORBA::ORB::_duplicate (orb))
121 Task::svc (void)
125 CORBA::Object_var object =
126 this->orb_->resolve_initial_references("RootPOA");
128 PortableServer::POA_var root_poa =
129 PortableServer::POA::_narrow (object.in ());
131 if (CORBA::is_nil (root_poa.in ()))
132 ACE_ERROR_RETURN ((LM_ERROR,
133 "ERROR: Panic <RootPOA> is nil\n"),
134 -1);
136 PortableServer::POAManager_var poa_manager =
137 root_poa->the_POAManager ();
139 object = this->orb_->resolve_initial_references ("RTORB");
141 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
144 object =
145 this->orb_->resolve_initial_references ("RTCurrent");
147 RTCORBA::Current_var current =
148 RTCORBA::Current::_narrow (object.in ());
150 // Create POA with CLIENT_PROPAGATED PriorityModelPolicy,
151 // and register Test object with it.
152 CORBA::PolicyList poa_policy_list;
153 poa_policy_list.length (1);
154 poa_policy_list[0] =
155 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
158 PortableServer::POA_var child_poa =
159 root_poa->create_POA ("Child_POA",
160 poa_manager.in (),
161 poa_policy_list);
163 Test_i server_impl (this->orb_.in ());
165 PortableServer::ObjectId_var id =
166 child_poa->activate_object (&server_impl);
168 CORBA::Object_var server =
169 child_poa->id_to_reference (id.in ());
171 // Print Object IOR.
172 CORBA::String_var ior =
173 this->orb_->object_to_string (server.in ());
175 ACE_DEBUG ((LM_DEBUG, "Activated as <%s>\n\n", ior.in ()));
177 if (ior_output_file != 0)
179 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
180 if (output_file == 0)
181 ACE_ERROR_RETURN ((LM_ERROR,
182 "Cannot open output file for writing IOR: %s",
183 ior_output_file),
184 -1);
185 ACE_OS::fprintf (output_file, "%s", ior.in ());
186 ACE_OS::fclose (output_file);
189 // Get the initial priority of the current thread.
190 CORBA::Short initial_thread_priority =
191 get_implicit_thread_CORBA_priority (this->orb_.in ());
193 current->the_priority (initial_thread_priority);
195 // Run ORB Event loop.
196 poa_manager->activate ();
198 this->orb_->run ();
200 ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n"));
202 // Get the final priority of the current thread.
203 CORBA::Short final_thread_priority =
204 current->the_priority ();
206 if (final_thread_priority != initial_thread_priority)
207 ACE_DEBUG ((LM_DEBUG,
208 "ERROR: Priority of the servant thread "
209 "has been permanently changed!\n"
210 "Initial priority: %d Final priority: %d\n",
211 initial_thread_priority, final_thread_priority));
212 else
213 ACE_DEBUG ((LM_DEBUG,
214 "Final priority of the servant thread"
215 " == its initial priority\n"));
218 catch (const ::CORBA::Exception & ex)
220 ex._tao_print_exception(
221 "Exception caught:");
222 return -1;
225 return 0;
229 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
231 int result = 0;
235 // Standard initialization:
236 // parse arguments and get all the references (ORB,
237 // RootPOA, RTORB, RTCurrent, POAManager).
238 CORBA::ORB_var orb =
239 CORBA::ORB_init (argc, argv);
241 if (parse_args (argc, argv) != 0)
242 return -1;
244 // Make sure we can support multiple priorities that are required
245 // for this test.
246 if (!check_supported_priorities (orb.in ()))
247 return 2;
249 // Thread Manager for managing task.
250 ACE_Thread_Manager thread_manager;
252 // Create task.
253 Task task (thread_manager,
254 orb.in ());
256 // Task activation flags.
257 long flags =
258 THR_NEW_LWP |
259 THR_JOINABLE |
260 orb->orb_core ()->orb_params ()->thread_creation_flags ();
262 // Activate task.
263 result =
264 task.activate (flags);
265 if (result == -1)
267 if (errno == EPERM)
269 ACE_ERROR_RETURN ((LM_ERROR,
270 "Cannot create thread with scheduling policy %s\n"
271 "because the user does not have the appropriate privileges, terminating program....\n"
272 "Check svc.conf options and/or run as root\n",
273 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
276 else
277 // Unexpected error.
278 ACE_ERROR_RETURN ((LM_ERROR,
279 "ERROR: Cannot create thread. errno = %d\n",
280 ACE_ERRNO_GET),
281 -1);
284 // Wait for task to exit.
285 result =
286 thread_manager.wait ();
288 catch (const ::CORBA::Exception & ex)
290 ex._tao_print_exception(
291 "Exception caught:");
292 return -1;
295 return result;