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
);
74 virtual int svc (void);
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.
99 virtual int svc (void);
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
)
149 ACE_DEBUG ((LM_DEBUG
,
150 "Callback method <foo_excep> called:\n"));
153 excep_holder
->raise_exception ();
155 catch (const CORBA::Exception
& ex
)
157 ex
._tao_print_exception ("Caught exception:");
161 void get_yadda (CORBA::Long result
)
163 ACE_DEBUG ((LM_DEBUG
,
164 "Callback method <get_yadda> called: result <%d>\n",
168 void get_yadda_excep (::Messaging::ExceptionHolder
*)
170 ACE_DEBUG ((LM_DEBUG
,
171 "Callback method <get_yadda_excep> called:\n"));
174 void set_yadda (void)
176 ACE_DEBUG ((LM_DEBUG
,
177 "Callback method <set_yadda> called:\n"));
180 void set_yadda_excep (::Messaging::ExceptionHolder
*)
182 ACE_DEBUG ((LM_DEBUG
,
183 "Callback method <set_yadda_excep> called:\n"));
189 void inout_arg_test (const char *)
191 ACE_DEBUG ((LM_DEBUG
,
192 "Callback method <set_yadda_excep> called:\n"));
195 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
199 A::AMI_Test_var ami_test_var_
;
203 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
208 CORBA::ORB_init (argc
, argv
);
210 if (parse_args (argc
, argv
) != 0)
213 A::AMI_Test_var server
;
215 CORBA::Object_var object
=
216 orb
->string_to_object (ior
);
217 server
= A::AMI_Test::_narrow (object
.in ());
219 if (CORBA::is_nil (server
.in ()))
221 ACE_ERROR_RETURN ((LM_ERROR
,
222 "Object reference <%s> is nil.\n",
227 // Activate POA to handle the call back.
229 CORBA::Object_var poa_object
=
230 orb
->resolve_initial_references("RootPOA");
232 if (CORBA::is_nil (poa_object
.in ()))
233 ACE_ERROR_RETURN ((LM_ERROR
,
234 " (%P|%t) Unable to initialize the POA.\n"),
237 PortableServer::POA_var root_poa
=
238 PortableServer::POA::_narrow (poa_object
.in ());
240 PortableServer::POAManager_var poa_manager
=
241 root_poa
->the_POAManager ();
243 poa_manager
->activate ();
245 // Let the client perform the test in a separate thread
247 PortableServer::ObjectId_var id
=
248 root_poa
->activate_object (&handler
);
250 CORBA::Object_var hnd_object
= root_poa
->id_to_reference (id
.in ());
252 A::AMI_AMI_TestHandler_var the_handler_var
=
253 A::AMI_AMI_TestHandler::_narrow (hnd_object
.in ());
255 handler
.set_ami_test (server
.in ());
257 Client
client (server
.in (), niterations
, the_handler_var
.in ());
258 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
260 ACE_ERROR_RETURN ((LM_ERROR
,
261 "Cannot activate client threads\n"),
264 // Main thread collects replies. It needs to collect
265 // <nthreads*niterations> replies.
266 number_of_replies
= nthreads
*niterations
;
270 ACE_DEBUG ((LM_DEBUG
,
271 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
272 number_of_replies
.value ()));
277 Worker
worker (orb
.in ());
278 if (worker
.activate (THR_NEW_LWP
| THR_JOINABLE
,
280 ACE_ERROR_RETURN ((LM_ERROR
,
281 "Cannot activate client threads\n"),
284 worker
.thr_mgr ()->wait ();
288 ACE_DEBUG ((LM_DEBUG
,
289 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
290 (nthreads
*niterations
) - number_of_replies
.value ()));
293 client
.thr_mgr ()->wait ();
295 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
299 root_poa
->destroy (1, // ethernalize objects
300 0); // wait for completion
304 catch (const CORBA::Exception
& ex
)
306 ex
._tao_print_exception ("Caught exception:");
310 return parameter_corruption
;
313 // ****************************************************************
315 Client::Client (A::AMI_Test_ptr server
,
317 A::AMI_AMI_TestHandler_ptr hnd
)
318 : ami_test_var_ (A::AMI_Test::_duplicate (server
))
319 , niterations_ (niterations
)
320 , the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (hnd
))
333 for (int i
= 0; i
< this->niterations_
; ++i
)
335 ami_test_var_
->sendc_foo (the_handler_var_
.in (), in_number
, in_str
);
339 ACE_DEBUG ((LM_DEBUG
,
340 "(%P|%t) <%d> Asynchronous methods issued\n",
344 catch (const CORBA::Exception
& ex
)
346 ex
._tao_print_exception ("MT_Client: exception raised");
351 Worker::Worker (CORBA::ORB_ptr orb
)
352 : orb_ (CORBA::ORB::_duplicate (orb
))
359 while (number_of_replies
> 0)
361 CORBA::Boolean pending
= this->orb_
->work_pending();
365 ACE_Time_Value
tm (1, 0);
366 this->orb_
->perform_work(tm
);