Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Reactor_Registration_Test.cpp
blobffee3485de3ba7009d2544a9b2e260ae7c29715b
2 //=============================================================================
3 /**
4 * @file Reactor_Registration_Test.cpp
6 * This is a test of registering handlers with the Reactor.
8 * @author Irfan Pyarali <irfan@oomworks.com>
9 */
10 //=============================================================================
12 #include "test_config.h"
13 #include "ace/Pipe.h"
14 #include "ace/Reactor.h"
15 #include "ace/Select_Reactor.h"
16 #include "ace/TP_Reactor.h"
17 #include "ace/WFMO_Reactor.h"
18 #include "ace/ACE.h"
20 static const char message[] = "abcdefghijklmnopqrstuvwxyz";
21 static const size_t message_size = 26;
22 static int iteration = 1;
24 class Event_Handler : public ACE_Event_Handler
26 public:
27 Event_Handler (ACE_Reactor &reactor,
28 ACE_HANDLE read,
29 ACE_HANDLE write);
31 ~Event_Handler () override;
33 int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE) override;
35 int handle_close (ACE_HANDLE handle,
36 ACE_Reactor_Mask close_mask) override;
38 ACE_Pipe pipe_;
39 bool ok_;
42 Event_Handler::Event_Handler (ACE_Reactor &reactor,
43 ACE_HANDLE read,
44 ACE_HANDLE write)
45 : ACE_Event_Handler (&reactor),
46 pipe_ (read, write),
47 ok_ (false)
49 if (read == ACE_INVALID_HANDLE)
51 if (0 != this->pipe_.open ())
53 ACE_ERROR ((LM_ERROR,
54 ACE_TEXT ("%p\n"),
55 ACE_TEXT ("Event_Handler pipe")));
56 return;
60 if (0 != this->reactor ()->register_handler (this->pipe_.read_handle (),
61 this,
62 ACE_Event_Handler::READ_MASK))
64 ACE_ERROR ((LM_ERROR,
65 ACE_TEXT ("%p\n"),
66 ACE_TEXT ("Event_Handler register_handler")));
67 return;
70 ssize_t result = ACE::send_n (this->pipe_.write_handle (),
71 message,
72 message_size);
73 if (result != ssize_t (message_size))
75 ACE_ERROR ((LM_ERROR,
76 ACE_TEXT ("Event_Handler sent %b bytes; should be %B\n"),
77 result, message_size));
78 if (result <= 0)
79 return;
82 ACE_DEBUG ((LM_DEBUG,
83 ACE_TEXT ("Event_Handler::Event_Handler for %@\n"),
84 this));
85 this->ok_ = true;
88 Event_Handler::~Event_Handler ()
90 ACE_DEBUG ((LM_DEBUG,
91 ACE_TEXT ("Event_Handler::~Event_Handler for %@\n"),
92 this));
95 int
96 Event_Handler::handle_input (ACE_HANDLE handle)
98 char buf[message_size + 1];
100 ssize_t result =
101 ACE::recv_n (handle,
102 buf,
103 sizeof buf - 1);
104 if (result != static_cast<ssize_t> (message_size))
105 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
106 result, message_size));
107 buf[result > 0 ? result : 0] = '\0';
109 ACE_DEBUG ((LM_DEBUG,
110 ACE_TEXT ("Message %C received for %@\n"),
111 buf,
112 this));
114 return -1;
118 Event_Handler::handle_close (ACE_HANDLE,
119 ACE_Reactor_Mask)
121 switch (iteration)
123 case 1:
124 new Event_Handler (*this->reactor (),
125 ACE_INVALID_HANDLE,
126 ACE_INVALID_HANDLE);
127 break;
128 case 2:
129 new Event_Handler (*this->reactor (),
130 this->pipe_.read_handle (),
131 this->pipe_.write_handle ());
132 break;
133 case 3:
134 this->reactor ()->end_reactor_event_loop ();
135 break;
138 iteration++;
139 delete this;
141 return 0;
144 void
145 test (ACE_Reactor_Impl &reactor_impl,
146 const char *reactor_type)
148 ACE_DEBUG ((LM_DEBUG,
149 ACE_TEXT ("\nTesting with %C\n\n"),
150 reactor_type));
152 ACE_Reactor reactor (&reactor_impl,
155 Event_Handler *e = new Event_Handler (reactor,
156 ACE_INVALID_HANDLE,
157 ACE_INVALID_HANDLE);
158 if (!e->ok_)
160 ACE_ERROR ((LM_ERROR,
161 ACE_TEXT ("Error initializing test; aborting.\n")));
162 delete e;
163 return;
166 reactor.run_reactor_event_loop ();
170 run_main (int, ACE_TCHAR *[])
172 ACE_START_TEST (ACE_TEXT ("Reactor_Registration_Test"));
174 iteration = 1;
175 ACE_Select_Reactor select_reactor;
176 test (select_reactor, "ACE_Select_Reactor");
178 iteration = 1;
179 ACE_TP_Reactor tp_reactor;
180 test (tp_reactor, "ACE_TP_Reactor");
182 // The ACE_WFMO_Reactor stuff needs Winsock2
183 #if defined (ACE_WIN32) && \
184 (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
185 iteration = 1;
186 ACE_WFMO_Reactor wfmo_reactor;
187 test (wfmo_reactor, "ACE_WFMO_Reactor");
188 #endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */
190 ACE_END_TEST;
192 return 0;