Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Bug_2805_Regression / client.cpp
blobfaaef05982467e8b2e0b990c5c31497d352270a8
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 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/Task.h"
15 #include "ace/Atomic_Op.h"
16 #include "ace/Synch_Traits.h"
17 #include "ace/OS_NS_Thread.h"
18 #include "ami_test_i.h"
20 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
21 int nthreads = 5;
22 int niterations = 5;
23 int debug = 0;
24 ACE_Atomic_Op<TAO_SYNCH_MUTEX, 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:"));
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 '?':
52 default:
53 ACE_ERROR_RETURN ((LM_ERROR,
54 "usage: %s "
55 "-d "
56 "-k <ior> "
57 "-n <nthreads> "
58 "-i <niterations> "
59 "\n",
60 argv [0]),
61 -1);
63 // Indicates successful parsing of the command line
64 return 0;
68 /**
69 * @class Client
71 * @brief Run the client thread
73 * Use the ACE_Task_Base class to run the client threads.
75 class Client : public ACE_Task_Base
77 public:
78 /// ctor
79 Client (A::AMI_Test_ptr server, int niterations);
81 /// The thread entry point.
82 virtual int svc ();
84 // private:
85 /// Var for the AMI_Test object.
86 A::AMI_Test_var ami_test_var_;
88 /// The number of iterations on each client thread.
89 int niterations_;
91 /// Var for AMI_AMI_Test_ReplyHandler object.
92 A::AMI_AMI_TestHandler_var the_handler_var_;
95 class Handler : public POA_A::AMI_AMI_TestHandler
97 public:
98 Handler () = default;
100 void foo (CORBA::Long result,
101 CORBA::Long out_l)
103 if (result == 0)
105 ACE_ERROR((LM_ERROR, "ERROR: Callback method detected parameter corruption.\n"));
106 parameter_corruption = 1;
109 int const reply = --number_of_replies;
111 if (debug)
113 ACE_DEBUG ((LM_DEBUG,
114 "(%P | %t) : Callback method %d called: result <%d>, out_arg <%d>\n",
115 (nthreads * niterations) - reply,
116 result,
117 out_l));
119 ACE_UNUSED_ARG (reply);
122 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
124 ACE_DEBUG ((LM_DEBUG,
125 "Callback method <foo_excep> called:\n"));
128 excep_holder->raise_exception ();
130 catch (const CORBA::Exception& ex)
132 ex._tao_print_exception ("Caught exception:");
136 void get_yadda (CORBA::Long result)
138 ACE_DEBUG ((LM_DEBUG,
139 "Callback method <get_yadda> called: result <%d>\n",
140 result));
143 void get_yadda_excep (::Messaging::ExceptionHolder *)
145 ACE_DEBUG ((LM_DEBUG,
146 "Callback method <get_yadda_excep> called:\n"));
149 void set_yadda ()
151 ACE_DEBUG ((LM_DEBUG,
152 "Callback method <set_yadda> called:\n"));
155 void set_yadda_excep (::Messaging::ExceptionHolder *)
157 ACE_DEBUG ((LM_DEBUG,
158 "Callback method <set_yadda_excep> called:\n"));
160 ~Handler () = default;
162 void inout_arg_test (const char *)
164 ACE_DEBUG ((LM_DEBUG,
165 "Callback method <set_yadda_excep> called:\n"));
168 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
173 // ReplyHandler.
174 Handler handler;
177 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
181 CORBA::ORB_var orb =
182 CORBA::ORB_init (argc, argv);
184 if (parse_args (argc, argv) != 0)
185 return 1;
187 A::AMI_Test_var server;
189 AMI_Test_i * servant =
190 new AMI_Test_i(orb.in(), in_number, in_str, true);
191 PortableServer::ServantBase_var safe (servant);
193 server = servant->_this();
195 if (CORBA::is_nil (server.in ()))
197 ACE_ERROR_RETURN ((LM_ERROR,
198 "Object reference <%s> is nil.\n",
199 ior),
203 // Activate POA to handle the call back.
205 CORBA::Object_var poa_object =
206 orb->resolve_initial_references("RootPOA");
208 if (CORBA::is_nil (poa_object.in ()))
209 ACE_ERROR_RETURN ((LM_ERROR,
210 " (%P|%t) Unable to initialize the POA.\n"),
213 PortableServer::POA_var root_poa =
214 PortableServer::POA::_narrow (poa_object.in ());
216 PortableServer::POAManager_var poa_manager =
217 root_poa->the_POAManager ();
219 poa_manager->activate ();
221 // Main thread collects replies. It needs to collect
222 // <nthreads*niterations> replies.
223 number_of_replies = nthreads * niterations;
225 // Let the client perform the test in a separate thread
227 Client client (server.in (), niterations);
228 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
229 nthreads) != 0)
230 ACE_ERROR_RETURN ((LM_ERROR,
231 "Cannot activate client threads\n"),
234 if (debug)
236 ACE_DEBUG ((LM_DEBUG,
237 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
238 number_of_replies.value ()));
241 // ORB loop.
243 while (number_of_replies > 0)
245 CORBA::Boolean pending = orb->work_pending();
247 if (pending)
249 orb->perform_work();
252 // On some systems this loop must yield or else the other threads
253 // will not get a chance to run.
254 ACE_OS::thr_yield();
257 if (debug)
259 ACE_DEBUG ((LM_DEBUG,
260 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
261 (nthreads*niterations) - number_of_replies.value ()));
264 client.thr_mgr ()->wait ();
266 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
268 root_poa->destroy (true, // ethernalize objects
269 false); // wait for completion
271 orb->destroy ();
273 catch (const CORBA::Exception& ex)
275 ex._tao_print_exception ("Caught exception:");
276 return 1;
279 return parameter_corruption;
282 // ****************************************************************
284 Client::Client (A::AMI_Test_ptr server,
285 int niterations)
286 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
287 niterations_ (niterations)
289 the_handler_var_ = handler._this (/* */);
293 Client::svc ()
297 for (int i = 0; i < this->niterations_; ++i)
299 ami_test_var_->sendc_foo (the_handler_var_.in (), in_number, in_str);
301 if (debug)
303 ACE_DEBUG ((LM_DEBUG,
304 "(%P | %t):<%d> Asynchronous methods issued\n",
305 niterations));
308 catch (const CORBA::Exception& ex)
310 ex._tao_print_exception ("MT_Client: exception raised");
312 return 0;