Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / performance-tests / Pluggable / PP_Test_Client.cpp
blob784745f6ab9ca980ad024f932edc6d82cba7a62c
1 #include "PP_Test_Client.h"
3 #include "tao/Timeprobe.h"
4 #include "tao/TAO_Internal.h"
5 #include "tao/debug.h"
7 #include "ace/Read_Buffer.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/OS_NS_errno.h"
10 #include "ace/OS_NS_fcntl.h"
11 #include "ace/OS_NS_unistd.h"
12 #include "ace/OS_NS_string.h"
14 #if defined (ACE_ENABLE_TIMEPROBES)
16 static const char *PP_Test_Client_Timeprobe_Description[] =
18 "PP_Test_Client::send_oneway - start",
19 "PP_Test_Client::send_oneway - end",
21 "PP_Test_Client::send_void - start",
22 "PP_Test_Client::send_void - end",
24 "PP_Test_Client::make_pluggable - start",
25 "PP_Test_Client::make_pluggable - end",
27 "PP_Test_Client::server_shutdown - start",
28 "PP_Test_Client::server_shutdown - end"
31 enum
33 // Timeprobe description table start key
34 PP_TEST_CLIENT_SEND_ONEWAY_START = 10000,
35 PP_TEST_CLIENT_SEND_ONEWAY_END,
37 PP_TEST_CLIENT_SEND_VOID_START,
38 PP_TEST_CLIENT_SEND_VOID_END,
40 PP_TEST_CLIENT_MAKE_PLUGGABLE_START,
41 PP_TEST_CLIENT_MAKE_PLUGGABLE_END,
43 PP_TEST_CLIENT_SERVER_SHUTDOWN_START,
44 PP_TEST_CLIENT_SERVER_SHUTDOWN_END
47 // Setup Timeprobes
48 ACE_TIMEPROBE_EVENT_DESCRIPTIONS (PP_Test_Client_Timeprobe_Description,
49 PP_TEST_CLIENT_SEND_ONEWAY_START);
51 #endif /* ACE_ENABLE_TIMEPROBES */
53 // Constructor.
54 PP_Test_Client::PP_Test_Client (int shutdown)
55 : orb_ (0),
56 factory_key_ (0),
57 loop_count_ (1),
58 shutdown_ (shutdown),
59 objref_ (Pluggable_Test::_nil ()),
60 call_count_ (0),
61 error_count_ (0),
62 factory_ior_file_ (0),
63 f_handle_ (ACE_INVALID_HANDLE),
64 only_void_ (0),
65 only_oneway_ (0)
69 // Reads the Cubit factory ior from a file
71 int
72 PP_Test_Client::read_ior (ACE_TCHAR *filename)
74 // Open the file for reading.
75 this->f_handle_ = ACE_OS::open (filename,0);
77 if (this->f_handle_ == ACE_INVALID_HANDLE)
79 ACE_ERROR_RETURN ((LM_ERROR,
80 "Unable to open %s for writing: %p\n",
81 filename),
82 -1);
85 ACE_Read_Buffer ior_buffer (this->f_handle_);
87 char *data = ior_buffer.read ();
89 if (data == 0)
91 ACE_ERROR_RETURN ((LM_ERROR,
92 "Unable to allocate memory to read ior: %p\n"),
93 -1);
96 this->factory_key_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
98 ior_buffer.alloc ()->free (data);
100 return 0;
103 // Parses the command line arguments and returns an error status.
106 PP_Test_Client::parse_args ()
108 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("ovdn:f:k:x"));
109 int c;
110 int result;
112 while ((c = get_opts ()) != -1)
113 switch (c)
115 case 'v':
116 this->only_void_ = 1;
117 break;
118 case 'o':
119 this->only_oneway_ = 1;
120 break;
121 case 'd': // debug flag
122 TAO_debug_level++;
123 break;
124 case 'n': // loop count
125 this->loop_count_ =
126 (u_int) ACE_OS::atoi (get_opts.opt_arg ());
127 break;
128 case 'f': // read the IOR from the file.
129 result = this->read_ior (get_opts.opt_arg ());
130 if (result < 0)
131 ACE_ERROR_RETURN ((LM_ERROR,
132 "Unable to read ior from %s : %p\n",
133 get_opts.opt_arg ()),
134 -1);
135 break;
136 case 'k': // read the cubit IOR from the command-line.
137 this->factory_key_ =
138 ACE_OS::strdup (get_opts.opt_arg ());
139 break;
140 case 'x':
141 ACE_DEBUG ((LM_DEBUG, "We will shutdown the server\n"));
142 this->shutdown_ = 1;
143 break;
144 case '?':
145 default:
146 ACE_ERROR_RETURN ((LM_ERROR,
147 "usage: %s"
148 " [-v]"
149 " [-o]"
150 " [-d]"
151 " [-n loopcount]"
152 " [-f factory-obj-ref-key-file]"
153 " [-k obj-ref-key]"
154 " [-x]"
155 "\n",
156 this->argv_ [0]),
157 -1);
160 // Indicates successful parsing of command line.
161 return 0;
164 // Oneway test.
166 void
167 PP_Test_Client::send_oneway ()
171 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SEND_ONEWAY_START);
172 this->objref_->send_oneway ();
173 this->call_count_++;
175 catch (const CORBA::Exception& ex)
177 ex._tao_print_exception ("from send_oneway");
179 this->error_count_++;
183 // Twoway test.
185 void
186 PP_Test_Client::send_void ()
190 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SEND_VOID_START);
191 this->objref_->send_void ();
192 this->call_count_++;
194 catch (const CORBA::Exception& ex)
196 ex._tao_print_exception ("from send_void");
198 this->error_count_++;
202 // Send an octet
204 // Execute client example code.
207 PP_Test_Client::run ()
209 if (this->only_void_)
211 return this->run_void ();
214 if (this->only_oneway_)
216 return this->run_oneway ();
219 CORBA::ULong i;
221 // Show the results one type at a time.
223 // VOID
224 this->call_count_ = 0;
225 this->error_count_ = 0;
227 for (i = 0; i < this->loop_count_; i++)
229 this->send_void ();
232 // ONEWAY
233 this->call_count_ = 0;
234 this->error_count_ = 0;
236 for (i = 0; i < this->loop_count_; i++)
238 this->send_oneway ();
241 // This causes a memPartFree on VxWorks.
242 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
243 this->shutdown_server (this->shutdown_);
245 return this->error_count_ == 0 ? 0 : 1;
249 PP_Test_Client::shutdown_server (int do_shutdown)
253 if (do_shutdown)
255 ACE_DEBUG ((LM_DEBUG,
256 "shutdown on Pluggable_Test object\n"));
258 this->objref_->shutdown ();
261 ACE_DEBUG ((LM_DEBUG,
262 "server, please ACE_OS::exit"));
265 catch (const CORBA::Exception& ex)
267 ex._tao_print_exception ("from shutdown_server");
269 return -1;
271 return 0;
275 PP_Test_Client::run_oneway ()
279 CORBA::ULong i;
281 // ONEWAY
282 this->call_count_ = 0;
283 this->error_count_ = 0;
285 for (i = 0; i < this->loop_count_; i++)
287 this->send_oneway ();
290 if (this->shutdown_)
292 ACE_DEBUG ((LM_DEBUG,
293 "shutdown on Pluggable_Test object\n"));
295 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
297 this->objref_->shutdown ();
299 ACE_DEBUG ((LM_DEBUG,
300 "server, please ACE_OS::exit"));
303 catch (const CORBA::Exception& ex)
305 ex._tao_print_exception ("from objref_->shutdown");
307 return -1;
309 return this->error_count_ == 0 ? 0 : 1;
313 PP_Test_Client::run_void ()
317 CORBA::ULong i;
319 // ONEWAY
320 this->call_count_ = 0;
321 this->error_count_ = 0;
323 for (i = 0; i < this->loop_count_; i++)
325 this->send_void ();
328 if (this->shutdown_)
330 ACE_DEBUG ((LM_DEBUG,
331 "shutdown on Pluggable_Test object\n"));
333 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
335 this->objref_->shutdown ();
337 ACE_DEBUG ((LM_DEBUG,
338 "server, please ACE_OS::exit"));
341 catch (const CORBA::Exception& ex)
343 ex._tao_print_exception ("from objref_->shutdown");
345 return -1;
347 return this->error_count_ == 0 ? 0 : 1;
350 PP_Test_Client::~PP_Test_Client ()
352 // Free resources and close the IOR files.
353 if (this->factory_ior_file_)
355 ACE_OS::fclose (this->factory_ior_file_);
358 if (this->f_handle_ != ACE_INVALID_HANDLE)
360 ACE_OS::close (this->f_handle_);
363 if (this->factory_key_ != 0)
365 ACE_OS::free (this->factory_key_);
370 PP_Test_Client::init (int argc, ACE_TCHAR **argv)
372 this->argc_ = argc;
373 this->argv_ = argv;
377 // Retrieve the ORB.
378 this->orb_ = CORBA::ORB_init (this->argc_,
379 this->argv_,
380 "internet");
381 // Parse command line and verify parameters.
382 if (this->parse_args () == -1)
384 return -1;
387 if (this->factory_key_ == 0)
389 ACE_ERROR_RETURN ((LM_ERROR,
390 "%s: no factory key specified\n",
391 this->argv_[0]),
392 -1);
395 CORBA::Object_var factory_object =
396 this->orb_->string_to_object (this->factory_key_);
398 this->factory_ =
399 Pluggable_Test_Factory::_narrow (factory_object.in());
401 if (CORBA::is_nil (this->factory_.in ()))
403 ACE_ERROR_RETURN ((LM_ERROR,
404 "invalid factory key <%s>\n",
405 this->factory_key_),
406 -1);
409 ACE_DEBUG ((LM_DEBUG,
410 "Factory received OK\n"));
412 // Now retrieve the Pluggable_Test obj ref corresponding to the key.
413 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_MAKE_PLUGGABLE_START);
415 this->objref_ = this->factory_->make_pluggable_test ();
417 if (CORBA::is_nil (this->objref_.in ()))
419 ACE_ERROR_RETURN ((LM_ERROR,
420 "null objref returned by factory\n"),
421 -1);
424 catch (const CORBA::Exception& ex)
426 ex._tao_print_exception ("Pluggable_Test::init");
427 return -1;
430 return 0;