Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / AMI / simple_client.cpp
blob0500ee744511eea26374f00029929c1f443f1af3
2 //=============================================================================
3 /**
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"
15 #include "ace/Task.h"
16 #include "ami_testS.h"
18 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
19 int niterations = 5;
20 int shutdown_flag = 0;
21 int debug = 0;
22 int result = 0;
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:i:x"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'd':
34 debug = 1;
35 break;
36 case 'k':
37 ior = get_opts.opt_arg ();
38 break;
39 case 'i':
40 niterations = ACE_OS::atoi (get_opts.opt_arg ());
41 break;
42 case 'x':
43 shutdown_flag = 1;
44 break;
45 case '?':
46 default:
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "usage: %s "
49 "-k <ior> "
50 "-i <niterations> "
51 "-x "
52 "\n",
53 argv [0]),
54 -1);
56 // Indicates successful parsing of the command line
57 return 0;
60 class Handler : public POA_A::AMI_AMI_TestHandler
62 public:
63 /// Constructor.
64 Handler () = default;
66 /// Destructor.
67 ~Handler () = default;
69 void foo (CORBA::Long ami_return_val,
70 CORBA::Long out_l)
72 if (debug)
74 ACE_DEBUG ((LM_DEBUG,
75 "Callback method <foo> called: result <%d>, out_arg <%d>\n",
76 ami_return_val,
77 out_l));
79 if (out_l != 931233)
81 ACE_ERROR ((LM_ERROR,
82 "ERROR: out_l not 931233: %d\n",
83 out_l));
84 result = 1;
86 if (ami_return_val != 931234)
88 ACE_ERROR ((LM_ERROR,
89 "ERROR: ami_return_val not 931234: %d\n",
90 ami_return_val));
91 result = 1;
95 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
97 ACE_DEBUG ((LM_DEBUG,
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"));
108 if (ex.id != 42)
110 ACE_ERROR ((LM_ERROR,
111 "ERROR: ex.id not 42: %d\n",
112 ex.id));
113 result = 1;
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 ()));
120 result = 1;
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",
136 result));
139 void get_yadda_excep (::Messaging::ExceptionHolder *)
141 ACE_DEBUG ((LM_DEBUG,
142 "Callback method <get_yadda_excep> called:\n"));
145 void set_yadda ()
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[])
173 CORBA::ORB_var orb =
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)
188 return 1;
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",
200 ior),
204 // Instantiate the ReplyHandler and register that with the POA.
205 Handler handler;
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 (),
225 "Let's talk AMI.");
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",
233 ni));
235 ami_test_var->sendc_foo (the_handler_var.in (),
237 "Let's talk AMI.");
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
249 if (debug)
251 ACE_DEBUG ((LM_DEBUG,
252 "<%d> Asynchronous methods issued\n",
253 niterations));
256 if (debug)
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,
264 "Let's talk SMI.");
266 for (ssize_t ni = 0; ni < niterations; ni++)
268 ACE_DEBUG ((LM_DEBUG,
269 "Sending another asynch message: %d\n",
270 ni));
272 ami_test_var->sendc_foo (the_handler_var.in (),
274 "Let's talk AMI.");
277 if (debug)
279 ACE_DEBUG ((LM_DEBUG,
280 "<%d> more asynchronous methods issued\n",
281 niterations));
284 if (debug)
286 ACE_DEBUG ((LM_DEBUG,
287 "Issuing a synchronous method to collect the AMI replies again\n"));
290 number = ami_test_var->foo (l,
292 "Let's talk SMI.");
294 if (debug)
296 ACE_DEBUG ((LM_DEBUG,
297 "Received the following number: %d\n",
298 number));
301 if (shutdown_flag)
303 ami_test_var->shutdown ();
306 poa_var->destroy (1, // ethernalize objects
307 0); // wait for completion
309 orb->destroy ();
311 catch (const CORBA::Exception& ex)
313 ex._tao_print_exception ("Caught exception:");
314 return 1;
317 return result;