Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / TransportCurrent / IIOP / client.cpp
blob141d2517b7a0fd4ba801a616ba5f996ee3985cc5
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
5 #include "tao/DynamicInterface/Request.h"
6 #include "tao/ORBInitializer_Registry.h"
8 #include "tao/TransportCurrent/Transport_Current.h"
10 #include "Client_Request_Interceptor.h"
11 #include "Current_TestC.h"
13 #include "Client_ORBInitializer.h"
15 // Prototype
17 int
18 test_transport_current (CORBA::ORB_ptr);
20 using namespace TAO;
22 const char* CLIENT_ORB_ID = "client orb";
23 const ACE_TCHAR* ior = ACE_TEXT("file://server.ior");
25 int nthreads = 1;
26 int niterations = 1;
27 int use_dii = 1;
30 int
31 parse_args (int argc, ACE_TCHAR *argv[])
33 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("yt:n:k:"));
34 int c;
36 while ((c = get_opts ()) != -1)
37 switch (c)
39 case 'y':
40 use_dii = 0; // Do not use DII
41 break;
42 case 'k':
43 ior = get_opts.opt_arg ();
44 break;
45 case 't':
46 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
47 break;
48 case 'n':
49 niterations = ACE_OS::atoi (get_opts.opt_arg ());
50 break;
51 default:
52 ACE_ERROR_RETURN ((LM_ERROR,
53 "Usage: %s "
54 "-k IOR "
55 "-t threads "
56 "-n iterations "
57 "-y "
58 "\n",
59 argv[0]),
60 -1);
62 return 0;
66 /// A helper class to encapsulate a task
68 class Worker : public ACE_Task_Base
70 public:
71 Worker (Test::Transport::CurrentTest_ptr server, int niterations, int use_dii_too);
72 virtual int svc ();
74 private:
75 // The server.
76 Test::Transport::CurrentTest_var server_;
78 // The number of iterations on each client thread.
79 int niterations_;
81 // Whether to use DII in addition to SII
82 int use_dii_too_;
86 /// Ctor
88 Worker::Worker (Test::Transport::CurrentTest_ptr server,
89 int niterations,
90 int use_dii_too)
91 : server_ (Test::Transport::CurrentTest::_duplicate (server))
92 , niterations_ (niterations)
93 , use_dii_too_ (use_dii_too)
98 /// Test referencing the TC data *inside* the context of a client-side
99 /// interceptor
102 Worker::svc ()
106 for (int i = 0; i < this->niterations_; ++i)
108 // Minimum CORBA does not define Object::_request, so we're just
109 // skipping the DII part in those cases.
110 #if (!defined(TAO_HAS_MINIMUM_CORBA) || (TAO_HAS_MINIMUM_CORBA == 0))
112 if (this->use_dii_too_)
114 ACE_DEBUG ((LM_DEBUG,
115 ACE_TEXT ("Client (%P|%t) Invoking server->invoked_by_client() via DII\n")));
117 CORBA::Request_var request =
118 this->server_->_request ("invoked_by_client");
120 request->set_return_type (CORBA::_tc_void);
122 request->invoke ();
125 #endif /* (!defined(TAO_HAS_MINIMUM_CORBA) || (TAO_HAS_MINIMUM_CORBA == 0)) */
127 ACE_DEBUG ((LM_DEBUG,
128 ACE_TEXT ("Client (%P|%t) Invoking server->invoked_by_client() via SII\n")));
130 this->server_->invoked_by_client ();
132 if (TAO_debug_level > 0 && i % 100 == 0)
133 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Iteration = %d\n"),
134 i));
137 catch (const CORBA::Exception& ex)
139 ex._tao_print_exception ("Client: exception raised");
141 return 0;
145 /// The main driver
147 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
149 #if TAO_HAS_TRANSPORT_CURRENT == 1
153 Test::Client_Request_Interceptor* cri = 0;
154 ACE_NEW_RETURN (cri,
155 Test::Client_Request_Interceptor (CLIENT_ORB_ID,
156 test_transport_current),
157 -1);
158 PortableInterceptor::ClientRequestInterceptor_var cri_safe (cri);
160 PortableInterceptor::ORBInitializer_ptr temp_initializer = 0;
161 ACE_NEW_RETURN (temp_initializer,
162 Test::Client_ORBInitializer (cri),
163 -1);
164 PortableInterceptor::ORBInitializer_var orb_initializer (temp_initializer);
166 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
168 CORBA::ORB_var orb =
169 CORBA::ORB_init (argc,
170 argv,
171 CLIENT_ORB_ID);
173 if (parse_args (argc, argv) != 0)
174 ACE_ERROR_RETURN ((LM_ERROR,
175 ACE_TEXT ("Client (%P|%t) Failure to parse the command line.\n"),
176 ior),
177 -1);
182 test_transport_current (orb.in ());
184 ACE_ERROR_RETURN ((LM_ERROR,
185 ACE_TEXT ("Client (%P|%t) ERROR: ")
186 ACE_TEXT ("TC invocation, outside of ")
187 ACE_TEXT ("interceptor context is undefined.")
188 ACE_TEXT (" Expected exception was not thrown\n")),
189 -1);
191 catch (const Transport::NoContext& )
193 ACE_DEBUG ((LM_DEBUG,
194 ACE_TEXT ("Client (%P|%t) Expected exception occurred when trying ")
195 ACE_TEXT ("to access traits outside the ")
196 ACE_TEXT ("interceptor or upcall context.\n")));
199 // Resolve the target object
200 CORBA::Object_var obj = orb->string_to_object (ior);
202 Test::Transport::CurrentTest_var server =
203 Test::Transport::CurrentTest::_narrow (obj.in ());
205 if (CORBA::is_nil (server.in ()))
206 ACE_ERROR_RETURN ((LM_ERROR,
207 ACE_TEXT ("Client (%P|%t) The server object reference <%s> is nil.\n"),
208 ior),
209 -1);
211 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Spawning %d threads\n"), nthreads));
213 // Spawn a number of clients doing the same thing for a
214 // predetermined number of times
215 Worker client (server.in (), niterations, use_dii);
217 #if defined (ACE_HAS_THREADS)
218 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
219 nthreads) != 0)
220 ACE_ERROR_RETURN ((LM_ERROR,
221 ACE_TEXT ("Client (%P|%t) Cannot activate %d client threads\n"),
222 nthreads),
223 -1);
224 client.thr_mgr ()->wait ();
225 #else
226 if (nthreads > 1)
227 ACE_ERROR ((LM_WARNING,
228 ACE_TEXT ("Client (%P|%t) Cannot use threads other than ")
229 ACE_TEXT ("the only one available.\n")));
230 client.svc ();
231 #endif
233 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Collected any threads\n")));
235 CORBA::Long result = 0;
237 // Verify enough interception points have been triggered
238 if (cri->interceptions () != 2 * // request & response
239 niterations * // iterations
240 nthreads * // threads
241 (2*use_dii)) // sii and dii, if needed
243 ACE_ERROR ((LM_ERROR,
244 ACE_TEXT ("Client (%P|%t) Expected %d client-side interceptions, but detected %d\n"),
245 2 * niterations * nthreads * (2*use_dii),
246 cri->interceptions ()));
248 else
250 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Invoking server->self_test()\n")));
252 // Self-test the server side
253 result = server->self_test ();
255 if (result != 0)
256 ACE_ERROR ((LM_ERROR,
257 ACE_TEXT ("Client (%P|%t) Server self-test reported failure\n")));
260 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Invoking oneway server->shutdown()\n")));
262 server->shutdown ();
264 orb->destroy ();
266 ACE_DEBUG ((LM_INFO,
267 ACE_TEXT ("Client (%P|%t) Completed %s\n"),
268 ((result == 0) ? ACE_TEXT ("successfuly") : ACE_TEXT ("with failure"))));
269 return result;
271 catch (const CORBA::Exception& ex)
273 ex._tao_print_exception (
274 ACE_TEXT (
275 "Client: Transport Current test (client-side) failed:"));
276 return -1;
279 #else /* TAO_HAS_TRANSPORT_CURRENT == 1 */
280 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Client (%P|%t) Need TAO_HAS_TRANSPORT_CURRENT enabled to run.\n")));
281 return 0;
282 #endif /* TAO_HAS_TRANSPORT_CURRENT == 1 */