Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3382_Regression / client.cpp
blob671d565d4b8c6106e99e2ff408e2d50ac7ae7079
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:
42 Task (ACE_Thread_Manager &thread_manager,
43 CORBA::ORB_ptr orb);
45 int svc (void);
47 CORBA::ORB_var orb_;
50 Task::Task (ACE_Thread_Manager &thread_manager,
51 CORBA::ORB_ptr orb)
52 : ACE_Task_Base (&thread_manager),
53 orb_ (CORBA::ORB::_duplicate (orb))
57 int
58 Task::svc (void)
61 int result = 0;
62 try
64 CORBA::Object_var object =
65 this->orb_->string_to_object (ior);
67 Test_var server =
68 Test::_narrow (object.in ());
70 if (CORBA::is_nil (server.in ()))
72 ACE_ERROR_RETURN ((LM_ERROR,
73 "ERROR: Object reference <%s> is nil\n",
74 ior),
75 -1);
78 // Check that the object is configured with CLIENT_PROPAGATED
79 // PriorityModelPolicy.
80 CORBA::Policy_var policy =
81 server->_get_policy (RTCORBA::PRIORITY_MODEL_POLICY_TYPE);
83 RTCORBA::PriorityModelPolicy_var priority_policy =
84 RTCORBA::PriorityModelPolicy::_narrow (policy.in ());
86 if (CORBA::is_nil (priority_policy.in ()))
87 ACE_ERROR_RETURN ((LM_ERROR,
88 "ERROR: Priority Model Policy not exposed!\n"),
89 -1);
91 RTCORBA::PriorityModel priority_model =
92 priority_policy->priority_model ();
94 if (priority_model != RTCORBA::CLIENT_PROPAGATED)
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "ERROR: priority_model != "
97 "RTCORBA::CLIENT_PROPAGATED!\n"),
98 -1);
100 // Make several invocations, changing the priority of this thread
101 // for each.
102 object =
103 this->orb_->resolve_initial_references ("RTCurrent");
105 RTCORBA::Current_var current =
106 RTCORBA::Current::_narrow (object.in ());
108 object = this->orb_->resolve_initial_references ("PriorityMappingManager");
110 RTCORBA::PriorityMappingManager_var mapping_manager =
111 RTCORBA::PriorityMappingManager::_narrow (object.in ());
113 RTCORBA::PriorityMapping *pm =
114 mapping_manager->mapping ();
116 int sched_policy =
117 this->orb_->orb_core ()->orb_params ()->ace_sched_policy ();
119 int max_priority =
120 ACE_Sched_Params::priority_max (sched_policy);
121 int min_priority =
122 ACE_Sched_Params::priority_min (sched_policy);
124 CORBA::Short native_priority =
125 (max_priority - min_priority) / 2;
127 CORBA::Short desired_priority = 0;
129 for (int i = 0; i < 3; ++i)
131 if (pm->to_CORBA (native_priority, desired_priority) == 0)
133 ACE_ERROR ((LM_ERROR,
134 "ERROR: Cannot convert native priority %d to corba priority\n",
135 native_priority));
136 result = -1;
137 break;
140 current->the_priority (desired_priority);
142 CORBA::Short priority =
143 current->the_priority ();
145 if (desired_priority != priority)
147 ACE_ERROR ((LM_ERROR,
148 "ERROR: No exception setting the priority but mismatch between requested and returned value from Current. "
149 "Set to %d but Current::the_priority returns %d\n", desired_priority, priority));
150 result = -1;
154 server->test_method (priority);
156 native_priority++;
159 // Shut down Server ORB.
160 server->shutdown ();
162 catch (const CORBA::DATA_CONVERSION& ex)
164 ex._tao_print_exception (
165 "Most likely, this is due to the in-ability "
166 "to set the thread priority.");
167 return -1;
169 catch (const CORBA::Exception & ae)
171 ae._tao_print_exception (
172 "Caught exception:");
173 return -1;
176 return result;
180 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
182 int result = 0;
185 // Register the interceptors to check for the RTCORBA
186 // service contexts in the reply messages.
187 PortableInterceptor::ORBInitializer_ptr temp_initializer;
189 ACE_NEW_RETURN (temp_initializer,
190 Client_ORBInitializer,
191 -1); // No exceptions yet!
192 PortableInterceptor::ORBInitializer_var initializer =
193 temp_initializer;
195 PortableInterceptor::register_orb_initializer (initializer.in ());
197 // Initialize and obtain reference to the Test object.
198 CORBA::ORB_var orb =
199 CORBA::ORB_init (argc, argv);
201 if (parse_args (argc, argv) != 0)
202 return -1;
204 // Make sure we can support multiple priorities that are required
205 // for this test.
206 if (!check_supported_priorities (orb.in ()))
207 return 2;
209 // Thread Manager for managing task.
210 ACE_Thread_Manager thread_manager;
212 // Create task.
213 Task task (thread_manager,
214 orb.in ());
216 // Task activation flags.
217 long flags =
218 THR_NEW_LWP |
219 THR_JOINABLE |
220 orb->orb_core ()->orb_params ()->thread_creation_flags ();
222 // Activate task.
223 result =
224 task.activate (flags);
225 if (result == -1)
227 if (errno == EPERM)
229 ACE_ERROR_RETURN ((LM_ERROR,
230 "Cannot create thread with scheduling policy %s\n"
231 "because the user does not have the appropriate privileges, terminating program....\n"
232 "Check svc.conf options and/or run as root\n",
233 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
236 else
237 // Unexpected error.
238 ACE_ERROR_RETURN ((LM_ERROR,
239 "ERROR: Cannot create thread. errno = %d\n",
240 ACE_ERRNO_GET),
241 -1);
244 // Wait for task to exit.
245 result =
246 thread_manager.wait ();
248 catch (const CORBA::Exception & ae)
250 ae._tao_print_exception (
251 "Caught exception:");
252 return -1;
255 return result;