Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / TransportCurrent / IIOP / client.cpp
blob0ac7557c468e848d707e612d6ef135f228b3a868
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 (void);
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 (void)
107 for (int i = 0; i < this->niterations_; ++i)
110 // Minimum CORBA does not define Object::_request, so we're just
111 // skipping the DII part in those cases.
112 #if (!defined(TAO_HAS_MINIMUM_CORBA) || (TAO_HAS_MINIMUM_CORBA == 0))
114 if (this->use_dii_too_)
116 ACE_DEBUG ((LM_DEBUG,
117 ACE_TEXT ("Client (%P|%t) Invoking server->invoked_by_client() via DII\n")));
119 CORBA::Request_var request =
120 this->server_->_request ("invoked_by_client");
122 request->set_return_type (CORBA::_tc_void);
124 request->invoke ();
127 #endif /* (!defined(TAO_HAS_MINIMUM_CORBA) || (TAO_HAS_MINIMUM_CORBA == 0)) */
129 ACE_DEBUG ((LM_DEBUG,
130 ACE_TEXT ("Client (%P|%t) Invoking server->invoked_by_client() via SII\n")));
132 this->server_->invoked_by_client ();
134 if (TAO_debug_level > 0 && i % 100 == 0)
135 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Iteration = %d\n"),
136 i));
139 catch (const CORBA::Exception& ex)
141 ex._tao_print_exception ("Client: exception raised");
143 return 0;
147 /// The main driver
149 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
151 #if TAO_HAS_TRANSPORT_CURRENT == 1
155 Test::Client_Request_Interceptor* cri = 0;
156 ACE_NEW_RETURN (cri,
157 Test::Client_Request_Interceptor (CLIENT_ORB_ID,
158 test_transport_current),
159 -1);
160 PortableInterceptor::ClientRequestInterceptor_var cri_safe (cri);
162 PortableInterceptor::ORBInitializer_ptr temp_initializer = 0;
163 ACE_NEW_RETURN (temp_initializer,
164 Test::Client_ORBInitializer (cri),
165 -1);
166 PortableInterceptor::ORBInitializer_var orb_initializer (temp_initializer);
168 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
170 CORBA::ORB_var orb =
171 CORBA::ORB_init (argc,
172 argv,
173 CLIENT_ORB_ID);
175 if (parse_args (argc, argv) != 0)
176 ACE_ERROR_RETURN ((LM_ERROR,
177 ACE_TEXT ("Client (%P|%t) Failure to parse the command line.\n"),
178 ior),
179 -1);
184 test_transport_current (orb.in ());
186 ACE_ERROR_RETURN ((LM_ERROR,
187 ACE_TEXT ("Client (%P|%t) ERROR: ")
188 ACE_TEXT ("TC invocation, outside of ")
189 ACE_TEXT ("interceptor context is undefined.")
190 ACE_TEXT (" Expected exception was not thrown\n")),
191 -1);
193 catch (const Transport::NoContext& )
195 ACE_DEBUG ((LM_DEBUG,
196 ACE_TEXT ("Client (%P|%t) Expected exception occurred when trying ")
197 ACE_TEXT ("to access traits outside the ")
198 ACE_TEXT ("interceptor or upcall context.\n")));
201 // Resolve the target object
202 CORBA::Object_var obj = orb->string_to_object (ior);
204 Test::Transport::CurrentTest_var server =
205 Test::Transport::CurrentTest::_narrow (obj.in ());
207 if (CORBA::is_nil (server.in ()))
208 ACE_ERROR_RETURN ((LM_ERROR,
209 ACE_TEXT ("Client (%P|%t) The server object reference <%s> is nil.\n"),
210 ior),
211 -1);
213 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Spawning %d threads\n"), nthreads));
215 // Spawn a number of clients doing the same thing for a
216 // predetermined number of times
217 Worker client (server.in (), niterations, use_dii);
219 #if defined (ACE_HAS_THREADS)
220 if (client.activate (THR_NEW_LWP | THR_JOINABLE,
221 nthreads) != 0)
222 ACE_ERROR_RETURN ((LM_ERROR,
223 ACE_TEXT ("Client (%P|%t) Cannot activate %d client threads\n"),
224 nthreads),
225 -1);
226 client.thr_mgr ()->wait ();
227 #else
228 if (nthreads > 1)
229 ACE_ERROR ((LM_WARNING,
230 ACE_TEXT ("Client (%P|%t) Cannot use threads other than ")
231 ACE_TEXT ("the only one available.\n")));
232 client.svc ();
233 #endif
235 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Collected any threads\n")));
237 CORBA::Long result = 0;
239 // Verify enough interception points have been triggered
240 if (cri->interceptions () != 2 * // request & response
241 niterations * // iterations
242 nthreads * // threads
243 (2*use_dii)) // sii and dii, if needed
245 ACE_ERROR ((LM_ERROR,
246 ACE_TEXT ("Client (%P|%t) Expected %d client-side interceptions, but detected %d\n"),
247 2 * niterations * nthreads * (2*use_dii),
248 cri->interceptions ()));
250 else
253 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Invoking server->self_test()\n")));
255 // Self-test the server side
256 result = server->self_test ();
258 if (result != 0)
259 ACE_ERROR ((LM_ERROR,
260 ACE_TEXT ("Client (%P|%t) Server self-test reported failure\n")));
263 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Client (%P|%t) Invoking oneway server->shutdown()\n")));
265 server->shutdown ();
267 orb->destroy ();
269 ACE_DEBUG ((LM_INFO,
270 ACE_TEXT ("Client (%P|%t) Completed %s\n"),
271 ((result == 0) ? ACE_TEXT ("successfuly") : ACE_TEXT ("with failure"))));
272 return result;
274 catch (const CORBA::Exception& ex)
276 ex._tao_print_exception (
277 ACE_TEXT (
278 "Client: Transport Current test (client-side) failed:"));
279 return -1;
282 #else /* TAO_HAS_TRANSPORT_CURRENT == 1 */
283 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Client (%P|%t) Need TAO_HAS_TRANSPORT_CURRENT enabled to run.\n")));
284 return 0;
285 #endif /* TAO_HAS_TRANSPORT_CURRENT == 1 */