Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / HTIOP / AMI / simple_client.cpp
blob1c7a118a8188f276f97a4febb7b79203e713a225
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 "../test_config.h"
16 #include "ace/OS_NS_sys_socket.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/Task.h"
20 #include "ami_testC.h"
21 #include "ami_testS.h"
25 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
26 int niterations = 5;
27 int shutdown_flag = 0;
28 int debug = 0;
30 int
31 parse_args (int argc, ACE_TCHAR *argv[])
33 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("dk:i:x"));
34 int c;
36 while ((c = get_opts ()) != -1)
37 switch (c)
39 case 'd':
40 debug = 1;
41 break;
42 case 'k':
43 ior = get_opts.opt_arg ();
44 break;
45 case 'i':
46 niterations = ACE_OS::atoi (get_opts.opt_arg ());
47 break;
48 case 'x':
49 shutdown_flag = 1;
50 break;
51 case '?':
52 default:
53 ACE_ERROR_RETURN ((LM_ERROR,
54 "usage: %s "
55 "-k <ior> "
56 "-i <niterations> "
57 "-x "
58 "\n",
59 argv [0]),
60 -1);
62 // Indicates successful parsing of the command line
63 return 0;
66 class Handler : public POA_A::AMI_AMI_TestHandler
68 public:
69 /// Constructor.
70 Handler (void) {};
72 /// Destructor.
73 ~Handler (void) {};
75 void foo (CORBA::Long ami_return_val,
76 CORBA::Long out_l)
78 if (debug)
80 ACE_DEBUG ((LM_DEBUG,
81 "Callback method <foo> called: result <%d>, out_arg <%d>\n",
82 ami_return_val,
83 out_l));
87 void foo_excep (::Messaging::ExceptionHolder * excep_holder)
90 ACE_DEBUG ((LM_DEBUG,
91 "Callback method <foo_excep> called:\n"
92 "Testing proper exception handling ...\n"));
93 try
95 excep_holder->raise_exception ();
97 catch (const A::DidTheRightThing&)
99 ACE_DEBUG ((LM_DEBUG,
100 "... exception received successfully\n"));
102 catch (const CORBA::Exception&)
104 ACE_DEBUG ((LM_DEBUG,
105 "... caught the wrong exception -> ERROR\n"));
110 void get_yadda (CORBA::Long result)
112 ACE_DEBUG ((LM_DEBUG,
113 "Callback method <get_yadda> called: result <%d>\n",
114 result));
117 void get_yadda_excep (::Messaging::ExceptionHolder *)
119 ACE_DEBUG ((LM_DEBUG,
120 "Callback method <get_yadda_excep> called:\n"));
123 void set_yadda (void)
125 ACE_DEBUG ((LM_DEBUG,
126 "Callback method <set_yadda> called:\n"));
129 void set_yadda_excep (::Messaging::ExceptionHolder *)
131 ACE_DEBUG ((LM_DEBUG,
132 "Callback method <set_yadda_excep> called:\n"));
138 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
140 ACE_START_TEST (ACE_TEXT ("HTIOP_AMI_simple_client"));
145 CORBA::ORB_var orb =
146 CORBA::ORB_init (argc, argv);
148 CORBA::Object_var object_var =
149 orb->resolve_initial_references ("RootPOA");
151 PortableServer::POA_var poa_var =
152 PortableServer::POA::_narrow (object_var.in ());
154 PortableServer::POAManager_var poa_manager_var =
155 poa_var->the_POAManager ();
157 poa_manager_var->activate ();
159 if (parse_args (argc, argv) != 0)
160 return 1;
162 // We reuse the object_var smart pointer!
163 object_var = orb->string_to_object (ior);
165 A::AMI_Test_var ami_test_var =
166 A::AMI_Test::_narrow (object_var.in ());
168 if (CORBA::is_nil (ami_test_var.in ()))
170 ACE_ERROR_RETURN ((LM_ERROR,
171 "Object reference <%s> is nil\n",
172 ior),
176 ACE_OS::socket_init ();
178 // Instantiate the ReplyHandler and register that with the POA.
179 Handler handler;
180 A::AMI_AMI_TestHandler_var the_handler_var =
181 handler._this ();
183 // Try out sending asynchronous messages without a reply handler
184 // registered. Things fail if we get an exception.
186 ami_test_var->sendc_foo (A::AMI_AMI_TestHandler::_nil (),
188 "");
191 // Trigger the DidTheRightThing exception on the server side
192 // by sending 0 to it.
193 ACE_DEBUG ((LM_DEBUG,
194 "Sending asynch message\n"));
196 ami_test_var->sendc_foo (the_handler_var.in (),
198 "Let's talk AMI.");
200 CORBA::Long l = 931247;
202 for (ssize_t ni = 0; ni < niterations; ni++)
204 ACE_DEBUG ((LM_DEBUG,
205 "Sending asynch message: %d\n",
206 ni));
208 ami_test_var->sendc_foo (the_handler_var.in (),
210 "Let's talk AMI.");
213 // Begin test of attributes
214 ami_test_var->sendc_get_yadda (the_handler_var.in ());
216 ami_test_var->sendc_set_yadda (the_handler_var.in (),
217 4711);
219 ami_test_var->sendc_get_yadda (the_handler_var.in ());
221 // End test of attributes
223 if (debug)
225 ACE_DEBUG ((LM_DEBUG,
226 "<%d> Asynchronous methods issued\n",
227 niterations));
230 if (debug)
232 ACE_DEBUG ((LM_DEBUG,
233 "Issuing a synchronous method to collect the AMI replies\n"));
236 //while (orb->work_pending())
237 // orb->perform_work ();
240 CORBA::Long number = ami_test_var->foo (l,
242 "Let's talk SMI.");
244 if (debug)
246 ACE_DEBUG ((LM_DEBUG,
247 "Received the following number: %d\n",
248 number));
251 if (shutdown_flag)
253 ACE_DEBUG ((LM_DEBUG, "invoking shutdown\n"));
254 ami_test_var->shutdown ();
257 poa_var->destroy (1, // ethernalize objects
258 0 // wait for completion
261 orb->destroy ();
263 catch (const CORBA::Exception& ex)
265 ex._tao_print_exception ("Caught exception:");
266 return 1;
269 ACE_END_TEST;
270 return 0;