summaryrefslogtreecommitdiff
path: root/ACE/tests/Reactor_Registration_Test.cpp
blob: 2e3aaf08cb9ee061050afab36584f478610affca (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Reactor_Registration_Test.cpp
//
// = DESCRIPTION
//    This is a test of registering handlers with the Reactor.
//
// = AUTHOR
//    Irfan Pyarali <irfan@oomworks.com>
//
// ============================================================================

#include "tests/test_config.h"
#include "ace/Pipe.h"
#include "ace/Reactor.h"
#include "ace/Select_Reactor.h"
#include "ace/TP_Reactor.h"
#include "ace/WFMO_Reactor.h"
#include "ace/ACE.h"

ACE_RCSID(tests, Reactor_Registration_Test, "$Id$")

static const char message[] = "abcdefghijklmnopqrstuvwxyz";
static const size_t message_size = 26;
static int iteration = 1;

class Event_Handler : public ACE_Event_Handler
{
public:

  Event_Handler (ACE_Reactor &reactor,
                 ACE_HANDLE read,
                 ACE_HANDLE write);

  ~Event_Handler (void);

  int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);

  int handle_close (ACE_HANDLE handle,
                    ACE_Reactor_Mask close_mask);

  ACE_Pipe pipe_;
  bool ok_;
};

Event_Handler::Event_Handler (ACE_Reactor &reactor,
                              ACE_HANDLE read,
                              ACE_HANDLE write)
  : ACE_Event_Handler (&reactor),
    pipe_ (read, write),
    ok_ (false)
{
  if (read == ACE_INVALID_HANDLE)
    {
      if (0 != this->pipe_.open ())
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("Event_Handler pipe")));
          return;
        }
    }

  if (0 != this->reactor ()->register_handler (this->pipe_.read_handle (),
                                               this,
                                               ACE_Event_Handler::READ_MASK))
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%p\n"),
                  ACE_TEXT ("Event_Handler register_handler")));
      return;
    }

  ssize_t result = ACE::send_n (this->pipe_.write_handle (),
                                message,
                                message_size);
  if (result != ssize_t (message_size))
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Event_Handler sent %b bytes; should be %B\n"),
                  result, message_size));
      if (result <= 0)
        return;
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Event_Handler::Event_Handler for %@\n"),
              this));
  this->ok_ = true;
}

Event_Handler::~Event_Handler (void)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Event_Handler::~Event_Handler for %@\n"),
              this));
}

int
Event_Handler::handle_input (ACE_HANDLE handle)
{
  char buf[message_size + 1];

  ssize_t result =
    ACE::recv_n (handle,
                 buf,
                 sizeof buf - 1);
  if (result != static_cast<ssize_t> (message_size))
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
                result, message_size));
  buf[result > 0 ? result : 0] = '\0';

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Message %C received for %@\n"),
              buf,
              this));

  return -1;
}

int
Event_Handler::handle_close (ACE_HANDLE,
                             ACE_Reactor_Mask)
{
  switch (iteration)
    {
    case 1:
      new Event_Handler (*this->reactor (),
                         ACE_INVALID_HANDLE,
                         ACE_INVALID_HANDLE);
      break;
    case 2:
      new Event_Handler (*this->reactor (),
                         this->pipe_.read_handle (),
                         this->pipe_.write_handle ());
      break;
    case 3:
      this->reactor ()->end_reactor_event_loop ();
      break;
    }

  iteration++;
  delete this;

  return 0;
}

void
test (ACE_Reactor_Impl &reactor_impl,
      const char *reactor_type)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("\nTesting with %C\n\n"),
              reactor_type));

  ACE_Reactor reactor (&reactor_impl,
                       0);

  Event_Handler *e = new Event_Handler (reactor,
                                        ACE_INVALID_HANDLE,
                                        ACE_INVALID_HANDLE);
  if (!e->ok_)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Error initializing test; aborting.\n")));
      delete e;
      return;
    }

  reactor.run_reactor_event_loop ();
}

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

  iteration = 1;
  ACE_Select_Reactor select_reactor;
  test (select_reactor, "ACE_Select_Reactor");

  iteration = 1;
  ACE_TP_Reactor tp_reactor;
  test (tp_reactor, "ACE_TP_Reactor");

  // The ACE_WFMO_Reactor stuff needs Winsock2
#if defined (ACE_WIN32) && \
    (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
  iteration = 1;
  ACE_WFMO_Reactor wfmo_reactor;
  test (wfmo_reactor, "ACE_WFMO_Reactor");
#endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */

  ACE_END_TEST;

  return 0;
}