summaryrefslogtreecommitdiff
path: root/tests/Reactor_Registration_Test.cpp
blob: a122dedbaabeb2446d2e04223fa39e5fe3a1cf3b (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
// $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/Select_Reactor.h"
#include "ace/TP_Reactor.h"
#include "ace/WFMO_Reactor.h"

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

static const char message[] = "abcdefghijklmnopqrstuvwxyz";
static const int 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_;
};

Event_Handler::Event_Handler (ACE_Reactor &reactor,
                              ACE_HANDLE read,
                              ACE_HANDLE write)
  : ACE_Event_Handler (&reactor),
    pipe_ (read, write)
{
  int result = 0;

  if (read == ACE_INVALID_HANDLE)
    {
      result =
                        this->pipe_.open ();
      ACE_ASSERT (result == 0);
      ACE_UNUSED_ARG (result);
    }

  result =
    this->reactor ()->register_handler (this->pipe_.read_handle (),
                                        this,
                                        ACE_Event_Handler::READ_MASK);
  ACE_ASSERT (result == 0);
  ACE_UNUSED_ARG (result);

  result =
    ACE::send_n (this->pipe_.write_handle (),
                 message,
                 message_size);
  ACE_ASSERT (result == message_size);
  ACE_UNUSED_ARG (result);

  ACE_DEBUG ((LM_DEBUG,
              "Event_Handler::Event_Handler for %x\n",
              this));
}

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

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

  int result =
    ACE::recv_n (handle,
                 buf,
                 sizeof buf - 1);
  ACE_ASSERT (result == message_size);
  ACE_UNUSED_ARG (result);

  buf[message_size] = '\0';

  ACE_DEBUG ((LM_DEBUG,
              "Message %s received for %x\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,
              "\nTesting with %s\n\n",
              reactor_type));

  ACE_Reactor reactor (&reactor_impl,
                       0);

  new Event_Handler (reactor,
                     ACE_INVALID_HANDLE,
                     ACE_INVALID_HANDLE);

  reactor.run_reactor_event_loop ();
}

int
ACE_TMAIN (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");

#if defined (ACE_WIN32)
  iteration = 1;
  ACE_WFMO_Reactor wfmo_reactor;
  test (wfmo_reactor, "ACE_WFMO_Reactor");
#endif /* ACE_WIN32 */

  ACE_END_TEST;

  return 0;
}