1
// client.cpp,v 1.14 2003/12/24 17:46:34 bala Exp
3
// ================================================================
9
// This is a client that uses buffered AMI calls.
14
// ================================================================
17
#include "tao/Messaging/Messaging.h"
19
#include "ace/Get_Opt.h"
20
#include "ace/Read_Buffer.h"
22
ACE_RCSID (Buffered_AMI,
24
"client.cpp,v 1.14 2003/12/24 17:46:34 bala Exp")
26
// Name of file contains ior.
27
static const char *IOR = "file://ior";
29
// Default iterations.
30
static CORBA::ULong iterations = 20;
32
// Default number of invocations to buffer before flushing.
33
static CORBA::Long message_count = iterations / 4;
35
// Time interval between invocation (in milli seconds).
36
static long interval = 1000;
38
// Flag indicates whether to shutdown remote server or not upon client
40
static int shutdown_server = 0;
42
// AMI call or regular call.
43
static int invoke_ami_style = 1;
45
// Setup buffering or not.
46
static int setup_buffering = 1;
48
// Flag indicates that all replies have been received
49
static int received_all_replies = 0;
51
class Reply_Handler : public POA_AMI_testHandler
54
void method (CORBA::ULong reply_number
55
ACE_ENV_ARG_DECL_NOT_USED)
56
ACE_THROW_SPEC ((CORBA::SystemException))
59
"client: AMI Reply %d @ %T\n",
62
// Last reply flips the flag.
63
if (reply_number == iterations)
64
received_all_replies = 1;
67
void method_excep (AMI_testExceptionHolder *holder
69
ACE_THROW_SPEC ((CORBA::SystemException))
73
holder->raise_method (ACE_ENV_SINGLE_ARG_PARAMETER);
76
ACE_CATCH(CORBA::SystemException, ex)
78
ACE_PRINT_EXCEPTION (ex, "Reply_Handler::method_excep: ");
83
void shutdown (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
84
ACE_THROW_SPEC ((CORBA::SystemException))
88
void shutdown_excep (AMI_testExceptionHolder *holder
90
ACE_THROW_SPEC ((CORBA::SystemException))
94
holder->raise_shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
97
ACE_CATCH(CORBA::SystemException, ex)
99
ACE_PRINT_EXCEPTION (ex, "Reply_Handler::shutdown_excep: ");
106
parse_args (int argc, char **argv)
108
ACE_Get_Opt get_opts (argc, argv, "a:b:k:m:i:t:x");
111
while ((c = get_opts ()) != -1)
115
IOR = get_opts.opt_arg ();
119
message_count = ::atoi (get_opts.opt_arg ());
123
invoke_ami_style = ::atoi (get_opts.opt_arg ());
127
setup_buffering = ::atoi (get_opts.opt_arg ());
131
iterations = ::atoi (get_opts.opt_arg ());
135
interval = ::atoi (get_opts.opt_arg ());
144
ACE_ERROR_RETURN ((LM_ERROR,
148
"-a invoke AMI style [0/1] "
149
"-b setup buffering [0/1] "
151
"-t interval between calls "
152
"-x shutdown server "
159
ACE_ERROR_RETURN ((LM_ERROR,
160
"Please specify the IOR for the servant\n"), -1);
162
// Without AMI, replies are immediate.
163
if (!invoke_ami_style)
164
received_all_replies = 1;
166
// Message count must be a multiple of iterations; otherwise we'll
167
// have some unsent messages left in the buffered queue. Even
168
// though we can explicitly flush the queue, I am being lazy and
169
// forcing the user to give the right numbers.
170
if ((iterations % message_count) != 0)
172
ACE_ERROR_RETURN ((LM_ERROR,
173
"<message_count> must be a multiple <iterations> "
174
"or the program should be changed to flush explicitly \n"),
178
// Indicates successful parsing of command line.
183
setup_buffering_constraints (CORBA::ORB_ptr orb
186
// Obtain PolicyCurrent.
187
CORBA::Object_var object = orb->resolve_initial_references ("PolicyCurrent"
188
ACE_ENV_ARG_PARAMETER);
191
// Narrow down to correct type.
192
CORBA::PolicyCurrent_var policy_current =
193
CORBA::PolicyCurrent::_narrow (object.in ()
194
ACE_ENV_ARG_PARAMETER);
197
// Start off with no constraints.
198
TAO::BufferingConstraint buffering_constraint;
199
buffering_constraint.mode = TAO::BUFFER_MESSAGE_COUNT;
200
buffering_constraint.message_count = message_count;
201
buffering_constraint.message_bytes = 0;
202
buffering_constraint.timeout = 0;
204
// Setup the buffering constraint any.
205
CORBA::Any buffering_constraint_any;
206
buffering_constraint_any <<= buffering_constraint;
208
// Setup the buffering constraint policy list.
209
CORBA::PolicyList buffering_constraint_policy_list (1);
210
buffering_constraint_policy_list.length (1);
212
// Setup the buffering constraint policy.
213
buffering_constraint_policy_list[0] =
214
orb->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE,
215
buffering_constraint_any
216
ACE_ENV_ARG_PARAMETER);
219
// Setup the constraints (at the ORB level).
220
policy_current->set_policy_overrides (buffering_constraint_policy_list,
222
ACE_ENV_ARG_PARAMETER);
225
// We are done with the policy.
226
buffering_constraint_policy_list[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
229
// Setup the none sync scope policy, i.e., the ORB will buffer AMI
231
Messaging::SyncScope sync_none = Messaging::SYNC_NONE;
233
// Setup the none sync scope any.
234
CORBA::Any sync_none_any;
235
sync_none_any <<= sync_none;
237
// Setup the none sync scope policy list.
238
CORBA::PolicyList sync_none_policy_list (1);
239
sync_none_policy_list.length (1);
241
// Setup the none sync scope policy.
242
sync_none_policy_list[0] =
243
orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
245
ACE_ENV_ARG_PARAMETER);
248
// Setup the none sync scope (at the ORB level).
249
policy_current->set_policy_overrides (sync_none_policy_list,
251
ACE_ENV_ARG_PARAMETER);
254
// We are now done with these policies.
255
sync_none_policy_list[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
260
main (int argc, char **argv)
262
ACE_DECLARE_NEW_CORBA_ENV;
266
// Initialize the ORB.
268
CORBA::ORB_init (argc,
271
ACE_ENV_ARG_PARAMETER);
274
// Initialize options based on command-line arguments.
275
int parse_args_result = parse_args (argc, argv);
276
if (parse_args_result != 0)
277
return parse_args_result;
279
CORBA::Object_var base =
280
orb->resolve_initial_references ("RootPOA"
281
ACE_ENV_ARG_PARAMETER);
284
PortableServer::POA_var root_poa =
285
PortableServer::POA::_narrow (base.in ()
286
ACE_ENV_ARG_PARAMETER);
289
// Get an object reference from the argument string.
290
base = orb->string_to_object (IOR
291
ACE_ENV_ARG_PARAMETER);
294
PortableServer::POAManager_var poa_manager =
295
root_poa->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
298
poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
301
// Try to narrow the object reference to a <test> reference.
302
test_var test_object = test::_narrow (base.in ()
303
ACE_ENV_ARG_PARAMETER);
306
Reply_Handler reply_handler_servant;
307
AMI_testHandler_var reply_handler_object = reply_handler_servant._this (ACE_ENV_SINGLE_ARG_PARAMETER);
312
setup_buffering_constraints (orb.in ()
313
ACE_ENV_ARG_PARAMETER);
317
for (CORBA::ULong i = 1; i <= iterations; ++i)
319
ACE_DEBUG ((LM_DEBUG,
320
"client: Iteration %d @ %T\n",
323
if (invoke_ami_style)
325
// Invoke the AMI method.
326
test_object->sendc_method (reply_handler_object.in (),
328
ACE_ENV_ARG_PARAMETER);
333
CORBA::ULong reply_number = 0;
335
// Invoke the regular method.
336
test_object->method (i,
338
ACE_ENV_ARG_PARAMETER);
341
ACE_DEBUG ((LM_DEBUG,
342
"client: Regular Reply %d @ %T\n",
346
// Interval between successive calls.
347
ACE_Time_Value sleep_interval (0,
350
orb->run (sleep_interval ACE_ENV_ARG_PARAMETER);
354
// Loop until all replies have been received.
355
while (!received_all_replies)
357
orb->perform_work ();
363
test_object->shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
367
root_poa->destroy (1,
369
ACE_ENV_ARG_PARAMETER);
372
// Destroy the ORB. On some platforms, e.g., Win32, the socket
373
// library is closed at the end of main(). This means that any
374
// socket calls made after main() fail. Hence if we wait for
375
// static destructors to flush the queues, it will be too late.
376
// Therefore, we use explicit destruction here and flush the
377
// queues before main() ends.
378
orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
383
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
384
"Exception caught:");
389
ACE_CHECK_RETURN (-1);