ACE+TAO-6_5_14
[ACE_TAO.git] / TAO / tests / Leader_Followers / client.cpp
blobe7419b54fce659dedd95a70f13e978b7faf438b3
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * See README.
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "ace/Get_Opt.h"
14 #include "ace/Read_Buffer.h"
15 #include "ace/Task.h"
16 #include "ace/OS_NS_unistd.h"
17 #include "testC.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
37 // shutdown.
38 static int shutdown_server = 0;
40 static int
41 parse_args (int argc, ACE_TCHAR **argv)
43 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:c:e:w:t:x"));
44 int c;
46 while ((c = get_opts ()) != -1)
47 switch (c)
49 case 'k':
50 IOR = get_opts.opt_arg ();
51 break;
53 case 'c':
54 number_of_client_threads = ACE_OS::atoi (get_opts.opt_arg ());
55 break;
57 case 'e':
58 number_of_event_loop_threads = ACE_OS::atoi (get_opts.opt_arg ());
59 break;
61 case 't':
62 event_loop_timeout = ACE_OS::atoi (get_opts.opt_arg ());
63 break;
65 case 'w':
66 remote_work = ACE_OS::atoi (get_opts.opt_arg ());
67 break;
69 case 'x':
70 shutdown_server = 1;
71 break;
73 case '?':
74 default:
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "usage: %s "
77 "-k IOR "
78 "-c number of client threads "
79 "-e number of event loop threads "
80 "-t event loop timeout "
81 "-w remote work "
82 "-x shutdown server "
83 "\n",
84 argv [0]),
85 -1);
88 if (IOR == 0)
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "Please specify the IOR for the servant\n"), -1);
92 // Indicates successful parsing of command line.
93 return 0;
96 class Client_Task : public ACE_Task_Base
98 public:
99 Client_Task (test_ptr t)
100 : test_ (test::_duplicate (t)),
101 work_so_far_ (0),
102 sleep_ (0)
106 int svc (void)
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:");
146 return -1;
150 return 0;
153 private:
154 // server reference.
155 test_var test_;
157 // Work counter.
158 u_long work_so_far_;
160 // Lock for work counter.
161 TAO_SYNCH_MUTEX lock_;
163 // Small pause to avoid overrunning the server.
164 long sleep_;
167 class Event_Loop_Task : public ACE_Task_Base
169 public:
170 Event_Loop_Task (CORBA::ORB_ptr orb)
171 : orb_ (CORBA::ORB::_duplicate (orb)),
172 event_loop_timeout_so_far_ (0)
176 int svc (void)
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:");
204 return -1;
208 return 0;
211 private:
212 // ORB reference.
213 CORBA::ORB_var orb_;
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.
229 CORBA::ORB_var 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"),
250 -1);
252 // Make sure to give the client threads enough time to become
253 // leaders.
254 ACE_OS::sleep (4);
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"),
262 -1);
264 event_loop_task.thr_mgr ()->wait ();
265 client_task.thr_mgr ()->wait ();
267 ACE_DEBUG ((LM_DEBUG, "Client: All threads finished @ %T\n"));
269 // Shutdown server.
270 if (shutdown_server)
272 server->shutdown ();
275 catch (const CORBA::Exception& ex)
277 ex._tao_print_exception ("Exception caught:");
278 return -1;
282 return 0;