summaryrefslogtreecommitdiff
path: root/ACE/tests/Dev_Poll_Reactor_Echo_Test.cpp
blob: b634d93a08148dffac72e08e9b7622266c5bdfb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//=============================================================================
/**
 *  @file    Dev_Poll_Reactor_Echo_Test.cpp
 *
 *  This test implements a simple echo server using the
 *  Dev_Poll_Reactor.  This forces the reactor to behave like a
 *  reactor would in a typical client/server application, i.e.,
 *  receive a message then send a messages.
 *  @author Justin Wilson <wilsonj@ociweb.com>
 */
//=============================================================================

#include "test_config.h"

#if defined (ACE_HAS_DEV_POLL) || defined (ACE_HAS_EVENT_POLL)

#include "ace/OS_NS_signal.h"
#include "ace/Reactor.h"
#include "ace/Dev_Poll_Reactor.h"

#include "ace/Acceptor.h"
#include "ace/Connector.h"

#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"

#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_netdb.h"

#include <queue>

using SVC_HANDLER = ACE_Svc_Handler<ACE_SOCK_Stream, ACE_NULL_SYNCH>;

// ----------------------------------------------------

class Client : public SVC_HANDLER
{
public:
  Client ();

  //FUZZ: disable check_for_lack_ACE_OS
  int open (void * = 0) override;
  //FUZZ: enable check_for_lack_ACE_OS

  int handle_output (ACE_HANDLE handle) override;

  int handle_input (ACE_HANDLE handle) override;

  int handle_timeout (const ACE_Time_Value &current_time,
                              const void *act) override;

  int handle_close (ACE_HANDLE handle,
                            ACE_Reactor_Mask mask) override;

  std::string sent;
  std::string received;

private:
  unsigned int call_count_;
};


class Server : public SVC_HANDLER
{
public:
  Server ();

  int handle_input (ACE_HANDLE handle) override;

  int handle_output (ACE_HANDLE handle) override;

  int handle_close (ACE_HANDLE handle,
                            ACE_Reactor_Mask mask) override;

private:
  int send_i (const char* buffer,
              size_t size);

  std::queue<std::string*> buffer_list_;
  size_t offset_;
};

// ----------------------------------------------------

Client::Client ()
  : call_count_ (0)
{
}

int
Client::open (void *)
{
  // Trigger writes on a timer.
  ACE_Time_Value delay (1, 0);
  ACE_Time_Value restart (1, 0);
  if (this->reactor ()->schedule_timer (this,
                                        0,
                                        delay,
                                        restart) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%t) %p\n"),
                         ACE_TEXT ("Unable to schedule client side ")
                         ACE_TEXT ("timer in ACE_Dev_Poll_Reactor")),
                        -1);
    }

  if (this->reactor ()->register_handler (this, ACE_Event_Handler::READ_MASK) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%t) %p\n"),
                         ACE_TEXT ("Unable to register for reading ")
                         ACE_TEXT ("in ACE_Dev_Poll_Reactor")),
                        -1);
    }

  return 0;
}

int
Client::handle_output (ACE_HANDLE handle)
{
  std::string buffer = "Hello, world!";
  ssize_t bytes_sent = this->peer ().send (buffer.data (), buffer.size ());

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) Client::handle_output; handle = %d")
              ACE_TEXT (" bytes sent %d\n"),
              handle,
              bytes_sent));

  if (bytes_sent == -1)
    {
      if (errno == EWOULDBLOCK)
        return 0;  // Flow control kicked in.
      else if (errno == EPIPE || errno == ECONNRESET)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) Client::handle_output; server ")
                      ACE_TEXT ("closed handle %d\n"),
                      this->peer ().get_handle ()));
          return -1;
        }
      else
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%t) %p\n"),
                           ACE_TEXT ("Client::handle_output")),
                          -1);
    }
  else if (bytes_sent == 0)
    return -1;
  else
    this->sent.append (buffer.substr (0, bytes_sent));

  return -1;
}

int
Client::handle_input (ACE_HANDLE handle)
{
  for (;;)
    {
      char buffer[BUFSIZ];
      ssize_t bytes_read = this->peer ().recv (buffer, BUFSIZ);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) Client::handle_input handle = %d bytes_read = %d\n"),
                  handle, bytes_read));

      if (bytes_read == -1 && errno == EWOULDBLOCK)
        {
          return 0;
        }
      else if (bytes_read <= 0)
        {
          // Closed.
          return -1;
        }
      else
        {
          this->received.append (buffer, bytes_read);
        }
    }
}

int
Client::handle_timeout (const ACE_Time_Value &, const void *)
{
  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("(%t) Expected client timeout occurred at: %T\n")));

  if (this->call_count_ != 10)
    {
      // Register for write.
      if (this->reactor ()->register_handler (this, ACE_Event_Handler::WRITE_MASK) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("(%t) %p\n"),
                             ACE_TEXT ("Unable to register for writing ")
                             ACE_TEXT ("in ACE_Dev_Poll_Reactor")),
                            -1);
        }
      this->call_count_++;
      return 0;
    }
  else
    {
      // Shutdown.
      if (this->reactor ()->end_reactor_event_loop () == 0)
        ACE_DEBUG ((LM_INFO,
                    ACE_TEXT ("(%t) Successful client reactor shutdown.\n")));
      else
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%t) %p\n"),
                    ACE_TEXT ("Failed client reactor shutdown")));

      // Force this service handler to be closed in either case.
      return -1;
    }
}

int
Client::handle_close (ACE_HANDLE handle,
                      ACE_Reactor_Mask mask)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) Client::handle_close handle = %d mask = %xd\n"), handle, mask));
  return 0;
  //return SVC_HANDLER::handle_close (handle, mask);
}

// ----------------------------------------------------

Server::Server ()
  : offset_ (0)
{
}

int
Server::handle_input (ACE_HANDLE handle)
{
  for (;;)
    {
      char buffer[BUFSIZ];
      ssize_t bytes_read = this->peer ().recv (buffer, BUFSIZ);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) Server::handle_input handle = %d bytes_read = %d\n"),
                  handle, bytes_read));

      if (bytes_read == -1 && errno == EWOULDBLOCK)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) Server::handle_input handle = %d EWOULDBLOCK\n"),
                      handle));
          return 0;
        }
      else if (bytes_read == 0)
        {
          // Closed.
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) Server::handle_input handle = %d CLOSED\n"),
                      handle));
          return -1;
        }
      else
        {
          if (send_i (buffer, bytes_read) == -1)
            return -1;
        }
    }
}

int
Server::send_i (const char* buffer,
                size_t size)
{
  if (size == 0)
    {
      return 0;
    }

  if (buffer_list_.empty ())
    {
      // Register for write.
      if (this->reactor ()->register_handler (this, ACE_Event_Handler::WRITE_MASK) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("(%t) %p\n"),
                             ACE_TEXT ("Unable to register for writing ")
                             ACE_TEXT ("in ACE_Dev_Poll_Reactor")),
                            -1);
        }
    }

  buffer_list_.push (new std::string (buffer, size));
  return 0;
}

int
Server::handle_output (ACE_HANDLE handle)
{
  while (!buffer_list_.empty ())
    {
      size_t bytes_to_send = buffer_list_.front ()->size () - offset_;
      ssize_t bytes_sent = this->peer ().send (buffer_list_.front ()->data () + offset_, bytes_to_send);

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) Server::handle_output; handle = %d")
                  ACE_TEXT (" bytes sent %d\n"),
                  handle, bytes_sent));

      if (bytes_sent == -1)
        {
          if (errno == EWOULDBLOCK)
            return 0;
          else if (errno == EPIPE || errno == ECONNRESET)
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%t) Client::handle_output; server ")
                          ACE_TEXT ("closed handle %d\n"),
                          this->peer ().get_handle ()));
              return -1;
            }
          else
            ACE_ERROR_RETURN ((LM_ERROR,
                               ACE_TEXT ("(%t) %p\n"),
                               ACE_TEXT ("Client::handle_output")),
                              -1);
        }
      else if (bytes_sent == 0)
        return -1;
      else
        {
          if (bytes_sent == static_cast<ssize_t> (bytes_to_send))
            {
              delete buffer_list_.front ();
              buffer_list_.pop ();
              offset_ = 0;
            }
          else
            {
              offset_ += bytes_sent;
            }
        }
    }

  return -1;
}

int
Server::handle_close (ACE_HANDLE handle,
                      ACE_Reactor_Mask mask)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) Server::handle_close handle = %d mask = %xd\n"), handle, mask));
  return 0;
  //return SVC_HANDLER::handle_close (handle, mask);
}

// ----------------------------------------------------

using ACCEPTOR = ACE_Acceptor<Server, ACE_SOCK_Acceptor>;
using CONNECTOR = ACE_Connector<Client, ACE_SOCK_Connector>;

// ----------------------------------------------------

class TestAcceptor : public ACCEPTOR
{
public:
  int accept_svc_handler (Server * handler) override
  {
    int result = this->ACCEPTOR::accept_svc_handler (handler);

    if (result != 0)
      {
        if (errno != EWOULDBLOCK)
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%t) %p\n"),
                      ACE_TEXT ("Unable to accept connection")));

        return result;
      }

    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%t) Accepted connection.  ")
                ACE_TEXT ("Stream handle: <%d>\n"),
                handler->get_handle ()));

    return result;
  }
};

// ----------------------------------------------------

class TestConnector : public CONNECTOR
{
public:
  int connect_svc_handler (
    CONNECTOR::handler_type *& handler,
    const CONNECTOR::addr_type &remote_addr,
    ACE_Time_Value *timeout,
    const CONNECTOR::addr_type &local_addr,
    int reuse_addr,
    int flags,
    int perms) override
  {
    const int result = this->CONNECTOR::connect_svc_handler (handler,
                                                             remote_addr,
                                                             timeout,
                                                             local_addr,
                                                             reuse_addr,
                                                             flags,
                                                             perms);

    if (result != 0)
      return result;

    ACE_TCHAR hostname[MAXHOSTNAMELEN];
    if (remote_addr.get_host_name (hostname,
                                   sizeof (hostname)) != 0)
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%t) %p\n"),
                           ACE_TEXT ("Unable to retrieve hostname")),
                          -1);
      }

    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%t) Connected to <%s:%d>.\n"),
                hostname,
                (int) remote_addr.get_port_number ()));

    return result;
  }

  int connect_svc_handler (
    CONNECTOR::handler_type *& handler,
    CONNECTOR::handler_type *& sh_copy,
    const CONNECTOR::addr_type &remote_addr,
    ACE_Time_Value *timeout,
    const CONNECTOR::addr_type &local_addr,
    int reuse_addr,
    int flags,
    int perms) override {
    sh_copy = handler;
    return this->connect_svc_handler (handler, remote_addr, timeout,
                                      local_addr, reuse_addr, flags,
                                      perms);
  }
};

// ----------------------------------------------------

static int
disable_signal (int sigmin, int sigmax)
{
#if !defined (ACE_LACKS_UNIX_SIGNALS)
  sigset_t signal_set;
  if (ACE_OS::sigemptyset (&signal_set) == - 1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("Error: (%P|%t):%p\n"),
                ACE_TEXT ("sigemptyset failed")));

  for (int i = sigmin; i <= sigmax; i++)
    ACE_OS::sigaddset (&signal_set, i);

  // Put the <signal_set>.
# if defined (ACE_LACKS_PTHREAD_THR_SIGSETMASK)
  // In multi-threaded application this is not POSIX compliant
  // but let's leave it just in case.
  if (ACE_OS::sigprocmask (SIG_BLOCK, &signal_set, 0) != 0)
# else
  if (ACE_OS::thr_sigsetmask (SIG_BLOCK, &signal_set, 0) != 0)
# endif /* ACE_LACKS_PTHREAD_THR_SIGSETMASK */
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: (%P|%t): %p\n"),
                       ACE_TEXT ("SIG_BLOCK failed")),
                      -1);
#else
  ACE_UNUSED_ARG (sigmin);
  ACE_UNUSED_ARG (sigmax);
#endif /* ACE_LACKS_UNIX_SIGNALS */

  return 0;
}

// ----------------------------------------------------

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Dev_Poll_Reactor_Echo_Test"));

  // Make sure we ignore SIGPIPE
  disable_signal (SIGPIPE, SIGPIPE);

  ACE_Dev_Poll_Reactor dp_reactor;
  dp_reactor.restart (1);          // Restart on EINTR
  ACE_Reactor reactor (&dp_reactor);

  TestConnector client;

  int flags = 0;
  ACE_SET_BITS (flags, ACE_NONBLOCK);  // Enable non-blocking in the
                                       // Svc_Handlers.

  if (client.open (&reactor, flags) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%t) %p\n"),
                       ACE_TEXT ("Unable to open client service handler")),
                      -1);

  unsigned short port = 54678;

  ACE_INET_Addr addr;

  if (addr.set (port, INADDR_LOOPBACK) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%t) %p\n"),
                       ACE_TEXT ("server_worker - ACE_INET_Addr::set")),
                      -1);

  TestAcceptor server;

  if (server.open (addr, &reactor, flags) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%t) %p\n"),
                       ACE_TEXT ("Unable to open server service handler")),
                      -1);

  Client *client_handler = 0;

  if (client.connect (client_handler, addr) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%t) %p\n"),
                       ACE_TEXT ("Unable to connect to server")),
                      -1);

  if (reactor.run_reactor_event_loop () != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%t) %p\n"),
                       ACE_TEXT ("Error when running client ")
                       ACE_TEXT ("reactor event loop")),
                      -1);

  ACE_DEBUG((LM_DEBUG, "sent: %C\n", client_handler->sent.c_str ()));
  ACE_DEBUG((LM_DEBUG, "received: %C\n", client_handler->received.c_str ()));

  ACE_TEST_ASSERT (client_handler->sent == client_handler->received);

  ACE_END_TEST;

  return 0;
}

#else

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Dev_Poll_Reactor_Echo_Test"));
  ACE_ERROR ((LM_INFO,
              ACE_TEXT ("Dev Poll and Event Poll are not supported ")
              ACE_TEXT ("on this platform\n")));
  ACE_END_TEST;
  return 0;
}

#endif  /* ACE_HAS_DEV_POLL || ACE_HAS_EVENT_POLL */