2 //=============================================================================
6 * This is a client that uses buffered AMI calls.
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"
20 // Name of file contains ior.
21 static const ACE_TCHAR
*IOR
= ACE_TEXT ("file://ior");
23 // Default iterations.
24 static CORBA::ULong iterations
= 20;
26 // Default number of invocations to buffer before flushing.
27 static CORBA::Long message_count
= iterations
/ 4;
29 // Time interval between invocation (in milli seconds).
30 static long interval
= 1000;
32 // Flag indicates whether to shutdown remote server or not upon client
34 static int shutdown_server
= 0;
36 // AMI call or regular call.
37 static int invoke_ami_style
= 1;
39 // Setup buffering or not.
40 static int setup_buffering
= 1;
42 // Flag indicates that all replies have been received
43 static int received_all_replies
= 0;
45 class Reply_Handler
: public POA_AMI_testHandler
48 void method (CORBA::ULong reply_number
)
51 "client: AMI Reply %d @ %T\n",
54 // Last reply flips the flag.
55 if (reply_number
== iterations
)
56 received_all_replies
= 1;
59 void method_excep (::Messaging::ExceptionHolder
*holder
)
63 holder
->raise_exception ();
65 catch (const CORBA::SystemException
& ex
)
67 ex
._tao_print_exception ("Reply_Handler::method_excep: ");
71 //FUZZ: disable check_for_lack_ACE_OS
75 //FUZZ: enable check_for_lack_ACE_OS
77 void shutdown_excep (::Messaging::ExceptionHolder
*holder
)
81 holder
->raise_exception ();
83 catch (const CORBA::SystemException
& ex
)
85 ex
._tao_print_exception ("Reply_Handler::shutdown_excep: ");
91 parse_args (int argc
, ACE_TCHAR
**argv
)
93 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("a:b:k:m:i:t:x"));
96 while ((c
= get_opts ()) != -1)
100 IOR
= get_opts
.opt_arg ();
104 message_count
= ACE_OS::atoi (get_opts
.opt_arg ());
108 invoke_ami_style
= ACE_OS::atoi (get_opts
.opt_arg ());
112 setup_buffering
= ACE_OS::atoi (get_opts
.opt_arg ());
116 iterations
= ACE_OS::atoi (get_opts
.opt_arg ());
120 interval
= ACE_OS::atoi (get_opts
.opt_arg ());
129 ACE_ERROR_RETURN ((LM_ERROR
,
133 "-a invoke AMI style [0/1] "
134 "-b setup buffering [0/1] "
136 "-t interval between calls "
137 "-x shutdown server "
144 ACE_ERROR_RETURN ((LM_ERROR
,
145 "Please specify the IOR for the servant\n"), -1);
147 // Without AMI, replies are immediate.
148 if (!invoke_ami_style
)
149 received_all_replies
= 1;
151 // Message count must be a multiple of iterations; otherwise we'll
152 // have some unsent messages left in the buffered queue. Even
153 // though we can explicitly flush the queue, I am being lazy and
154 // forcing the user to give the right numbers.
155 if ((iterations
% message_count
) != 0)
157 ACE_ERROR_RETURN ((LM_ERROR
,
158 "<message_count> must be a multiple <iterations> "
159 "or the program should be changed to flush explicitly\n"),
163 // Indicates successful parsing of command line.
168 setup_buffering_constraints (CORBA::ORB_ptr orb
)
170 // Obtain PolicyCurrent.
171 CORBA::Object_var object
= orb
->resolve_initial_references ("PolicyCurrent");
173 // Narrow down to correct type.
174 CORBA::PolicyCurrent_var policy_current
=
175 CORBA::PolicyCurrent::_narrow (object
.in ());
177 // Start off with no constraints.
178 TAO::BufferingConstraint buffering_constraint
;
179 buffering_constraint
.mode
= TAO::BUFFER_MESSAGE_COUNT
;
180 buffering_constraint
.message_count
= message_count
;
181 buffering_constraint
.message_bytes
= 0;
182 buffering_constraint
.timeout
= 0;
184 // Setup the buffering constraint any.
185 CORBA::Any buffering_constraint_any
;
186 buffering_constraint_any
<<= buffering_constraint
;
188 // Setup the buffering constraint policy list.
189 CORBA::PolicyList
buffering_constraint_policy_list (1);
190 buffering_constraint_policy_list
.length (1);
192 // Setup the buffering constraint policy.
193 buffering_constraint_policy_list
[0] =
194 orb
->create_policy (TAO::BUFFERING_CONSTRAINT_POLICY_TYPE
,
195 buffering_constraint_any
);
197 // Setup the constraints (at the ORB level).
198 policy_current
->set_policy_overrides (buffering_constraint_policy_list
,
199 CORBA::ADD_OVERRIDE
);
201 // We are done with the policy.
202 buffering_constraint_policy_list
[0]->destroy ();
204 // Setup the none sync scope policy, i.e., the ORB will buffer AMI
206 Messaging::SyncScope sync_none
= Messaging::SYNC_NONE
;
208 // Setup the none sync scope any.
209 CORBA::Any sync_none_any
;
210 sync_none_any
<<= sync_none
;
212 // Setup the none sync scope policy list.
213 CORBA::PolicyList
sync_none_policy_list (1);
214 sync_none_policy_list
.length (1);
216 // Setup the none sync scope policy.
217 sync_none_policy_list
[0] =
218 orb
->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE
,
221 // Setup the none sync scope (at the ORB level).
222 policy_current
->set_policy_overrides (sync_none_policy_list
,
223 CORBA::ADD_OVERRIDE
);
225 // We are now done with these policies.
226 sync_none_policy_list
[0]->destroy ();
230 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
235 // Initialize the ORB.
237 CORBA::ORB_init (argc
, argv
);
239 // Initialize options based on command-line arguments.
240 int parse_args_result
= parse_args (argc
, argv
);
241 if (parse_args_result
!= 0)
242 return parse_args_result
;
244 CORBA::Object_var base
=
245 orb
->resolve_initial_references ("RootPOA");
247 PortableServer::POA_var root_poa
=
248 PortableServer::POA::_narrow (base
.in ());
250 // Get an object reference from the argument string.
251 base
= orb
->string_to_object (IOR
);
253 PortableServer::POAManager_var poa_manager
=
254 root_poa
->the_POAManager ();
256 poa_manager
->activate ();
258 // Try to narrow the object reference to a <test> reference.
259 test_var test_object
= test::_narrow (base
.in ());
261 Reply_Handler reply_handler_servant
;
262 AMI_testHandler_var reply_handler_object
= reply_handler_servant
._this ();
266 setup_buffering_constraints (orb
.in ());
269 for (CORBA::ULong i
= 1; i
<= iterations
; ++i
)
271 ACE_DEBUG ((LM_DEBUG
,
272 "client: Iteration %d @ %T\n",
275 if (invoke_ami_style
)
277 // Invoke the AMI method.
278 test_object
->sendc_method (reply_handler_object
.in (),
283 CORBA::ULong reply_number
= 0;
285 // Invoke the regular method.
286 test_object
->method (i
,
289 ACE_DEBUG ((LM_DEBUG
,
290 "client: Regular Reply %d @ %T\n",
294 // Interval between successive calls.
295 ACE_Time_Value
sleep_interval (0,
298 orb
->run (sleep_interval
);
301 // Loop until all replies have been received.
302 while (!received_all_replies
)
304 orb
->perform_work ();
310 test_object
->shutdown ();
313 root_poa
->destroy (1,
316 // Destroy the ORB. On some platforms, e.g., Win32, the socket
317 // library is closed at the end of main(). This means that any
318 // socket calls made after main() fail. Hence if we wait for
319 // static destructors to flush the queues, it will be too late.
320 // Therefore, we use explicit destruction here and flush the
321 // queues before main() ends.
324 catch (const CORBA::Exception
& ex
)
326 ex
._tao_print_exception ("Exception caught:");