=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / Leader_Followers / client.cpp
blobf3f43e495995b78f13b33f000f9665492264f0aa
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 ()
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:");
145 return -1;
149 return 0;
152 private:
153 // server reference.
154 test_var test_;
156 // Work counter.
157 u_long work_so_far_;
159 // Lock for work counter.
160 TAO_SYNCH_MUTEX lock_;
162 // Small pause to avoid overrunning the server.
163 long sleep_;
166 class Event_Loop_Task : public ACE_Task_Base
168 public:
169 Event_Loop_Task (CORBA::ORB_ptr orb)
170 : orb_ (CORBA::ORB::_duplicate (orb)),
171 event_loop_timeout_so_far_ (0)
175 int svc ()
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:");
202 return -1;
206 return 0;
209 private:
210 // ORB reference.
211 CORBA::ORB_var orb_;
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.
226 CORBA::ORB_var 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"),
247 -1);
249 // Make sure to give the client threads enough time to become
250 // leaders.
251 ACE_OS::sleep (4);
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"),
259 -1);
261 event_loop_task.thr_mgr ()->wait ();
262 client_task.thr_mgr ()->wait ();
264 ACE_DEBUG ((LM_DEBUG, "Client: All threads finished @ %T\n"));
266 // Shutdown server.
267 if (shutdown_server)
269 server->shutdown ();
272 catch (const CORBA::Exception& ex)
274 ex._tao_print_exception ("Exception caught:");
275 return -1;
279 return 0;