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/OS_NS_sys_socket.h"
15 #include "ace/Get_Opt.h"
17 #include "ami_testC.h"
18 #include "ami_testS.h"
21 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
25 int number_of_replies
= 0;
28 parse_args (int argc
, ACE_TCHAR
*argv
[])
30 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:n:i:"));
33 while ((c
= get_opts ()) != -1)
40 ior
= get_opts
.opt_arg ();
43 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
46 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
50 ACE_ERROR_RETURN ((LM_ERROR
,
60 // Indicates successful parsing of the command line
67 * @brief Run the client thread
69 * Use the ACE_Task_Base class to run the client threads.
71 class Client
: public ACE_Task_Base
75 Client (A::AMI_Test_ptr server
, int niterations
);
77 /// The thread entry point.
81 /// Var for the AMI_Test object.
82 A::AMI_Test_var ami_test_var_
;
84 /// The number of iterations on each client thread.
87 /// Var for AMI_AMI_Test_ReplyHandler object.
88 A::AMI_AMI_TestHandler_var the_handler_var_
;
91 class Handler
: public POA_A::AMI_AMI_TestHandler
96 void foo (CORBA::Long result
,
101 ACE_DEBUG ((LM_DEBUG
,
102 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
110 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
112 ACE_DEBUG ((LM_DEBUG
,
113 "Callback method <foo_excep> called:\n"));
116 excep_holder
->raise_exception ();
118 catch (const CORBA::Exception
& ex
)
120 ex
._tao_print_exception ("Caught exception:");
124 void get_yadda (CORBA::Long result
)
126 ACE_DEBUG ((LM_DEBUG
,
127 "Callback method <get_yadda> called: result <%d>\n",
131 void get_yadda_excep (::Messaging::ExceptionHolder
*)
133 ACE_DEBUG ((LM_DEBUG
,
134 "Callback method <get_yadda_excep> called:\n"));
139 ACE_DEBUG ((LM_DEBUG
,
140 "Callback method <set_yadda> called:\n"));
143 void set_yadda_excep (::Messaging::ExceptionHolder
*)
145 ACE_DEBUG ((LM_DEBUG
,
146 "Callback method <set_yadda_excep> called:\n"));
155 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
160 CORBA::ORB_init (argc
, argv
);
162 if (parse_args (argc
, argv
) != 0)
165 ACE_OS::socket_init ();
167 CORBA::Object_var object
=
168 orb
->string_to_object (ior
);
170 A::AMI_Test_var server
=
171 A::AMI_Test::_narrow (object
.in ());
173 if (CORBA::is_nil (server
.in ()))
175 ACE_ERROR_RETURN ((LM_ERROR
,
176 "Object reference <%s> is nil\n",
181 // Activate POA to handle the call back.
183 CORBA::Object_var poa_object
=
184 orb
->resolve_initial_references("RootPOA");
186 if (CORBA::is_nil (poa_object
.in ()))
187 ACE_ERROR_RETURN ((LM_ERROR
,
188 " (%P|%t) Unable to initialize the POA.\n"),
191 PortableServer::POA_var root_poa
=
192 PortableServer::POA::_narrow (poa_object
.in ());
194 PortableServer::POAManager_var poa_manager
=
195 root_poa
->the_POAManager ();
197 poa_manager
->activate ();
199 // Let the client perform the test in a separate thread
201 Client
client (server
.in (), niterations
);
202 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
204 ACE_ERROR_RETURN ((LM_ERROR
,
205 "Cannot activate client threads\n"),
208 // Main thread collects replies. It needs to collect
209 // <nthreads*niterations> replies.
210 number_of_replies
= nthreads
* niterations
;
214 ACE_DEBUG ((LM_DEBUG
,
215 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
221 while (number_of_replies
> 0)
223 CORBA::Boolean pending
=
234 ACE_DEBUG ((LM_DEBUG
,
235 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
236 (nthreads
*niterations
) - number_of_replies
));
240 client
.thr_mgr ()->wait ();
242 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
244 //client.ami_test_var_->shutdown ();
246 root_poa
->destroy (true, // ethernalize objects
247 false); // wait for completion
251 catch (const CORBA::Exception
& ex
)
253 ex
._tao_print_exception ("Caught exception:");
260 // ****************************************************************
262 Client::Client (A::AMI_Test_ptr server
,
264 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
265 niterations_ (niterations
)
267 the_handler_var_
= handler
._this (/* */);
275 CORBA::Long number
= 931232;
277 for (int i
= 0; i
< this->niterations_
; ++i
)
279 ami_test_var_
->sendc_foo (the_handler_var_
.in (),
285 ACE_DEBUG ((LM_DEBUG
,
286 "(%P | %t):<%d> Asynchronous methods issued\n",
290 catch (const CORBA::Exception
& ex
)
292 ex
._tao_print_exception ("MT_Client: exception raised");