Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Bug_3672_Regression / client.cpp
blob4e4ee1ddf23b943a5e4073338927f17d5812d7de
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 "ami_test_i.h"
16 #include "tao/ORB_Core.h"
18 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
19 int nthreads = 1;
20 int niterations = 1;
21 int debug = 1;
22 int number_of_replies = 0;
24 CORBA::Long in_number = 931232;
25 const char * in_str = "Let's talk AMI.";
26 int parameter_corruption = 0;
28 int
29 parse_args (int argc, ACE_TCHAR *argv[])
31 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:n:i:"));
32 int c;
34 while ((c = get_opts ()) != -1)
35 switch (c)
37 case 'd':
38 debug = 1;
39 break;
40 case 'k':
41 ior = get_opts.opt_arg ();
42 break;
43 case 'n':
44 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
45 break;
46 case 'i':
47 niterations = ACE_OS::atoi (get_opts.opt_arg ());
48 break;
49 case '?':
50 default:
51 ACE_ERROR_RETURN ((LM_ERROR,
52 "usage: %s "
53 "-d "
54 "-k <ior> "
55 "-n <nthreads> "
56 "-i <niterations> "
57 "\n",
58 argv [0]),
59 -1);
61 // Indicates successful parsing of the command line
62 return 0;
66 /**
67 * @class Client
69 * @brief Run the client thread
71 * Use the ACE_Task_Base class to run the client threads.
73 class Client : public ACE_Task_Base
75 public:
76 /// ctor
77 Client (A::AMI_Test_ptr server, int niterations, A::AMI_AMI_TestHandler_ptr handler);
79 /// The thread entry point.
80 virtual int svc ();
82 /// Set all members to nil
83 void clear ();
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 ()
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 if (debug)
114 ACE_DEBUG ((LM_DEBUG,
115 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
116 result,
117 out_l));
120 --number_of_replies;
123 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
125 ACE_DEBUG ((LM_DEBUG,
126 "Callback method <foo_excep> called:\n"));
129 excep_holder->raise_exception ();
131 catch (const CORBA::Exception& ex)
133 ex._tao_print_exception ("Caught exception:");
137 ~Handler ()
143 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
147 CORBA::ORB_var orb =
148 CORBA::ORB_init (argc, argv);
150 if (parse_args (argc, argv) != 0)
151 return 1;
153 CORBA::Object_var object =
154 orb->string_to_object (ior);
155 A::AMI_Test_var server = A::AMI_Test::_narrow (object.in ());
157 if (CORBA::is_nil (server.in ()))
159 ACE_ERROR_RETURN ((LM_ERROR,
160 "Object reference <%s> is nil.\n",
161 ior),
165 // Activate POA to handle the call back.
167 CORBA::Object_var poa_object =
168 orb->resolve_initial_references("RootPOA");
170 if (CORBA::is_nil (poa_object.in ()))
171 ACE_ERROR_RETURN ((LM_ERROR,
172 " (%P|%t) Unable to initialize the POA.\n"),
175 PortableServer::POA_var root_poa =
176 PortableServer::POA::_narrow (poa_object.in ());
178 PortableServer::POAManager_var poa_manager =
179 root_poa->the_POAManager ();
181 poa_manager->activate ();
183 // Let the client perform the test in a separate thread
184 Handler* handler = 0;
185 ACE_NEW_RETURN (handler,
186 Handler,
188 PortableServer::ServantBase_var owner_transfer(handler);
190 PortableServer::ObjectId_var id =
191 root_poa->activate_object (handler);
193 CORBA::Object_var object2 = root_poa->id_to_reference (id.in ());
195 A::AMI_AMI_TestHandler_var hello = A::AMI_AMI_TestHandler::_narrow (object2.in ());
196 object2 = CORBA::Object::_nil ();
198 server->shutdown (); // oneway, so returns here immediately but server waits 5 sec
200 Client client (server.in (), niterations, hello.in ());
201 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
202 nthreads) != 0)
203 ACE_ERROR_RETURN ((LM_ERROR,
204 "Cannot activate client threads\n"),
207 // Main thread collects replies. It needs to collect
208 // <nthreads*niterations> replies.
209 number_of_replies = nthreads *niterations;
211 if (debug)
213 ACE_DEBUG ((LM_DEBUG,
214 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
215 number_of_replies));
218 // ORB loop.
219 ACE_Time_Value tv (1,0);
220 orb->run (tv);
222 if (debug)
224 ACE_DEBUG ((LM_DEBUG,
225 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
226 (nthreads*niterations) - number_of_replies));
229 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
231 client.wait ();
233 tv = ACE_Time_Value (1,0);
234 orb->run (tv);
236 root_poa->deactivate_object (id.in ());
237 root_poa->destroy (true, // ethernalize objects
238 false); // wait for completion
240 hello = A::AMI_AMI_TestHandler::_nil ();
241 root_poa = PortableServer::POA::_nil ();
242 poa_object = CORBA::Object::_nil ();
243 object = CORBA::Object::_nil ();
244 server = A::AMI_Test::_nil ();
245 poa_manager = PortableServer::POAManager::_nil ();
246 client.clear ();
248 orb->shutdown ();
249 orb->destroy ();
251 CORBA::ULong ref_count = orb->_refcount();
253 if (ref_count > 1)
255 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Refcount orb %d\n"), ref_count));
256 ++parameter_corruption;
258 else
260 TAO_ORB_Core* core = orb->orb_core ();
261 if (core != 0)
263 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Core <> null\n")));
264 ++parameter_corruption;
267 orb = CORBA::ORB::_nil ();
269 catch (const CORBA::Exception& ex)
271 ex._tao_print_exception ("Caught exception:");
272 return 1;
275 return parameter_corruption;
278 // ****************************************************************
280 void Client::clear ()
282 ami_test_var_ = A::AMI_Test::_nil ();
283 the_handler_var_ = A::AMI_AMI_TestHandler::_nil ();
286 Client::Client (A::AMI_Test_ptr server,
287 int niterations,
288 A::AMI_AMI_TestHandler_ptr handler)
289 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
290 niterations_ (niterations),
291 the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (handler))
296 Client::svc ()
300 for (int i = 0; i < this->niterations_; ++i)
302 ami_test_var_->sendc_foo (the_handler_var_.in ());
304 if (debug)
306 ACE_DEBUG ((LM_DEBUG,
307 "(%P | %t):<%d> Asynchronous methods issued\n",
308 niterations));
311 catch (const CORBA::Exception& ex)
313 ex._tao_print_exception ("MT_Client: exception raised");
315 return 0;