ACE+TAO-7_0_8
[ACE_TAO.git] / TAO / examples / Buffered_Oneways / client.cpp
blobab4408618262322d668478ffdf8218d64bd751cb
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * This is a client that uses buffered oneways.
8 * @author Irfan Pyarali
9 */
10 //=============================================================================
13 #include "testC.h"
14 #include "tao/Messaging/Messaging.h"
15 #include "tao/AnyTypeCode/Any.h"
16 #include "tao/AnyTypeCode/TAOA.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/Read_Buffer.h"
22 // Name of file contains ior.
23 static const ACE_TCHAR *IOR = ACE_TEXT ("file://ior");
25 // Default iterations.
26 static CORBA::ULong iterations = 20;
28 // Default number of bytes to buffer before flushing.
29 static CORBA::Long message_bytes = -1;
31 // Default number of invocations to buffer before flushing.
32 static CORBA::Long message_count = iterations / 4;
34 // Default number of iterations before explicit flushing.
35 static CORBA::Long flush_count = -1;
37 // Time interval for implicit flushing (in milli seconds).
38 static long timeout = -1;
40 // Time interval between invocation (in milli seconds).
41 static long interval = 1000;
43 // Flag indicates whether to shutdown remote server or not upon client
44 // shutdown.
45 static int shutdown_server = 0;
47 static int
48 parse_args (int argc, ACE_TCHAR **argv)
50 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:f:c:b:i:z:t:x"));
51 int c;
53 while ((c = get_opts ()) != -1)
54 switch (c)
56 case 'k':
57 IOR = get_opts.opt_arg ();
58 break;
60 case 'f':
61 flush_count = ACE_OS::atoi (get_opts.opt_arg ());
62 break;
64 case 'c':
65 message_count = ACE_OS::atoi (get_opts.opt_arg ());
66 break;
68 case 'b':
69 message_bytes = ACE_OS::atoi (get_opts.opt_arg ());
70 break;
72 case 'i':
73 iterations = ACE_OS::atoi (get_opts.opt_arg ());
74 break;
76 case 'z':
77 interval = ACE_OS::atoi (get_opts.opt_arg ());
78 break;
80 case 't':
81 timeout = ACE_OS::atoi (get_opts.opt_arg ());
82 break;
84 case 'x':
85 shutdown_server = 1;
86 break;
88 case '?':
89 default:
90 ACE_ERROR_RETURN ((LM_ERROR,
91 "usage: %s "
92 "-k IOR "
93 "-f flush count "
94 "-c message count "
95 "-b message bytes "
96 "-i iterations "
97 "-z interval between calls "
98 "-t implicit flush timeout "
99 "-x shutdown server "
100 "\n",
101 argv [0]),
102 -1);
105 if (IOR == 0)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "Please specify the IOR for the servant\n"), -1);
109 // Indicates successful parsing of command line.
110 return 0;
114 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
119 // Initialize the ORB.
120 CORBA::ORB_var orb =
121 CORBA::ORB_init (argc, argv);
123 // Initialize options based on command-line arguments.
124 int parse_args_result = parse_args (argc, argv);
125 if (parse_args_result != 0)
126 return parse_args_result;
128 // Get an object reference from the argument string.
129 CORBA::Object_var base =
130 orb->string_to_object (IOR);
132 // Try to narrow the object reference to a <test> reference.
133 test_var test_object = test::_narrow (base.in ());
135 // Obtain PolicyCurrent.
136 base = orb->resolve_initial_references ("PolicyCurrent");
138 // Narrow down to correct type.
139 CORBA::PolicyCurrent_var policy_current =
140 CORBA::PolicyCurrent::_narrow (base.in ());
142 // Setup the none sync scope policy, i.e., the ORB will buffer
143 // oneways.
144 Messaging::SyncScope sync_none = Messaging::SYNC_NONE;
146 // Setup the none sync scope any.
147 CORBA::Any sync_none_any;
148 sync_none_any <<= sync_none;
150 // Setup the none sync scope policy list.
151 CORBA::PolicyList sync_none_policy_list (1);
152 sync_none_policy_list.length (1);
154 // Setup the none sync scope policy.
155 sync_none_policy_list[0] =
156 orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
157 sync_none_any);
159 // Setup the none sync scope.
160 policy_current->set_policy_overrides (sync_none_policy_list,
161 CORBA::ADD_OVERRIDE);
163 // We are now done with this policy.
164 sync_none_policy_list[0]->destroy ();
166 // Start off with no constraints.
167 TAO::BufferingConstraint buffering_constraint;
168 buffering_constraint.mode = TAO::BUFFER_FLUSH;
169 buffering_constraint.message_count = 0;
170 buffering_constraint.message_bytes = 0;
171 buffering_constraint.timeout = 0;
173 // If valid <message_count>, set the implicit flushing to
174 // account for queued messages.
175 if (message_count != -1)
177 buffering_constraint.mode |= TAO::BUFFER_MESSAGE_COUNT;
178 buffering_constraint.message_count = message_count;
181 // If valid <message_bytes>, set the implicit flushing to
182 // account for queued bytes.
183 if (message_bytes != -1)
185 buffering_constraint.mode |= TAO::BUFFER_MESSAGE_BYTES;
186 buffering_constraint.message_bytes = message_bytes;
189 // If valid <timeout>, set the implicit flushing to account for
190 // timeouts.
191 if (timeout != -1)
193 buffering_constraint.mode |= TAO::BUFFER_TIMEOUT;
194 buffering_constraint.timeout = timeout * 10000;
197 // Setup the buffering constraint any.
198 CORBA::Any buffering_constraint_any;
199 buffering_constraint_any <<= buffering_constraint;
201 // Setup the buffering constraint policy list.
202 CORBA::PolicyList buffering_constraint_policy_list (1);
203 buffering_constraint_policy_list.length (1);
205 // Setup the buffering constraint policy.
206 buffering_constraint_policy_list[0] =
207 orb->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE,
208 buffering_constraint_any);
210 if (buffering_constraint.mode == TAO::BUFFER_FLUSH)
212 ACE_ERROR((LM_ERROR, "Error : Must specify a timeout, message"
213 " count, or message bytes constraint.\n"));
214 return 1;
217 // Setup the constraints.
218 policy_current->set_policy_overrides (buffering_constraint_policy_list,
219 CORBA::ADD_OVERRIDE);
222 // We use this policy again later. Therefore, we don't destroy
223 // it right away.
226 // Setup the explicit flushing policy.
227 TAO::BufferingConstraint buffering_flush;
228 buffering_flush.mode = TAO::BUFFER_FLUSH;
229 buffering_flush.message_count = 0;
230 buffering_flush.message_bytes = 0;
231 buffering_flush.timeout = 0;
233 // Setup the buffering flush any.
234 CORBA::Any buffering_flush_any;
235 buffering_flush_any <<= buffering_flush;
237 // Setup the buffering flush policy list.
238 CORBA::PolicyList buffering_flush_policy_list (1);
239 buffering_flush_policy_list.length (1);
241 // Setup the buffering flush policy.
242 buffering_flush_policy_list[0] =
243 orb->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE,
244 buffering_flush_any);
247 // Explicit flushing policy will be used later.
250 for (CORBA::ULong i = 1; i <= iterations; ++i)
252 // Explicit flushing (is specified).
253 if (flush_count != -1 &&
254 i % flush_count == 0)
256 // Setup explicit flushing.
257 policy_current->set_policy_overrides (buffering_flush_policy_list,
258 CORBA::ADD_OVERRIDE);
260 ACE_DEBUG ((LM_DEBUG,
261 "client: Iteration %d @ %T\n",
262 i));
264 // Invoke the oneway method.
265 test_object->method (i);
267 // Reset buffering policy.
268 policy_current->set_policy_overrides (buffering_constraint_policy_list,
269 CORBA::ADD_OVERRIDE);
271 else
273 ACE_DEBUG ((LM_DEBUG,
274 "client: Iteration %d @ %T\n",
275 i));
277 // Invoke the oneway method.
278 test_object->method (i);
281 // Interval between successive calls.
282 ACE_Time_Value sleep_interval (0,
283 interval * 1000);
285 orb->run (sleep_interval);
288 // Shutdown server.
289 if (shutdown_server)
291 test_object->shutdown ();
294 // We are done with the policy.
295 buffering_constraint_policy_list[0]->destroy ();
297 // We are done with the policy.
298 buffering_flush_policy_list[0]->destroy ();
300 // Destroy the ORB. On some platforms, e.g., Win32, the socket
301 // library is closed at the end of main(). This means that any
302 // socket calls made after main() fail. Hence if we wait for
303 // static destructors to flush the queues, it will be too late.
304 // Therefore, we use explicit destruction here and flush the
305 // queues before main() ends.
306 orb->destroy ();
308 catch (const CORBA::Exception& ex)
310 ex._tao_print_exception ("Exception caught:");
311 return -1;
314 return 0;