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 //=============================================================================
13 #include "ace/Get_Opt.h"
15 #include "ami_test_i.h"
16 #include "tao/ORB_Core.h"
18 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
22 int number_of_replies
= 0;
24 CORBA::Long in_number
= 931232;
25 const char * in_str
= "Let's talk AMI.";
26 int parameter_corruption
= 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
69 * @brief Run the client thread
71 * Use the ACE_Task_Base class to run the client threads.
73 class Client
: public ACE_Task_Base
77 Client (A::AMI_Test_ptr server
, int niterations
, A::AMI_AMI_TestHandler_ptr handler
);
79 /// The thread entry point.
82 /// Set all members to nil
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;
114 ACE_DEBUG ((LM_DEBUG
,
115 "(%P | %t) : Callback method called: result <%d>, out_arg <%d>\n",
123 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
125 ACE_DEBUG ((LM_DEBUG
,
126 "Callback method <foo_excep> called:\n"));
129 excep_holder
->raise_exception ();
131 catch (const CORBA::Exception
& ex
)
133 ex
._tao_print_exception ("Caught exception:");
143 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
148 CORBA::ORB_init (argc
, argv
);
150 if (parse_args (argc
, argv
) != 0)
153 CORBA::Object_var object
=
154 orb
->string_to_object (ior
);
155 A::AMI_Test_var server
= A::AMI_Test::_narrow (object
.in ());
157 if (CORBA::is_nil (server
.in ()))
159 ACE_ERROR_RETURN ((LM_ERROR
,
160 "Object reference <%s> is nil.\n",
165 // Activate POA to handle the call back.
167 CORBA::Object_var poa_object
=
168 orb
->resolve_initial_references("RootPOA");
170 if (CORBA::is_nil (poa_object
.in ()))
171 ACE_ERROR_RETURN ((LM_ERROR
,
172 " (%P|%t) Unable to initialize the POA.\n"),
175 PortableServer::POA_var root_poa
=
176 PortableServer::POA::_narrow (poa_object
.in ());
178 PortableServer::POAManager_var poa_manager
=
179 root_poa
->the_POAManager ();
181 poa_manager
->activate ();
183 // Let the client perform the test in a separate thread
184 Handler
* handler
= 0;
185 ACE_NEW_RETURN (handler
,
188 PortableServer::ServantBase_var
owner_transfer(handler
);
190 PortableServer::ObjectId_var id
=
191 root_poa
->activate_object (handler
);
193 CORBA::Object_var object2
= root_poa
->id_to_reference (id
.in ());
195 A::AMI_AMI_TestHandler_var hello
= A::AMI_AMI_TestHandler::_narrow (object2
.in ());
196 object2
= CORBA::Object::_nil ();
198 server
->shutdown (); // oneway, so returns here immediately but server waits 5 sec
200 Client
client (server
.in (), niterations
, hello
.in ());
201 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
203 ACE_ERROR_RETURN ((LM_ERROR
,
204 "Cannot activate client threads\n"),
207 // Main thread collects replies. It needs to collect
208 // <nthreads*niterations> replies.
209 number_of_replies
= nthreads
*niterations
;
213 ACE_DEBUG ((LM_DEBUG
,
214 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
219 ACE_Time_Value
tv (1,0);
224 ACE_DEBUG ((LM_DEBUG
,
225 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
226 (nthreads
*niterations
) - number_of_replies
));
229 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
233 tv
= ACE_Time_Value (1,0);
236 root_poa
->deactivate_object (id
.in ());
237 root_poa
->destroy (true, // ethernalize objects
238 false); // wait for completion
240 hello
= A::AMI_AMI_TestHandler::_nil ();
241 root_poa
= PortableServer::POA::_nil ();
242 poa_object
= CORBA::Object::_nil ();
243 object
= CORBA::Object::_nil ();
244 server
= A::AMI_Test::_nil ();
245 poa_manager
= PortableServer::POAManager::_nil ();
251 CORBA::ULong ref_count
= orb
->_refcount();
255 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT("Refcount orb %d\n"), ref_count
));
256 ++parameter_corruption
;
260 TAO_ORB_Core
* core
= orb
->orb_core ();
263 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT("Core <> null\n")));
264 ++parameter_corruption
;
267 orb
= CORBA::ORB::_nil ();
269 catch (const CORBA::Exception
& ex
)
271 ex
._tao_print_exception ("Caught exception:");
275 return parameter_corruption
;
278 // ****************************************************************
280 void Client::clear ()
282 ami_test_var_
= A::AMI_Test::_nil ();
283 the_handler_var_
= A::AMI_AMI_TestHandler::_nil ();
286 Client::Client (A::AMI_Test_ptr server
,
288 A::AMI_AMI_TestHandler_ptr handler
)
289 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
290 niterations_ (niterations
),
291 the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (handler
))
300 for (int i
= 0; i
< this->niterations_
; ++i
)
302 ami_test_var_
->sendc_foo (the_handler_var_
.in ());
306 ACE_DEBUG ((LM_DEBUG
,
307 "(%P | %t):<%d> Asynchronous methods issued\n",
311 catch (const CORBA::Exception
& ex
)
313 ex
._tao_print_exception ("MT_Client: exception raised");