Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / HTIOP / AMI / client.cpp
blob4d9b5e0e3edd9d4082bf37fb20c6f475379a9a04
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"
22 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
23 int nthreads = 5;
24 int niterations = 5;
25 int debug = 0;
26 int number_of_replies = 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;
65 /**
66 * @class Client
68 * @brief Run the client thread
70 * Use the ACE_Task_Base class to run the client threads.
72 class Client : public ACE_Task_Base
74 public:
75 /// ctor
76 Client (A::AMI_Test_ptr server, int niterations);
78 /// The thread entry point.
79 virtual int svc (void);
81 // private:
82 /// Var for the AMI_Test object.
83 A::AMI_Test_var ami_test_var_;
85 /// The number of iterations on each client thread.
86 int niterations_;
88 /// Var for AMI_AMI_Test_ReplyHandler object.
89 A::AMI_AMI_TestHandler_var the_handler_var_;
92 class Handler : public POA_A::AMI_AMI_TestHandler
94 public:
95 Handler (void) {};
97 void foo (CORBA::Long result,
98 CORBA::Long out_l)
100 if (debug)
102 ACE_DEBUG ((LM_DEBUG,
103 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
104 result,
105 out_l));
108 number_of_replies--;
111 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
114 ACE_DEBUG ((LM_DEBUG,
115 "Callback method <foo_excep> called:\n"));
118 excep_holder->raise_exception ();
120 catch (const CORBA::Exception& ex)
122 ex._tao_print_exception ("Caught exception:");
126 void get_yadda (CORBA::Long result)
128 ACE_DEBUG ((LM_DEBUG,
129 "Callback method <get_yadda> called: result <%d>\n",
130 result));
133 void get_yadda_excep (::Messaging::ExceptionHolder *)
135 ACE_DEBUG ((LM_DEBUG,
136 "Callback method <get_yadda_excep> called:\n"));
139 void set_yadda (void)
141 ACE_DEBUG ((LM_DEBUG,
142 "Callback method <set_yadda> called:\n"));
145 void set_yadda_excep (::Messaging::ExceptionHolder *)
147 ACE_DEBUG ((LM_DEBUG,
148 "Callback method <set_yadda_excep> called:\n"));
150 ~Handler (void) {};
153 // ReplyHandler.
154 Handler handler;
157 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
161 CORBA::ORB_var orb =
162 CORBA::ORB_init (argc, argv);
164 if (parse_args (argc, argv) != 0)
165 return 1;
167 ACE_OS::socket_init ();
169 CORBA::Object_var object =
170 orb->string_to_object (ior);
172 A::AMI_Test_var server =
173 A::AMI_Test::_narrow (object.in ());
175 if (CORBA::is_nil (server.in ()))
177 ACE_ERROR_RETURN ((LM_ERROR,
178 "Object reference <%s> is nil\n",
179 ior),
183 // Activate POA to handle the call back.
185 CORBA::Object_var poa_object =
186 orb->resolve_initial_references("RootPOA");
188 if (CORBA::is_nil (poa_object.in ()))
189 ACE_ERROR_RETURN ((LM_ERROR,
190 " (%P|%t) Unable to initialize the POA.\n"),
193 PortableServer::POA_var root_poa =
194 PortableServer::POA::_narrow (poa_object.in ());
196 PortableServer::POAManager_var poa_manager =
197 root_poa->the_POAManager ();
199 poa_manager->activate ();
201 // Let the client perform the test in a separate thread
203 Client client (server.in (), niterations);
204 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
205 nthreads) != 0)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 "Cannot activate client threads\n"),
210 // Main thread collects replies. It needs to collect
211 // <nthreads*niterations> replies.
212 number_of_replies = nthreads * niterations;
214 if (debug)
216 ACE_DEBUG ((LM_DEBUG,
217 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
218 number_of_replies));
221 // ORB loop.
223 while (number_of_replies > 0)
225 CORBA::Boolean pending =
226 orb->work_pending();
228 if (pending)
230 orb->perform_work();
234 if (debug)
236 ACE_DEBUG ((LM_DEBUG,
237 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
238 (nthreads*niterations) - number_of_replies));
242 client.thr_mgr ()->wait ();
244 ACE_DEBUG ((LM_DEBUG, "threads finished\n"));
246 //client.ami_test_var_->shutdown ();
248 root_poa->destroy (1, // ethernalize objects
249 0 // wait for completion
252 orb->destroy ();
254 catch (const CORBA::Exception& ex)
256 ex._tao_print_exception ("Caught exception:");
257 return 1;
260 return 0;
263 // ****************************************************************
265 Client::Client (A::AMI_Test_ptr server,
266 int niterations)
267 : ami_test_var_ (A::AMI_Test::_duplicate (server)),
268 niterations_ (niterations)
270 the_handler_var_ = handler._this (/* */);
274 Client::svc (void)
278 CORBA::Long number = 931232;
280 for (int i = 0; i < this->niterations_; ++i)
282 ami_test_var_->sendc_foo (the_handler_var_.in (),
283 number,
284 "Let's talk AMI.");
286 if (debug)
288 ACE_DEBUG ((LM_DEBUG,
289 "(%P | %t):<%d> Asynchronous methods issued\n",
290 niterations));
293 catch (const CORBA::Exception& ex)
295 ex._tao_print_exception ("MT_Client: exception raised");
297 return 0;