Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / performance-tests / Pluggable / PP_Test_Client.cpp
blob4cf2427b933f4bb8700d58b7527e583dbaf4dc46
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 (void)
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 (void)
172 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SEND_ONEWAY_START);
173 this->objref_->send_oneway ();
174 this->call_count_++;
176 catch (const CORBA::Exception& ex)
178 ex._tao_print_exception ("from send_oneway");
180 this->error_count_++;
184 // Twoway test.
186 void
187 PP_Test_Client::send_void (void)
192 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SEND_VOID_START);
193 this->objref_->send_void ();
194 this->call_count_++;
196 catch (const CORBA::Exception& ex)
198 ex._tao_print_exception ("from send_void");
200 this->error_count_++;
204 // Send an octet
206 // Execute client example code.
209 PP_Test_Client::run ()
211 if (this->only_void_)
213 return this->run_void ();
216 if (this->only_oneway_)
218 return this->run_oneway ();
221 CORBA::ULong i;
223 // Show the results one type at a time.
225 // VOID
226 this->call_count_ = 0;
227 this->error_count_ = 0;
229 for (i = 0; i < this->loop_count_; i++)
231 this->send_void ();
234 // ONEWAY
235 this->call_count_ = 0;
236 this->error_count_ = 0;
238 for (i = 0; i < this->loop_count_; i++)
240 this->send_oneway ();
243 // This causes a memPartFree on VxWorks.
244 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
245 this->shutdown_server (this->shutdown_);
247 return this->error_count_ == 0 ? 0 : 1;
251 PP_Test_Client::shutdown_server (int do_shutdown)
256 if (do_shutdown)
258 ACE_DEBUG ((LM_DEBUG,
259 "shutdown on Pluggable_Test object\n"));
261 this->objref_->shutdown ();
264 ACE_DEBUG ((LM_DEBUG,
265 "server, please ACE_OS::exit"));
268 catch (const CORBA::Exception& ex)
270 ex._tao_print_exception ("from shutdown_server");
272 return -1;
274 return 0;
278 PP_Test_Client::run_oneway (void)
283 CORBA::ULong i;
285 // ONEWAY
286 this->call_count_ = 0;
287 this->error_count_ = 0;
289 for (i = 0; i < this->loop_count_; i++)
291 this->send_oneway ();
294 if (this->shutdown_)
296 ACE_DEBUG ((LM_DEBUG,
297 "shutdown on Pluggable_Test object\n"));
299 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
301 this->objref_->shutdown ();
303 ACE_DEBUG ((LM_DEBUG,
304 "server, please ACE_OS::exit"));
307 catch (const CORBA::Exception& ex)
309 ex._tao_print_exception ("from objref_->shutdown");
311 return -1;
313 return this->error_count_ == 0 ? 0 : 1;
317 PP_Test_Client::run_void (void)
322 CORBA::ULong i;
324 // ONEWAY
325 this->call_count_ = 0;
326 this->error_count_ = 0;
328 for (i = 0; i < this->loop_count_; i++)
330 this->send_void ();
333 if (this->shutdown_)
335 ACE_DEBUG ((LM_DEBUG,
336 "shutdown on Pluggable_Test object\n"));
338 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_SERVER_SHUTDOWN_START);
340 this->objref_->shutdown ();
342 ACE_DEBUG ((LM_DEBUG,
343 "server, please ACE_OS::exit"));
346 catch (const CORBA::Exception& ex)
348 ex._tao_print_exception ("from objref_->shutdown");
350 return -1;
352 return this->error_count_ == 0 ? 0 : 1;
355 PP_Test_Client::~PP_Test_Client (void)
357 // Free resources and close the IOR files.
358 if (this->factory_ior_file_)
360 ACE_OS::fclose (this->factory_ior_file_);
363 if (this->f_handle_ != ACE_INVALID_HANDLE)
365 ACE_OS::close (this->f_handle_);
368 if (this->factory_key_ != 0)
370 ACE_OS::free (this->factory_key_);
375 PP_Test_Client::init (int argc, ACE_TCHAR **argv)
377 this->argc_ = argc;
378 this->argv_ = argv;
382 // Retrieve the ORB.
383 this->orb_ = CORBA::ORB_init (this->argc_,
384 this->argv_,
385 "internet");
386 // Parse command line and verify parameters.
387 if (this->parse_args () == -1)
389 return -1;
392 if (this->factory_key_ == 0)
394 ACE_ERROR_RETURN ((LM_ERROR,
395 "%s: no factory key specified\n",
396 this->argv_[0]),
397 -1);
400 CORBA::Object_var factory_object =
401 this->orb_->string_to_object (this->factory_key_);
403 this->factory_ =
404 Pluggable_Test_Factory::_narrow (factory_object.in());
406 if (CORBA::is_nil (this->factory_.in ()))
408 ACE_ERROR_RETURN ((LM_ERROR,
409 "invalid factory key <%s>\n",
410 this->factory_key_),
411 -1);
414 ACE_DEBUG ((LM_DEBUG,
415 "Factory received OK\n"));
417 // Now retrieve the Pluggable_Test obj ref corresponding to the key.
418 ACE_FUNCTION_TIMEPROBE (PP_TEST_CLIENT_MAKE_PLUGGABLE_START);
420 this->objref_ = this->factory_->make_pluggable_test ();
422 if (CORBA::is_nil (this->objref_.in ()))
424 ACE_ERROR_RETURN ((LM_ERROR,
425 "null objref returned by factory\n"),
426 -1);
429 catch (const CORBA::Exception& ex)
431 ex._tao_print_exception ("Pluggable_Test::init");
432 return -1;
435 return 0;