Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / AMI / client.cpp
blobd68560f8842fade688d5b98a23b6b8ec21e4b194
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * A client which uses the AMI callback model.
8 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
9 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
11 //=============================================================================
14 #include "ace/Get_Opt.h"
15 #include "ace/Task.h"
16 #include "ami_test_i.h"
18 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
19 int nthreads = 5;
20 int niterations = 5;
21 int debug = 0;
22 int shutdown_flag = 0;
23 int perform_work_flag = 1;
24 int number_of_replies = 0;
26 CORBA::Long in_number = 931232;
27 const char * in_str = "Let's talk AMI.";
28 int parameter_corruption = 0;
30 int
31 parse_args (int argc, ACE_TCHAR *argv[])
33 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:n:i:xw"));
34 int c;
36 while ((c = get_opts ()) != -1)
37 switch (c)
39 case 'd':
40 debug = 1;
41 break;
42 case 'k':
43 ior = get_opts.opt_arg ();
44 break;
45 case 'n':
46 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
47 break;
48 case 'i':
49 niterations = ACE_OS::atoi (get_opts.opt_arg ());
50 break;
51 case 'x':
52 shutdown_flag = 1;
53 break;
54 case 'w':
55 perform_work_flag = 0;
56 break;
57 case '?':
58 default:
59 ACE_ERROR_RETURN ((LM_ERROR,
60 "usage: %s "
61 "-d "
62 "-k <ior> "
63 "-n <nthreads> "
64 "-i <niterations> "
65 "-x"
66 "\n",
67 argv [0]),
68 -1);
70 // Indicates successful parsing of the command line
71 return 0;
75 /**
76 * @class Client
78 * @brief Run the client thread
80 * Use the ACE_Task_Base class to run the client threads.
82 class Client : public ACE_Task_Base
84 public:
85 /// ctor
86 Client (A::AMI_Test_ptr server, int niterations);
88 /// The thread entry point.
89 virtual int svc ();
91 // private:
92 /// Var for the AMI_Test object.
93 A::AMI_Test_var ami_test_var_;
95 /// The number of iterations on each client thread.
96 int niterations_;
98 /// Var for AMI_AMI_Test_ReplyHandler object.
99 A::AMI_AMI_TestHandler_var the_handler_var_;
102 class Handler : public POA_A::AMI_AMI_TestHandler
104 public:
105 Handler ()
109 void foo (CORBA::Long result,
110 CORBA::Long out_l)
112 if (result == 0)
114 ACE_ERROR((LM_ERROR, "ERROR: Callback method detected parameter corruption.\n"));
115 parameter_corruption = 1;
118 if (debug)
120 ACE_DEBUG ((LM_DEBUG,
121 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
122 result,
123 out_l));
126 --number_of_replies;
129 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
131 ACE_DEBUG ((LM_DEBUG,
132 "Callback method <foo_excep> called:\n"));
135 excep_holder->raise_exception ();
137 catch (const CORBA::Exception& ex)
139 ex._tao_print_exception ("Caught exception:");
143 void get_yadda (CORBA::Long result)
145 ACE_DEBUG ((LM_DEBUG,
146 "Callback method <get_yadda> called: result <%d>\n",
147 result));
150 void get_yadda_excep (::Messaging::ExceptionHolder *)
152 ACE_DEBUG ((LM_DEBUG,
153 "Callback method <get_yadda_excep> called:\n"));
156 void set_yadda ()
158 ACE_DEBUG ((LM_DEBUG,
159 "Callback method <set_yadda> called:\n"));
162 void set_yadda_excep (::Messaging::ExceptionHolder *)
164 ACE_DEBUG ((LM_DEBUG,
165 "Callback method <set_yadda_excep> called:\n"));
167 ~Handler ()
171 void inout_arg_test (const char *)
173 ACE_DEBUG ((LM_DEBUG,
174 "Callback method <set_yadda_excep> called:\n"));
177 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
182 // ReplyHandler.
183 Handler handler;
186 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
190 CORBA::ORB_var orb =
191 CORBA::ORB_init (argc, argv);
193 if (parse_args (argc, argv) != 0)
194 return 1;
196 A::AMI_Test_var server;
198 CORBA::Object_var object =
199 orb->string_to_object (ior);
200 server = A::AMI_Test::_narrow (object.in ());
202 if (CORBA::is_nil (server.in ()))
204 ACE_ERROR_RETURN ((LM_ERROR,
205 "Object reference <%s> is nil.\n",
206 ior),
210 // Activate POA to handle the call back.
212 CORBA::Object_var poa_object =
213 orb->resolve_initial_references("RootPOA");
215 if (CORBA::is_nil (poa_object.in ()))
216 ACE_ERROR_RETURN ((LM_ERROR,
217 " (%P|%t) Unable to initialize the POA.\n"),
220 PortableServer::POA_var root_poa =
221 PortableServer::POA::_narrow (poa_object.in ());
223 PortableServer::POAManager_var poa_manager =
224 root_poa->the_POAManager ();
226 poa_manager->activate ();
228 // Let the client perform the test in a separate thread
230 Client client (server.in (), niterations);
231 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
232 nthreads) != 0)
233 ACE_ERROR_RETURN ((LM_ERROR,
234 "Cannot activate client threads\n"),
237 // Main thread collects replies. It needs to collect
238 // <nthreads*niterations> replies.
239 number_of_replies = nthreads *niterations;
241 if (perform_work_flag)
243 if (debug)
245 ACE_DEBUG ((LM_DEBUG,
246 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
247 number_of_replies));
250 // ORB loop.
252 while (number_of_replies > 0)
254 CORBA::Boolean pending = orb->work_pending();
256 if (pending)
258 orb->perform_work();
262 if (debug)
264 ACE_DEBUG ((LM_DEBUG,
265 "(%P|%t) : Exited perform_work loop\n"));
269 client.thr_mgr ()->wait ();
271 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
273 if (debug)
275 ACE_DEBUG ((LM_DEBUG,
276 "(%P|%t) : Received <%d> replies\n",
277 (nthreads*niterations) - number_of_replies));
280 if (shutdown_flag)
282 server->shutdown ();
285 root_poa->destroy (true, // ethernalize objects
286 false); // wait for completion
288 orb->destroy ();
290 catch (const CORBA::Exception& ex)
292 ex._tao_print_exception ("Caught exception:");
293 return 1;
296 return parameter_corruption;
299 // ****************************************************************
301 Client::Client (A::AMI_Test_ptr server,
302 int niterations)
303 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
304 niterations_ (niterations)
306 the_handler_var_ = handler._this (/* */);
310 Client::svc ()
314 for (int i = 0; i < this->niterations_; ++i)
316 ami_test_var_->sendc_foo (the_handler_var_.in (), in_number, in_str);
318 if (debug)
320 ACE_DEBUG ((LM_DEBUG,
321 "(%P | %t):<%d> Asynchronous methods issued\n",
322 niterations));
325 catch (const CORBA::Exception& ex)
327 ex._tao_print_exception ("MT_Client: exception raised");
329 return 0;