More tests update
[ACE_TAO.git] / TAO / tests / AMI / simple_client.cpp
blob47a4fef904756f10a2558bec9447f43bed91c913
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 (void) {};
66 /// Destructor.
67 ~Handler (void) {};
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)
98 ACE_DEBUG ((LM_DEBUG,
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"));
109 if (ex.id != 42)
111 ACE_ERROR ((LM_ERROR,
112 "ERROR: ex.id not 42: %d\n",
113 ex.id));
114 result = 1;
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 ()));
121 result = 1;
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",
137 result));
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[])
174 CORBA::ORB_var orb =
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)
189 return 1;
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",
201 ior),
205 // Instantiate the ReplyHandler and register that with the POA.
206 Handler handler;
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 (),
226 "Let's talk AMI.");
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",
234 ni));
236 ami_test_var->sendc_foo (the_handler_var.in (),
238 "Let's talk AMI.");
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
250 if (debug)
252 ACE_DEBUG ((LM_DEBUG,
253 "<%d> Asynchronous methods issued\n",
254 niterations));
257 if (debug)
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,
265 "Let's talk SMI.");
267 for (ssize_t ni = 0; ni < niterations; ni++)
269 ACE_DEBUG ((LM_DEBUG,
270 "Sending another asynch message: %d\n",
271 ni));
273 ami_test_var->sendc_foo (the_handler_var.in (),
275 "Let's talk AMI.");
278 if (debug)
280 ACE_DEBUG ((LM_DEBUG,
281 "<%d> more asynchronous methods issued\n",
282 niterations));
285 if (debug)
287 ACE_DEBUG ((LM_DEBUG,
288 "Issuing a synchronous method to collect the AMI replies again\n"));
291 number = ami_test_var->foo (l,
293 "Let's talk SMI.");
295 if (debug)
297 ACE_DEBUG ((LM_DEBUG,
298 "Received the following number: %d\n",
299 number));
302 if (shutdown_flag)
304 ami_test_var->shutdown ();
307 poa_var->destroy (1, // ethernalize objects
308 0); // wait for completion
310 orb->destroy ();
312 catch (const CORBA::Exception& ex)
314 ex._tao_print_exception ("Caught exception:");
315 return 1;
318 return result;