Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Client_Propagated / server.cpp
blobd257aa53b1bad2867543e86e85d55764206b9bbd
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 ();
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 ()
68 this->orb_->shutdown (false);
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:
105 Task (ACE_Thread_Manager &thread_manager,
106 CORBA::ORB_ptr orb);
108 int svc ();
110 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 ()
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");
140 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
142 object =
143 this->orb_->resolve_initial_references ("RTCurrent");
144 RTCORBA::Current_var current =
145 RTCORBA::Current::_narrow (object.in ());
147 // Create POA with CLIENT_PROPAGATED PriorityModelPolicy,
148 // and register Test object with it.
149 CORBA::PolicyList poa_policy_list;
150 poa_policy_list.length (1);
151 poa_policy_list[0] =
152 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
155 PortableServer::POA_var child_poa =
156 root_poa->create_POA ("Child_POA",
157 poa_manager.in (),
158 poa_policy_list);
160 Test_i server_impl (this->orb_.in ());
162 PortableServer::ObjectId_var id =
163 child_poa->activate_object (&server_impl);
165 CORBA::Object_var server =
166 child_poa->id_to_reference (id.in ());
168 // Print Object IOR.
169 CORBA::String_var ior =
170 this->orb_->object_to_string (server.in ());
172 ACE_DEBUG ((LM_DEBUG, "Activated as <%s>\n\n", ior.in ()));
174 if (ior_output_file != 0)
176 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
177 if (output_file == 0)
178 ACE_ERROR_RETURN ((LM_ERROR,
179 "Cannot open output file for writing IOR: %s",
180 ior_output_file),
181 -1);
182 ACE_OS::fprintf (output_file, "%s", ior.in ());
183 ACE_OS::fclose (output_file);
186 // Get the initial priority of the current thread.
187 // Note that it is an error to access the priority via the current in a thread that it
188 // where it hasn't been set following the fix for tao681.
189 CORBA::Short initial_thread_priority
190 = get_implicit_thread_CORBA_priority (this->orb_.in ());
191 current->the_priority (initial_thread_priority);
193 // Run ORB Event loop.
194 poa_manager->activate ();
196 this->orb_->run ();
198 ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n"));
200 // Get the final priority of the current thread.
201 CORBA::Short final_thread_priority =
202 current->the_priority ();
204 if (final_thread_priority != initial_thread_priority)
205 ACE_DEBUG ((LM_DEBUG,
206 "ERROR: Priority of the servant thread "
207 "has been permanently changed!\n"
208 "Initial priority: %d Final priority: %d\n",
209 initial_thread_priority, final_thread_priority));
210 else
211 ACE_DEBUG ((LM_DEBUG,
212 "Final priority of the servant thread"
213 " = its initial priority\n"));
215 catch (const CORBA::Exception& ex)
217 ex._tao_print_exception ("Exception caught:");
218 return -1;
221 return 0;
225 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
229 // Standard initialization:
230 // parse arguments and get all the references (ORB,
231 // RootPOA, RTORB, RTCurrent, POAManager).
232 CORBA::ORB_var orb =
233 CORBA::ORB_init (argc, argv);
235 if (parse_args (argc, argv) != 0)
236 return -1;
238 // Make sure we can support multiple priorities that are required
239 // for this test.
240 if (!check_supported_priorities (orb.in ()))
241 return 2;
243 // Thread Manager for managing task.
244 ACE_Thread_Manager thread_manager;
246 // Create task.
247 Task task (thread_manager,
248 orb.in ());
250 // Task activation flags.
251 long flags =
252 THR_NEW_LWP |
253 THR_JOINABLE |
254 orb->orb_core ()->orb_params ()->thread_creation_flags ();
256 // Activate task.
257 int result =
258 task.activate (flags);
259 if (result == -1)
261 if (errno == EPERM)
263 ACE_ERROR_RETURN ((LM_ERROR,
264 "Cannot create thread with scheduling policy %s\n"
265 "because the user does not have the appropriate privileges, terminating program....\n"
266 "Check svc.conf options and/or run as root\n",
267 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
270 else
271 // Unexpected error.
272 ACE_ASSERT (0);
275 // Wait for task to exit.
276 result =
277 thread_manager.wait ();
278 ACE_ASSERT (result != -1);
280 catch (const CORBA::Exception& ex)
282 ex._tao_print_exception ("Exception caught");
283 return -1;
286 return 0;