ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Max_Default_Port_Test.cpp
blob2e555005b3a340b373720706ba83e22d6eacbc34
1 //=============================================================================
2 /**
3 * @file Max_Default_Port_Test.cpp
5 * This is a test for ACE_MAX_DEFAULT_PORT value. The test tests the
6 * highest value of the port number at which an event handler can be
7 * registered and a Connector can be connected to.
9 * Some weird behaviour has been reported on Windows NT (sp 3) when
10 * the port number exceeds 65279 resulting ACE_MAX_DEFAULT_PORT to set
11 * to zero on that platform.
13 * In this test, the event handler is started at the port value
14 * USHRT_MAX and decremented for 'ports_to_test' port values and tested
15 * if the highest port number used agrees with ACE_MAX_DEFAULT_PORT value.
17 * @author Chanaka Liyanaarachchi <chanaka@ociweb.com>
19 //=============================================================================
22 #include "test_config.h"
23 #include "ace/Reactor.h"
24 #include "ace/SOCK_Connector.h"
25 #include "ace/Thread_Manager.h"
26 #include "ace/OS_NS_unistd.h"
27 #include "ace/Time_Value.h"
29 #include "Max_Default_Port_Test.h"
31 // implement a retry and recuperation mechanism for VxWorks because test will otherwise fail
32 // on a non-optimized kernel with several ETIME errors on the client connects.
33 #if defined (ACE_VXWORKS)
34 int retry_port_ = 0;
35 #endif
37 My_Accept_Handler::My_Accept_Handler (ACE_INET_Addr &addr)
38 : addr_ (addr)
40 if (addr.get_port_number() != 0)
41 this->open (addr);
45 My_Accept_Handler::~My_Accept_Handler ()
47 this->peer_acceptor_.close (); // Prevent handle leaks
51 int
52 My_Accept_Handler::open (ACE_INET_Addr &addr)
55 if (this->peer_acceptor_.open (addr, 1) == -1)
57 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
58 ACE_TEXT ("My_Accept_Handler open")));
59 ACE_OS::exit (1);
62 return 0;
66 ACE_HANDLE
67 My_Accept_Handler::get_handle () const
69 return this->peer_acceptor_.get_handle ();
72 int
73 My_Accept_Handler::handle_input (ACE_HANDLE)
76 if (this->peer_acceptor_.accept(this->stream_, 0) == -1) {
77 ACE_ERROR((LM_ERROR, ACE_TEXT ("%p\n"),
78 ACE_TEXT ("peer_acceptor.accept")));
79 ACE_OS::exit(1);
82 ACE_DEBUG ((LM_DEBUG,
83 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
85 // Close the opened stream, else it'll leak a handle. Don't close
86 // the acceptor here, though, because get_handle() needs it to
87 // correctly allow removal from the reactor later. It gets closed
88 // in the destructor.
89 this->stream_.close ();
91 return 0;
94 u_short
95 My_Accept_Handler::port ()
97 return this->addr_.get_port_number();
100 long max_connected_port = 0;
102 void *
103 client (void *arg)
105 ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);
107 ACE_INET_Addr server_addr (remote_addr->get_port_number (),
108 ACE_LOCALHOST,
109 AF_INET);
111 ACE_SOCK_Stream cli_stream;
112 ACE_SOCK_Connector con;
113 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
115 ACE_DEBUG ((LM_DEBUG,
116 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
117 server_addr.get_port_number()));
119 // Initiate connection with server; don't wait forever
120 if (con.connect (cli_stream,
121 server_addr,
122 &timeout) == -1)
124 #if defined (ACE_VXWORKS)
125 if (errno == ETIME)
127 if ( ++retry_port_<6 )
129 ACE_DEBUG ((LM_DEBUG,
130 ACE_TEXT ("(%P|%t) Going to retry port %d\n"),
131 server_addr.get_port_number()));
134 if ( retry_port_>5 )
136 retry_port_ = 0;
137 #endif
138 ACE_ERROR ((LM_ERROR,
139 ACE_TEXT ("(%P|%t) %p\n"),
140 ACE_TEXT ("connection failed")));
142 #if defined (ACE_VXWORKS)
144 #endif
145 return 0;
148 #if defined (ACE_VXWORKS)
149 retry_port_ = 0;
150 #endif
152 // if connect succesful, what is the max port number we connected
153 // up to now.
154 int connected_port = server_addr.get_port_number ();
156 if (connected_port > max_connected_port)
157 max_connected_port = connected_port;
159 cli_stream.close ();
161 return 0;
165 run_main (int argc, ACE_TCHAR *argv[])
168 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test"));
170 ACE_UNUSED_ARG (argc);
171 ACE_UNUSED_ARG (argv);
173 #ifndef ACE_LACKS_ACCEPT
175 u_short max_listened_port = 0;
177 #if defined (__Lynx__)
178 // LynxOS can handle only 80 test iterations.
179 // This needs to be investigated further -- olli 12.11.2007
180 const u_short ports_to_test = 80;
181 #else
182 const u_short ports_to_test = 300;
183 #endif
185 //Ports beyond 65279 were said to bad on NT sp 3.
186 for (u_short idx = USHRT_MAX; idx != USHRT_MAX - ports_to_test; --idx)
188 #if defined (ACE_VXWORKS)
189 if (retry_port_>0)
191 ++idx;
192 ACE_OS::sleep (ACE_Time_Value (2*ACE_DEFAULT_TIMEOUT));
194 #endif
196 ACE_INET_Addr addr (idx);
198 My_Accept_Handler *eh = new My_Accept_Handler (addr);
201 if ( ACE_Reactor::instance()->register_handler (
203 ACE_Event_Handler::ACCEPT_MASK) == -1)
205 ACE_ERROR_RETURN ((LM_ERROR,
206 ACE_TEXT ("%p\n"),
207 ACE_TEXT ("Failed to register event handler")),
211 ACE_DEBUG ((LM_DEBUG, "Registered event handler at %d\n", idx));
213 ACE_Time_Value tv (1);
215 #if defined (ACE_HAS_THREADS)
217 if (ACE_Thread_Manager::instance ()->spawn_n
219 ACE_THR_FUNC (client),
220 reinterpret_cast <void *> (&addr),
221 THR_NEW_LWP | THR_DETACHED) == -1)
223 ACE_ERROR_RETURN ((LM_ERROR,
224 ACE_TEXT ("(%P|%t) %p\n"),
225 ACE_TEXT ("thread create failed")),
228 ACE_Thread_Manager::instance ()->wait ();
230 #else
231 ACE_UNUSED_ARG (client);
232 ACE_ERROR ((LM_ERROR,
233 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
234 1));
235 #endif //ACE_HAS_THREADS
237 if (ACE_Reactor::instance()->handle_events (tv) == -1 )
239 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
240 ACE_TEXT ("Reactor::handle_events")),
244 // see if I can register a reactor at this port.
245 if (eh->port () == idx)
247 if (idx > max_listened_port)
248 max_listened_port = idx;
250 else
252 ACE_ERROR_RETURN ((LM_ERROR,
253 ACE_TEXT ("Test Fail, listening port %d\n"),
254 eh->port()),
258 ACE_Reactor::instance()->remove_handler (
260 ACE_Event_Handler::ACCEPT_MASK |
261 ACE_Event_Handler::DONT_CALL);
262 delete eh;
265 ACE_DEBUG ((LM_DEBUG,
266 "Value of ACE_MAX_DEFAULT_PORT %d\n",
267 ACE_MAX_DEFAULT_PORT));
269 ACE_DEBUG ((LM_DEBUG,
270 "Highest port value I can listen at %d\n",
271 max_listened_port));
273 ACE_DEBUG ((LM_DEBUG,
274 "Highest port value I can connect to %d\n",
275 max_connected_port));
277 if ((max_listened_port == ACE_MAX_DEFAULT_PORT) &&
278 (max_connected_port == ACE_MAX_DEFAULT_PORT))
280 ACE_DEBUG ((LM_DEBUG,
281 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
282 max_listened_port));
284 else
286 ACE_ERROR ((LM_ERROR,
287 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
288 ACE_TEXT ("or %d port may be busy; got to %d\n"),
289 ACE_MAX_DEFAULT_PORT, max_listened_port));
293 #endif // ACE_LACKS_ACCEPT
294 ACE_END_TEST;
295 return 0;