Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3672_Regression / client.cpp
blob609f07b77548a7c1f319700b2b0ecfdc25ab7446
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 (void);
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 (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 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)
126 ACE_DEBUG ((LM_DEBUG,
127 "Callback method <foo_excep> called:\n"));
130 excep_holder->raise_exception ();
132 catch (const CORBA::Exception& ex)
134 ex._tao_print_exception ("Caught exception:");
138 ~Handler (void)
144 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
148 CORBA::ORB_var orb =
149 CORBA::ORB_init (argc, argv);
151 if (parse_args (argc, argv) != 0)
152 return 1;
154 CORBA::Object_var object =
155 orb->string_to_object (ior);
156 A::AMI_Test_var server = A::AMI_Test::_narrow (object.in ());
158 if (CORBA::is_nil (server.in ()))
160 ACE_ERROR_RETURN ((LM_ERROR,
161 "Object reference <%s> is nil.\n",
162 ior),
166 // Activate POA to handle the call back.
168 CORBA::Object_var poa_object =
169 orb->resolve_initial_references("RootPOA");
171 if (CORBA::is_nil (poa_object.in ()))
172 ACE_ERROR_RETURN ((LM_ERROR,
173 " (%P|%t) Unable to initialize the POA.\n"),
176 PortableServer::POA_var root_poa =
177 PortableServer::POA::_narrow (poa_object.in ());
179 PortableServer::POAManager_var poa_manager =
180 root_poa->the_POAManager ();
182 poa_manager->activate ();
184 // Let the client perform the test in a separate thread
185 Handler* handler = 0;
186 ACE_NEW_RETURN (handler,
187 Handler,
189 PortableServer::ServantBase_var owner_transfer(handler);
191 PortableServer::ObjectId_var id =
192 root_poa->activate_object (handler);
194 CORBA::Object_var object2 = root_poa->id_to_reference (id.in ());
196 A::AMI_AMI_TestHandler_var hello = A::AMI_AMI_TestHandler::_narrow (object2.in ());
197 object2 = CORBA::Object::_nil ();
199 server->shutdown (); // oneway, so returns here immediately but server waits 5 sec
201 Client client (server.in (), niterations, hello.in ());
202 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
203 nthreads) != 0)
204 ACE_ERROR_RETURN ((LM_ERROR,
205 "Cannot activate client threads\n"),
208 // Main thread collects replies. It needs to collect
209 // <nthreads*niterations> replies.
210 number_of_replies = nthreads *niterations;
212 if (debug)
214 ACE_DEBUG ((LM_DEBUG,
215 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
216 number_of_replies));
219 // ORB loop.
220 ACE_Time_Value tv (1,0);
221 orb->run (tv);
223 if (debug)
225 ACE_DEBUG ((LM_DEBUG,
226 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
227 (nthreads*niterations) - number_of_replies));
230 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
232 client.wait ();
234 tv = ACE_Time_Value (1,0);
235 orb->run (tv);
237 root_poa->deactivate_object (id.in ());
238 root_poa->destroy (1, // ethernalize objects
239 0); // wait for completion
241 hello = A::AMI_AMI_TestHandler::_nil ();
242 root_poa = PortableServer::POA::_nil ();
243 poa_object = CORBA::Object::_nil ();
244 object = CORBA::Object::_nil ();
245 server = A::AMI_Test::_nil ();
246 poa_manager = PortableServer::POAManager::_nil ();
247 client.clear ();
249 orb->shutdown ();
250 orb->destroy ();
252 CORBA::ULong ref_count = orb->_refcount();
254 if (ref_count > 1)
256 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Refcount orb %d\n"), ref_count));
257 ++parameter_corruption;
259 else
261 TAO_ORB_Core* core = orb->orb_core ();
262 if (core != 0)
264 ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Core <> null\n")));
265 ++parameter_corruption;
268 orb = CORBA::ORB::_nil ();
270 catch (const CORBA::Exception& ex)
272 ex._tao_print_exception ("Caught exception:");
273 return 1;
277 return parameter_corruption;
280 // ****************************************************************
282 void Client::clear ()
284 ami_test_var_ = A::AMI_Test::_nil ();
285 the_handler_var_ = A::AMI_AMI_TestHandler::_nil ();
288 Client::Client (A::AMI_Test_ptr server,
289 int niterations,
290 A::AMI_AMI_TestHandler_ptr handler)
291 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
292 niterations_ (niterations),
293 the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (handler))
298 Client::svc (void)
302 for (int i = 0; i < this->niterations_; ++i)
304 ami_test_var_->sendc_foo (the_handler_var_.in ());
306 if (debug)
308 ACE_DEBUG ((LM_DEBUG,
309 "(%P | %t):<%d> Asynchronous methods issued\n",
310 niterations));
313 catch (const CORBA::Exception& ex)
315 ex._tao_print_exception ("MT_Client: exception raised");
317 return 0;