Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / RTCORBA / Persistent_IOR / server.cpp
blob96e72cfbca7f53e70db80f9136c3a69075035e0c
1 #include "ace/Get_Opt.h"
2 #include "testS.h"
3 #include "tao/RTPortableServer/RTPortableServer.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_string.h"
7 #include "../check_supported_priorities.cpp"
9 class test_i :
10 public POA_test
12 public:
13 test_i (CORBA::ORB_ptr orb,
14 PortableServer::POA_ptr poa);
16 void method ();
18 //FUZZ: disable check_for_lack_ACE_OS
19 void shutdown ();
20 //FUZZ: enable check_for_lack_ACE_OS
22 PortableServer::POA_ptr _default_POA ();
24 private:
25 CORBA::ORB_var orb_;
26 PortableServer::POA_var poa_;
29 test_i::test_i (CORBA::ORB_ptr orb,
30 PortableServer::POA_ptr poa)
31 : orb_ (CORBA::ORB::_duplicate (orb)),
32 poa_ (PortableServer::POA::_duplicate (poa))
36 void
37 test_i::method ()
39 ACE_DEBUG ((LM_DEBUG,
40 "test_i::method\n"));
43 void
44 test_i::shutdown ()
46 ACE_DEBUG ((LM_DEBUG,
47 "test_i::shutdown\n"));
49 this->orb_->shutdown (false);
52 PortableServer::POA_ptr
53 test_i::_default_POA ()
55 return PortableServer::POA::_duplicate (this->poa_.in ());
58 static const ACE_TCHAR *persistent_ior = ACE_TEXT ("persistent_ior");
59 static const ACE_TCHAR *transient_ior = ACE_TEXT ("transient_ior");
60 static const ACE_TCHAR *tp_persistent_ior = ACE_TEXT ("tp_persistent_ior");
61 static int debug = 0;
63 static CORBA::ULong stacksize = 0;
64 static CORBA::ULong static_threads = 2;
65 static CORBA::ULong dynamic_threads = 0;
66 static RTCORBA::Priority default_thread_priority;
67 static CORBA::Boolean allow_request_buffering = 0;
68 static CORBA::ULong max_buffered_requests = 0;
69 static CORBA::ULong max_request_buffer_size = 0;
71 int
72 parse_args (int argc, ACE_TCHAR *argv[])
74 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("a:p:t:d:"));
75 int c;
77 while ((c = get_opts ()) != -1)
78 switch (c)
80 case 'a':
81 tp_persistent_ior = get_opts.opt_arg ();
82 break;
84 case 'p':
85 persistent_ior = get_opts.opt_arg ();
86 break;
88 case 't':
89 transient_ior = get_opts.opt_arg ();
90 break;
92 case 'd':
93 debug = ACE_OS::atoi (get_opts.opt_arg ());
94 break;
96 case '?':
97 default:
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "usage: %s "
100 "-a <tp persistent ior file> "
101 "-p <persistent ior file> "
102 "-t <transient ior file> "
103 "-d <debug> "
104 "\n",
105 argv [0]),
106 -1);
109 // Indicates successful parsing of the command line
110 return 0;
113 static void
114 write_iors_to_file (CORBA::Object_ptr object,
115 CORBA::ORB_ptr orb,
116 const ACE_TCHAR *filename)
118 FILE *file =
119 ACE_OS::fopen (filename, "w");
120 ACE_ASSERT (file != 0);
122 CORBA::String_var ior =
123 orb->object_to_string (object);
125 if (debug)
126 ACE_DEBUG ((LM_DEBUG,
127 "%s: %C\n",
128 filename,
129 ior.in ()));
131 u_int result = 0;
132 result =
133 ACE_OS::fprintf (file,
134 "%s",
135 ior.in ());
137 ACE_ASSERT (result == ACE_OS::strlen (ior.in ()));
138 ACE_UNUSED_ARG (result);
140 ACE_OS::fclose (file);
143 void
144 create_poa_and_servant_with_persistent_policy (CORBA::ORB_ptr orb,
145 PortableServer::POA_ptr root_poa,
146 PortableServer::POAManager_ptr poa_manager)
148 CORBA::PolicyList policies;
150 CORBA::Policy_var implicit_activation_policy =
151 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
153 CORBA::Policy_var lifespan_policy =
154 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
156 policies.length (policies.length () + 1);
157 policies[policies.length () - 1] =
158 implicit_activation_policy;
160 policies.length (policies.length () + 1);
161 policies[policies.length () - 1] =
162 lifespan_policy;
164 PortableServer::POA_var poa =
165 root_poa->create_POA ("persistent_child",
166 poa_manager,
167 policies);
169 test_i *servant = 0;
170 ACE_NEW_THROW_EX (servant,
171 test_i (orb,
172 poa.in ()),
173 CORBA::NO_MEMORY ());
175 PortableServer::ServantBase_var safe_servant (servant);
177 test_var test =
178 servant->_this ();
180 write_iors_to_file (test.in (),
181 orb,
182 persistent_ior);
185 void
186 create_poa_and_servant_with_tp_policy (CORBA::ORB_ptr orb,
187 RTCORBA::RTORB_ptr rt_orb,
188 PortableServer::POA_ptr root_poa,
189 PortableServer::POAManager_ptr poa_manager)
191 RTCORBA::ThreadpoolId threadpool_id =
192 rt_orb->create_threadpool (stacksize,
193 static_threads,
194 dynamic_threads,
195 default_thread_priority,
196 allow_request_buffering,
197 max_buffered_requests,
198 max_request_buffer_size);
200 CORBA::Policy_var threadpool_policy =
201 rt_orb->create_threadpool_policy (threadpool_id);
203 CORBA::Policy_var implicit_activation_policy =
204 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
206 CORBA::PolicyList policies;
208 policies.length (policies.length () + 1);
209 policies[policies.length () - 1] =
210 implicit_activation_policy;
212 policies.length (policies.length () + 1);
213 policies[policies.length () - 1] =
214 threadpool_policy;
216 PortableServer::POA_var poa =
217 root_poa->create_POA ("tp_child",
218 poa_manager,
219 policies);
221 test_i *servant = 0;
222 ACE_NEW_THROW_EX (servant,
223 test_i (orb,
224 poa.in ()),
225 CORBA::NO_MEMORY ());
227 PortableServer::ServantBase_var safe_servant (servant);
229 test_var test =
230 servant->_this ();
232 write_iors_to_file (test.in (),
233 orb,
234 transient_ior);
237 void
238 create_poa_with_tp_and_persistent_policy (CORBA::ORB_ptr orb,
239 RTCORBA::RTORB_ptr rt_orb,
240 PortableServer::POA_ptr root_poa,
241 PortableServer::POAManager_ptr poa_manager)
243 RTCORBA::ThreadpoolId threadpool_id =
244 rt_orb->create_threadpool (stacksize,
245 static_threads,
246 dynamic_threads,
247 default_thread_priority,
248 allow_request_buffering,
249 max_buffered_requests,
250 max_request_buffer_size);
252 CORBA::Policy_var threadpool_policy =
253 rt_orb->create_threadpool_policy (threadpool_id);
255 CORBA::Policy_var implicit_activation_policy =
256 root_poa->create_implicit_activation_policy (PortableServer::IMPLICIT_ACTIVATION);
258 CORBA::Policy_var lifespan_policy =
259 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
261 CORBA::PolicyList policies;
263 policies.length (policies.length () + 1);
264 policies[policies.length () - 1] =
265 implicit_activation_policy;
267 policies.length (policies.length () + 1);
268 policies[policies.length () - 1] =
269 threadpool_policy;
271 policies.length (policies.length () + 1);
272 policies[policies.length () - 1] =
273 lifespan_policy;
275 PortableServer::POA_var poa =
276 root_poa->create_POA ("tp_persistent_child",
277 poa_manager,
278 policies);
280 test_i *servant = 0;
281 ACE_NEW_THROW_EX (servant,
282 test_i (orb,
283 poa.in ()),
284 CORBA::NO_MEMORY ());
286 PortableServer::ServantBase_var safe_servant (servant);
288 test_var test =
289 servant->_this ();
291 write_iors_to_file (test.in (),
292 orb,
293 tp_persistent_ior);
297 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
301 CORBA::ORB_var orb =
302 CORBA::ORB_init (argc, argv);
304 int result =
305 parse_args (argc, argv);
306 if (result != 0)
307 return result;
309 CORBA::Object_var object =
310 orb->resolve_initial_references ("RTORB");
312 RTCORBA::RTORB_var rt_orb =
313 RTCORBA::RTORB::_narrow (object.in ());
315 object =
316 orb->resolve_initial_references ("RTCurrent");
318 RTCORBA::Current_var current =
319 RTCORBA::Current::_narrow (object.in ());
321 default_thread_priority =
322 get_implicit_thread_CORBA_priority (orb.in ());
324 object =
325 orb->resolve_initial_references ("RootPOA");
327 PortableServer::POA_var root_poa =
328 PortableServer::POA::_narrow (object.in ());
330 PortableServer::POAManager_var poa_manager =
331 root_poa->the_POAManager ();
333 create_poa_and_servant_with_tp_policy (orb.in (),
334 rt_orb.in (),
335 root_poa.in (),
336 poa_manager.in ());
338 create_poa_and_servant_with_persistent_policy (orb.in (),
339 root_poa.in (),
340 poa_manager.in ());
342 create_poa_with_tp_and_persistent_policy (orb.in (),
343 rt_orb.in (),
344 root_poa.in (),
345 poa_manager.in ());
347 poa_manager->activate ();
349 orb->run ();
351 orb->destroy ();
353 catch (const CORBA::INTERNAL& ex)
355 if ((ex.minor() & 0x1F) == 9) {
356 ACE_DEBUG ((LM_DEBUG, "You must be superuser to run "
357 "this test on this platform.\n"));
358 return 0;
360 else {
361 ex._tao_print_exception ("Exception caught");
362 return -1;
365 catch (const CORBA::Exception& ex)
367 ex._tao_print_exception ("Exception caught");
368 return -1;
371 return 0;