Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / HTIOP / AMI / client.cpp
blob022fdae0c7d2061ccfb9602f438215cf0387ea68
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/OS_NS_sys_socket.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/Task.h"
17 #include "ami_testC.h"
18 #include "ami_testS.h"
21 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
22 int nthreads = 5;
23 int niterations = 5;
24 int debug = 0;
25 int number_of_replies = 0;
27 int
28 parse_args (int argc, ACE_TCHAR *argv[])
30 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:n:i:"));
31 int c;
33 while ((c = get_opts ()) != -1)
34 switch (c)
36 case 'd':
37 debug = 1;
38 break;
39 case 'k':
40 ior = get_opts.opt_arg ();
41 break;
42 case 'n':
43 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
44 break;
45 case 'i':
46 niterations = ACE_OS::atoi (get_opts.opt_arg ());
47 break;
48 case '?':
49 default:
50 ACE_ERROR_RETURN ((LM_ERROR,
51 "usage: %s "
52 "-d "
53 "-k <ior> "
54 "-n <nthreads> "
55 "-i <niterations> "
56 "\n",
57 argv [0]),
58 -1);
60 // Indicates successful parsing of the command line
61 return 0;
64 /**
65 * @class Client
67 * @brief Run the client thread
69 * Use the ACE_Task_Base class to run the client threads.
71 class Client : public ACE_Task_Base
73 public:
74 /// ctor
75 Client (A::AMI_Test_ptr server, int niterations);
77 /// The thread entry point.
78 virtual int svc ();
80 // private:
81 /// Var for the AMI_Test object.
82 A::AMI_Test_var ami_test_var_;
84 /// The number of iterations on each client thread.
85 int niterations_;
87 /// Var for AMI_AMI_Test_ReplyHandler object.
88 A::AMI_AMI_TestHandler_var the_handler_var_;
91 class Handler : public POA_A::AMI_AMI_TestHandler
93 public:
94 Handler () {};
96 void foo (CORBA::Long result,
97 CORBA::Long out_l)
99 if (debug)
101 ACE_DEBUG ((LM_DEBUG,
102 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
103 result,
104 out_l));
107 number_of_replies--;
110 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
112 ACE_DEBUG ((LM_DEBUG,
113 "Callback method <foo_excep> called:\n"));
116 excep_holder->raise_exception ();
118 catch (const CORBA::Exception& ex)
120 ex._tao_print_exception ("Caught exception:");
124 void get_yadda (CORBA::Long result)
126 ACE_DEBUG ((LM_DEBUG,
127 "Callback method <get_yadda> called: result <%d>\n",
128 result));
131 void get_yadda_excep (::Messaging::ExceptionHolder *)
133 ACE_DEBUG ((LM_DEBUG,
134 "Callback method <get_yadda_excep> called:\n"));
137 void set_yadda ()
139 ACE_DEBUG ((LM_DEBUG,
140 "Callback method <set_yadda> called:\n"));
143 void set_yadda_excep (::Messaging::ExceptionHolder *)
145 ACE_DEBUG ((LM_DEBUG,
146 "Callback method <set_yadda_excep> called:\n"));
148 ~Handler () {};
151 // ReplyHandler.
152 Handler handler;
155 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
159 CORBA::ORB_var orb =
160 CORBA::ORB_init (argc, argv);
162 if (parse_args (argc, argv) != 0)
163 return 1;
165 ACE_OS::socket_init ();
167 CORBA::Object_var object =
168 orb->string_to_object (ior);
170 A::AMI_Test_var server =
171 A::AMI_Test::_narrow (object.in ());
173 if (CORBA::is_nil (server.in ()))
175 ACE_ERROR_RETURN ((LM_ERROR,
176 "Object reference <%s> is nil\n",
177 ior),
181 // Activate POA to handle the call back.
183 CORBA::Object_var poa_object =
184 orb->resolve_initial_references("RootPOA");
186 if (CORBA::is_nil (poa_object.in ()))
187 ACE_ERROR_RETURN ((LM_ERROR,
188 " (%P|%t) Unable to initialize the POA.\n"),
191 PortableServer::POA_var root_poa =
192 PortableServer::POA::_narrow (poa_object.in ());
194 PortableServer::POAManager_var poa_manager =
195 root_poa->the_POAManager ();
197 poa_manager->activate ();
199 // Let the client perform the test in a separate thread
201 Client client (server.in (), niterations);
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.
221 while (number_of_replies > 0)
223 CORBA::Boolean pending =
224 orb->work_pending();
226 if (pending)
228 orb->perform_work();
232 if (debug)
234 ACE_DEBUG ((LM_DEBUG,
235 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
236 (nthreads*niterations) - number_of_replies));
240 client.thr_mgr ()->wait ();
242 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
244 //client.ami_test_var_->shutdown ();
246 root_poa->destroy (true, // ethernalize objects
247 false); // wait for completion
249 orb->destroy ();
251 catch (const CORBA::Exception& ex)
253 ex._tao_print_exception ("Caught exception:");
254 return 1;
257 return 0;
260 // ****************************************************************
262 Client::Client (A::AMI_Test_ptr server,
263 int niterations)
264 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
265 niterations_ (niterations)
267 the_handler_var_ = handler._this (/* */);
271 Client::svc ()
275 CORBA::Long number = 931232;
277 for (int i = 0; i < this->niterations_; ++i)
279 ami_test_var_->sendc_foo (the_handler_var_.in (),
280 number,
281 "Let's talk AMI.");
283 if (debug)
285 ACE_DEBUG ((LM_DEBUG,
286 "(%P | %t):<%d> Asynchronous methods issued\n",
287 niterations));
290 catch (const CORBA::Exception& ex)
292 ex._tao_print_exception ("MT_Client: exception raised");
294 return 0;