Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Bug_3567_Regression / client.cpp
blob42fa8210ad865ebe9f9e5ff74457dd4e642d71e9
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * A client which uses the AMI callback model.
7 */
8 //=============================================================================
11 #include "ace/Get_Opt.h"
12 #include "ace/Task.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ami_test_i.h"
16 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
17 int nthreads = 5;
18 int niterations = 10;
19 int debug = 0;
20 ACE_Atomic_Op <TAO_SYNCH_MUTEX, long> number_of_replies = 0;
22 CORBA::Long in_number = 931232;
23 const char * in_str = "Let's talk AMI.";
24 int parameter_corruption = 0;
26 int
27 parse_args (int argc, ACE_TCHAR *argv[])
29 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:n:i:"));
30 int c;
32 while ((c = get_opts ()) != -1)
33 switch (c)
35 case 'd':
36 debug = 1;
37 break;
38 case 'k':
39 ior = get_opts.opt_arg ();
40 break;
41 case 'n':
42 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
43 break;
44 case 'i':
45 niterations = ACE_OS::atoi (get_opts.opt_arg ());
46 break;
47 case '?':
48 default:
49 ACE_ERROR_RETURN ((LM_ERROR,
50 "usage: %s "
51 "-d "
52 "-k <ior> "
53 "-n <nthreads> "
54 "-i <niterations> "
55 "\n",
56 argv [0]),
57 -1);
59 // Indicates successful parsing of the command line
60 return 0;
63 /**
64 * Run a server thread
66 * Use the ACE_Task_Base class to run server threads
68 class Worker : public ACE_Task_Base
70 public:
71 Worker (CORBA::ORB_ptr orb);
72 // ctor
74 virtual int svc ();
75 // The thread entry point.
77 private:
78 CORBA::ORB_var orb_;
79 // The orb
83 /**
84 * @class Client
86 * @brief Run the client thread
88 * Use the ACE_Task_Base class to run the client threads.
90 class Client : public ACE_Task_Base
92 public:
93 /// ctor
94 Client (A::AMI_Test_ptr server, int niterations, A::AMI_AMI_TestHandler_ptr hnd);
95 /// dtor
96 ~Client ();
98 /// The thread entry point.
99 virtual int svc ();
101 // private:
102 /// Var for the AMI_Test object.
103 A::AMI_Test_var ami_test_var_;
105 /// The number of iterations on each client thread.
106 int niterations_;
108 /// Var for AMI_AMI_Test_ReplyHandler object.
109 A::AMI_AMI_TestHandler_var the_handler_var_;
112 class Handler : public POA_A::AMI_AMI_TestHandler
114 public:
115 Handler ()
119 void set_ami_test (A::AMI_Test_ptr ami_test)
121 ami_test_var_ = A::AMI_Test::_duplicate (ami_test);
124 void foo (CORBA::Long result,
125 CORBA::Long out_l)
127 if (result == 0)
129 ACE_ERROR((LM_ERROR, "ERROR: Callback method detected parameter corruption.\n"));
130 parameter_corruption = 1;
133 if (debug)
135 ACE_DEBUG ((LM_DEBUG,
136 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
137 result,
138 out_l));
140 ACE_OS::sleep (1);
141 ami_test_var_->sendc_set_yadda (0, 5);
143 --number_of_replies;
146 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
148 ACE_DEBUG ((LM_DEBUG,
149 "Callback method <foo_excep> called:\n"));
152 excep_holder->raise_exception ();
154 catch (const CORBA::Exception& ex)
156 ex._tao_print_exception ("Caught exception:");
160 void get_yadda (CORBA::Long result)
162 ACE_DEBUG ((LM_DEBUG,
163 "Callback method <get_yadda> called: result <%d>\n",
164 result));
167 void get_yadda_excep (::Messaging::ExceptionHolder *)
169 ACE_DEBUG ((LM_DEBUG,
170 "Callback method <get_yadda_excep> called:\n"));
173 void set_yadda ()
175 ACE_DEBUG ((LM_DEBUG,
176 "Callback method <set_yadda> called:\n"));
179 void set_yadda_excep (::Messaging::ExceptionHolder *)
181 ACE_DEBUG ((LM_DEBUG,
182 "Callback method <set_yadda_excep> called:\n"));
184 ~Handler ()
188 void inout_arg_test (const char *)
190 ACE_DEBUG ((LM_DEBUG,
191 "Callback method <set_yadda_excep> called:\n"));
194 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
198 A::AMI_Test_var ami_test_var_;
202 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
206 CORBA::ORB_var orb =
207 CORBA::ORB_init (argc, argv);
209 if (parse_args (argc, argv) != 0)
210 return 1;
212 A::AMI_Test_var server;
214 CORBA::Object_var object =
215 orb->string_to_object (ior);
216 server = A::AMI_Test::_narrow (object.in ());
218 if (CORBA::is_nil (server.in ()))
220 ACE_ERROR_RETURN ((LM_ERROR,
221 "Object reference <%s> is nil.\n",
222 ior),
226 // Activate POA to handle the call back.
228 CORBA::Object_var poa_object =
229 orb->resolve_initial_references("RootPOA");
231 if (CORBA::is_nil (poa_object.in ()))
232 ACE_ERROR_RETURN ((LM_ERROR,
233 " (%P|%t) Unable to initialize the POA.\n"),
236 PortableServer::POA_var root_poa =
237 PortableServer::POA::_narrow (poa_object.in ());
239 PortableServer::POAManager_var poa_manager =
240 root_poa->the_POAManager ();
242 poa_manager->activate ();
244 // Let the client perform the test in a separate thread
245 Handler handler;
246 PortableServer::ObjectId_var id =
247 root_poa->activate_object (&handler);
249 CORBA::Object_var hnd_object = root_poa->id_to_reference (id.in ());
251 A::AMI_AMI_TestHandler_var the_handler_var =
252 A::AMI_AMI_TestHandler::_narrow (hnd_object.in ());
254 handler.set_ami_test (server.in ());
256 Client client (server.in (), niterations, the_handler_var.in ());
257 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
258 nthreads) != 0)
259 ACE_ERROR_RETURN ((LM_ERROR,
260 "Cannot activate client threads\n"),
263 // Main thread collects replies. It needs to collect
264 // <nthreads*niterations> replies.
265 number_of_replies = nthreads *niterations;
267 if (debug)
269 ACE_DEBUG ((LM_DEBUG,
270 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
271 number_of_replies.value ()));
274 // ORB loop.
276 Worker worker (orb.in ());
277 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
278 nthreads) != 0)
279 ACE_ERROR_RETURN ((LM_ERROR,
280 "Cannot activate client threads\n"),
283 worker.thr_mgr ()->wait ();
285 if (debug)
287 ACE_DEBUG ((LM_DEBUG,
288 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
289 (nthreads*niterations) - number_of_replies.value ()));
292 client.thr_mgr ()->wait ();
294 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
296 server->shutdown ();
298 root_poa->destroy (true, // ethernalize objects
299 false); // wait for completion
301 orb->destroy ();
303 catch (const CORBA::Exception& ex)
305 ex._tao_print_exception ("Caught exception:");
306 return 1;
309 return parameter_corruption;
312 // ****************************************************************
314 Client::Client (A::AMI_Test_ptr server,
315 int niterations,
316 A::AMI_AMI_TestHandler_ptr hnd)
317 : ami_test_var_ (A::AMI_Test::_duplicate (server))
318 , niterations_ (niterations)
319 , the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (hnd))
323 Client::~Client ()
328 Client::svc ()
332 for (int i = 0; i < this->niterations_; ++i)
334 ami_test_var_->sendc_foo (the_handler_var_.in (), in_number, in_str);
336 if (debug)
338 ACE_DEBUG ((LM_DEBUG,
339 "(%P|%t) <%d> Asynchronous methods issued\n",
340 niterations));
343 catch (const CORBA::Exception& ex)
345 ex._tao_print_exception ("MT_Client: exception raised");
347 return 0;
350 Worker::Worker (CORBA::ORB_ptr orb)
351 : orb_ (CORBA::ORB::_duplicate (orb))
356 Worker::svc ()
358 while (number_of_replies > 0)
360 CORBA::Boolean pending = this->orb_->work_pending();
362 if (pending)
364 ACE_Time_Value tm (1, 0);
365 this->orb_->perform_work(tm);
369 return 0;