Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_3801_Regression / simple_client.cpp
blobb14fdb5b1d8035270bf507f6360c5b5163c3635a
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"
17 #include "ami_testS.h"
19 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
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: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 'x':
40 shutdown_flag = 1;
41 break;
42 case '?':
43 default:
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "usage: %s "
46 "-k <ior> "
47 "-x "
48 "\n",
49 argv [0]),
50 -1);
52 // Indicates successful parsing of the command line
53 return 0;
56 class Handler : public POA_A::AMI_AMI_TestHandler
58 public:
59 /// Constructor.
60 Handler () = default;
62 /// Destructor.
63 ~Handler () = default;
65 void get_yadda (CORBA::Long result)
67 ACE_ERROR ((LM_ERROR,
68 "Error: Callback method <get_yadda> called: result <%d>\n",
69 result));
70 result = 1;
73 void get_yadda_excep (::Messaging::ExceptionHolder *excep_holder)
75 ACE_DEBUG ((LM_DEBUG,
76 "Callback method <get_yadda_excep> called:\n"));
77 try
79 excep_holder->raise_exception ();
81 catch (const A::DidTheRightThingB& ex)
83 ACE_DEBUG ((LM_DEBUG,
84 "... exception received successfully\n"));
85 if (ex.id != 42)
87 ACE_ERROR ((LM_ERROR,
88 "ERROR: ex.id not 42: %d\n",
89 ex.id));
90 result = 1;
92 if (ACE_OS::strcmp (ex.whatDidTheRightThing.in (), "Hello world") != 0)
94 ACE_ERROR ((LM_ERROR,
95 "ERROR: ex.whatDidTheRightThing not ok: <%C>\n",
96 ex.whatDidTheRightThing.in ()));
97 result = 1;
100 catch (const CORBA::Exception& ex)
102 ex._tao_print_exception ("ERROR");
103 ACE_ERROR ((LM_ERROR,
104 "... caught the wrong exception -> ERROR\n"));
108 void set_yadda ()
110 ACE_ERROR ((LM_ERROR,
111 "Error: Callback method <set_yadda> called:\n"));
112 result = 1;
115 void set_yadda_excep (::Messaging::ExceptionHolder *excep_holder)
117 ACE_DEBUG ((LM_DEBUG,
118 "Callback method <set_yadda_excep> called:\n"));
121 excep_holder->raise_exception ();
123 catch (const A::DidTheRightThing& ex)
125 ACE_DEBUG ((LM_DEBUG,
126 "... exception received successfully\n"));
127 if (ex.id != 42)
129 ACE_ERROR ((LM_ERROR,
130 "ERROR: ex.id not 42: %d\n",
131 ex.id));
132 result = 1;
134 if (ACE_OS::strcmp (ex.whatDidTheRightThing.in (), "Hello world") != 0)
136 ACE_ERROR ((LM_ERROR,
137 "ERROR: ex.whatDidTheRightThing not ok: <%C>\n",
138 ex.whatDidTheRightThing.in ()));
139 result = 1;
142 catch (const CORBA::Exception& ex)
144 ex._tao_print_exception ("ERROR");
145 ACE_ERROR ((LM_ERROR,
146 "... caught the wrong exception -> ERROR\n"));
150 void get_dadda (CORBA::Long result)
152 ACE_ERROR ((LM_ERROR,
153 "Error: Callback method <get_dadda> called: result <%d>\n",
154 result));
155 result = 1;
158 void get_dadda_excep (::Messaging::ExceptionHolder *excep_holder)
160 ACE_DEBUG ((LM_DEBUG,
161 "Callback method <get_dadda_excep> called:\n"));
164 excep_holder->raise_exception ();
166 catch (const A::DidTheRightThing& ex)
168 ACE_DEBUG ((LM_DEBUG,
169 "... exception received successfully\n"));
170 if (ex.id != 42)
172 ACE_ERROR ((LM_ERROR,
173 "ERROR: ex.id not 42: %d\n",
174 ex.id));
175 result = 1;
177 if (ACE_OS::strcmp (ex.whatDidTheRightThing.in (), "Hello world") != 0)
179 ACE_ERROR ((LM_ERROR,
180 "ERROR: ex.whatDidTheRightThing not ok: <%C>\n",
181 ex.whatDidTheRightThing.in ()));
182 result = 1;
185 catch (const CORBA::Exception& ex)
187 ex._tao_print_exception ("ERROR");
188 ACE_ERROR ((LM_ERROR,
189 "... caught the wrong exception -> ERROR\n"));
193 void inout_arg_test (const char *)
195 ACE_DEBUG ((LM_DEBUG,
196 "Callback method <set_yadda_excep> called:\n"));
199 void inout_arg_test_excep (::Messaging::ExceptionHolder *)
205 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
209 CORBA::ORB_var orb =
210 CORBA::ORB_init (argc, argv);
212 CORBA::Object_var object_var =
213 orb->resolve_initial_references ("RootPOA");
215 PortableServer::POA_var poa_var =
216 PortableServer::POA::_narrow (object_var.in ());
218 PortableServer::POAManager_var poa_manager_var =
219 poa_var->the_POAManager ();
221 poa_manager_var->activate ();
223 if (parse_args (argc, argv) != 0)
224 return 1;
226 // We reuse the object_var smart pointer!
227 object_var = orb->string_to_object (ior);
229 A::AMI_Test_var ami_test_var =
230 A::AMI_Test::_narrow (object_var.in ());
232 if (CORBA::is_nil (ami_test_var.in ()))
234 ACE_ERROR_RETURN ((LM_ERROR,
235 "Object reference <%s> is nil.\n",
236 ior),
240 // Instantiate the ReplyHandler and register that with the POA.
241 Handler handler;
242 PortableServer::ObjectId_var id =
243 poa_var->activate_object (&handler);
245 CORBA::Object_var object = poa_var->id_to_reference (id.in ());
247 A::AMI_AMI_TestHandler_var the_handler_var =
248 A::AMI_AMI_TestHandler::_narrow (object.in ());
250 // Begin test of attributes
251 ami_test_var->sendc_get_yadda (the_handler_var.in ());
253 ami_test_var->sendc_set_yadda (the_handler_var.in (), 4711);
255 ami_test_var->sendc_get_yadda (the_handler_var.in ());
257 ami_test_var->sendc_get_dadda (the_handler_var.in ());
258 // End test of attributes
260 if (debug)
262 ACE_DEBUG ((LM_DEBUG,
263 "Issuing a synchronous method to collect the AMI replies\n"));
266 CORBA::String_var mystring = CORBA::string_dup ("Hello");
268 ami_test_var->inout_arg_test (mystring.inout ());
270 if (shutdown_flag)
272 ami_test_var->shutdown ();
275 poa_var->destroy (1, // ethernalize objects
276 0); // wait for completion
278 orb->destroy ();
280 catch (const CORBA::Exception& ex)
282 ex._tao_print_exception ("Caught exception:");
283 return 1;
286 return result;