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
67 ~Handler () = default;
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
)
98 "Callback method <foo_excep> called:\n"
99 "Testing proper exception handling ...\n"));
102 excep_holder
->raise_exception ();
104 catch (const A::DidTheRightThing
& ex
)
106 ACE_DEBUG ((LM_DEBUG
,
107 "... exception received successfully\n"));
110 ACE_ERROR ((LM_ERROR
,
111 "ERROR: ex.id not 42: %d\n",
115 if (ACE_OS::strcmp (ex
.whatDidTheRightThing
.in (), "Hello world") != 0)
117 ACE_ERROR ((LM_ERROR
,
118 "ERROR: ex.whatDidTheRightThing not ok: <%C>\n",
119 ex
.whatDidTheRightThing
.in ()));
123 catch (const CORBA::Exception
& ex
)
125 ex
._tao_print_exception ("ERROR");
126 ACE_ERROR ((LM_ERROR
,
127 "... caught the wrong exception -> ERROR\n"));
132 void get_yadda (CORBA::Long result
)
134 ACE_DEBUG ((LM_DEBUG
,
135 "Callback method <get_yadda> called: result <%d>\n",
139 void get_yadda_excep (::Messaging::ExceptionHolder
*)
141 ACE_DEBUG ((LM_DEBUG
,
142 "Callback method <get_yadda_excep> called:\n"));
147 ACE_DEBUG ((LM_DEBUG
,
148 "Callback method <set_yadda> called:\n"));
151 void set_yadda_excep (::Messaging::ExceptionHolder
*)
153 ACE_DEBUG ((LM_DEBUG
,
154 "Callback method <set_yadda_excep> called:\n"));
157 void inout_arg_test (const char *)
159 ACE_DEBUG ((LM_DEBUG
,
160 "Callback method <set_yadda_excep> called:\n"));
163 void inout_arg_test_excep (::Messaging::ExceptionHolder
*)
169 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
174 CORBA::ORB_init (argc
, argv
);
176 CORBA::Object_var object_var
=
177 orb
->resolve_initial_references ("RootPOA");
179 PortableServer::POA_var poa_var
=
180 PortableServer::POA::_narrow (object_var
.in ());
182 PortableServer::POAManager_var poa_manager_var
=
183 poa_var
->the_POAManager ();
185 poa_manager_var
->activate ();
187 if (parse_args (argc
, argv
) != 0)
190 // We reuse the object_var smart pointer!
191 object_var
= orb
->string_to_object (ior
);
193 A::AMI_Test_var ami_test_var
=
194 A::AMI_Test::_narrow (object_var
.in ());
196 if (CORBA::is_nil (ami_test_var
.in ()))
198 ACE_ERROR_RETURN ((LM_ERROR
,
199 "Object reference <%s> is nil.\n",
204 // Instantiate the ReplyHandler and register that with the POA.
206 PortableServer::ObjectId_var id
=
207 poa_var
->activate_object (&handler
);
209 CORBA::Object_var object
= poa_var
->id_to_reference (id
.in ());
211 A::AMI_AMI_TestHandler_var the_handler_var
=
212 A::AMI_AMI_TestHandler::_narrow (object
.in ());
214 // Try out sending asynchronous messages without a reply handler
215 // registered. Things fail if we get an exception.
216 ami_test_var
->sendc_foo (A::AMI_AMI_TestHandler::_nil (), 0, "");
218 // Trigger the DidTheRightThing exception on the server side
219 // by sending 0 to it.
220 ACE_DEBUG ((LM_DEBUG
,
221 "Sending asynch message\n"));
223 ami_test_var
->sendc_foo (the_handler_var
.in (),
227 CORBA::Long l
= 931247;
229 for (ssize_t ni
= 0; ni
< niterations
; ni
++)
231 ACE_DEBUG ((LM_DEBUG
,
232 "Sending asynch message: %d\n",
235 ami_test_var
->sendc_foo (the_handler_var
.in (),
240 // Begin test of attributes
241 ami_test_var
->sendc_get_yadda (the_handler_var
.in ());
243 ami_test_var
->sendc_set_yadda (the_handler_var
.in (), 4711);
245 ami_test_var
->sendc_get_yadda (the_handler_var
.in ());
247 // End test of attributes
251 ACE_DEBUG ((LM_DEBUG
,
252 "<%d> Asynchronous methods issued\n",
258 ACE_DEBUG ((LM_DEBUG
,
259 "Issuing a synchronous method to collect the AMI replies\n"));
262 CORBA::Long number
= ami_test_var
->foo (l
,
266 for (ssize_t ni
= 0; ni
< niterations
; ni
++)
268 ACE_DEBUG ((LM_DEBUG
,
269 "Sending another asynch message: %d\n",
272 ami_test_var
->sendc_foo (the_handler_var
.in (),
279 ACE_DEBUG ((LM_DEBUG
,
280 "<%d> more asynchronous methods issued\n",
286 ACE_DEBUG ((LM_DEBUG
,
287 "Issuing a synchronous method to collect the AMI replies again\n"));
290 number
= ami_test_var
->foo (l
,
296 ACE_DEBUG ((LM_DEBUG
,
297 "Received the following number: %d\n",
303 ami_test_var
->shutdown ();
306 poa_var
->destroy (1, // ethernalize objects
307 0); // wait for completion
311 catch (const CORBA::Exception
& ex
)
313 ex
._tao_print_exception ("Caught exception:");