ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Cached_Accept_Conn_Test.cpp
blob960816e3402ff9155d8ddbfd017bfe4630c14391
2 //=============================================================================
3 /**
4 * @file Cached_Accept_Conn_Test.cpp
6 * The test illustrates how the <ACE_Strategy_Connector> works by
7 * showing how you can cache connections on the client using
8 * different caching strategies. Also how connections can be purged
9 * explicitly and implicitly if needed from the connection cache
10 * maintained by the connector. The <ACE_Strategy_Acceptor> can also
11 * explicitly purge connections from the process CONNECTION CACHE on
12 * demand.
14 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
16 //=============================================================================
19 #ifndef CACHED_ACCEPT_CONNECTION_TEST
20 #define CACHED_ACCEPT_CONNECTION_TEST
22 #include "test_config.h"
24 #include "Cached_Accept_Conn_Test.h"
26 #include "ace/OS_NS_string.h"
27 #include "ace/Get_Opt.h"
29 #if defined(_MSC_VER)
30 #pragma warning(disable:4503)
31 #endif /* _MSC_VER */
33 // Note: To keep both sunCC5.0 without debugging symbols and gcc2.7.3
34 // happy, it was necessary to have the definitions of the methods of
35 // the Accept_Strategy before the instantiations.
37 // HPUX doesn't accept these declaration after their usage.
39 // For some strange reason this must *not* be static since otherwise
40 // certain versions of SunC++ will not link properly.
41 int connection_accepted = 0;
43 // For some strange reason this must *not* be static since otherwise
44 // certain versions of SunC++ will not link properly.
45 int debug = 0;
47 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1>
48 Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2>::Accept_Strategy (CACHED_CONNECT_STRATEGY &caching_connect_strategy)
49 : caching_connect_strategy_ (caching_connect_strategy)
53 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> int
54 Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2>::open (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
55 bool restart)
57 int result = ACCEPT_STRATEGY_BASE::open (local_addr, restart);
59 if (result == 0)
60 return result;
62 // If the error occurred due to the fact that the file descriptor
63 // limit was exhausted, then purge the connection cache of some
64 // entries.
65 result = this->out_of_sockets_handler ();
66 if (result == -1)
67 return -1;
69 // If we are able to purge, try again.
70 return ACCEPT_STRATEGY_BASE::open (local_addr, restart);
73 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> int
74 Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2>::accept_svc_handler (SVC_HANDLER *svc_handler)
76 // Stop the event loop.
77 connection_accepted = 1;
79 // Try to find out if the implementation of the reactor that we are
80 // using requires us to reset the event association for the newly
81 // created handle. This is because the newly created handle will
82 // inherit the properties of the listen handle, including its event
83 // associations.
84 int reset_new_handle = this->reactor_->uses_event_associations ();
86 int result = this->acceptor ().accept (svc_handler->peer (), // stream
87 0, // remote address
88 0, // timeout
89 1, // restart
90 reset_new_handle // reset new handler
92 if (result == 0)
94 if (debug)
95 ACE_DEBUG ((LM_DEBUG,
96 ACE_TEXT ("Accept succeeded with handle %d\n"),
97 svc_handler->get_handle ()));
98 return result;
101 // If the error occurred due to the fact that the file descriptor
102 // limit was exhausted, then purge the connection cache of some
103 // entries.
104 if (0 != this->out_of_sockets_handler ())
105 ACE_ERROR ((LM_ERROR,
106 ACE_TEXT ("%p\n"),
107 ACE_TEXT ("out_of_sockets_handler")));
109 // Close down handler to avoid memory leaks.
110 svc_handler->close (0);
111 return -1;
114 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1> int
115 Accept_Strategy<SVC_HANDLER, ACE_PEER_ACCEPTOR_2>::out_of_sockets_handler ()
117 if (ACE::out_of_handles (errno))
119 // Close connections which are cached by explicitly purging the
120 // connection cache maintained by the connector.
121 ACE_DEBUG ((LM_DEBUG,
122 ACE_TEXT ("Purging connections from Connection Cache...\n")));
124 return this->caching_connect_strategy_.purge_connections ();
127 return -1;
130 using ACCEPT_STRATEGY = Accept_Strategy<Server_Svc_Handler, ACE_SOCK_Acceptor>;
132 Client_Svc_Handler::Client_Svc_Handler (ACE_Thread_Manager *t)
133 : ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> (t)
138 Client_Svc_Handler::open (void *)
140 if (debug)
141 ACE_DEBUG ((LM_DEBUG,
142 ACE_TEXT ("opening Client_Svc_Handler %@ with handle %d\n"),
143 this,
144 this->peer ().get_handle ()));
145 return 0;
149 Client_Svc_Handler::close (u_long flags)
151 ACE_DEBUG ((LM_DEBUG,
152 ACE_TEXT ("Closing Client_Svc_Handler %@ with handle %d\n"),
153 this,
154 this->peer ().get_handle ()));
155 return ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>::close (flags);
159 Server_Svc_Handler::Server_Svc_Handler (ACE_Thread_Manager *t)
160 : ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> (t)
165 Server_Svc_Handler::open (void *)
167 if (debug)
168 ACE_DEBUG ((LM_DEBUG,
169 ACE_TEXT ("opening Server_Svc_Handler %@ with handle %d\n"),
170 this,
171 this->peer ().get_handle ()));
173 return this->close ();
176 enum Caching_Strategy_Type
178 ACE_LFU,
179 ACE_FIFO,
180 ACE_LRU,
181 ACE_NULL,
182 ACE_ALL
185 // Default number of clients/servers.
186 static int listen_once = 1;
187 static int user_has_specified_iterations = 0;
188 static size_t keep_handles_available = 100;
189 static double purge_percentage = 20;
190 static Caching_Strategy_Type caching_strategy_type = ACE_ALL;
192 // On Win32, the handle gobbling doesn't work. Therefore, we need
193 // more iterations to get to the handle limit.
194 #if defined (ACE_WIN32)
195 static int iterations = 2000;
196 #else
197 static int iterations = 200;
198 #endif /* ACE_WIN32 */
201 static int
202 cached_connect (STRATEGY_CONNECTOR &con,
203 const ACE_INET_Addr &server_addr)
205 // This will make sure we get the host information correct.
206 ACE_INET_Addr remote_addr (server_addr.get_port_number (),
207 ACE_LOCALHOST);
209 // Perform a blocking connect to the server using the Strategy
210 // Connector with a connection caching strategy.
211 Client_Svc_Handler *svc_handler = 0;
212 int result = con.connect (svc_handler,
213 remote_addr);
214 if (result == -1)
215 ACE_ERROR_RETURN ((LM_ERROR,
216 ACE_TEXT ("%p\n"),
217 ACE_TEXT ("connection failed")),
218 -1);
219 else
220 if (debug)
221 ACE_DEBUG ((LM_DEBUG,
222 ACE_TEXT ("Connection successful to server at port %d!\n"),
223 remote_addr.get_port_number ()));
225 // Reset Svc_Handler state.
226 svc_handler->recycle_state (ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE);
228 return 0;
231 static void
232 server ()
234 int result = 1;
236 while (connection_accepted == 0)
237 result = ACE_Reactor::instance ()->handle_events ();
239 connection_accepted = 0;
241 ACE_UNUSED_ARG (result);
244 static void
245 test_connection_management (CACHING_STRATEGY &caching_strategy)
247 // Configure the Strategy Connector with a strategy that caches
248 // connection.
249 CACHED_CONNECT_STRATEGY caching_connect_strategy (caching_strategy);
251 NULL_CREATION_STRATEGY creation_strategy;
252 NULL_ACTIVATION_STRATEGY activation_strategy;
254 STRATEGY_CONNECTOR strategy_connector (0,
255 &creation_strategy,
256 &caching_connect_strategy,
257 &activation_strategy);
259 // Connect strategy is required by the <out_of_sockets_handler>.
260 ACCEPT_STRATEGY listen_one_time_accept_strategy (caching_connect_strategy);
262 // If <listen_once> is true, only one Acceptor is used for the test.
263 ACCEPTOR listen_one_time_acceptor;
264 ACE_INET_Addr server_addr;
266 int result =
267 listen_one_time_acceptor.open (ACE_sap_any_cast (const ACE_INET_Addr &),
268 ACE_Reactor::instance (),
270 &listen_one_time_accept_strategy);
271 if (result != 0)
273 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor open")));
274 return;
277 result = listen_one_time_acceptor.acceptor ().get_local_addr (server_addr);
278 if (result != 0)
280 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor getaddr")));
281 listen_one_time_acceptor.close ();
282 return;
285 for (int i = 1; i <= iterations; ++i)
287 ACE_DEBUG ((LM_DEBUG,
288 ACE_TEXT ("%T iteration %d\n"),
289 i));
291 // Connect strategy is required by the <out_of_sockets_handler>.
292 ACCEPT_STRATEGY listen_multiple_times_accept_strategy (caching_connect_strategy);
294 // If <listen_once> is false, one Acceptor is used for every
295 // iteration.
296 ACCEPTOR listen_multiple_times_acceptor;
298 if (!listen_once)
300 // Bind acceptor to any port and then find out what the port
301 // was.
302 if (listen_multiple_times_acceptor.open (ACE_sap_any_cast (const ACE_INET_Addr &),
303 ACE_Reactor::instance (),
305 &listen_multiple_times_accept_strategy) == -1)
307 ACE_ERROR ((LM_ERROR,
308 ACE_TEXT ("%p\n"),
309 ACE_TEXT ("open")));
310 break;
313 if (listen_multiple_times_acceptor.acceptor ().get_local_addr (server_addr) == -1)
315 ACE_ERROR ((LM_ERROR,
316 ACE_TEXT ("%p\n"),
317 ACE_TEXT ("get_local_addr")));
318 break;
323 if (debug)
324 ACE_DEBUG ((LM_DEBUG,
325 ACE_TEXT ("starting server at port %d\n"),
326 server_addr.get_port_number ()));
328 // Run the cached blocking test.
329 if (-1 == cached_connect (strategy_connector, server_addr))
330 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("cached_connect")));
331 else
332 server ();
336 void
337 test_caching_strategy_type ()
339 CACHING_STRATEGY *caching_strategy = 0;
341 switch (caching_strategy_type)
343 case ACE_NULL:
344 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nNull_Caching_Strategy\n\n")));
345 ACE_NEW (caching_strategy,
346 NULL_CACHING_STRATEGY_ADAPTER);
347 break;
349 case ACE_LRU:
350 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nLRU_Caching_Strategy\n\n")));
351 ACE_NEW (caching_strategy,
352 LRU_CACHING_STRATEGY_ADAPTER);
353 break;
355 case ACE_LFU:
356 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nLFU_Caching_Strategy\n\n")));
357 ACE_NEW (caching_strategy,
358 LFU_CACHING_STRATEGY_ADAPTER);
359 break;
361 case ACE_FIFO:
362 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nFIFO_Caching_Strategy\n\n")));
363 ACE_NEW (caching_strategy,
364 FIFO_CACHING_STRATEGY_ADAPTER);
365 break;
367 case ACE_ALL: // Just to remove warnings!
368 break;
371 caching_strategy->purge_percent (purge_percentage);
372 test_connection_management (*caching_strategy);
373 delete caching_strategy;
377 parse_args (int argc, ACE_TCHAR *argv[])
379 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("l:i:p:c:a:d"));
381 int cc;
383 while ((cc = get_opt ()) != -1)
384 switch (cc)
386 case 'd':
387 debug = 1;
388 break;
389 case 'l':
390 listen_once = ACE_OS::atoi (get_opt.opt_arg ());
391 break;
392 case 'i':
393 iterations = ACE_OS::atoi (get_opt.opt_arg ());
394 user_has_specified_iterations = 1;
395 break;
396 case 'p':
397 purge_percentage = ACE_OS::atoi (get_opt.opt_arg ());
398 break;
399 case 'c':
400 // Note that if null caching strategy is used then this test
401 // will fail if the number of servers exceed number of open
402 // files allowed for the process.
403 if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("null")) == 0)
404 caching_strategy_type = ACE_NULL;
405 if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("lru")) == 0)
406 caching_strategy_type = ACE_LRU;
407 if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("lfu")) == 0)
408 caching_strategy_type = ACE_LFU;
409 if (ACE_OS::strcmp (get_opt.opt_arg (), ACE_TEXT ("fifo")) == 0)
410 caching_strategy_type = ACE_FIFO;
411 break;
412 case 'a':
413 keep_handles_available = ACE_OS::atoi (get_opt.opt_arg ());
414 break;
415 case '?':
416 case 'h':
417 default:
418 ACE_ERROR ((LM_ERROR,
419 ACE_TEXT ("usage: %s ")
420 ACE_TEXT ("[-t (timeout)] ")
421 ACE_TEXT ("[-c (caching strategy: lru / lfu / fifo / null [default = all])] ")
422 ACE_TEXT ("[-i (iterations)] ")
423 ACE_TEXT ("[-l (listen once)] ")
424 ACE_TEXT ("[-d (addition debugging output)] ")
425 ACE_TEXT ("[-p (purge percent)] ")
426 ACE_TEXT ("[-a (keep handles available)] "),
427 ACE_TEXT ("Cached_Accept_Conn_Test")));
428 return -1;
431 return 0;
435 run_main (int argc, ACE_TCHAR *argv[])
437 // Validate options.
438 int result = parse_args (argc, argv);
439 if (result != 0)
440 return result;
442 #if defined (ACE_WIN32)
443 // Somehow, on Win32, the <listen once> option allows us to create
444 // more handles.
445 if (!user_has_specified_iterations &&
446 listen_once)
447 iterations *= 2;
448 #endif /* ACE_WIN32 */
450 // Start the test only if options are valid.
451 ACE_START_TEST (ACE_TEXT ("Cached_Accept_Conn_Test"));
453 // Remove the extra debugging attributes from Log_Msg output.
454 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::VERBOSE_LITE);
456 // The reactor's constructor changes the handle limit for the
457 // process.
458 ACE_Reactor::instance ();
460 // Consume all handles in the process, leaving us
461 // <keep_handles_available> to play with.
462 ACE_Handle_Gobbler handle_gobbler;
463 if (0 != handle_gobbler.consume_handles (keep_handles_available))
464 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("handle_gobbler")));
466 // Do we need to test all the strategies. Note, that the less
467 // useful null strategy is ignored in this case.
468 if (caching_strategy_type == ACE_ALL)
470 caching_strategy_type = ACE_LRU;
471 test_caching_strategy_type ();
473 // Default iterations are too many; if the user hasn't specified
474 // otherwise, we'll shrink the iterations for LFU and FIFO.
475 if (!user_has_specified_iterations)
476 iterations /= 100;
478 caching_strategy_type = ACE_LFU;
479 test_caching_strategy_type ();
481 caching_strategy_type = ACE_FIFO;
482 test_caching_strategy_type ();
484 else
486 test_caching_strategy_type ();
489 ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
491 ACE_END_TEST;
492 return 0;
496 #endif /* CACHED_ACCEPT_CONNECTION_TEST */