Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / IPC_SAP / SSL_SAP / SSL-client.cpp
blobcc035647927ee9c184968fc18a6169fb463e1dd4
1 // This tests the features of the <ACE_SSL_SOCK_Connector> and
2 // <ACE_SSL_SOCK_Stream> classes. In addition, it can be used to test the
3 // oneway and twoway latency and throughput at the socket-level. This
4 // is useful as a baseline to compare against ORB-level performance
5 // for the same types of data.
7 #include "ace/OS_NS_string.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/INET_Addr.h"
10 #include "ace/Thread_Manager.h"
11 #include "ace/Singleton.h"
12 #include "ace/Get_Opt.h"
13 #include "ace/High_Res_Timer.h"
14 #include "ace/Truncate.h"
16 #include "ace/SSL/SSL_SOCK_Connector.h"
18 #include "SSL-client.h"
20 Options::Options ()
21 : host_ (ACE_DEFAULT_SERVER_HOST),
22 port_ (ACE_DEFAULT_SERVER_PORT),
23 sleep_time_ (0, 0), // By default, don't sleep between calls.
24 threads_ (10),
25 message_len_ (0),
26 message_buf_ (0),
27 io_source_ (ACE_INVALID_HANDLE), // Defaults to using the generator.
28 iterations_ (10000),
29 oneway_ (1) // Make oneway calls the default.
30 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
31 , barrier_ (0)
32 #endif /* ACE_MT_SAFE */
34 ACE_OS::strcpy (quit_string_, "q");
37 Options::~Options ()
39 ACE_MT (delete this->barrier_);
40 delete [] this->message_buf_;
43 // Options Singleton.
44 typedef ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX> OPTIONS;
46 int
47 Options::init ()
49 // Check for default case.
50 if (this->message_len_ == 0)
51 this->message_len_ = ACE_OS::strlen ("TAO");
53 this->message_len_ += sizeof (ACE_UINT32);
55 ACE_NEW_RETURN (this->message_buf_,
56 char[this->message_len_],
57 -1);
59 // Copy the length into the beginning of the message.
60 ACE_UINT32 length =
61 ntohl (ACE_Utils::truncate_cast<u_long> (this->message_len_));
63 ACE_OS::memcpy ((void *) this->message_buf_,
64 (void *) &length,
65 sizeof length);
67 ACE_OS::memset ((void *) (this->message_buf_ + sizeof (ACE_UINT32)),
68 'a',
69 this->message_len_ - sizeof (ACE_UINT32));
71 // Allocate the barrier with the correct count.
72 ACE_MT (ACE_NEW_RETURN (this->barrier_,
73 ACE_Barrier (
74 ACE_Utils::truncate_cast<unsigned int> (
75 this->threads_)),
76 -1));
77 return 0;
80 size_t
81 Options::message_len () const
83 return this->message_len_;
86 const void *
87 Options::message_buf () const
89 return this->message_buf_;
92 ssize_t
93 Options::read (void *buf, size_t len, size_t &iteration)
95 ACE_UNUSED_ARG (len);
97 if (this->io_source_ == ACE_STDIN)
98 return ACE_OS::read (ACE_STDIN, buf, len);
99 else if (iteration >= this->iterations_)
100 return 0;
101 else
103 ACE_OS::memcpy (buf,
104 this->message_buf (),
105 len);
106 iteration++;
107 return ACE_Utils::truncate_cast<ssize_t> (len);
112 Options::parse_args (int argc, ACE_TCHAR *argv[])
114 //FUZZ: disable check_for_lack_ACE_OS
115 ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("2h:i:m:p:q:st:T:"), 1);
117 for (int c; (c = getopt ()) != -1; )
118 //FUZZ: enable check_for_lack_ACE_OS
119 switch (c)
121 case '2': // Disable the oneway client.
122 this->oneway_ = 0;
123 break;
124 case 'h':
125 this->host_ = getopt.opt_arg ();
126 break;
127 case 'i':
128 this->iterations_ = ACE_OS::atoi (getopt.opt_arg ());
129 break;
130 case 'm':
131 this->message_len_ = ACE_OS::atoi (getopt.opt_arg ());
132 break;
133 case 'p':
134 this->port_ = ACE_OS::atoi (getopt.opt_arg ());
135 break;
136 case 'q':
137 ACE_OS::strncpy (this->quit_string_,
138 ACE_TEXT_ALWAYS_CHAR (getopt.opt_arg ()),
139 QUIT_STRING_SIZE);
140 break;
141 case 's':
142 this->io_source_ = ACE_STDIN;
143 break;
144 case 't':
145 this->threads_ = (size_t) ACE_OS::atoi (getopt.opt_arg ());
146 break;
147 case 'T':
148 this->sleep_time_.set (0, ACE_OS::atoi (getopt.opt_arg ()));
149 break;
150 default:
151 ACE_ERROR_RETURN ((LM_ERROR,
152 ACE_TEXT ("(%P|%t) usage: %n [-2] [-h <host>] ")
153 ACE_TEXT ("[-i iterations] [-m message-size] ")
154 ACE_TEXT ("[-p <port>] [-q <quit string>] ")
155 ACE_TEXT ("[-s] [-t <threads>] [-T <sleep_time>]\n")),
156 -1);
159 return this->init ();
162 u_short
163 Options::port () const
165 return this->port_;
168 const ACE_TCHAR *
169 Options::host () const
171 return this->host_;
174 const char *
175 Options::quit_string () const
177 return this->quit_string_;
180 size_t
181 Options::threads () const
183 return this->threads_;
186 const ACE_Time_Value &
187 Options::sleep_time () const
189 return this->sleep_time_;
192 char *
193 Options::shared_client_test (u_short port,
194 ACE_SSL_SOCK_Stream &cli_stream)
196 ACE_INET_Addr remote_addr (port, this->host_);
198 ACE_SSL_SOCK_Connector con;
200 if (con.connect (cli_stream,
201 remote_addr) == -1)
202 ACE_ERROR_RETURN ((LM_ERROR,
203 ACE_TEXT ("(%P|%t) %p\n"),
204 ACE_TEXT ("connection failed")),
206 else
207 ACE_DEBUG ((LM_DEBUG,
208 ACE_TEXT ("(%P|%t) connected to %C at port %d\n"),
209 remote_addr.get_host_name (),
210 remote_addr.get_port_number ()));
212 // Allocate the transmit buffer.
213 char *buf;
214 ACE_NEW_RETURN (buf,
215 char[this->message_len ()],
218 ACE_DEBUG ((LM_DEBUG,
219 ACE_TEXT ("(%P|%t) waiting...\n")));
221 // Wait for all other threads to finish initialization.
222 ACE_MT (this->barrier_->wait ());
223 return buf;
225 // Static function entry point to the oneway client service.
227 void *
228 Options::oneway_client_test (void *)
230 Options *options = OPTIONS::instance ();
231 ACE_SSL_SOCK_Stream cli_stream;
233 // Add 1 to the port to trigger the oneway test!
234 char *request = options->shared_client_test (options->port () + 1,
235 cli_stream);
236 if (request == 0)
237 return 0;
239 // This variable is allocated off the stack to obviate the need for
240 // locking.
241 size_t iteration = 0;
243 // Keep track of return value.
244 size_t result = 0;
245 ACE_INT32 len =
246 ACE_Utils::truncate_cast<ACE_INT32> (options->message_len ());
248 ACE_DEBUG ((LM_DEBUG,
249 ACE_TEXT ("(%P|%t) starting oneway transmission\n")));
251 // Perform oneway transmission of data to server (correctly handles
252 // "incomplete writes").
254 for (ssize_t r_bytes;
255 (r_bytes = options->read (request, len, iteration)) > 0;
256 // Transmit at the proper rate.
257 ACE_OS::sleep (options->sleep_time ()))
258 if (ACE_OS::memcmp (request,
259 options->quit_string (),
260 ACE_OS::strlen (options->quit_string ())) == 0)
261 break;
262 else if (cli_stream.send_n (request, r_bytes) == -1)
264 ACE_ERROR ((LM_ERROR,
265 ACE_TEXT ("(%P|%t) %p\n"),
266 ACE_TEXT ("send_n")));
267 result = size_t (-1);
268 break;
271 // Close the connection.
272 cli_stream.close ();
274 delete [] request;
275 return (void *) result;
278 // Static function entry point to the twoway client service.
280 void *
281 Options::twoway_client_test (void *)
283 Options *options = OPTIONS::instance ();
285 ACE_SSL_SOCK_Stream cli_stream;
287 char *request = options->shared_client_test (options->port (),
288 cli_stream);
289 if (request == 0)
290 return 0;
292 // This variable is allocated off the stack to obviate the need for
293 // locking.
294 size_t iteration = 0;
296 // Keep track of return value.
297 size_t result = 0;
299 // Timer business.
300 ACE_High_Res_Timer timer;
302 ACE_INT32 len =
303 ACE_Utils::truncate_cast<ACE_INT32> (options->message_len ());
305 ACE_DEBUG ((LM_DEBUG,
306 ACE_TEXT ("(%P|%t) starting twoway transmission\n")));
308 // Perform twoway transmission of data to server (correctly handles
309 // "incomplete writes").
311 for (ssize_t r_bytes;
312 (r_bytes = options->read (request, len, iteration)) > 0;
313 // Transmit at the proper rate.
314 ACE_OS::sleep (options->sleep_time ()))
315 if (ACE_OS::memcmp (request,
316 options->quit_string (),
317 ACE_OS::strlen (options->quit_string ())) == 0)
318 break;
320 // Transmit <request> to the server.
321 else
323 // Note that we use the incremental feature of the
324 // <ACE_High_Res_Timer> so that we don't get "charged" for the
325 // <ACE_OS::sleep> used to control the rate at which requests
326 // are sent.
327 timer.start_incr ();
329 if (cli_stream.send_n (request, r_bytes) == -1)
331 ACE_ERROR ((LM_ERROR,
332 ACE_TEXT ("(%P|%t) %p\n"),
333 ACE_TEXT ("send_n")));
334 result = size_t (-1);
335 break;
337 // Receive the reply from the server. Normally, it just sends
338 // back 24 bytes, which is typical for an IIOP reply.
339 else if (cli_stream.recv (request, r_bytes) <= 0)
341 ACE_ERROR ((LM_ERROR,
342 ACE_TEXT ("(%P|%t) %p\n"),
343 ACE_TEXT ("recv")));
344 result = size_t (-1);
345 break;
348 timer.stop_incr ();
351 ACE_Time_Value tv;
353 timer.elapsed_time_incr (tv);
354 double real_time = static_cast<double> (tv.sec () * ACE_ONE_SECOND_IN_USECS + tv.usec ());
355 double messages_per_sec =
356 iteration * double (ACE_ONE_SECOND_IN_USECS) / real_time;
358 ACE_DEBUG ((LM_DEBUG,
359 ACE_TEXT ("(%t) messages = %d\n")
360 ACE_TEXT ("(%t) usec-per-message = %f\n")
361 ACE_TEXT ("(%t) messages-per-second = %0.00f\n"),
362 iteration,
363 real_time / double (iteration),
364 messages_per_sec < 0 ? 0 : messages_per_sec));
366 // Close the connection.
367 cli_stream.close ();
369 delete [] request;
370 return (void *) result;
373 ACE_THR_FUNC
374 Options::thr_func ()
376 if (this->oneway_ == 0)
377 return ACE_THR_FUNC (&Options::twoway_client_test);
378 else
379 return ACE_THR_FUNC (&Options::oneway_client_test);
382 static int
383 run_client ()
385 // Raise the socket handle limit to the maximum.
386 ACE::set_handle_limit ();
388 #if defined (ACE_HAS_THREADS)
389 if (ACE_Thread_Manager::instance ()->spawn_n (
390 OPTIONS::instance ()->threads (),
391 OPTIONS::instance ()->thr_func ()) == -1)
392 ACE_ERROR_RETURN ((LM_ERROR,
393 ACE_TEXT ("(%P|%t) %p\n"),
394 ACE_TEXT ("spawn_n")),
396 else
397 ACE_Thread_Manager::instance ()->wait ();
398 #else
399 (void) *(OPTIONS::instance ()->thr_func) ();
400 #endif /* ACE_HAS_THREADS */
401 return 0;
405 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
407 ACE_SSL_Context *context = ACE_SSL_Context::instance ();
409 context->certificate ("./dummy.pem", SSL_FILETYPE_PEM);
410 context->private_key ("./key.pem", SSL_FILETYPE_PEM);
412 // Initialize the logger.
413 ACE_LOG_MSG->open (argv[0]);
415 if (OPTIONS::instance ()->parse_args (argc, argv) == -1)
416 return -1;
418 // Run the client
419 run_client ();
421 return 0;