More tests update
[ACE_TAO.git] / TAO / tests / Bug_3567_Regression / client.cpp
blob3cc35b7b4085aad5bd5203c246e16c1ab5f3e7ad
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 (void);
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 (void);
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 (void)
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)
149 ACE_DEBUG ((LM_DEBUG,
150 "Callback method <foo_excep> called:\n"));
153 excep_holder->raise_exception ();
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Caught exception:");
161 void get_yadda (CORBA::Long result)
163 ACE_DEBUG ((LM_DEBUG,
164 "Callback method <get_yadda> called: result <%d>\n",
165 result));
168 void get_yadda_excep (::Messaging::ExceptionHolder *)
170 ACE_DEBUG ((LM_DEBUG,
171 "Callback method <get_yadda_excep> called:\n"));
174 void set_yadda (void)
176 ACE_DEBUG ((LM_DEBUG,
177 "Callback method <set_yadda> called:\n"));
180 void set_yadda_excep (::Messaging::ExceptionHolder *)
182 ACE_DEBUG ((LM_DEBUG,
183 "Callback method <set_yadda_excep> called:\n"));
185 ~Handler (void)
189 void inout_arg_test (const char *)
191 ACE_DEBUG ((LM_DEBUG,
192 "Callback method <set_yadda_excep> called:\n"));
195 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
199 A::AMI_Test_var ami_test_var_;
203 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
207 CORBA::ORB_var orb =
208 CORBA::ORB_init (argc, argv);
210 if (parse_args (argc, argv) != 0)
211 return 1;
213 A::AMI_Test_var server;
215 CORBA::Object_var object =
216 orb->string_to_object (ior);
217 server = A::AMI_Test::_narrow (object.in ());
219 if (CORBA::is_nil (server.in ()))
221 ACE_ERROR_RETURN ((LM_ERROR,
222 "Object reference <%s> is nil.\n",
223 ior),
227 // Activate POA to handle the call back.
229 CORBA::Object_var poa_object =
230 orb->resolve_initial_references("RootPOA");
232 if (CORBA::is_nil (poa_object.in ()))
233 ACE_ERROR_RETURN ((LM_ERROR,
234 " (%P|%t) Unable to initialize the POA.\n"),
237 PortableServer::POA_var root_poa =
238 PortableServer::POA::_narrow (poa_object.in ());
240 PortableServer::POAManager_var poa_manager =
241 root_poa->the_POAManager ();
243 poa_manager->activate ();
245 // Let the client perform the test in a separate thread
246 Handler handler;
247 PortableServer::ObjectId_var id =
248 root_poa->activate_object (&handler);
250 CORBA::Object_var hnd_object = root_poa->id_to_reference (id.in ());
252 A::AMI_AMI_TestHandler_var the_handler_var =
253 A::AMI_AMI_TestHandler::_narrow (hnd_object.in ());
255 handler.set_ami_test (server.in ());
257 Client client (server.in (), niterations, the_handler_var.in ());
258 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
259 nthreads) != 0)
260 ACE_ERROR_RETURN ((LM_ERROR,
261 "Cannot activate client threads\n"),
264 // Main thread collects replies. It needs to collect
265 // <nthreads*niterations> replies.
266 number_of_replies = nthreads *niterations;
268 if (debug)
270 ACE_DEBUG ((LM_DEBUG,
271 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
272 number_of_replies.value ()));
275 // ORB loop.
277 Worker worker (orb.in ());
278 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
279 nthreads) != 0)
280 ACE_ERROR_RETURN ((LM_ERROR,
281 "Cannot activate client threads\n"),
284 worker.thr_mgr ()->wait ();
286 if (debug)
288 ACE_DEBUG ((LM_DEBUG,
289 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
290 (nthreads*niterations) - number_of_replies.value ()));
293 client.thr_mgr ()->wait ();
295 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
297 server->shutdown ();
299 root_poa->destroy (1, // ethernalize objects
300 0); // wait for completion
302 orb->destroy ();
304 catch (const CORBA::Exception& ex)
306 ex._tao_print_exception ("Caught exception:");
307 return 1;
310 return parameter_corruption;
313 // ****************************************************************
315 Client::Client (A::AMI_Test_ptr server,
316 int niterations,
317 A::AMI_AMI_TestHandler_ptr hnd)
318 : ami_test_var_ (A::AMI_Test::_duplicate (server))
319 , niterations_ (niterations)
320 , the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (hnd))
324 Client::~Client ()
329 Client::svc (void)
333 for (int i = 0; i < this->niterations_; ++i)
335 ami_test_var_->sendc_foo (the_handler_var_.in (), in_number, in_str);
337 if (debug)
339 ACE_DEBUG ((LM_DEBUG,
340 "(%P|%t) <%d> Asynchronous methods issued\n",
341 niterations));
344 catch (const CORBA::Exception& ex)
346 ex._tao_print_exception ("MT_Client: exception raised");
348 return 0;
351 Worker::Worker (CORBA::ORB_ptr orb)
352 : orb_ (CORBA::ORB::_duplicate (orb))
357 Worker::svc (void)
359 while (number_of_replies > 0)
361 CORBA::Boolean pending = this->orb_->work_pending();
363 if (pending)
365 ACE_Time_Value tm (1, 0);
366 this->orb_->perform_work(tm);
370 return 0;