Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Client_Propagated / server.cpp
blob2d7e68a5d927adc452abe4a8660b654f5a52fcf6
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 //FUZZ: disable check_for_lack_ACE_OS
22 void shutdown (void);
23 //FUZZ: enable check_for_lack_ACE_OS
25 private:
26 CORBA::ORB_var orb_;
27 // The ORB
30 Test_i::Test_i (CORBA::ORB_ptr orb)
31 : orb_ (CORBA::ORB::_duplicate (orb))
35 void
36 Test_i::test_method (CORBA::Short priority)
38 // Use RTCurrent to find out the CORBA priority of the current
39 // thread.
41 CORBA::Object_var obj =
42 this->orb_->resolve_initial_references ("RTCurrent");
44 RTCORBA::Current_var current =
45 RTCORBA::Current::_narrow (obj.in ());
47 if (CORBA::is_nil (obj.in ()))
48 throw CORBA::INTERNAL ();
50 CORBA::Short servant_thread_priority =
51 current->the_priority ();
53 // Print out the info.
54 if (servant_thread_priority != priority)
55 ACE_DEBUG ((LM_DEBUG,
56 "ERROR: servant thread priority is not equal "
57 "to method argument.\n"));
59 ACE_DEBUG ((LM_DEBUG,
60 "Client priority: %d "
61 "Servant thread priority: %d\n",
62 priority, servant_thread_priority));
65 void
66 Test_i::shutdown (void)
68 this->orb_->shutdown (0);
71 //*************************************************************************
73 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
75 // Parse command-line arguments.
76 int
77 parse_args (int argc, ACE_TCHAR *argv[])
79 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
80 int c;
82 while ((c = get_opts ()) != -1)
83 switch (c)
85 case 'o':
86 ior_output_file = get_opts.opt_arg ();
87 break;
89 case '?':
90 default:
91 ACE_ERROR_RETURN ((LM_ERROR,
92 "usage: %s "
93 "-o <iorfile>"
94 "\n",
95 argv [0]),
96 -1);
99 return 0;
102 class Task : public ACE_Task_Base
104 public:
106 Task (ACE_Thread_Manager &thread_manager,
107 CORBA::ORB_ptr orb);
109 int svc (void);
111 CORBA::ORB_var orb_;
115 Task::Task (ACE_Thread_Manager &thread_manager,
116 CORBA::ORB_ptr orb)
117 : ACE_Task_Base (&thread_manager),
118 orb_ (CORBA::ORB::_duplicate (orb))
123 Task::svc (void)
127 CORBA::Object_var object =
128 this->orb_->resolve_initial_references("RootPOA");
130 PortableServer::POA_var root_poa =
131 PortableServer::POA::_narrow (object.in ());
133 if (CORBA::is_nil (root_poa.in ()))
134 ACE_ERROR_RETURN ((LM_ERROR,
135 "ERROR: Panic <RootPOA> is nil\n"),
136 -1);
138 PortableServer::POAManager_var poa_manager =
139 root_poa->the_POAManager ();
141 object = this->orb_->resolve_initial_references ("RTORB");
142 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
144 object =
145 this->orb_->resolve_initial_references ("RTCurrent");
146 RTCORBA::Current_var current =
147 RTCORBA::Current::_narrow (object.in ());
149 // Create POA with CLIENT_PROPAGATED PriorityModelPolicy,
150 // and register Test object with it.
151 CORBA::PolicyList poa_policy_list;
152 poa_policy_list.length (1);
153 poa_policy_list[0] =
154 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
157 PortableServer::POA_var child_poa =
158 root_poa->create_POA ("Child_POA",
159 poa_manager.in (),
160 poa_policy_list);
162 Test_i server_impl (this->orb_.in ());
164 PortableServer::ObjectId_var id =
165 child_poa->activate_object (&server_impl);
167 CORBA::Object_var server =
168 child_poa->id_to_reference (id.in ());
170 // Print Object IOR.
171 CORBA::String_var ior =
172 this->orb_->object_to_string (server.in ());
174 ACE_DEBUG ((LM_DEBUG, "Activated as <%s>\n\n", ior.in ()));
176 if (ior_output_file != 0)
178 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
179 if (output_file == 0)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 "Cannot open output file for writing IOR: %s",
182 ior_output_file),
183 -1);
184 ACE_OS::fprintf (output_file, "%s", ior.in ());
185 ACE_OS::fclose (output_file);
188 // Get the initial priority of the current thread.
189 // Note that it is an error to access the priority via the current in a thread that it
190 // where it hasn't been set following the fix for tao681.
191 CORBA::Short initial_thread_priority
192 = 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 ("Exception caught:");
221 return -1;
224 return 0;
228 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
232 // Standard initialization:
233 // parse arguments and get all the references (ORB,
234 // RootPOA, RTORB, RTCurrent, POAManager).
235 CORBA::ORB_var orb =
236 CORBA::ORB_init (argc, argv);
238 if (parse_args (argc, argv) != 0)
239 return -1;
241 // Make sure we can support multiple priorities that are required
242 // for this test.
243 if (!check_supported_priorities (orb.in ()))
244 return 2;
246 // Thread Manager for managing task.
247 ACE_Thread_Manager thread_manager;
249 // Create task.
250 Task task (thread_manager,
251 orb.in ());
253 // Task activation flags.
254 long flags =
255 THR_NEW_LWP |
256 THR_JOINABLE |
257 orb->orb_core ()->orb_params ()->thread_creation_flags ();
259 // Activate task.
260 int result =
261 task.activate (flags);
262 if (result == -1)
264 if (errno == EPERM)
266 ACE_ERROR_RETURN ((LM_ERROR,
267 "Cannot create thread with scheduling policy %s\n"
268 "because the user does not have the appropriate privileges, terminating program....\n"
269 "Check svc.conf options and/or run as root\n",
270 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
273 else
274 // Unexpected error.
275 ACE_ASSERT (0);
278 // Wait for task to exit.
279 result =
280 thread_manager.wait ();
281 ACE_ASSERT (result != -1);
283 catch (const CORBA::Exception& ex)
285 ex._tao_print_exception ("Exception caught");
286 return -1;
289 return 0;