Use =default for skeleton copy constructor
[ACE_TAO.git] / TAO / tests / RTCORBA / Bug_3382_Regression / server.cpp
blobad4588a3b80ccb10e4db9a271db0cfc1ea95d21f
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 void shutdown ();
23 private:
24 CORBA::ORB_var orb_;
25 // The ORB
28 Test_i::Test_i (CORBA::ORB_ptr orb)
29 : orb_ (CORBA::ORB::_duplicate (orb))
33 void
34 Test_i::test_method (CORBA::Short priority)
36 // Use RTCurrent to find out the CORBA priority of the current
37 // thread.
39 CORBA::Object_var obj =
40 this->orb_->resolve_initial_references ("RTCurrent");
42 RTCORBA::Current_var current =
43 RTCORBA::Current::_narrow (obj.in ());
45 if (CORBA::is_nil (obj.in ()))
46 throw CORBA::INTERNAL ();
48 CORBA::Short servant_thread_priority =
49 current->the_priority ();
51 // Print out the info.
52 if (servant_thread_priority != priority)
53 ACE_DEBUG ((LM_DEBUG,
54 "ERROR: servant thread priority is not equal "
55 "to method argument.\n"));
57 ACE_DEBUG ((LM_DEBUG,
58 "Client priority: %d "
59 "Servant thread priority: %d\n",
60 priority, servant_thread_priority));
63 void
64 Test_i::shutdown ()
66 this->orb_->shutdown (false);
69 //*************************************************************************
71 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
73 // Parse command-line arguments.
74 int
75 parse_args (int argc, ACE_TCHAR *argv[])
77 ACE_Get_Opt get_opts (argc, argv, "o:");
78 int c;
80 while ((c = get_opts ()) != -1)
81 switch (c)
83 case 'o':
84 ior_output_file = get_opts.opt_arg ();
85 break;
87 case '?':
88 default:
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "usage: %s "
91 "-o <iorfile>"
92 "\n",
93 argv [0]),
94 -1);
97 return 0;
100 class Task : public ACE_Task_Base
102 public:
103 Task (ACE_Thread_Manager &thread_manager,
104 CORBA::ORB_ptr orb);
106 int svc ();
108 CORBA::ORB_var orb_;
111 Task::Task (ACE_Thread_Manager &thread_manager,
112 CORBA::ORB_ptr orb)
113 : ACE_Task_Base (&thread_manager),
114 orb_ (CORBA::ORB::_duplicate (orb))
119 Task::svc ()
123 CORBA::Object_var object =
124 this->orb_->resolve_initial_references("RootPOA");
126 PortableServer::POA_var root_poa =
127 PortableServer::POA::_narrow (object.in ());
129 if (CORBA::is_nil (root_poa.in ()))
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "ERROR: Panic <RootPOA> is nil\n"),
132 -1);
134 PortableServer::POAManager_var poa_manager =
135 root_poa->the_POAManager ();
137 object = this->orb_->resolve_initial_references ("RTORB");
139 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
142 object =
143 this->orb_->resolve_initial_references ("RTCurrent");
145 RTCORBA::Current_var current =
146 RTCORBA::Current::_narrow (object.in ());
148 // Create POA with CLIENT_PROPAGATED PriorityModelPolicy,
149 // and register Test object with it.
150 CORBA::PolicyList poa_policy_list;
151 poa_policy_list.length (1);
152 poa_policy_list[0] =
153 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
156 PortableServer::POA_var child_poa =
157 root_poa->create_POA ("Child_POA",
158 poa_manager.in (),
159 poa_policy_list);
161 Test_i server_impl (this->orb_.in ());
163 PortableServer::ObjectId_var id =
164 child_poa->activate_object (&server_impl);
166 CORBA::Object_var server =
167 child_poa->id_to_reference (id.in ());
169 // Print Object IOR.
170 CORBA::String_var ior =
171 this->orb_->object_to_string (server.in ());
173 ACE_DEBUG ((LM_DEBUG, "Activated as <%s>\n\n", ior.in ()));
175 if (ior_output_file != 0)
177 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
178 if (output_file == 0)
179 ACE_ERROR_RETURN ((LM_ERROR,
180 "Cannot open output file for writing IOR: %s",
181 ior_output_file),
182 -1);
183 ACE_OS::fprintf (output_file, "%s", ior.in ());
184 ACE_OS::fclose (output_file);
187 // Get the initial priority of the current thread.
188 CORBA::Short initial_thread_priority =
189 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(
218 "Exception caught:");
219 return -1;
222 return 0;
226 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
228 int result = 0;
232 // Standard initialization:
233 // parse arguments and get all the references (ORB,
234 // RootPOA, RTORB, RTCurrent, POAManager).
235 CORBA::ORB_var orb =
236 CORBA::ORB_init (argc, argv);
238 if (parse_args (argc, argv) != 0)
239 return -1;
241 // Make sure we can support multiple priorities that are required
242 // for this test.
243 if (!check_supported_priorities (orb.in ()))
244 return 2;
246 // Thread Manager for managing task.
247 ACE_Thread_Manager thread_manager;
249 // Create task.
250 Task task (thread_manager,
251 orb.in ());
253 // Task activation flags.
254 long flags =
255 THR_NEW_LWP |
256 THR_JOINABLE |
257 orb->orb_core ()->orb_params ()->thread_creation_flags ();
259 // Activate task.
260 result =
261 task.activate (flags);
262 if (result == -1)
264 if (errno == EPERM)
266 ACE_ERROR_RETURN ((LM_ERROR,
267 "Cannot create thread with scheduling policy %s\n"
268 "because the user does not have the appropriate privileges, terminating program....\n"
269 "Check svc.conf options and/or run as root\n",
270 sched_policy_name (orb->orb_core ()->orb_params ()->ace_sched_policy ())),
273 else
274 // Unexpected error.
275 ACE_ERROR_RETURN ((LM_ERROR,
276 "ERROR: Cannot create thread. errno = %d\n",
277 ACE_ERRNO_GET),
278 -1);
281 // Wait for task to exit.
282 result =
283 thread_manager.wait ();
285 catch (const ::CORBA::Exception & ex)
287 ex._tao_print_exception(
288 "Exception caught:");
289 return -1;
292 return result;