Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Banded_Connections / client.cpp
blobc821f25d5a081de0167ae0c96939fed8f1f97544
1 #include "testC.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "tao/RTCORBA/RTCORBA.h"
5 #include "tao/ORB_Core.h"
6 #include "../check_supported_priorities.cpp"
8 // Default IOR files.
9 const ACE_TCHAR *ior1 = ACE_TEXT("file://test1.ior");
10 const ACE_TCHAR *ior2 = ACE_TEXT("file://test2.ior");
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("n:o:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'n':
22 ior1 = get_opts.opt_arg ();
23 break;
24 case 'o':
25 ior2 = get_opts.opt_arg ();
26 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s "
32 "-n <ior> "
33 "-o <ior> "
34 "\n",
35 argv [0]),
36 -1);
39 return 0;
42 CORBA::Short
43 get_server_priority (Test_ptr server)
45 // Get the Priority Model Policy from the stub.
46 CORBA::Policy_var policy =
47 server->_get_policy (RTCORBA::PRIORITY_MODEL_POLICY_TYPE);
49 // Narrow down to correct type.
50 RTCORBA::PriorityModelPolicy_var priority_policy =
51 RTCORBA::PriorityModelPolicy::_narrow (policy.in ());
53 // Make sure that we have the SERVER_DECLARED priority model.
54 RTCORBA::PriorityModel priority_model =
55 priority_policy->priority_model ();
56 if (priority_model != RTCORBA::SERVER_DECLARED)
57 ACE_ERROR_RETURN ((LM_ERROR,
58 "ERROR: priority_model != "
59 "RTCORBA::SERVER_DECLARED!\n"),
60 -1);
62 // Return the server priority.
63 return priority_policy->server_priority ();
66 void
67 invocation_exception_test (Test_ptr obj,
68 CORBA::Short priority)
70 try
72 // Invoke method on test object.
73 obj->test_method (1,
74 priority);
76 // This next line of code should not run because an exception
77 // should have been raised.
78 ACE_DEBUG ((LM_DEBUG, "ERROR: no exception caught\n"));
80 catch (const CORBA::INV_POLICY& )
82 // Expected exception.
83 ACE_DEBUG ((LM_DEBUG,
84 "INV_POLICY exception is caught as expected.\n"));
86 catch (const CORBA::Exception&)
88 // Unexpected exception.
89 ACE_DEBUG ((LM_DEBUG, "Error: unexpected exception caught\n"));
90 throw;
94 class Task : public ACE_Task_Base
96 public:
97 Task (ACE_Thread_Manager &thread_manager,
98 CORBA::ORB_ptr orb);
100 int svc ();
102 CORBA::ORB_var orb_;
105 Task::Task (ACE_Thread_Manager &thread_manager,
106 CORBA::ORB_ptr orb)
107 : ACE_Task_Base (&thread_manager),
108 orb_ (CORBA::ORB::_duplicate (orb))
113 Task::svc ()
117 // Get the RTORB.
118 CORBA::Object_var object =
119 this->orb_->resolve_initial_references ("RTORB");
121 RTCORBA::RTORB_var rt_orb =
122 RTCORBA::RTORB::_narrow (object.in ());
124 // Get the RTCurrent.
125 object =
126 this->orb_->resolve_initial_references ("RTCurrent");
128 RTCORBA::Current_var current =
129 RTCORBA::Current::_narrow (object.in ());
131 // We need to set the client thread CORBA priority
132 current->the_priority (get_implicit_thread_CORBA_priority (this->orb_.in ()));
134 // Test object 1 (with CLIENT_PROPAGATED priority model).
135 object =
136 this->orb_->string_to_object (ior1);
138 Test_var client_propagated_obj =
139 Test::_narrow (object.in ());
141 // Test object 2 (with SERVER_DECLARED priority model).
142 object = this->orb_->string_to_object (ior2);
144 Test_var server_declared_obj =
145 Test::_narrow (object.in ());
147 // Test: Attempt to set priority bands that do not match server
148 // resource configuration on the <client_propagated_obj>.
149 // Should get INV_POLICY exception.
150 ACE_DEBUG ((LM_DEBUG,
151 "\n<---Test--->: Client bands do not match server lanes\n\n"));
153 // False bands.
154 RTCORBA::PriorityBands false_bands;
155 false_bands.length (2);
156 false_bands[0].low = 10000;
157 false_bands[0].high = 10005;
158 false_bands[1].low = 26000;
159 false_bands[1].high = 30000;
160 CORBA::PolicyList policies;
161 policies.length (1);
162 policies[0] =
163 rt_orb->create_priority_banded_connection_policy (false_bands);
165 // Set false bands at the object level. Note that a new object
166 // is returned.
167 object =
168 client_propagated_obj->_set_policy_overrides (policies,
169 CORBA::SET_OVERRIDE);
171 client_propagated_obj =
172 Test::_narrow (object.in ());
174 // Invoking on this object with false bands should produce an
175 // exception.
176 invocation_exception_test (client_propagated_obj.in (),
179 // Get the correct bands from the <server_declared_obj>.
180 policies[0] =
181 server_declared_obj->_get_policy (RTCORBA::PRIORITY_BANDED_CONNECTION_POLICY_TYPE);
183 RTCORBA::PriorityBandedConnectionPolicy_var bands_policy =
184 RTCORBA::PriorityBandedConnectionPolicy::_narrow (policies[0]);
186 RTCORBA::PriorityBands_var bands =
187 bands_policy->priority_bands ();
189 // Set the proper bands at the object level. Note that a new
190 // object is returned.
191 object =
192 client_propagated_obj->_set_policy_overrides (policies,
193 CORBA::SET_OVERRIDE);
195 // Overwrite existing <client_propagated_obj>.
196 client_propagated_obj =
197 Test::_narrow (object.in ());
199 // Test: Attempt invocation on <client_propagated_obj> with
200 // client thread priority not matching any of the bands. Should
201 // get INV_POLICY exception.
202 ACE_DEBUG ((LM_DEBUG,
203 "\n<---Test--->: Client threads does not match band\n\n"));
205 // Choose a priority one higher than the high priority of the
206 // highest band.
207 CORBA::Short client_priority =
208 bands[bands->length () - 1].high + 1;
210 // Reset the current thread's priority.
211 current->the_priority (client_priority);
213 // Invoking on this object with an invalid client thread
214 // priority should produce an exception.
215 invocation_exception_test (client_propagated_obj.in (),
216 client_priority);
218 // Test: Make invocations on the <client_propagated_obj>.
219 ACE_DEBUG ((LM_DEBUG,
220 "\n<---Test--->: Invoking on client propagated object\n\n"));
222 // Make an invocation for each band.
223 for (CORBA::ULong i = 0;
224 i < bands->length ();
225 ++i)
227 // Select a priority in the middle of the band.
228 CORBA::Short client_priority =
229 (bands[i].low + bands[i].high) / 2;
231 // Reset the current thread's priority.
232 current->the_priority (client_priority);
234 // Invoke test method on server.
235 client_propagated_obj->test_method (1, // CLIENT_PROPAGATED
236 client_priority);
239 // Test: Attempt invocation with the same thread priority, but
240 // now on the <server_declared_obj>. This should succeed.
241 ACE_DEBUG ((LM_DEBUG,
242 "\n<---Test--->: Invoking on server declared object\n\n"));
244 // Get the <server_priority> from the stub.
245 CORBA::Short server_priority =
246 get_server_priority (server_declared_obj.in ());
248 // Invoke test method on server.
249 server_declared_obj->test_method (0, // SERVER_DECLARED
250 server_priority);
252 // Testing over. Shut down Server ORB.
253 server_declared_obj->shutdown ();
255 catch (const CORBA::Exception& ex)
257 ex._tao_print_exception (
258 "Unexpected exception in Banded_Connections test client:");
259 return -1;
262 return 0;
266 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
270 // Initialize ORB.
271 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
273 // Parse arguments.
274 int result = parse_args (argc, argv);
275 if (result != 0)
276 return result;
278 // Make sure we can support multiple priorities that are required
279 // for this test.
280 if (!check_supported_priorities (orb.in ()))
281 return 2;
283 // Thread Manager for managing task.
284 ACE_Thread_Manager thread_manager;
286 // Create task.
287 Task task (thread_manager,
288 orb.in ());
290 // Task activation flags.
291 long flags =
292 THR_NEW_LWP |
293 THR_JOINABLE |
294 orb->orb_core ()->orb_params ()->thread_creation_flags ();
296 // Activate task.
297 result =
298 task.activate (flags);
299 if (result == -1)
301 if (errno == EPERM)
303 ACE_ERROR_RETURN ((LM_ERROR,
304 "Cannot create thread with scheduling policy %s\n"
305 "because the user does not have the appropriate privileges, terminating program....\n"
306 "Check svc.conf options and/or run as root\n",
307 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
310 else
311 // Unexpected error.
312 ACE_ASSERT (0);
315 // Wait for task to exit.
316 result =
317 thread_manager.wait ();
318 ACE_ASSERT (result != -1);
320 orb->destroy ();
322 catch (const CORBA::Exception& ex)
324 ex._tao_print_exception (
325 "Unexpected exception in Banded_Connections test client:");
326 return -1;
329 return 0;