Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2805_Regression / client.cpp
blob8ffd6a12f679cc36ec58970ce172fee2e3a87398
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 "ace/Atomic_Op.h"
17 #include "ace/Synch_Traits.h"
18 #include "ace/OS_NS_Thread.h"
19 #include "ami_test_i.h"
21 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
22 int nthreads = 5;
23 int niterations = 5;
24 int debug = 0;
25 ACE_Atomic_Op<TAO_SYNCH_MUTEX, int> number_of_replies = 0;
27 CORBA::Long in_number = 931232;
28 const char * in_str = "Let's talk AMI.";
29 int parameter_corruption = 0;
31 int
32 parse_args (int argc, ACE_TCHAR *argv[])
34 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:n:i:"));
35 int c;
37 while ((c = get_opts ()) != -1)
38 switch (c)
40 case 'd':
41 debug = 1;
42 break;
43 case 'k':
44 ior = get_opts.opt_arg ();
45 break;
46 case 'n':
47 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
48 break;
49 case 'i':
50 niterations = ACE_OS::atoi (get_opts.opt_arg ());
51 break;
52 case '?':
53 default:
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "usage: %s "
56 "-d "
57 "-k <ior> "
58 "-n <nthreads> "
59 "-i <niterations> "
60 "\n",
61 argv [0]),
62 -1);
64 // Indicates successful parsing of the command line
65 return 0;
69 /**
70 * @class Client
72 * @brief Run the client thread
74 * Use the ACE_Task_Base class to run the client threads.
76 class Client : public ACE_Task_Base
78 public:
79 /// ctor
80 Client (A::AMI_Test_ptr server, int niterations);
82 /// The thread entry point.
83 virtual int svc (void);
85 // private:
86 /// Var for the AMI_Test object.
87 A::AMI_Test_var ami_test_var_;
89 /// The number of iterations on each client thread.
90 int niterations_;
92 /// Var for AMI_AMI_Test_ReplyHandler object.
93 A::AMI_AMI_TestHandler_var the_handler_var_;
96 class Handler : public POA_A::AMI_AMI_TestHandler
98 public:
99 Handler (void)
103 void foo (CORBA::Long result,
104 CORBA::Long out_l)
106 if (result == 0)
108 ACE_ERROR((LM_ERROR, "ERROR: Callback method detected parameter corruption.\n"));
109 parameter_corruption = 1;
112 int const reply = --number_of_replies;
114 if (debug)
116 ACE_DEBUG ((LM_DEBUG,
117 "(%P | %t) : Callback method %d called: result <%d>, out_arg <%d>\n",
118 (nthreads * niterations) - reply,
119 result,
120 out_l));
122 ACE_UNUSED_ARG (reply);
125 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
128 ACE_DEBUG ((LM_DEBUG,
129 "Callback method <foo_excep> called:\n"));
132 excep_holder->raise_exception ();
134 catch (const CORBA::Exception& ex)
136 ex._tao_print_exception ("Caught exception:");
140 void get_yadda (CORBA::Long result)
142 ACE_DEBUG ((LM_DEBUG,
143 "Callback method <get_yadda> called: result <%d>\n",
144 result));
147 void get_yadda_excep (::Messaging::ExceptionHolder *)
149 ACE_DEBUG ((LM_DEBUG,
150 "Callback method <get_yadda_excep> called:\n"));
153 void set_yadda (void)
155 ACE_DEBUG ((LM_DEBUG,
156 "Callback method <set_yadda> called:\n"));
159 void set_yadda_excep (::Messaging::ExceptionHolder *)
161 ACE_DEBUG ((LM_DEBUG,
162 "Callback method <set_yadda_excep> called:\n"));
164 ~Handler (void)
168 void inout_arg_test (const char *)
170 ACE_DEBUG ((LM_DEBUG,
171 "Callback method <set_yadda_excep> called:\n"));
174 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
179 // ReplyHandler.
180 Handler handler;
183 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
187 CORBA::ORB_var orb =
188 CORBA::ORB_init (argc, argv);
190 if (parse_args (argc, argv) != 0)
191 return 1;
193 A::AMI_Test_var server;
195 AMI_Test_i * servant =
196 new AMI_Test_i(orb.in(), in_number, in_str, true);
197 PortableServer::ServantBase_var safe (servant);
199 server = servant->_this();
201 if (CORBA::is_nil (server.in ()))
203 ACE_ERROR_RETURN ((LM_ERROR,
204 "Object reference <%s> is nil.\n",
205 ior),
209 // Activate POA to handle the call back.
211 CORBA::Object_var poa_object =
212 orb->resolve_initial_references("RootPOA");
214 if (CORBA::is_nil (poa_object.in ()))
215 ACE_ERROR_RETURN ((LM_ERROR,
216 " (%P|%t) Unable to initialize the POA.\n"),
219 PortableServer::POA_var root_poa =
220 PortableServer::POA::_narrow (poa_object.in ());
222 PortableServer::POAManager_var poa_manager =
223 root_poa->the_POAManager ();
225 poa_manager->activate ();
227 // Main thread collects replies. It needs to collect
228 // <nthreads*niterations> replies.
229 number_of_replies = nthreads * niterations;
231 // Let the client perform the test in a separate thread
233 Client client (server.in (), niterations);
234 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
235 nthreads) != 0)
236 ACE_ERROR_RETURN ((LM_ERROR,
237 "Cannot activate client threads\n"),
240 if (debug)
242 ACE_DEBUG ((LM_DEBUG,
243 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
244 number_of_replies.value ()));
247 // ORB loop.
249 while (number_of_replies > 0)
251 CORBA::Boolean pending = orb->work_pending();
253 if (pending)
255 orb->perform_work();
258 // On some systems this loop must yield or else the other threads
259 // will not get a chance to run.
260 ACE_OS::thr_yield();
263 if (debug)
265 ACE_DEBUG ((LM_DEBUG,
266 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
267 (nthreads*niterations) - number_of_replies.value ()));
270 client.thr_mgr ()->wait ();
272 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
274 root_poa->destroy (1, // ethernalize objects
275 0); // wait for completion
277 orb->destroy ();
279 catch (const CORBA::Exception& ex)
281 ex._tao_print_exception ("Caught exception:");
282 return 1;
285 return parameter_corruption;
288 // ****************************************************************
290 Client::Client (A::AMI_Test_ptr server,
291 int niterations)
292 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
293 niterations_ (niterations)
295 the_handler_var_ = handler._this (/* */);
299 Client::svc (void)
303 for (int i = 0; i < this->niterations_; ++i)
305 ami_test_var_->sendc_foo (the_handler_var_.in (), in_number, in_str);
307 if (debug)
309 ACE_DEBUG ((LM_DEBUG,
310 "(%P | %t):<%d> Asynchronous methods issued\n",
311 niterations));
314 catch (const CORBA::Exception& ex)
316 ex._tao_print_exception ("MT_Client: exception raised");
318 return 0;