2 //=============================================================================
6 * A client which uses the AMI callback model and tests whether exceptions
7 * are redirected to the reply handler when client and servant are
9 * @author Johnny Willemsen <jwillemsen@remedy.nl>
11 //=============================================================================
14 #include "ace/Get_Opt.h"
16 #include "ace/Atomic_Op.h"
17 #include "ace/Synch_Traits.h"
18 #include "ami_test_i.h"
20 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
24 ACE_Atomic_Op
<TAO_SYNCH_MUTEX
, int> number_of_replies
= 0;
26 int invalid_exception
= 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
);
79 /// The thread entry point.
80 virtual int svc (void);
83 /// Var for the AMI_Test object.
84 A::AMI_Test_var ami_test_var_
;
86 /// The number of iterations on each client thread.
89 /// Var for AMI_AMI_Test_ReplyHandler object.
90 A::AMI_AMI_TestHandler_var the_handler_var_
;
93 class Handler
: public POA_A::AMI_AMI_TestHandler
96 Handler (void) : request_ (0)
102 int const reply
= --number_of_replies
;
106 ACE_DEBUG ((LM_DEBUG
,
107 "(%P | %t) : Callback method %d called\n",
108 (nthreads
* niterations
) - reply
));
110 ACE_UNUSED_ARG (reply
);
114 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
116 int const reply
= --number_of_replies
;
119 ACE_DEBUG ((LM_DEBUG
,
120 "(%P | %t) : Callback excep method %d called\n",
121 (nthreads
* niterations
) - reply
));
123 ACE_UNUSED_ARG (reply
);
126 excep_holder
->raise_exception ();
128 catch (const A::DidTheRightThing
& ex
)
133 ex
._tao_print_exception ("Caught exception:");
137 ACE_DEBUG ((LM_DEBUG
, "Caught correct exception\n"));
140 catch (const CORBA::UNKNOWN
& ex
)
145 ex
._tao_print_exception ("Caught exception:");
149 ACE_DEBUG ((LM_DEBUG
, "Caught correct exception\n"));
166 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
171 CORBA::ORB_init (argc
, argv
);
173 if (parse_args (argc
, argv
) != 0)
176 A::AMI_Test_var server
;
178 AMI_Test_i
* servant
=
179 new AMI_Test_i(orb
.in());
180 PortableServer::ServantBase_var
safe (servant
);
182 server
= servant
->_this();
184 if (CORBA::is_nil (server
.in ()))
186 ACE_ERROR_RETURN ((LM_ERROR
,
187 "Object reference <%s> is nil.\n",
192 // Activate POA to handle the call back.
194 CORBA::Object_var poa_object
=
195 orb
->resolve_initial_references("RootPOA");
197 if (CORBA::is_nil (poa_object
.in ()))
198 ACE_ERROR_RETURN ((LM_ERROR
,
199 " (%P|%t) Unable to initialize the POA.\n"),
202 PortableServer::POA_var root_poa
=
203 PortableServer::POA::_narrow (poa_object
.in ());
205 PortableServer::POAManager_var poa_manager
=
206 root_poa
->the_POAManager ();
208 poa_manager
->activate ();
210 // Main thread collects replies. It needs to collect
211 // <nthreads*niterations> replies.
212 number_of_replies
= nthreads
* niterations
;
214 // Let the client perform the test in a separate thread
216 Client
client (server
.in (), niterations
);
217 if (client
.activate (THR_NEW_LWP
| THR_JOINABLE
,
219 ACE_ERROR_RETURN ((LM_ERROR
,
220 "Cannot activate client threads\n"),
225 ACE_DEBUG ((LM_DEBUG
,
226 "(%P|%t) : Entering perform_work loop to receive <%d> replies\n",
227 number_of_replies
.value ()));
232 while (number_of_replies
> 0)
234 CORBA::Boolean pending
= orb
->work_pending();
241 // On some systems this loop must yield or else the other threads
242 // will not get a chance to run.
248 ACE_DEBUG ((LM_DEBUG
,
249 "(%P|%t) : Exited perform_work loop Received <%d> replies\n",
250 (nthreads
*niterations
) - number_of_replies
.value ()));
253 client
.thr_mgr ()->wait ();
255 ACE_DEBUG ((LM_DEBUG
, "threads finished\n"));
257 root_poa
->destroy (1, // ethernalize objects
258 0); // wait for completion
262 catch (const CORBA::Exception
& ex
)
264 ex
._tao_print_exception ("Caught exception:");
268 return invalid_exception
;
271 // ****************************************************************
273 Client::Client (A::AMI_Test_ptr server
,
275 : ami_test_var_ (A::AMI_Test::_duplicate (server
)),
276 niterations_ (niterations
)
278 the_handler_var_
= handler
._this (/* */);
286 for (int i
= 0; i
< this->niterations_
; ++i
)
288 ami_test_var_
->sendc_foo (the_handler_var_
.in (), i
);
292 ACE_DEBUG ((LM_DEBUG
,
293 "(%P | %t):<%d> Asynchronous methods issued\n",
297 catch (const CORBA::Exception
& ex
)
299 ex
._tao_print_exception ("MT_Client: exception raised");
300 invalid_exception
= 1;