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"
22 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
26 int number_of_replies
= 0;
29 parse_args (int argc
, ACE_TCHAR
*argv
[])
31 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:n:i:"));
34 while ((c
= get_opts ()) != -1)
41 ior
= get_opts
.opt_arg ();
44 nthreads
= ACE_OS::atoi (get_opts
.opt_arg ());
47 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
51 ACE_ERROR_RETURN ((LM_ERROR
,
61 // Indicates successful parsing of the command line
68 * @brief Run the client thread
70 * Use the ACE_Task_Base class to run the client threads.
72 class Client
: public ACE_Task_Base
76 Client (A::AMI_Test_ptr server
, int niterations
);
78 /// The thread entry point.
79 virtual int svc (void);
82 /// Var for the AMI_Test object.
83 A::AMI_Test_var ami_test_var_
;
85 /// The number of iterations on each client thread.
88 /// Var for AMI_AMI_Test_ReplyHandler object.
89 A::AMI_AMI_TestHandler_var the_handler_var_
;
92 class Handler
: public POA_A::AMI_AMI_TestHandler
97 void foo (CORBA::Long result
,
102 ACE_DEBUG ((LM_DEBUG
,
103 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
111 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
114 ACE_DEBUG ((LM_DEBUG
,
115 "Callback method <foo_excep> called:\n"));
118 excep_holder
->raise_exception ();
120 catch (const CORBA::Exception
& ex
)
122 ex
._tao_print_exception ("Caught exception:");
126 void get_yadda (CORBA::Long result
)
128 ACE_DEBUG ((LM_DEBUG
,
129 "Callback method <get_yadda> called: result <%d>\n",
133 void get_yadda_excep (::Messaging::ExceptionHolder
*)
135 ACE_DEBUG ((LM_DEBUG
,
136 "Callback method <get_yadda_excep> called:\n"));
139 void set_yadda (void)
141 ACE_DEBUG ((LM_DEBUG
,
142 "Callback method <set_yadda> called:\n"));
145 void set_yadda_excep (::Messaging::ExceptionHolder
*)
147 ACE_DEBUG ((LM_DEBUG
,
148 "Callback method <set_yadda_excep> called:\n"));
157 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
162 CORBA::ORB_init (argc
, argv
);
164 if (parse_args (argc
, argv
) != 0)
167 ACE_OS::socket_init ();
169 CORBA::Object_var object
=
170 orb
->string_to_object (ior
);
172 A::AMI_Test_var server
=
173 A::AMI_Test::_narrow (object
.in ());
175 if (CORBA::is_nil (server
.in ()))
177 ACE_ERROR_RETURN ((LM_ERROR
,
178 "Object reference <%s> is nil\n",
183 // Activate POA to handle the call back.
185 CORBA::Object_var poa_object
=
186 orb
->resolve_initial_references("RootPOA");
188 if (CORBA::is_nil (poa_object
.in ()))
189 ACE_ERROR_RETURN ((LM_ERROR
,
190 " (%P|%t) Unable to initialize the POA.\n"),
193 PortableServer::POA_var root_poa
=
194 PortableServer::POA::_narrow (poa_object
.in ());
196 PortableServer::POAManager_var poa_manager
=
197 root_poa
->the_POAManager ();
199 poa_manager
->activate ();
201 // Let the client perform the test in a separate thread
203 Client
client (server
.in (), niterations
);
204 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
206 ACE_ERROR_RETURN ((LM_ERROR
,
207 "Cannot activate client threads\n"),
210 // Main thread collects replies. It needs to collect
211 // <nthreads*niterations> replies.
212 number_of_replies
= nthreads
* niterations
;
216 ACE_DEBUG ((LM_DEBUG
,
217 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
223 while (number_of_replies
> 0)
225 CORBA::Boolean pending
=
236 ACE_DEBUG ((LM_DEBUG
,
237 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
238 (nthreads
*niterations
) - number_of_replies
));
242 client
.thr_mgr ()->wait ();
244 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
246 //client.ami_test_var_->shutdown ();
248 root_poa
->destroy (1, // ethernalize objects
249 0 // wait for completion
254 catch (const CORBA::Exception
& ex
)
256 ex
._tao_print_exception ("Caught exception:");
263 // ****************************************************************
265 Client::Client (A::AMI_Test_ptr server
,
267 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
268 niterations_ (niterations
)
270 the_handler_var_
= handler
._this (/* */);
278 CORBA::Long number
= 931232;
280 for (int i
= 0; i
< this->niterations_
; ++i
)
282 ami_test_var_
->sendc_foo (the_handler_var_
.in (),
288 ACE_DEBUG ((LM_DEBUG
,
289 "(%P | %t):<%d> Asynchronous methods issued\n",
293 catch (const CORBA::Exception
& ex
)
295 ex
._tao_print_exception ("MT_Client: exception raised");