2 //=============================================================================
6 * A client which uses the AMI callback model.
8 //=============================================================================
11 #include "ace/Get_Opt.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ami_test_i.h"
16 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
20 ACE_Atomic_Op
<TAO_SYNCH_MUTEX
, long> number_of_replies
= 0;
22 CORBA::Long in_number
= 931232;
23 const char * in_str
= "Let's talk AMI.";
24 int parameter_corruption
= 0;
27 parse_args (int argc
, ACE_TCHAR
*argv
[])
29 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:n:i:"));
32 while ((c
= get_opts ()) != -1)
39 ior
= get_opts
.opt_arg ();
42 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
45 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
49 ACE_ERROR_RETURN ((LM_ERROR
,
59 // Indicates successful parsing of the command line
66 * Use the ACE_Task_Base class to run server threads
68 class Worker
: public ACE_Task_Base
71 Worker (CORBA::ORB_ptr orb
);
75 // The thread entry point.
86 * @brief Run the client thread
88 * Use the ACE_Task_Base class to run the client threads.
90 class Client
: public ACE_Task_Base
94 Client (A::AMI_Test_ptr server
, int niterations
, A::AMI_AMI_TestHandler_ptr hnd
);
98 /// The thread entry point.
102 /// Var for the AMI_Test object.
103 A::AMI_Test_var ami_test_var_
;
105 /// The number of iterations on each client thread.
108 /// Var for AMI_AMI_Test_ReplyHandler object.
109 A::AMI_AMI_TestHandler_var the_handler_var_
;
112 class Handler
: public POA_A::AMI_AMI_TestHandler
119 void set_ami_test (A::AMI_Test_ptr ami_test
)
121 ami_test_var_
= A::AMI_Test::_duplicate (ami_test
);
124 void foo (CORBA::Long result
,
129 ACE_ERROR((LM_ERROR
, "ERROR: Callback method detected parameter corruption.\n"));
130 parameter_corruption
= 1;
135 ACE_DEBUG ((LM_DEBUG
,
136 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
141 ami_test_var_
->sendc_set_yadda (0, 5);
146 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
148 ACE_DEBUG ((LM_DEBUG
,
149 "Callback method <foo_excep> called:\n"));
152 excep_holder
->raise_exception ();
154 catch (const CORBA::Exception
& ex
)
156 ex
._tao_print_exception ("Caught exception:");
160 void get_yadda (CORBA::Long result
)
162 ACE_DEBUG ((LM_DEBUG
,
163 "Callback method <get_yadda> called: result <%d>\n",
167 void get_yadda_excep (::Messaging::ExceptionHolder
*)
169 ACE_DEBUG ((LM_DEBUG
,
170 "Callback method <get_yadda_excep> called:\n"));
175 ACE_DEBUG ((LM_DEBUG
,
176 "Callback method <set_yadda> called:\n"));
179 void set_yadda_excep (::Messaging::ExceptionHolder
*)
181 ACE_DEBUG ((LM_DEBUG
,
182 "Callback method <set_yadda_excep> called:\n"));
188 void inout_arg_test (const char *)
190 ACE_DEBUG ((LM_DEBUG
,
191 "Callback method <set_yadda_excep> called:\n"));
194 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
198 A::AMI_Test_var ami_test_var_
;
202 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
207 CORBA::ORB_init (argc
, argv
);
209 if (parse_args (argc
, argv
) != 0)
212 A::AMI_Test_var server
;
214 CORBA::Object_var object
=
215 orb
->string_to_object (ior
);
216 server
= A::AMI_Test::_narrow (object
.in ());
218 if (CORBA::is_nil (server
.in ()))
220 ACE_ERROR_RETURN ((LM_ERROR
,
221 "Object reference <%s> is nil.\n",
226 // Activate POA to handle the call back.
228 CORBA::Object_var poa_object
=
229 orb
->resolve_initial_references("RootPOA");
231 if (CORBA::is_nil (poa_object
.in ()))
232 ACE_ERROR_RETURN ((LM_ERROR
,
233 " (%P|%t) Unable to initialize the POA.\n"),
236 PortableServer::POA_var root_poa
=
237 PortableServer::POA::_narrow (poa_object
.in ());
239 PortableServer::POAManager_var poa_manager
=
240 root_poa
->the_POAManager ();
242 poa_manager
->activate ();
244 // Let the client perform the test in a separate thread
246 PortableServer::ObjectId_var id
=
247 root_poa
->activate_object (&handler
);
249 CORBA::Object_var hnd_object
= root_poa
->id_to_reference (id
.in ());
251 A::AMI_AMI_TestHandler_var the_handler_var
=
252 A::AMI_AMI_TestHandler::_narrow (hnd_object
.in ());
254 handler
.set_ami_test (server
.in ());
256 Client
client (server
.in (), niterations
, the_handler_var
.in ());
257 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
259 ACE_ERROR_RETURN ((LM_ERROR
,
260 "Cannot activate client threads\n"),
263 // Main thread collects replies. It needs to collect
264 // <nthreads*niterations> replies.
265 number_of_replies
= nthreads
*niterations
;
269 ACE_DEBUG ((LM_DEBUG
,
270 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
271 number_of_replies
.value ()));
276 Worker
worker (orb
.in ());
277 if (worker
.activate (THR_NEW_LWP
| THR_JOINABLE
,
279 ACE_ERROR_RETURN ((LM_ERROR
,
280 "Cannot activate client threads\n"),
283 worker
.thr_mgr ()->wait ();
287 ACE_DEBUG ((LM_DEBUG
,
288 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
289 (nthreads
*niterations
) - number_of_replies
.value ()));
292 client
.thr_mgr ()->wait ();
294 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
298 root_poa
->destroy (true, // ethernalize objects
299 false); // wait for completion
303 catch (const CORBA::Exception
& ex
)
305 ex
._tao_print_exception ("Caught exception:");
309 return parameter_corruption
;
312 // ****************************************************************
314 Client::Client (A::AMI_Test_ptr server
,
316 A::AMI_AMI_TestHandler_ptr hnd
)
317 : ami_test_var_ (A::AMI_Test::_duplicate (server
))
318 , niterations_ (niterations
)
319 , the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (hnd
))
332 for (int i
= 0; i
< this->niterations_
; ++i
)
334 ami_test_var_
->sendc_foo (the_handler_var_
.in (), in_number
, in_str
);
338 ACE_DEBUG ((LM_DEBUG
,
339 "(%P|%t) <%d> Asynchronous methods issued\n",
343 catch (const CORBA::Exception
& ex
)
345 ex
._tao_print_exception ("MT_Client: exception raised");
350 Worker::Worker (CORBA::ORB_ptr orb
)
351 : orb_ (CORBA::ORB::_duplicate (orb
))
358 while (number_of_replies
> 0)
360 CORBA::Boolean pending
= this->orb_
->work_pending();
364 ACE_Time_Value
tm (1, 0);
365 this->orb_
->perform_work(tm
);