// $Id$ // ============================================================================ // // = LIBRARY // tests // // = FILENAME // Conn_Test.cpp // // = DESCRIPTION // This is a test of the and // classes. The test forks processes or spawns threads (depending // upon the platform) and then executes client and server allowing // them to connect and exchange data. The test also illustrates // how the works by showing how you can // cache connections on the client. // // = AUTHOR // Doug Schmidt, Chris Cleeland, and Irfan Pyarali // // ============================================================================ #include "test_config.h" #include "ace/SOCK_Connector.h" #include "ace/LOCK_SOCK_Acceptor.h" #include "ace/Acceptor.h" #include "ace/Handle_Set.h" #include "ace/Connector.h" #include "ace/Strategies.h" #include "ace/Auto_Ptr.h" #include "ace/Get_Opt.h" #include "Conn_Test.h" ACE_RCSID(tests, Conn_Test, "$Id$") #if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 USELIB("..\ace\aced.lib"); //--------------------------------------------------------------------------- #endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */ // Default number of clients/servers. #if defined (ACE_HAS_PHARLAP) // PharLap is, by default, resource contrained. Test for something that works // on the default configuration. static int n_servers = 2; static int n_clients = 4; #else static int n_servers = 4; static int n_clients = 10; #endif /* ACE_HAS_PHARLAP */ static int n_client_iterations = 2; Svc_Handler::Svc_Handler (ACE_Thread_Manager *) { } int Svc_Handler::open (void *) { ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%P|%t) opening Svc_Handler %d with handle %d\n"), this, this->peer ().get_handle ())); // Enable non-blocking I/O. if (this->peer ().enable (ACE_NONBLOCK) == -1) ACE_ERROR_RETURN ((LM_ERROR, ASYS_TEXT ("(%P|%t) %p\n"), ASYS_TEXT ("enable")), -1); return 0; } int Svc_Handler::recycle (void *) { ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%P|%t) recycling Svc_Handler %d with handle %d\n"), this, this->peer ().get_handle ())); return 0; } void Svc_Handler::send_data (void) { // Send data to server. for (char *c = ACE_ALPHABET; *c != '\0'; c++) if (this->peer ().send_n (c, 1) == -1) ACE_ERROR ((LM_ERROR, ASYS_TEXT ("(%P|%t) %p\n"), ASYS_TEXT ("send_n"))); } void Svc_Handler::recv_data (void) { ACE_SOCK_Stream &new_stream = this->peer (); ACE_Handle_Set handle_set; handle_set.set_bit (new_stream.get_handle ()); char *t = ACE_ALPHABET; // Read data from client (terminate on error). for (ssize_t r_bytes; ;) { // Since we're in non-blocking mode we need to use