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 "ace/Atomic_Op.h"
17 #include "ace/Synch_Traits.h"
18 #include "ace/OS_NS_Thread.h"
19 #include "ami_test_i.h"
21 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
25 ACE_Atomic_Op
<TAO_SYNCH_MUTEX
, int> number_of_replies
= 0;
27 CORBA::Long in_number
= 931232;
28 const char * in_str
= "Let's talk AMI.";
29 int parameter_corruption
= 0;
32 parse_args (int argc
, ACE_TCHAR
*argv
[])
34 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:n:i:"));
37 while ((c
= get_opts ()) != -1)
44 ior
= get_opts
.opt_arg ();
47 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
50 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
54 ACE_ERROR_RETURN ((LM_ERROR
,
64 // Indicates successful parsing of the command line
72 * @brief Run the client thread
74 * Use the ACE_Task_Base class to run the client threads.
76 class Client
: public ACE_Task_Base
80 Client (A::AMI_Test_ptr server
, int niterations
);
82 /// The thread entry point.
83 virtual int svc (void);
86 /// Var for the AMI_Test object.
87 A::AMI_Test_var ami_test_var_
;
89 /// The number of iterations on each client thread.
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
103 void foo (CORBA::Long result
,
108 ACE_ERROR((LM_ERROR
, "ERROR: Callback method detected parameter corruption.\n"));
109 parameter_corruption
= 1;
112 int const reply
= --number_of_replies
;
116 ACE_DEBUG ((LM_DEBUG
,
117 "(%P | %t) : Callback method %d called: result <%d>, out_arg <%d>\n",
118 (nthreads
* niterations
) - reply
,
122 ACE_UNUSED_ARG (reply
);
125 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
128 ACE_DEBUG ((LM_DEBUG
,
129 "Callback method <foo_excep> called:\n"));
132 excep_holder
->raise_exception ();
134 catch (const CORBA::Exception
& ex
)
136 ex
._tao_print_exception ("Caught exception:");
140 void get_yadda (CORBA::Long result
)
142 ACE_DEBUG ((LM_DEBUG
,
143 "Callback method <get_yadda> called: result <%d>\n",
147 void get_yadda_excep (::Messaging::ExceptionHolder
*)
149 ACE_DEBUG ((LM_DEBUG
,
150 "Callback method <get_yadda_excep> called:\n"));
153 void set_yadda (void)
155 ACE_DEBUG ((LM_DEBUG
,
156 "Callback method <set_yadda> called:\n"));
159 void set_yadda_excep (::Messaging::ExceptionHolder
*)
161 ACE_DEBUG ((LM_DEBUG
,
162 "Callback method <set_yadda_excep> called:\n"));
168 void inout_arg_test (const char *)
170 ACE_DEBUG ((LM_DEBUG
,
171 "Callback method <set_yadda_excep> called:\n"));
174 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
183 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
188 CORBA::ORB_init (argc
, argv
);
190 if (parse_args (argc
, argv
) != 0)
193 A::AMI_Test_var server
;
195 AMI_Test_i
* servant
=
196 new AMI_Test_i(orb
.in(), in_number
, in_str
, true);
197 PortableServer::ServantBase_var
safe (servant
);
199 server
= servant
->_this();
201 if (CORBA::is_nil (server
.in ()))
203 ACE_ERROR_RETURN ((LM_ERROR
,
204 "Object reference <%s> is nil.\n",
209 // Activate POA to handle the call back.
211 CORBA::Object_var poa_object
=
212 orb
->resolve_initial_references("RootPOA");
214 if (CORBA::is_nil (poa_object
.in ()))
215 ACE_ERROR_RETURN ((LM_ERROR
,
216 " (%P|%t) Unable to initialize the POA.\n"),
219 PortableServer::POA_var root_poa
=
220 PortableServer::POA::_narrow (poa_object
.in ());
222 PortableServer::POAManager_var poa_manager
=
223 root_poa
->the_POAManager ();
225 poa_manager
->activate ();
227 // Main thread collects replies. It needs to collect
228 // <nthreads*niterations> replies.
229 number_of_replies
= nthreads
* niterations
;
231 // Let the client perform the test in a separate thread
233 Client
client (server
.in (), niterations
);
234 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
236 ACE_ERROR_RETURN ((LM_ERROR
,
237 "Cannot activate client threads\n"),
242 ACE_DEBUG ((LM_DEBUG
,
243 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
244 number_of_replies
.value ()));
249 while (number_of_replies
> 0)
251 CORBA::Boolean pending
= orb
->work_pending();
258 // On some systems this loop must yield or else the other threads
259 // will not get a chance to run.
265 ACE_DEBUG ((LM_DEBUG
,
266 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
267 (nthreads
*niterations
) - number_of_replies
.value ()));
270 client
.thr_mgr ()->wait ();
272 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
274 root_poa
->destroy (1, // ethernalize objects
275 0); // wait for completion
279 catch (const CORBA::Exception
& ex
)
281 ex
._tao_print_exception ("Caught exception:");
285 return parameter_corruption
;
288 // ****************************************************************
290 Client::Client (A::AMI_Test_ptr server
,
292 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
293 niterations_ (niterations
)
295 the_handler_var_
= handler
._this (/* */);
303 for (int i
= 0; i
< this->niterations_
; ++i
)
305 ami_test_var_
->sendc_foo (the_handler_var_
.in (), in_number
, in_str
);
309 ACE_DEBUG ((LM_DEBUG
,
310 "(%P | %t):<%d> Asynchronous methods issued\n",
314 catch (const CORBA::Exception
& ex
)
316 ex
._tao_print_exception ("MT_Client: exception raised");