Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / AMI / client.cpp
blobf2f7aace2da500367498616eecad8c7441ff2845
1 #include "Echo_Handler.h"
2 #include "Client_ORBInitializer.h"
3 #include "Client_Interceptor.h"
5 #include "tao/ORBInitializer_Registry.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/Log_Msg.h"
9 #include <iostream>
11 const ACE_TCHAR *ior = ACE_TEXT ("file://test.ior");
12 static int exit_status = 0;
13 const unsigned long ITERATIONS = 100;
15 int
16 parse_args (int argc, ACE_TCHAR *argv[])
18 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
19 int c;
21 while ((c = get_opts ()) != -1)
22 switch (c)
24 case 'k':
25 ior = get_opts.opt_arg ();
26 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s "
32 "-k <ior> "
33 "\n",
34 argv [0]),
35 -1);
37 // Indicates successful parsing of the command line
38 return 0;
41 static void test_synchronous (Test::Echo_ptr echo);
43 static void test_ami (CORBA::ORB_ptr orb,
44 Test::Echo_ptr echo);
45 int
46 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
48 try
51 PortableInterceptor::ORBInitializer_var initializer (
52 new Client_ORBInitializer);
53 PortableInterceptor::register_orb_initializer (initializer.in());
56 CORBA::ORB_var orb =
57 CORBA::ORB_init (argc, argv);
59 if (parse_args (argc, argv) != 0)
60 return 1;
62 CORBA::Object_var poa_object =
63 orb->resolve_initial_references ("RootPOA");
65 PortableServer::POA_var root_poa =
66 PortableServer::POA::_narrow (poa_object.in ());
68 if (CORBA::is_nil (root_poa.in ()))
69 ACE_ERROR_RETURN ((LM_ERROR,
70 " (%P|%t) Panic: nil RootPOA\n"),
71 1);
73 PortableServer::POAManager_var poa_manager =
74 root_poa->the_POAManager ();
76 CORBA::Object_var tmp =
77 orb->string_to_object (ior);
79 Test::Echo_var echo =
80 Test::Echo::_narrow (tmp.in ());
82 poa_manager->activate ();
84 if (CORBA::is_nil (echo.in ()))
86 ACE_ERROR_RETURN ((LM_DEBUG,
87 "Nil Test::Echo reference <%s>\n",
88 ior),
89 1);
92 test_synchronous (echo.in ());
94 test_ami (orb.in (),
95 echo.in ());
97 echo->shutdown ();
99 orb->destroy ();
101 unsigned long request_count =
102 Echo_Client_Request_Interceptor::request_count;
103 unsigned long response_count =
104 Echo_Client_Request_Interceptor::reply_count
105 + Echo_Client_Request_Interceptor::other_count
106 + Echo_Client_Request_Interceptor::exception_count;
108 if (request_count != response_count)
110 ACE_ERROR ((LM_ERROR,
111 "ERROR: Mismatched count of requests and responses "
112 " (request = %d, response = %d)\n",
113 request_count, response_count));
116 if (request_count == 0)
118 ACE_ERROR ((LM_ERROR,
119 "ERROR: No requests handled "));
122 if (response_count == 0)
124 ACE_ERROR ((LM_ERROR,
125 "ERROR: No response handled "));
128 catch (const CORBA::Exception& ex)
130 ex._tao_print_exception ("Exception caught:");
131 return 1;
134 return exit_status;
137 static void
138 test_synchronous (Test::Echo_ptr echo)
140 unsigned long initial_request_count =
141 Echo_Client_Request_Interceptor::request_count;
142 unsigned long initial_reply_count =
143 Echo_Client_Request_Interceptor::reply_count;
145 for (unsigned long i = 0; i != ITERATIONS; ++i)
147 CORBA::String_var s =
148 echo->echo_operation ("dummy message");
151 unsigned long total_request_count =
152 Echo_Client_Request_Interceptor::request_count - initial_request_count;
153 unsigned long total_reply_count =
154 Echo_Client_Request_Interceptor::reply_count - initial_reply_count;
156 if (total_request_count != ITERATIONS
157 || total_reply_count != ITERATIONS)
159 ACE_ERROR((LM_ERROR,
160 "ERROR: Invalid or mismatched request/reply "
161 "count (request = %d, reply = %d)\n",
162 total_request_count, total_reply_count));
163 exit_status = 1;
166 if (total_request_count == 0)
168 ACE_ERROR ((LM_ERROR,
169 "ERROR: No synchronouse requests handled "));
172 if (total_reply_count == 0)
174 ACE_ERROR ((LM_ERROR,
175 "ERROR: No synchronouse requests handled "));
179 static void
180 test_ami (CORBA::ORB_ptr orb,
181 Test::Echo_ptr echo)
183 Test::AMI_EchoHandler_var echo_handler;
184 Echo_Handler * echo_handler_impl = new Echo_Handler;
186 PortableServer::ServantBase_var safe_echo_handler = echo_handler_impl;
188 echo_handler =
189 echo_handler_impl->_this ();
191 unsigned long initial_reply_count =
192 Echo_Client_Request_Interceptor::reply_count;
193 unsigned long initial_request_count =
194 Echo_Client_Request_Interceptor::request_count;
195 unsigned long initial_other_count =
196 Echo_Client_Request_Interceptor::other_count;
198 for (unsigned long i = 0; i != ITERATIONS; ++i)
200 echo->sendc_echo_operation (
201 echo_handler.in (),
202 "dummy message");
205 unsigned long total_reply_count =
206 Echo_Client_Request_Interceptor::reply_count - initial_reply_count;
207 unsigned long total_request_count =
208 Echo_Client_Request_Interceptor::request_count -
209 (total_reply_count + initial_request_count);
210 unsigned long total_other_count =
211 Echo_Client_Request_Interceptor::other_count - initial_other_count;
213 if (total_request_count != ITERATIONS
214 || total_other_count != ITERATIONS)
216 ACE_ERROR((LM_ERROR,
217 "ERROR: In test_ami () unexpected request/other "
218 "count (request = %d, other = %d)\n",
219 total_request_count, total_other_count));
220 exit_status = 1;
223 while (echo_handler_impl->replies () != ITERATIONS)
225 CORBA::Boolean pending =
226 orb->work_pending ();
228 if (pending)
230 orb->perform_work ();
235 total_request_count =
236 Echo_Client_Request_Interceptor::request_count - initial_request_count;
237 total_reply_count =
238 Echo_Client_Request_Interceptor::reply_count - initial_reply_count;
240 // total_request_count is 2*ITERATIONS since it's incremented twice for
241 // each call. Once for Echo and once for Echo_Handler.
242 if (total_request_count != 2 * ITERATIONS
243 || total_reply_count != ITERATIONS)
245 ACE_ERROR((LM_ERROR,
246 "ERROR: In test_ami () unexpected request/reply "
247 "count (request = %d, reply = %d)\n",
248 total_request_count, total_reply_count));
249 exit_status = 1;
253 #if 0
254 static void
255 wait_for_exception (CORBA::ORB_ptr orb,
256 Test::Echo_ptr echo)
258 ACE_Time_Value tv (1, 0);
259 orb->run (tv);
261 bool exception_detected = false;
263 while(!exception_detected)
267 CORBA::String_var dummy =
268 echo->echo_operation ("foo");
270 catch (const CORBA::Exception& ex)
272 exception_detected = true;
276 tv = ACE_Time_Value (1, 0);
277 orb->run (tv);
280 #endif /*if 0*/