2 //=============================================================================
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/Get_Opt.h"
16 #include "ami_test_i.h"
18 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
22 int shutdown_flag
= 0;
23 int perform_work_flag
= 1;
24 int number_of_replies
= 0;
26 CORBA::Long in_number
= 931232;
27 const char * in_str
= "Let's talk AMI.";
28 int parameter_corruption
= 0;
31 parse_args (int argc
, ACE_TCHAR
*argv
[])
33 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:n:i:xw"));
36 while ((c
= get_opts ()) != -1)
43 ior
= get_opts
.opt_arg ();
46 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
49 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
55 perform_work_flag
= 0;
59 ACE_ERROR_RETURN ((LM_ERROR
,
70 // Indicates successful parsing of the command line
78 * @brief Run the client thread
80 * Use the ACE_Task_Base class to run the client threads.
82 class Client
: public ACE_Task_Base
86 Client (A::AMI_Test_ptr server
, int niterations
);
88 /// The thread entry point.
92 /// Var for the AMI_Test object.
93 A::AMI_Test_var ami_test_var_
;
95 /// The number of iterations on each client thread.
98 /// Var for AMI_AMI_Test_ReplyHandler object.
99 A::AMI_AMI_TestHandler_var the_handler_var_
;
102 class Handler
: public POA_A::AMI_AMI_TestHandler
109 void foo (CORBA::Long result
,
114 ACE_ERROR((LM_ERROR
, "ERROR: Callback method detected parameter corruption.\n"));
115 parameter_corruption
= 1;
120 ACE_DEBUG ((LM_DEBUG
,
121 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
129 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
131 ACE_DEBUG ((LM_DEBUG
,
132 "Callback method <foo_excep> called:\n"));
135 excep_holder
->raise_exception ();
137 catch (const CORBA::Exception
& ex
)
139 ex
._tao_print_exception ("Caught exception:");
143 void get_yadda (CORBA::Long result
)
145 ACE_DEBUG ((LM_DEBUG
,
146 "Callback method <get_yadda> called: result <%d>\n",
150 void get_yadda_excep (::Messaging::ExceptionHolder
*)
152 ACE_DEBUG ((LM_DEBUG
,
153 "Callback method <get_yadda_excep> called:\n"));
158 ACE_DEBUG ((LM_DEBUG
,
159 "Callback method <set_yadda> called:\n"));
162 void set_yadda_excep (::Messaging::ExceptionHolder
*)
164 ACE_DEBUG ((LM_DEBUG
,
165 "Callback method <set_yadda_excep> called:\n"));
171 void inout_arg_test (const char *)
173 ACE_DEBUG ((LM_DEBUG
,
174 "Callback method <set_yadda_excep> called:\n"));
177 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
186 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
191 CORBA::ORB_init (argc
, argv
);
193 if (parse_args (argc
, argv
) != 0)
196 A::AMI_Test_var server
;
198 CORBA::Object_var object
=
199 orb
->string_to_object (ior
);
200 server
= A::AMI_Test::_narrow (object
.in ());
202 if (CORBA::is_nil (server
.in ()))
204 ACE_ERROR_RETURN ((LM_ERROR
,
205 "Object reference <%s> is nil.\n",
210 // Activate POA to handle the call back.
212 CORBA::Object_var poa_object
=
213 orb
->resolve_initial_references("RootPOA");
215 if (CORBA::is_nil (poa_object
.in ()))
216 ACE_ERROR_RETURN ((LM_ERROR
,
217 " (%P|%t) Unable to initialize the POA.\n"),
220 PortableServer::POA_var root_poa
=
221 PortableServer::POA::_narrow (poa_object
.in ());
223 PortableServer::POAManager_var poa_manager
=
224 root_poa
->the_POAManager ();
226 poa_manager
->activate ();
228 // Let the client perform the test in a separate thread
230 Client
client (server
.in (), niterations
);
231 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
233 ACE_ERROR_RETURN ((LM_ERROR
,
234 "Cannot activate client threads\n"),
237 // Main thread collects replies. It needs to collect
238 // <nthreads*niterations> replies.
239 number_of_replies
= nthreads
*niterations
;
241 if (perform_work_flag
)
245 ACE_DEBUG ((LM_DEBUG
,
246 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
252 while (number_of_replies
> 0)
254 CORBA::Boolean pending
= orb
->work_pending();
264 ACE_DEBUG ((LM_DEBUG
,
265 "(%P|%t) : Exited perform_work loop\n"));
269 client
.thr_mgr ()->wait ();
271 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
275 ACE_DEBUG ((LM_DEBUG
,
276 "(%P|%t) : Received <%d> replies\n",
277 (nthreads
*niterations
) - number_of_replies
));
285 root_poa
->destroy (true, // ethernalize objects
286 false); // wait for completion
290 catch (const CORBA::Exception
& ex
)
292 ex
._tao_print_exception ("Caught exception:");
296 return parameter_corruption
;
299 // ****************************************************************
301 Client::Client (A::AMI_Test_ptr server
,
303 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
304 niterations_ (niterations
)
306 the_handler_var_
= handler
._this (/* */);
314 for (int i
= 0; i
< this->niterations_
; ++i
)
316 ami_test_var_
->sendc_foo (the_handler_var_
.in (), in_number
, in_str
);
320 ACE_DEBUG ((LM_DEBUG
,
321 "(%P | %t):<%d> Asynchronous methods issued\n",
325 catch (const CORBA::Exception
& ex
)
327 ex
._tao_print_exception ("MT_Client: exception raised");