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 //=============================================================================
13 #include "ace/Get_Opt.h"
15 #include "ace/Atomic_Op.h"
16 #include "ace/Synch_Traits.h"
17 #include "ace/OS_NS_Thread.h"
18 #include "ami_test_i.h"
20 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
24 ACE_Atomic_Op
<TAO_SYNCH_MUTEX
, 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:"));
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 ());
53 ACE_ERROR_RETURN ((LM_ERROR
,
63 // Indicates successful parsing of the command line
71 * @brief Run the client thread
73 * Use the ACE_Task_Base class to run the client threads.
75 class Client
: public ACE_Task_Base
79 Client (A::AMI_Test_ptr server
, int niterations
);
81 /// The thread entry point.
85 /// Var for the AMI_Test object.
86 A::AMI_Test_var ami_test_var_
;
88 /// The number of iterations on each client thread.
91 /// Var for AMI_AMI_Test_ReplyHandler object.
92 A::AMI_AMI_TestHandler_var the_handler_var_
;
95 class Handler
: public POA_A::AMI_AMI_TestHandler
100 void foo (CORBA::Long result
,
105 ACE_ERROR((LM_ERROR
, "ERROR: Callback method detected parameter corruption.\n"));
106 parameter_corruption
= 1;
109 int const reply
= --number_of_replies
;
113 ACE_DEBUG ((LM_DEBUG
,
114 "(%P | %t) : Callback method %d called: result <%d>, out_arg <%d>\n",
115 (nthreads
* niterations
) - reply
,
119 ACE_UNUSED_ARG (reply
);
122 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
124 ACE_DEBUG ((LM_DEBUG
,
125 "Callback method <foo_excep> called:\n"));
128 excep_holder
->raise_exception ();
130 catch (const CORBA::Exception
& ex
)
132 ex
._tao_print_exception ("Caught exception:");
136 void get_yadda (CORBA::Long result
)
138 ACE_DEBUG ((LM_DEBUG
,
139 "Callback method <get_yadda> called: result <%d>\n",
143 void get_yadda_excep (::Messaging::ExceptionHolder
*)
145 ACE_DEBUG ((LM_DEBUG
,
146 "Callback method <get_yadda_excep> called:\n"));
151 ACE_DEBUG ((LM_DEBUG
,
152 "Callback method <set_yadda> called:\n"));
155 void set_yadda_excep (::Messaging::ExceptionHolder
*)
157 ACE_DEBUG ((LM_DEBUG
,
158 "Callback method <set_yadda_excep> called:\n"));
160 ~Handler () = default;
162 void inout_arg_test (const char *)
164 ACE_DEBUG ((LM_DEBUG
,
165 "Callback method <set_yadda_excep> called:\n"));
168 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
177 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
182 CORBA::ORB_init (argc
, argv
);
184 if (parse_args (argc
, argv
) != 0)
187 A::AMI_Test_var server
;
189 AMI_Test_i
* servant
=
190 new AMI_Test_i(orb
.in(), in_number
, in_str
, true);
191 PortableServer::ServantBase_var
safe (servant
);
193 server
= servant
->_this();
195 if (CORBA::is_nil (server
.in ()))
197 ACE_ERROR_RETURN ((LM_ERROR
,
198 "Object reference <%s> is nil.\n",
203 // Activate POA to handle the call back.
205 CORBA::Object_var poa_object
=
206 orb
->resolve_initial_references("RootPOA");
208 if (CORBA::is_nil (poa_object
.in ()))
209 ACE_ERROR_RETURN ((LM_ERROR
,
210 " (%P|%t) Unable to initialize the POA.\n"),
213 PortableServer::POA_var root_poa
=
214 PortableServer::POA::_narrow (poa_object
.in ());
216 PortableServer::POAManager_var poa_manager
=
217 root_poa
->the_POAManager ();
219 poa_manager
->activate ();
221 // Main thread collects replies. It needs to collect
222 // <nthreads*niterations> replies.
223 number_of_replies
= nthreads
* niterations
;
225 // Let the client perform the test in a separate thread
227 Client
client (server
.in (), niterations
);
228 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
230 ACE_ERROR_RETURN ((LM_ERROR
,
231 "Cannot activate client threads\n"),
236 ACE_DEBUG ((LM_DEBUG
,
237 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
238 number_of_replies
.value ()));
243 while (number_of_replies
> 0)
245 CORBA::Boolean pending
= orb
->work_pending();
252 // On some systems this loop must yield or else the other threads
253 // will not get a chance to run.
259 ACE_DEBUG ((LM_DEBUG
,
260 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
261 (nthreads
*niterations
) - number_of_replies
.value ()));
264 client
.thr_mgr ()->wait ();
266 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
268 root_poa
->destroy (true, // ethernalize objects
269 false); // wait for completion
273 catch (const CORBA::Exception
& ex
)
275 ex
._tao_print_exception ("Caught exception:");
279 return parameter_corruption
;
282 // ****************************************************************
284 Client::Client (A::AMI_Test_ptr server
,
286 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
287 niterations_ (niterations
)
289 the_handler_var_
= handler
._this (/* */);
297 for (int i
= 0; i
< this->niterations_
; ++i
)
299 ami_test_var_
->sendc_foo (the_handler_var_
.in (), in_number
, in_str
);
303 ACE_DEBUG ((LM_DEBUG
,
304 "(%P | %t):<%d> Asynchronous methods issued\n",
308 catch (const CORBA::Exception
& ex
)
310 ex
._tao_print_exception ("MT_Client: exception raised");