2 //=============================================================================
4 * @file simple_client.cpp
6 * A very simple client which uses the AMI callback model.
8 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
9 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
11 //=============================================================================
14 #include "ace/Get_Opt.h"
16 #include "ami_testS.h"
18 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
20 int shutdown_flag
= 0;
25 parse_args (int argc
, ACE_TCHAR
*argv
[])
27 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("dk:i:x"));
30 while ((c
= get_opts ()) != -1)
37 ior
= get_opts
.opt_arg ();
40 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
47 ACE_ERROR_RETURN ((LM_ERROR
,
56 // Indicates successful parsing of the command line
60 class Handler
: public POA_A::AMI_AMI_TestHandler
69 void foo (CORBA::Long ami_return_val
,
75 "Callback method <foo> called: result <%d>, out_arg <%d>\n",
82 "ERROR: out_l not 931233: %d\n",
86 if (ami_return_val
!= 931234)
89 "ERROR: ami_return_val not 931234: %d\n",
95 void foo_excep (::Messaging::ExceptionHolder
* excep_holder
)
99 "Callback method <foo_excep> called:\n"
100 "Testing proper exception handling ...\n"));
103 excep_holder
->raise_exception ();
105 catch (const A::DidTheRightThing
& ex
)
107 ACE_DEBUG ((LM_DEBUG
,
108 "... exception received successfully\n"));
111 ACE_ERROR ((LM_ERROR
,
112 "ERROR: ex.id not 42: %d\n",
116 if (ACE_OS::strcmp (ex
.whatDidTheRightThing
.in (), "Hello world") != 0)
118 ACE_ERROR ((LM_ERROR
,
119 "ERROR: ex.whatDidTheRightThing not ok: <%C>\n",
120 ex
.whatDidTheRightThing
.in ()));
124 catch (const CORBA::Exception
& ex
)
126 ex
._tao_print_exception ("ERROR");
127 ACE_ERROR ((LM_ERROR
,
128 "... caught the wrong exception -> ERROR\n"));
133 void get_yadda (CORBA::Long result
)
135 ACE_DEBUG ((LM_DEBUG
,
136 "Callback method <get_yadda> called: result <%d>\n",
140 void get_yadda_excep (::Messaging::ExceptionHolder
*)
142 ACE_DEBUG ((LM_DEBUG
,
143 "Callback method <get_yadda_excep> called:\n"));
146 void set_yadda (void)
148 ACE_DEBUG ((LM_DEBUG
,
149 "Callback method <set_yadda> called:\n"));
152 void set_yadda_excep (::Messaging::ExceptionHolder
*)
154 ACE_DEBUG ((LM_DEBUG
,
155 "Callback method <set_yadda_excep> called:\n"));
158 void inout_arg_test (const char *)
160 ACE_DEBUG ((LM_DEBUG
,
161 "Callback method <set_yadda_excep> called:\n"));
164 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
170 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
175 CORBA::ORB_init (argc
, argv
);
177 CORBA::Object_var object_var
=
178 orb
->resolve_initial_references ("RootPOA");
180 PortableServer::POA_var poa_var
=
181 PortableServer::POA::_narrow (object_var
.in ());
183 PortableServer::POAManager_var poa_manager_var
=
184 poa_var
->the_POAManager ();
186 poa_manager_var
->activate ();
188 if (parse_args (argc
, argv
) != 0)
191 // We reuse the object_var smart pointer!
192 object_var
= orb
->string_to_object (ior
);
194 A::AMI_Test_var ami_test_var
=
195 A::AMI_Test::_narrow (object_var
.in ());
197 if (CORBA::is_nil (ami_test_var
.in ()))
199 ACE_ERROR_RETURN ((LM_ERROR
,
200 "Object reference <%s> is nil.\n",
205 // Instantiate the ReplyHandler and register that with the POA.
207 PortableServer::ObjectId_var id
=
208 poa_var
->activate_object (&handler
);
210 CORBA::Object_var object
= poa_var
->id_to_reference (id
.in ());
212 A::AMI_AMI_TestHandler_var the_handler_var
=
213 A::AMI_AMI_TestHandler::_narrow (object
.in ());
215 // Try out sending asynchronous messages without a reply handler
216 // registered. Things fail if we get an exception.
217 ami_test_var
->sendc_foo (A::AMI_AMI_TestHandler::_nil (), 0, "");
219 // Trigger the DidTheRightThing exception on the server side
220 // by sending 0 to it.
221 ACE_DEBUG ((LM_DEBUG
,
222 "Sending asynch message\n"));
224 ami_test_var
->sendc_foo (the_handler_var
.in (),
228 CORBA::Long l
= 931247;
230 for (ssize_t ni
= 0; ni
< niterations
; ni
++)
232 ACE_DEBUG ((LM_DEBUG
,
233 "Sending asynch message: %d\n",
236 ami_test_var
->sendc_foo (the_handler_var
.in (),
241 // Begin test of attributes
242 ami_test_var
->sendc_get_yadda (the_handler_var
.in ());
244 ami_test_var
->sendc_set_yadda (the_handler_var
.in (), 4711);
246 ami_test_var
->sendc_get_yadda (the_handler_var
.in ());
248 // End test of attributes
252 ACE_DEBUG ((LM_DEBUG
,
253 "<%d> Asynchronous methods issued\n",
259 ACE_DEBUG ((LM_DEBUG
,
260 "Issuing a synchronous method to collect the AMI replies\n"));
263 CORBA::Long number
= ami_test_var
->foo (l
,
267 for (ssize_t ni
= 0; ni
< niterations
; ni
++)
269 ACE_DEBUG ((LM_DEBUG
,
270 "Sending another asynch message: %d\n",
273 ami_test_var
->sendc_foo (the_handler_var
.in (),
280 ACE_DEBUG ((LM_DEBUG
,
281 "<%d> more asynchronous methods issued\n",
287 ACE_DEBUG ((LM_DEBUG
,
288 "Issuing a synchronous method to collect the AMI replies again\n"));
291 number
= ami_test_var
->foo (l
,
297 ACE_DEBUG ((LM_DEBUG
,
298 "Received the following number: %d\n",
304 ami_test_var
->shutdown ();
307 poa_var
->destroy (1, // ethernalize objects
308 0); // wait for completion
312 catch (const CORBA::Exception
& ex
)
314 ex
._tao_print_exception ("Caught exception:");