2 //=============================================================================
8 * @author Irfan Pyarali
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/Read_Buffer.h"
16 #include "ace/OS_NS_unistd.h"
19 #include "tao/Strategies/advanced_resource.h"
21 // Name of file contains ior.
22 static const ACE_TCHAR
*IOR
= ACE_TEXT ("file://ior");
24 // Number of client threads.
25 static int number_of_client_threads
= 3;
27 // Number of event loop threads.
28 static int number_of_event_loop_threads
= 1;
30 // Amount of remote work (in milli seconds).
31 static u_long remote_work
= 5000;
33 // Run event loop for this much time (in milli seconds).
34 static u_long event_loop_timeout
= 7000;
36 // Flag indicates whether to shutdown remote server or not upon client
38 static int shutdown_server
= 0;
41 parse_args (int argc
, ACE_TCHAR
**argv
)
43 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("k:c:e:w:t:x"));
46 while ((c
= get_opts ()) != -1)
50 IOR
= get_opts
.opt_arg ();
54 number_of_client_threads
= ACE_OS::atoi (get_opts
.opt_arg ());
58 number_of_event_loop_threads
= ACE_OS::atoi (get_opts
.opt_arg ());
62 event_loop_timeout
= ACE_OS::atoi (get_opts
.opt_arg ());
66 remote_work
= ACE_OS::atoi (get_opts
.opt_arg ());
75 ACE_ERROR_RETURN ((LM_ERROR
,
78 "-c number of client threads "
79 "-e number of event loop threads "
80 "-t event loop timeout "
89 ACE_ERROR_RETURN ((LM_ERROR
,
90 "Please specify the IOR for the servant\n"), -1);
92 // Indicates successful parsing of command line.
96 class Client_Task
: public ACE_Task_Base
99 Client_Task (test_ptr t
)
100 : test_ (test::_duplicate (t
)),
111 u_long work_from_this_thread
= 0;
112 ACE_Time_Value sleep_for_this_thread
;
115 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
, -1);
117 this->work_so_far_
+= remote_work
/ number_of_client_threads
;
118 work_from_this_thread
= this->work_so_far_
;
120 sleep_for_this_thread
.msec (this->sleep_
);
121 this->sleep_
+= 1000 / number_of_client_threads
;
124 // Small pause to avoid overrunning the server.
125 ACE_OS::sleep (sleep_for_this_thread
);
127 // Invoke the method.
128 ACE_DEBUG ((LM_DEBUG
,
129 "Client: Invoking server from thread %t for time %d @ %T\n",
130 work_from_this_thread
));
132 CORBA::ULong result
= this->test_
->method (work_from_this_thread
);
134 if (work_from_this_thread
!= result
)
136 ACE_DEBUG ((LM_DEBUG
,
137 "Client: result is %d\n", result
));
138 ACE_ASSERT (work_from_this_thread
== result
);
141 ACE_DEBUG ((LM_DEBUG
, "Client: client loop finished for thread %t @ %T\n"));
143 catch (const CORBA::Exception
& ex
)
145 ex
._tao_print_exception ("Exception caught in thread:");
160 // Lock for work counter.
161 TAO_SYNCH_MUTEX lock_
;
163 // Small pause to avoid overrunning the server.
167 class Event_Loop_Task
: public ACE_Task_Base
170 Event_Loop_Task (CORBA::ORB_ptr orb
)
171 : orb_ (CORBA::ORB::_duplicate (orb
)),
172 event_loop_timeout_so_far_ (0)
181 u_long event_loop_timeout_for_this_thread
= 0;
184 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
, -1);
186 this->event_loop_timeout_so_far_
+= event_loop_timeout
/ number_of_event_loop_threads
;
187 event_loop_timeout_for_this_thread
= this->event_loop_timeout_so_far_
;
190 ACE_DEBUG ((LM_DEBUG
,
191 "Client: Event loop thread %t starting event loop for %d milli seconds @ %T\n",
192 event_loop_timeout_for_this_thread
));
194 ACE_Time_Value
timeout (0,
195 event_loop_timeout_for_this_thread
* 1000);
197 this->orb_
->run (timeout
);
199 ACE_DEBUG ((LM_DEBUG
, "Client: Event loop finished for thread %t @ %T\n"));
201 catch (const CORBA::Exception
& ex
)
203 ex
._tao_print_exception ("Exception caught in thread:");
215 // Event loop timeout counter.
216 u_long event_loop_timeout_so_far_
;
218 // Lock for event loop timeout counter.
219 TAO_SYNCH_MUTEX lock_
;
223 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
228 // Initialize the ORB.
230 CORBA::ORB_init (argc
, argv
);
232 // Initialize options based on command-line arguments.
233 int parse_args_result
= parse_args (argc
, argv
);
234 if (parse_args_result
!= 0)
235 return parse_args_result
;
237 // Get an object reference from the argument string.
238 CORBA::Object_var object
=
239 orb
->string_to_object (IOR
);
241 // Try to narrow the object reference to a <server> reference.
242 test_var server
= test::_narrow (object
.in ());
244 Client_Task
client_task (server
.in ());
246 if (client_task
.activate (THR_NEW_LWP
| THR_JOINABLE
,
247 number_of_client_threads
) != 0)
248 ACE_ERROR_RETURN ((LM_ERROR
,
249 "Cannot activate client threads\n"),
252 // Make sure to give the client threads enough time to become
256 Event_Loop_Task
event_loop_task (orb
.in ());
258 if (event_loop_task
.activate (THR_NEW_LWP
| THR_JOINABLE
,
259 number_of_event_loop_threads
) != 0)
260 ACE_ERROR_RETURN ((LM_ERROR
,
261 "Cannot activate event_loop threads\n"),
264 event_loop_task
.thr_mgr ()->wait ();
265 client_task
.thr_mgr ()->wait ();
267 ACE_DEBUG ((LM_DEBUG
, "Client: All threads finished @ %T\n"));
275 catch (const CORBA::Exception
& ex
)
277 ex
._tao_print_exception ("Exception caught:");