Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3382_Regression / client.cpp
blob5c3004eea7fb9bed75a8c9d9e8a73e14b55e0c8f
1 #include "testC.h"
2 #include "ace/Task.h"
3 #include "tao/ORB_Core.h"
4 #include "Client_ORBInitializer.h"
5 #include "tao/ORBInitializer_Registry.h"
6 #include "tao/RTCORBA/RTCORBA.h"
7 #include "tao/RTCORBA/Priority_Mapping_Manager.h"
8 #include "ace/Get_Opt.h"
9 #include "../check_supported_priorities.cpp"
11 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, "k:");
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'k':
23 ior = get_opts.opt_arg ();
24 break;
25 case '?':
26 default:
27 ACE_ERROR_RETURN ((LM_ERROR,
28 "usage: %s "
29 "-k <ior> "
30 "\n",
31 argv [0]),
32 -1);
35 return 0;
38 class Task : public ACE_Task_Base
40 public:
41 Task (ACE_Thread_Manager &thread_manager,
42 CORBA::ORB_ptr orb);
44 int svc ();
46 CORBA::ORB_var orb_;
49 Task::Task (ACE_Thread_Manager &thread_manager,
50 CORBA::ORB_ptr orb)
51 : ACE_Task_Base (&thread_manager),
52 orb_ (CORBA::ORB::_duplicate (orb))
56 int
57 Task::svc ()
59 int result = 0;
60 try
62 CORBA::Object_var object =
63 this->orb_->string_to_object (ior);
65 Test_var server =
66 Test::_narrow (object.in ());
68 if (CORBA::is_nil (server.in ()))
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "ERROR: Object reference <%s> is nil\n",
72 ior),
73 -1);
76 // Check that the object is configured with CLIENT_PROPAGATED
77 // PriorityModelPolicy.
78 CORBA::Policy_var policy =
79 server->_get_policy (RTCORBA::PRIORITY_MODEL_POLICY_TYPE);
81 RTCORBA::PriorityModelPolicy_var priority_policy =
82 RTCORBA::PriorityModelPolicy::_narrow (policy.in ());
84 if (CORBA::is_nil (priority_policy.in ()))
85 ACE_ERROR_RETURN ((LM_ERROR,
86 "ERROR: Priority Model Policy not exposed!\n"),
87 -1);
89 RTCORBA::PriorityModel priority_model =
90 priority_policy->priority_model ();
92 if (priority_model != RTCORBA::CLIENT_PROPAGATED)
93 ACE_ERROR_RETURN ((LM_ERROR,
94 "ERROR: priority_model != "
95 "RTCORBA::CLIENT_PROPAGATED!\n"),
96 -1);
98 // Make several invocations, changing the priority of this thread
99 // for each.
100 object =
101 this->orb_->resolve_initial_references ("RTCurrent");
103 RTCORBA::Current_var current =
104 RTCORBA::Current::_narrow (object.in ());
106 object = this->orb_->resolve_initial_references ("PriorityMappingManager");
108 RTCORBA::PriorityMappingManager_var mapping_manager =
109 RTCORBA::PriorityMappingManager::_narrow (object.in ());
111 RTCORBA::PriorityMapping *pm =
112 mapping_manager->mapping ();
114 int sched_policy =
115 this->orb_->orb_core ()->orb_params ()->ace_sched_policy ();
117 int max_priority =
118 ACE_Sched_Params::priority_max (sched_policy);
119 int min_priority =
120 ACE_Sched_Params::priority_min (sched_policy);
122 CORBA::Short native_priority =
123 (max_priority - min_priority) / 2;
125 CORBA::Short desired_priority = 0;
127 for (int i = 0; i < 3; ++i)
129 if (pm->to_CORBA (native_priority, desired_priority) == 0)
131 ACE_ERROR ((LM_ERROR,
132 "ERROR: Cannot convert native priority %d to corba priority\n",
133 native_priority));
134 result = -1;
135 break;
138 current->the_priority (desired_priority);
140 CORBA::Short priority =
141 current->the_priority ();
143 if (desired_priority != priority)
145 ACE_ERROR ((LM_ERROR,
146 "ERROR: No exception setting the priority but mismatch between requested and returned value from Current. "
147 "Set to %d but Current::the_priority returns %d\n", desired_priority, priority));
148 result = -1;
152 server->test_method (priority);
154 native_priority++;
157 // Shut down Server ORB.
158 server->shutdown ();
160 catch (const CORBA::DATA_CONVERSION& ex)
162 ex._tao_print_exception (
163 "Most likely, this is due to the in-ability "
164 "to set the thread priority.");
165 return -1;
167 catch (const CORBA::Exception & ae)
169 ae._tao_print_exception (
170 "Caught exception:");
171 return -1;
174 return result;
178 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
180 int result = 0;
183 // Register the interceptors to check for the RTCORBA
184 // service contexts in the reply messages.
185 PortableInterceptor::ORBInitializer_ptr temp_initializer;
187 ACE_NEW_RETURN (temp_initializer,
188 Client_ORBInitializer,
189 -1); // No exceptions yet!
190 PortableInterceptor::ORBInitializer_var initializer =
191 temp_initializer;
193 PortableInterceptor::register_orb_initializer (initializer.in ());
195 // Initialize and obtain reference to the Test object.
196 CORBA::ORB_var orb =
197 CORBA::ORB_init (argc, argv);
199 if (parse_args (argc, argv) != 0)
200 return -1;
202 // Make sure we can support multiple priorities that are required
203 // for this test.
204 if (!check_supported_priorities (orb.in ()))
205 return 2;
207 // Thread Manager for managing task.
208 ACE_Thread_Manager thread_manager;
210 // Create task.
211 Task task (thread_manager,
212 orb.in ());
214 // Task activation flags.
215 long flags =
216 THR_NEW_LWP |
217 THR_JOINABLE |
218 orb->orb_core ()->orb_params ()->thread_creation_flags ();
220 // Activate task.
221 result =
222 task.activate (flags);
223 if (result == -1)
225 if (errno == EPERM)
227 ACE_ERROR_RETURN ((LM_ERROR,
228 "Cannot create thread with scheduling policy %s\n"
229 "because the user does not have the appropriate privileges, terminating program....\n"
230 "Check svc.conf options and/or run as root\n",
231 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
234 else
235 // Unexpected error.
236 ACE_ERROR_RETURN ((LM_ERROR,
237 "ERROR: Cannot create thread. errno = %d\n",
238 ACE_ERRNO_GET),
239 -1);
242 // Wait for task to exit.
243 result =
244 thread_manager.wait ();
246 catch (const CORBA::Exception & ae)
248 ae._tao_print_exception (
249 "Caught exception:");
250 return -1;
253 return result;