Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Reactor_Registration_Test.cpp
blob3f11928bfe0df8c63d2e4c52631981a69aa4848b
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:
28 Event_Handler (ACE_Reactor &reactor,
29 ACE_HANDLE read,
30 ACE_HANDLE write);
32 ~Event_Handler (void);
34 int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
36 int handle_close (ACE_HANDLE handle,
37 ACE_Reactor_Mask close_mask);
39 ACE_Pipe pipe_;
40 bool ok_;
43 Event_Handler::Event_Handler (ACE_Reactor &reactor,
44 ACE_HANDLE read,
45 ACE_HANDLE write)
46 : ACE_Event_Handler (&reactor),
47 pipe_ (read, write),
48 ok_ (false)
50 if (read == ACE_INVALID_HANDLE)
52 if (0 != this->pipe_.open ())
54 ACE_ERROR ((LM_ERROR,
55 ACE_TEXT ("%p\n"),
56 ACE_TEXT ("Event_Handler pipe")));
57 return;
61 if (0 != this->reactor ()->register_handler (this->pipe_.read_handle (),
62 this,
63 ACE_Event_Handler::READ_MASK))
65 ACE_ERROR ((LM_ERROR,
66 ACE_TEXT ("%p\n"),
67 ACE_TEXT ("Event_Handler register_handler")));
68 return;
71 ssize_t result = ACE::send_n (this->pipe_.write_handle (),
72 message,
73 message_size);
74 if (result != ssize_t (message_size))
76 ACE_ERROR ((LM_ERROR,
77 ACE_TEXT ("Event_Handler sent %b bytes; should be %B\n"),
78 result, message_size));
79 if (result <= 0)
80 return;
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT ("Event_Handler::Event_Handler for %@\n"),
85 this));
86 this->ok_ = true;
89 Event_Handler::~Event_Handler (void)
91 ACE_DEBUG ((LM_DEBUG,
92 ACE_TEXT ("Event_Handler::~Event_Handler for %@\n"),
93 this));
96 int
97 Event_Handler::handle_input (ACE_HANDLE handle)
99 char buf[message_size + 1];
101 ssize_t result =
102 ACE::recv_n (handle,
103 buf,
104 sizeof buf - 1);
105 if (result != static_cast<ssize_t> (message_size))
106 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Handler recv'd %b bytes; expected %B\n"),
107 result, message_size));
108 buf[result > 0 ? result : 0] = '\0';
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("Message %C received for %@\n"),
112 buf,
113 this));
115 return -1;
119 Event_Handler::handle_close (ACE_HANDLE,
120 ACE_Reactor_Mask)
122 switch (iteration)
124 case 1:
125 new Event_Handler (*this->reactor (),
126 ACE_INVALID_HANDLE,
127 ACE_INVALID_HANDLE);
128 break;
129 case 2:
130 new Event_Handler (*this->reactor (),
131 this->pipe_.read_handle (),
132 this->pipe_.write_handle ());
133 break;
134 case 3:
135 this->reactor ()->end_reactor_event_loop ();
136 break;
139 iteration++;
140 delete this;
142 return 0;
145 void
146 test (ACE_Reactor_Impl &reactor_impl,
147 const char *reactor_type)
149 ACE_DEBUG ((LM_DEBUG,
150 ACE_TEXT ("\nTesting with %C\n\n"),
151 reactor_type));
153 ACE_Reactor reactor (&reactor_impl,
156 Event_Handler *e = new Event_Handler (reactor,
157 ACE_INVALID_HANDLE,
158 ACE_INVALID_HANDLE);
159 if (!e->ok_)
161 ACE_ERROR ((LM_ERROR,
162 ACE_TEXT ("Error initializing test; aborting.\n")));
163 delete e;
164 return;
167 reactor.run_reactor_event_loop ();
171 run_main (int, ACE_TCHAR *[])
173 ACE_START_TEST (ACE_TEXT ("Reactor_Registration_Test"));
175 iteration = 1;
176 ACE_Select_Reactor select_reactor;
177 test (select_reactor, "ACE_Select_Reactor");
179 iteration = 1;
180 ACE_TP_Reactor tp_reactor;
181 test (tp_reactor, "ACE_TP_Reactor");
183 // The ACE_WFMO_Reactor stuff needs Winsock2
184 #if defined (ACE_WIN32) && \
185 (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
186 iteration = 1;
187 ACE_WFMO_Reactor wfmo_reactor;
188 test (wfmo_reactor, "ACE_WFMO_Reactor");
189 #endif /* ACE_WIN32 && ACE_HAS_WINSOCK2 */
191 ACE_END_TEST;
193 return 0;