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.
80 virtual int svc (void);
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
)
126 ACE_DEBUG ((LM_DEBUG
,
127 "Callback method <foo_excep> called:\n"));
130 excep_holder
->raise_exception ();
132 catch (const CORBA::Exception
& ex
)
134 ex
._tao_print_exception ("Caught exception:");
144 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
149 CORBA::ORB_init (argc
, argv
);
151 if (parse_args (argc
, argv
) != 0)
154 CORBA::Object_var object
=
155 orb
->string_to_object (ior
);
156 A::AMI_Test_var server
= A::AMI_Test::_narrow (object
.in ());
158 if (CORBA::is_nil (server
.in ()))
160 ACE_ERROR_RETURN ((LM_ERROR
,
161 "Object reference <%s> is nil.\n",
166 // Activate POA to handle the call back.
168 CORBA::Object_var poa_object
=
169 orb
->resolve_initial_references("RootPOA");
171 if (CORBA::is_nil (poa_object
.in ()))
172 ACE_ERROR_RETURN ((LM_ERROR
,
173 " (%P|%t) Unable to initialize the POA.\n"),
176 PortableServer::POA_var root_poa
=
177 PortableServer::POA::_narrow (poa_object
.in ());
179 PortableServer::POAManager_var poa_manager
=
180 root_poa
->the_POAManager ();
182 poa_manager
->activate ();
184 // Let the client perform the test in a separate thread
185 Handler
* handler
= 0;
186 ACE_NEW_RETURN (handler
,
189 PortableServer::ServantBase_var
owner_transfer(handler
);
191 PortableServer::ObjectId_var id
=
192 root_poa
->activate_object (handler
);
194 CORBA::Object_var object2
= root_poa
->id_to_reference (id
.in ());
196 A::AMI_AMI_TestHandler_var hello
= A::AMI_AMI_TestHandler::_narrow (object2
.in ());
197 object2
= CORBA::Object::_nil ();
199 server
->shutdown (); // oneway, so returns here immediately but server waits 5 sec
201 Client
client (server
.in (), niterations
, hello
.in ());
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",
220 ACE_Time_Value
tv (1,0);
225 ACE_DEBUG ((LM_DEBUG
,
226 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
227 (nthreads
*niterations
) - number_of_replies
));
230 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
234 tv
= ACE_Time_Value (1,0);
237 root_poa
->deactivate_object (id
.in ());
238 root_poa
->destroy (1, // ethernalize objects
239 0); // wait for completion
241 hello
= A::AMI_AMI_TestHandler::_nil ();
242 root_poa
= PortableServer::POA::_nil ();
243 poa_object
= CORBA::Object::_nil ();
244 object
= CORBA::Object::_nil ();
245 server
= A::AMI_Test::_nil ();
246 poa_manager
= PortableServer::POAManager::_nil ();
252 CORBA::ULong ref_count
= orb
->_refcount();
256 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT("Refcount orb %d\n"), ref_count
));
257 ++parameter_corruption
;
261 TAO_ORB_Core
* core
= orb
->orb_core ();
264 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT("Core <> null\n")));
265 ++parameter_corruption
;
268 orb
= CORBA::ORB::_nil ();
270 catch (const CORBA::Exception
& ex
)
272 ex
._tao_print_exception ("Caught exception:");
277 return parameter_corruption
;
280 // ****************************************************************
282 void Client::clear ()
284 ami_test_var_
= A::AMI_Test::_nil ();
285 the_handler_var_
= A::AMI_AMI_TestHandler::_nil ();
288 Client::Client (A::AMI_Test_ptr server
,
290 A::AMI_AMI_TestHandler_ptr handler
)
291 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
292 niterations_ (niterations
),
293 the_handler_var_ (A::AMI_AMI_TestHandler::_duplicate (handler
))
302 for (int i
= 0; i
< this->niterations_
; ++i
)
304 ami_test_var_
->sendc_foo (the_handler_var_
.in ());
308 ACE_DEBUG ((LM_DEBUG
,
309 "(%P | %t):<%d> Asynchronous methods issued\n",
313 catch (const CORBA::Exception
& ex
)
315 ex
._tao_print_exception ("MT_Client: exception raised");