ACE+TAO-6_5_17
[ACE_TAO.git] / TAO / tests / Timed_Buffered_Oneways / client.cpp
blob290770e341a8618fd594f9eb175ce3b70ce95d8b
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * This is a client that uses oneways with buffering constraints
7 * and roundtrip timeout constraints.
9 * @author Irfan Pyarali
11 //=============================================================================
14 #include "testC.h"
16 #include "tao/Messaging/Messaging.h"
17 #include "tao/AnyTypeCode/TAOA.h"
18 #include "tao/AnyTypeCode/Any.h"
19 #include "tao/TAOC.h"
20 #include "ace/Get_Opt.h"
21 #include "ace/Read_Buffer.h"
22 #include "ace/OS_NS_unistd.h"
23 #include "ace/OS_NS_sys_time.h"
25 // Eager buffering option.
26 static int eager_buffering = 0;
28 // Name of file contains ior.
29 static const ACE_TCHAR *IOR = ACE_TEXT ("file://ior");
31 // Default iterations.
32 static u_long iterations = 20;
34 // Default number of bytes to send as data.
35 static CORBA::ULong data_bytes = 100000;
37 // Default roundtrip timeout (in milli seconds).
38 static long timeout = 500;
40 // Default amount of work.
41 static CORBA::ULong work = 3000;
43 // Time interval between invocation (in milli seconds).
44 static u_long interval = 500;
46 // Flag indicates whether to shutdown remote server or not upon client
47 // shutdown.
48 static int shutdown_server = 0;
50 static int
51 parse_args (int argc, ACE_TCHAR **argv)
53 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("ek:i:d:t:w:z:x"));
54 int c;
56 while ((c = get_opts ()) != -1)
57 switch (c)
59 case 'e':
60 eager_buffering = 1;
61 break;
63 case 'k':
64 IOR = get_opts.opt_arg ();
65 break;
67 case 'i':
68 iterations = ACE_OS::atoi (get_opts.opt_arg ());
69 break;
71 case 'd':
72 data_bytes = ACE_OS::atoi (get_opts.opt_arg ());
73 break;
75 case 't':
76 timeout = ACE_OS::atoi (get_opts.opt_arg ());
77 break;
79 case 'w':
80 work = ACE_OS::atoi (get_opts.opt_arg ());
81 break;
83 case 'z':
84 interval = ACE_OS::atoi (get_opts.opt_arg ());
85 break;
87 case 'x':
88 shutdown_server = 1;
89 break;
91 case '?':
92 default:
93 ACE_ERROR_RETURN ((LM_ERROR,
94 "usage: %s "
95 "-e eager buffering [default is delayed] "
96 "-k IOR "
97 "-i iterations "
98 "-d data bytes "
99 "-t roundtrip timeout "
100 "-w remote work "
101 "-z interval between calls "
102 "-x shutdown server "
103 "\n",
104 argv [0]),
105 -1);
108 if (IOR == 0)
109 ACE_ERROR_RETURN ((LM_ERROR,
110 "Please specify the IOR for the servant\n"), -1);
112 // Indicates successful parsing of command line.
113 return 0;
116 test_ptr
117 setup_policies (CORBA::ORB_ptr orb, test_ptr object)
120 test_var object_with_policy;
121 CORBA::PolicyList policy_list (1);
122 if (timeout == -1)
124 object_with_policy = test::_duplicate (object);
126 else
128 policy_list.length (1);
129 TimeBase::TimeT rt_timeout = 10 * 1000 * timeout;
131 CORBA::Any rt_timeout_any;
132 rt_timeout_any <<= rt_timeout;
133 policy_list[0] =
134 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
135 rt_timeout_any);
137 CORBA::Object_var object_temp =
138 object->_set_policy_overrides (policy_list,
139 CORBA::ADD_OVERRIDE);
141 object_with_policy = test::_narrow (object_temp.in ());
143 policy_list[0]->destroy ();
146 Messaging::SyncScope sync =
147 eager_buffering ? Messaging::SYNC_NONE : TAO::SYNC_DELAYED_BUFFERING;
149 CORBA::Any sync_any;
150 sync_any <<= sync;
152 policy_list.length (1);
153 policy_list[0] =
154 orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
155 sync_any);
157 CORBA::Object_var object_temp =
158 object_with_policy->_set_policy_overrides (policy_list,
159 CORBA::ADD_OVERRIDE);
161 test_var object_with_two_policies = test::_narrow (object_temp.in ());
163 policy_list[0]->destroy ();
165 return object_with_two_policies._retn ();
169 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
174 // Initialize the ORB.
175 CORBA::ORB_var orb =
176 CORBA::ORB_init (argc, argv);
178 // Initialize options based on command-line arguments.
179 int parse_args_result = parse_args (argc, argv);
180 if (parse_args_result != 0)
181 return parse_args_result;
183 // Get an object reference from the argument string.
184 CORBA::Object_var object =
185 orb->string_to_object (IOR);
187 // Try to narrow the object reference to a <test> reference.
188 test_var test_object_no_policy = test::_narrow (object.in ());
190 // Setup buffering and timeout
191 test_var test_object = setup_policies (orb.in (),
192 test_object_no_policy.in ());
194 test::data the_data (data_bytes);
195 the_data.length (data_bytes);
197 for (CORBA::ULong i = 1; i <= iterations; ++i)
199 ACE_Time_Value start = ACE_OS::gettimeofday (), end;
202 // Invoke the oneway method.
203 test_object->method (i,
204 start.msec (),
205 the_data,
206 work);
207 end = ACE_OS::gettimeofday ();
209 ACE_DEBUG ((LM_DEBUG,
210 "client:\t%d took\t%dms\n",
211 i, (end - start).msec ()));
213 // Interval between successive calls.
214 ACE_Time_Value sleep_interval (0, interval * 1000);
216 // If we don't run the orb, then no data will be sent, and no
217 // connection will be made initially.
218 orb->run (sleep_interval);
220 catch (const CORBA::TIMEOUT& )
222 // The timeout could be from a previous loop.
223 // A simplistic analysis could incorrectly conclude
224 // that as an unexpected timeout exception.
225 // We need to maintain a base start time thats updated
226 // in each iteration.
228 ACE_DEBUG ((LM_DEBUG, "client: caught expected timeout\n"));
232 ACE_DEBUG ((LM_DEBUG, "client: flushing\n"));
233 test_object_no_policy->flush ();
235 ACE_DEBUG ((LM_DEBUG, "client: Shutting down...\n"));
236 if (shutdown_server)
238 ACE_DEBUG ((LM_DEBUG,"client killing server\n"));
239 long now = ACE_OS::gettimeofday ().msec ();
240 test_object_no_policy->shutdown (now);
243 orb->shutdown (1);
245 // Destroy the ORB. On some platforms, e.g., Win32, the socket
246 // library is closed at the end of main(). This means that any
247 // socket calls made after main() fail. Hence if we wait for
248 // static destructors to flush the queues, it will be too late.
249 // Therefore, we use explicit destruction here and flush the
250 // queues before main() ends.
251 orb->destroy ();
253 catch (const CORBA::Exception& ex)
255 ex._tao_print_exception ("Client side exception caught:");
256 return -1;
259 return 0;