2 //=============================================================================
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
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"
30 #pragma warning(disable:4503)
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.
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
,
57 int result
= ACCEPT_STRATEGY_BASE::open (local_addr
, restart
);
62 // If the error occurred due to the fact that the file descriptor
63 // limit was exhausted, then purge the connection cache of some
65 result
= this->out_of_sockets_handler ();
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
84 int reset_new_handle
= this->reactor_
->uses_event_associations ();
86 int result
= this->acceptor ().accept (svc_handler
->peer (), // stream
90 reset_new_handle
// reset new handler
96 ACE_TEXT ("Accept succeeded with handle %d\n"),
97 svc_handler
->get_handle ()));
101 // If the error occurred due to the fact that the file descriptor
102 // limit was exhausted, then purge the connection cache of some
104 if (0 != this->out_of_sockets_handler ())
105 ACE_ERROR ((LM_ERROR
,
107 ACE_TEXT ("out_of_sockets_handler")));
109 // Close down handler to avoid memory leaks.
110 svc_handler
->close (0);
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 ();
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 *)
141 ACE_DEBUG ((LM_DEBUG
,
142 ACE_TEXT ("opening Client_Svc_Handler %@ with handle %d\n"),
144 this->peer ().get_handle ()));
149 Client_Svc_Handler::close (u_long flags
)
151 ACE_DEBUG ((LM_DEBUG
,
152 ACE_TEXT ("Closing Client_Svc_Handler %@ with handle %d\n"),
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 *)
168 ACE_DEBUG ((LM_DEBUG
,
169 ACE_TEXT ("opening Server_Svc_Handler %@ with handle %d\n"),
171 this->peer ().get_handle ()));
173 return this->close ();
176 enum Caching_Strategy_Type
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;
197 static int iterations
= 200;
198 #endif /* ACE_WIN32 */
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 (),
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
,
215 ACE_ERROR_RETURN ((LM_ERROR
,
217 ACE_TEXT ("connection failed")),
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
);
236 while (connection_accepted
== 0)
237 result
= ACE_Reactor::instance ()->handle_events ();
239 connection_accepted
= 0;
241 ACE_UNUSED_ARG (result
);
245 test_connection_management (CACHING_STRATEGY
&caching_strategy
)
247 // Configure the Strategy Connector with a strategy that caches
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,
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
;
267 listen_one_time_acceptor
.open (ACE_sap_any_cast (const ACE_INET_Addr
&),
268 ACE_Reactor::instance (),
270 &listen_one_time_accept_strategy
);
273 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor open")));
277 result
= listen_one_time_acceptor
.acceptor ().get_local_addr (server_addr
);
280 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor getaddr")));
281 listen_one_time_acceptor
.close ();
285 for (int i
= 1; i
<= iterations
; ++i
)
287 ACE_DEBUG ((LM_DEBUG
,
288 ACE_TEXT ("%T iteration %d\n"),
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
296 ACCEPTOR listen_multiple_times_acceptor
;
300 // Bind acceptor to any port and then find out what the port
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
,
313 if (listen_multiple_times_acceptor
.acceptor ().get_local_addr (server_addr
) == -1)
315 ACE_ERROR ((LM_ERROR
,
317 ACE_TEXT ("get_local_addr")));
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")));
337 test_caching_strategy_type ()
339 CACHING_STRATEGY
*caching_strategy
= 0;
341 switch (caching_strategy_type
)
344 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nNull_Caching_Strategy\n\n")));
345 ACE_NEW (caching_strategy
,
346 NULL_CACHING_STRATEGY_ADAPTER
);
350 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nLRU_Caching_Strategy\n\n")));
351 ACE_NEW (caching_strategy
,
352 LRU_CACHING_STRATEGY_ADAPTER
);
356 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nLFU_Caching_Strategy\n\n")));
357 ACE_NEW (caching_strategy
,
358 LFU_CACHING_STRATEGY_ADAPTER
);
362 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nFIFO_Caching_Strategy\n\n")));
363 ACE_NEW (caching_strategy
,
364 FIFO_CACHING_STRATEGY_ADAPTER
);
367 case ACE_ALL
: // Just to remove warnings!
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"));
383 while ((cc
= get_opt ()) != -1)
390 listen_once
= ACE_OS::atoi (get_opt
.opt_arg ());
393 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
394 user_has_specified_iterations
= 1;
397 purge_percentage
= ACE_OS::atoi (get_opt
.opt_arg ());
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
;
413 keep_handles_available
= ACE_OS::atoi (get_opt
.opt_arg ());
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")));
435 run_main (int argc
, ACE_TCHAR
*argv
[])
438 int result
= parse_args (argc
, argv
);
442 #if defined (ACE_WIN32)
443 // Somehow, on Win32, the <listen once> option allows us to create
445 if (!user_has_specified_iterations
&&
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
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
)
478 caching_strategy_type
= ACE_LFU
;
479 test_caching_strategy_type ();
481 caching_strategy_type
= ACE_FIFO
;
482 test_caching_strategy_type ();
486 test_caching_strategy_type ();
489 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE_LITE
);
496 #endif /* CACHED_ACCEPT_CONNECTION_TEST */