2 //=============================================================================
6 * This is a client that uses buffered oneways.
8 * @author Irfan Pyarali
10 //=============================================================================
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"
21 // Name of file contains ior.
22 static const ACE_TCHAR
*IOR
= ACE_TEXT ("file://ior");
24 // Default iterations.
25 static CORBA::ULong iterations
= 20;
27 // Default number of bytes to buffer before flushing.
28 static CORBA::Long message_bytes
= -1;
30 // Default number of invocations to buffer before flushing.
31 static CORBA::Long message_count
= iterations
/ 4;
33 // Default number of iterations before explicit flushing.
34 static CORBA::Long flush_count
= -1;
36 // Time interval for implicit flushing (in milli seconds).
37 static long timeout
= -1;
39 // Time interval between invocation (in milli seconds).
40 static long interval
= 1000;
42 // Flag indicates whether to shutdown remote server or not upon client
44 static int shutdown_server
= 0;
47 parse_args (int argc
, ACE_TCHAR
**argv
)
49 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("k:f:c:b:i:z:t:x"));
52 while ((c
= get_opts ()) != -1)
56 IOR
= get_opts
.opt_arg ();
60 flush_count
= ACE_OS::atoi (get_opts
.opt_arg ());
64 message_count
= ACE_OS::atoi (get_opts
.opt_arg ());
68 message_bytes
= ACE_OS::atoi (get_opts
.opt_arg ());
72 iterations
= ACE_OS::atoi (get_opts
.opt_arg ());
76 interval
= ACE_OS::atoi (get_opts
.opt_arg ());
80 timeout
= ACE_OS::atoi (get_opts
.opt_arg ());
89 ACE_ERROR_RETURN ((LM_ERROR
,
96 "-z interval between calls "
97 "-t implicit flush timeout "
105 ACE_ERROR_RETURN ((LM_ERROR
,
106 "Please specify the IOR for the servant\n"), -1);
108 // Indicates successful parsing of command line.
113 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
117 // Initialize the ORB.
119 CORBA::ORB_init (argc
, argv
);
121 // Initialize options based on command-line arguments.
122 int parse_args_result
= parse_args (argc
, argv
);
123 if (parse_args_result
!= 0)
124 return parse_args_result
;
126 // Get an object reference from the argument string.
127 CORBA::Object_var base
=
128 orb
->string_to_object (IOR
);
130 // Try to narrow the object reference to a <test> reference.
131 test_var test_object
= test::_narrow (base
.in ());
133 // Obtain PolicyCurrent.
134 base
= orb
->resolve_initial_references ("PolicyCurrent");
136 // Narrow down to correct type.
137 CORBA::PolicyCurrent_var policy_current
=
138 CORBA::PolicyCurrent::_narrow (base
.in ());
140 // Setup the none sync scope policy, i.e., the ORB will buffer
142 Messaging::SyncScope sync_none
= Messaging::SYNC_NONE
;
144 // Setup the none sync scope any.
145 CORBA::Any sync_none_any
;
146 sync_none_any
<<= sync_none
;
148 // Setup the none sync scope policy list.
149 CORBA::PolicyList
sync_none_policy_list (1);
150 sync_none_policy_list
.length (1);
152 // Setup the none sync scope policy.
153 sync_none_policy_list
[0] =
154 orb
->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE
,
157 // Setup the none sync scope.
158 policy_current
->set_policy_overrides (sync_none_policy_list
,
159 CORBA::ADD_OVERRIDE
);
161 // We are now done with this policy.
162 sync_none_policy_list
[0]->destroy ();
164 // Start off with no constraints.
165 TAO::BufferingConstraint buffering_constraint
;
166 buffering_constraint
.mode
= TAO::BUFFER_FLUSH
;
167 buffering_constraint
.message_count
= 0;
168 buffering_constraint
.message_bytes
= 0;
169 buffering_constraint
.timeout
= 0;
171 // If valid <message_count>, set the implicit flushing to
172 // account for queued messages.
173 if (message_count
!= -1)
175 buffering_constraint
.mode
|= TAO::BUFFER_MESSAGE_COUNT
;
176 buffering_constraint
.message_count
= message_count
;
179 // If valid <message_bytes>, set the implicit flushing to
180 // account for queued bytes.
181 if (message_bytes
!= -1)
183 buffering_constraint
.mode
|= TAO::BUFFER_MESSAGE_BYTES
;
184 buffering_constraint
.message_bytes
= message_bytes
;
187 // If valid <timeout>, set the implicit flushing to account for
191 buffering_constraint
.mode
|= TAO::BUFFER_TIMEOUT
;
192 buffering_constraint
.timeout
= timeout
* 10000;
195 // Setup the buffering constraint any.
196 CORBA::Any buffering_constraint_any
;
197 buffering_constraint_any
<<= buffering_constraint
;
199 // Setup the buffering constraint policy list.
200 CORBA::PolicyList
buffering_constraint_policy_list (1);
201 buffering_constraint_policy_list
.length (1);
203 // Setup the buffering constraint policy.
204 buffering_constraint_policy_list
[0] =
205 orb
->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE
,
206 buffering_constraint_any
);
208 if (buffering_constraint
.mode
== TAO::BUFFER_FLUSH
)
210 ACE_ERROR((LM_ERROR
, "Error : Must specify a timeout, message"
211 " count, or message bytes constraint.\n"));
215 // Setup the constraints.
216 policy_current
->set_policy_overrides (buffering_constraint_policy_list
,
217 CORBA::ADD_OVERRIDE
);
220 // We use this policy again later. Therefore, we don't destroy
224 // Setup the explicit flushing policy.
225 TAO::BufferingConstraint buffering_flush
;
226 buffering_flush
.mode
= TAO::BUFFER_FLUSH
;
227 buffering_flush
.message_count
= 0;
228 buffering_flush
.message_bytes
= 0;
229 buffering_flush
.timeout
= 0;
231 // Setup the buffering flush any.
232 CORBA::Any buffering_flush_any
;
233 buffering_flush_any
<<= buffering_flush
;
235 // Setup the buffering flush policy list.
236 CORBA::PolicyList
buffering_flush_policy_list (1);
237 buffering_flush_policy_list
.length (1);
239 // Setup the buffering flush policy.
240 buffering_flush_policy_list
[0] =
241 orb
->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE
,
242 buffering_flush_any
);
245 // Explicit flushing policy will be used later.
248 for (CORBA::ULong i
= 1; i
<= iterations
; ++i
)
250 // Explicit flushing (is specified).
251 if (flush_count
!= -1 &&
252 i
% flush_count
== 0)
254 // Setup explicit flushing.
255 policy_current
->set_policy_overrides (buffering_flush_policy_list
,
256 CORBA::ADD_OVERRIDE
);
258 ACE_DEBUG ((LM_DEBUG
,
259 "client: Iteration %d @ %T\n",
262 // Invoke the oneway method.
263 test_object
->method (i
);
265 // Reset buffering policy.
266 policy_current
->set_policy_overrides (buffering_constraint_policy_list
,
267 CORBA::ADD_OVERRIDE
);
271 ACE_DEBUG ((LM_DEBUG
,
272 "client: Iteration %d @ %T\n",
275 // Invoke the oneway method.
276 test_object
->method (i
);
279 // Interval between successive calls.
280 ACE_Time_Value
sleep_interval (0,
283 orb
->run (sleep_interval
);
289 test_object
->shutdown ();
292 // We are done with the policy.
293 buffering_constraint_policy_list
[0]->destroy ();
295 // We are done with the policy.
296 buffering_flush_policy_list
[0]->destroy ();
298 // Destroy the ORB. On some platforms, e.g., Win32, the socket
299 // library is closed at the end of main(). This means that any
300 // socket calls made after main() fail. Hence if we wait for
301 // static destructors to flush the queues, it will be too late.
302 // Therefore, we use explicit destruction here and flush the
303 // queues before main() ends.
306 catch (const CORBA::Exception
& ex
)
308 ex
._tao_print_exception ("Exception caught:");