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
)),
110 u_long work_from_this_thread
= 0;
111 ACE_Time_Value sleep_for_this_thread
;
114 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
, -1);
116 this->work_so_far_
+= remote_work
/ number_of_client_threads
;
117 work_from_this_thread
= this->work_so_far_
;
119 sleep_for_this_thread
.msec (this->sleep_
);
120 this->sleep_
+= 1000 / number_of_client_threads
;
123 // Small pause to avoid overrunning the server.
124 ACE_OS::sleep (sleep_for_this_thread
);
126 // Invoke the method.
127 ACE_DEBUG ((LM_DEBUG
,
128 "Client: Invoking server from thread %t for time %d @ %T\n",
129 work_from_this_thread
));
131 CORBA::ULong result
= this->test_
->method (work_from_this_thread
);
133 if (work_from_this_thread
!= result
)
135 ACE_DEBUG ((LM_DEBUG
,
136 "Client: result is %d\n", result
));
137 ACE_ASSERT (work_from_this_thread
== result
);
140 ACE_DEBUG ((LM_DEBUG
, "Client: client loop finished for thread %t @ %T\n"));
142 catch (const CORBA::Exception
& ex
)
144 ex
._tao_print_exception ("Exception caught in thread:");
159 // Lock for work counter.
160 TAO_SYNCH_MUTEX lock_
;
162 // Small pause to avoid overrunning the server.
166 class Event_Loop_Task
: public ACE_Task_Base
169 Event_Loop_Task (CORBA::ORB_ptr orb
)
170 : orb_ (CORBA::ORB::_duplicate (orb
)),
171 event_loop_timeout_so_far_ (0)
179 u_long event_loop_timeout_for_this_thread
= 0;
182 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
, -1);
184 this->event_loop_timeout_so_far_
+= event_loop_timeout
/ number_of_event_loop_threads
;
185 event_loop_timeout_for_this_thread
= this->event_loop_timeout_so_far_
;
188 ACE_DEBUG ((LM_DEBUG
,
189 "Client: Event loop thread %t starting event loop for %d milli seconds @ %T\n",
190 event_loop_timeout_for_this_thread
));
192 ACE_Time_Value
timeout (0,
193 event_loop_timeout_for_this_thread
* 1000);
195 this->orb_
->run (timeout
);
197 ACE_DEBUG ((LM_DEBUG
, "Client: Event loop finished for thread %t @ %T\n"));
199 catch (const CORBA::Exception
& ex
)
201 ex
._tao_print_exception ("Exception caught in thread:");
213 // Event loop timeout counter.
214 u_long event_loop_timeout_so_far_
;
216 // Lock for event loop timeout counter.
217 TAO_SYNCH_MUTEX lock_
;
221 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
225 // Initialize the ORB.
227 CORBA::ORB_init (argc
, argv
);
229 // Initialize options based on command-line arguments.
230 int parse_args_result
= parse_args (argc
, argv
);
231 if (parse_args_result
!= 0)
232 return parse_args_result
;
234 // Get an object reference from the argument string.
235 CORBA::Object_var object
=
236 orb
->string_to_object (IOR
);
238 // Try to narrow the object reference to a <server> reference.
239 test_var server
= test::_narrow (object
.in ());
241 Client_Task
client_task (server
.in ());
243 if (client_task
.activate (THR_NEW_LWP
| THR_JOINABLE
,
244 number_of_client_threads
) != 0)
245 ACE_ERROR_RETURN ((LM_ERROR
,
246 "Cannot activate client threads\n"),
249 // Make sure to give the client threads enough time to become
253 Event_Loop_Task
event_loop_task (orb
.in ());
255 if (event_loop_task
.activate (THR_NEW_LWP
| THR_JOINABLE
,
256 number_of_event_loop_threads
) != 0)
257 ACE_ERROR_RETURN ((LM_ERROR
,
258 "Cannot activate event_loop threads\n"),
261 event_loop_task
.thr_mgr ()->wait ();
262 client_task
.thr_mgr ()->wait ();
264 ACE_DEBUG ((LM_DEBUG
, "Client: All threads finished @ %T\n"));
272 catch (const CORBA::Exception
& ex
)
274 ex
._tao_print_exception ("Exception caught:");