Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Timed_Buffered_Oneways / client.cpp
bloba80f7fbca30440133b251cb78d516444ef131366
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)
119 test_var object_with_policy;
120 CORBA::PolicyList policy_list (1);
121 if (timeout == -1)
123 object_with_policy = test::_duplicate (object);
125 else
127 policy_list.length (1);
128 TimeBase::TimeT rt_timeout = 10 * 1000 * timeout;
130 CORBA::Any rt_timeout_any;
131 rt_timeout_any <<= rt_timeout;
132 policy_list[0] =
133 orb->create_policy (Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
134 rt_timeout_any);
136 CORBA::Object_var object_temp =
137 object->_set_policy_overrides (policy_list,
138 CORBA::ADD_OVERRIDE);
140 object_with_policy = test::_narrow (object_temp.in ());
142 policy_list[0]->destroy ();
145 Messaging::SyncScope sync =
146 eager_buffering ? Messaging::SYNC_NONE : TAO::SYNC_DELAYED_BUFFERING;
148 CORBA::Any sync_any;
149 sync_any <<= sync;
151 policy_list.length (1);
152 policy_list[0] =
153 orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
154 sync_any);
156 CORBA::Object_var object_temp =
157 object_with_policy->_set_policy_overrides (policy_list,
158 CORBA::ADD_OVERRIDE);
160 test_var object_with_two_policies = test::_narrow (object_temp.in ());
162 policy_list[0]->destroy ();
164 return object_with_two_policies._retn ();
168 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
172 // Initialize the ORB.
173 CORBA::ORB_var orb =
174 CORBA::ORB_init (argc, argv);
176 // Initialize options based on command-line arguments.
177 int parse_args_result = parse_args (argc, argv);
178 if (parse_args_result != 0)
179 return parse_args_result;
181 // Get an object reference from the argument string.
182 CORBA::Object_var object =
183 orb->string_to_object (IOR);
185 // Try to narrow the object reference to a <test> reference.
186 test_var test_object_no_policy = test::_narrow (object.in ());
188 // Setup buffering and timeout
189 test_var test_object = setup_policies (orb.in (),
190 test_object_no_policy.in ());
192 test::data the_data (data_bytes);
193 the_data.length (data_bytes);
195 for (CORBA::ULong i = 1; i <= iterations; ++i)
197 ACE_Time_Value start = ACE_OS::gettimeofday (), end;
200 // Invoke the oneway method.
201 test_object->method (i,
202 start.msec (),
203 the_data,
204 work);
205 end = ACE_OS::gettimeofday ();
207 ACE_DEBUG ((LM_DEBUG,
208 "client:\t%d took\t%dms\n",
209 i, (end - start).msec ()));
211 // Interval between successive calls.
212 ACE_Time_Value sleep_interval (0, interval * 1000);
214 // If we don't run the orb, then no data will be sent, and no
215 // connection will be made initially.
216 orb->run (sleep_interval);
218 catch (const CORBA::TIMEOUT& )
220 // The timeout could be from a previous loop.
221 // A simplistic analysis could incorrectly conclude
222 // that as an unexpected timeout exception.
223 // We need to maintain a base start time thats updated
224 // in each iteration.
226 ACE_DEBUG ((LM_DEBUG, "client: caught expected timeout\n"));
230 ACE_DEBUG ((LM_DEBUG, "client: flushing\n"));
231 test_object_no_policy->flush ();
233 ACE_DEBUG ((LM_DEBUG, "client: Shutting down...\n"));
234 if (shutdown_server)
236 ACE_DEBUG ((LM_DEBUG,"client killing server\n"));
237 long now = ACE_OS::gettimeofday ().msec ();
238 test_object_no_policy->shutdown (now);
241 orb->shutdown (true);
243 // Destroy the ORB. On some platforms, e.g., Win32, the socket
244 // library is closed at the end of main(). This means that any
245 // socket calls made after main() fail. Hence if we wait for
246 // static destructors to flush the queues, it will be too late.
247 // Therefore, we use explicit destruction here and flush the
248 // queues before main() ends.
249 orb->destroy ();
251 catch (const CORBA::Exception& ex)
253 ex._tao_print_exception ("Client side exception caught:");
254 return -1;
257 return 0;