Use =default for skeleton copy constructor
[ACE_TAO.git] / TAO / tests / RTCORBA / Client_Propagated / client.cpp
blobb91f6002b6005a16a52afbafbad663203fe13a5b
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, ACE_TEXT("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 try
61 CORBA::Object_var object =
62 this->orb_->string_to_object (ior);
64 Test_var server =
65 Test::_narrow (object.in ());
67 if (CORBA::is_nil (server.in ()))
69 ACE_ERROR_RETURN ((LM_ERROR,
70 "ERROR: Object reference <%s> is nil\n",
71 ior),
72 -1);
75 // Check that the object is configured with CLIENT_PROPAGATED
76 // PriorityModelPolicy.
77 CORBA::Policy_var policy =
78 server->_get_policy (RTCORBA::PRIORITY_MODEL_POLICY_TYPE);
80 RTCORBA::PriorityModelPolicy_var priority_policy =
81 RTCORBA::PriorityModelPolicy::_narrow (policy.in ());
83 if (CORBA::is_nil (priority_policy.in ()))
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "ERROR: Priority Model Policy not exposed!\n"),
86 -1);
88 RTCORBA::PriorityModel priority_model =
89 priority_policy->priority_model ();
91 if (priority_model != RTCORBA::CLIENT_PROPAGATED)
92 ACE_ERROR_RETURN ((LM_ERROR,
93 "ERROR: priority_model != "
94 "RTCORBA::CLIENT_PROPAGATED!\n"),
95 -1);
97 // Make several invocation, changing the priority of this thread
98 // for each.
99 object =
100 this->orb_->resolve_initial_references ("RTCurrent");
101 RTCORBA::Current_var current =
102 RTCORBA::Current::_narrow (object.in ());
104 object = this->orb_->resolve_initial_references ("PriorityMappingManager");
105 RTCORBA::PriorityMappingManager_var mapping_manager =
106 RTCORBA::PriorityMappingManager::_narrow (object.in ());
108 RTCORBA::PriorityMapping *pm =
109 mapping_manager->mapping ();
111 int sched_policy =
112 this->orb_->orb_core ()->orb_params ()->ace_sched_policy ();
114 int max_priority =
115 ACE_Sched_Params::priority_max (sched_policy);
116 int min_priority =
117 ACE_Sched_Params::priority_min (sched_policy);
119 CORBA::Short native_priority =
120 (max_priority + min_priority) / 2;
122 CORBA::Short desired_priority = 0;
124 if (pm->to_CORBA (native_priority, desired_priority) == 0)
125 ACE_ERROR_RETURN ((LM_ERROR,
126 "Cannot convert native priority %d to corba priority\n",
127 native_priority),
128 -1);
130 for (int i = 0; i < 3; ++i)
132 current->the_priority (desired_priority);
134 CORBA::Short priority =
135 current->the_priority ();
137 if (desired_priority != priority)
138 ACE_ERROR_RETURN ((LM_ERROR,
139 "ERROR: Unable to set thread "
140 "priority to %d\n", desired_priority),
141 -1);
144 server->test_method (priority);
146 desired_priority++;
149 // Shut down Server ORB.
150 server->shutdown ();
152 catch (const CORBA::DATA_CONVERSION& ex)
154 ex._tao_print_exception (
155 "Most likely, this is due to the in-ability ""to set the thread priority.");
156 return -1;
158 catch (const CORBA::Exception& ex)
160 ex._tao_print_exception ("Caught exception:");
161 return -1;
164 return 0;
168 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
172 // Register the interceptors to check for the RTCORBA
173 // service contexts in the reply messages.
174 PortableInterceptor::ORBInitializer_ptr temp_initializer;
176 ACE_NEW_RETURN (temp_initializer,
177 Client_ORBInitializer,
178 -1); // No exceptions yet!
179 PortableInterceptor::ORBInitializer_var initializer =
180 temp_initializer;
182 PortableInterceptor::register_orb_initializer (initializer.in ());
184 // Initialize and obtain reference to the Test object.
185 CORBA::ORB_var orb =
186 CORBA::ORB_init (argc, argv);
188 if (parse_args (argc, argv) != 0)
189 return -1;
191 // Make sure we can support multiple priorities that are required
192 // for this test.
193 if (!check_supported_priorities (orb.in ()))
194 return 2;
196 // Thread Manager for managing task.
197 ACE_Thread_Manager thread_manager;
199 // Create task.
200 Task task (thread_manager,
201 orb.in ());
203 // Task activation flags.
204 long flags =
205 THR_NEW_LWP |
206 THR_JOINABLE |
207 orb->orb_core ()->orb_params ()->thread_creation_flags ();
209 // Activate task.
210 int result =
211 task.activate (flags);
212 if (result == -1)
214 if (errno == EPERM)
216 ACE_ERROR_RETURN ((LM_ERROR,
217 "Cannot create thread with scheduling policy %s\n"
218 "because the user does not have the appropriate privileges, terminating program....\n"
219 "Check svc.conf options and/or run as root\n",
220 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
223 else
224 // Unexpected error.
225 ACE_ASSERT (0);
228 // Wait for task to exit.
229 result =
230 thread_manager.wait ();
231 ACE_ASSERT (result != -1);
233 catch (const CORBA::Exception& ex)
235 ex._tao_print_exception ("Caught exception:");
236 return -1;
239 return 0;