diff --git a/include/GVOHub.hpp b/include/GVOHub.hpp index 83a0b49..8f01492 100644 --- a/include/GVOHub.hpp +++ b/include/GVOHub.hpp @@ -142,7 +142,7 @@ protected: std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint) std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others - int m_socket; //!< Hub socket + socklen_t m_socket; //!< Hub socket int m_cback_socket; //!< Hub socket to callback clients clients* m_clients; //!< Structure of registered clients void* m_shmem; //!< Pointer to shared memory address diff --git a/src/vo/GVOHub.cpp b/src/vo/GVOHub.cpp index 155a242..944e627 100644 --- a/src/vo/GVOHub.cpp +++ b/src/vo/GVOHub.cpp @@ -246,7 +246,7 @@ void GVOHub::init_members(void) m_version = "1.3"; m_hub_id = "gammalib_hub"; m_socket = -1; // Signals no socket - m_cback_socket = -1; + m_cback_socket = -1; // Signals no socket m_clients = NULL; m_shmem = NULL; m_shmem_handler = 0; @@ -314,7 +314,16 @@ void GVOHub::copy_members(const GVOHub& hub) ***************************************************************************/ void GVOHub::free_members(void) { - + // Close sockets + if (m_socket != -1) { + close(m_socket); + m_socket = -1; + } + if (m_cback_socket != -1) { + close(m_cback_socket); + m_cback_socket = -1; + } + // Remove lockfile std::string lockurl = get_hub_lockfile(); std::remove(lockurl.c_str()); @@ -356,10 +365,10 @@ void GVOHub::start_hub(void) } // Create Hub socket - socklen_t hub_socket = socket(AF_INET, SOCK_STREAM, 0); + m_socket = socket(AF_INET, SOCK_STREAM, 0); // Creation of hub main socket - if (hub_socket < 0) { + if (m_socket < 0) { std::string msg = "Unable to create Hub socket. Errno="+ gammalib::str(errno); throw GException::runtime_error(G_START_HUB, msg); @@ -367,13 +376,13 @@ void GVOHub::start_hub(void) // Set hub main socket to allow multiple connections int opt = 1; - if (setsockopt(hub_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)) < 0) { + if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)) < 0) { std::string msg = "Unable to set Hub socket to multiple connections." " Errno="+gammalib::str(errno); throw GException::runtime_error(G_START_HUB, msg); } #ifdef SO_REUSEPORT - if (setsockopt(hub_socket, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, sizeof(opt)) < 0) { + if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, sizeof(opt)) < 0) { std::string msg = "Unable to set Hub socket to multiple connections." " Errno="+gammalib::str(errno); throw GException::runtime_error(G_START_HUB, msg); @@ -381,7 +390,7 @@ void GVOHub::start_hub(void) #endif // Server socket is opened. Now, bind it to the port, with family etc. - if (bind(hub_socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { + if (bind(m_socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { std::string msg = "Unable to bind Hub socket to server socket. Errno="+ gammalib::str(errno); throw GException::runtime_error(G_START_HUB, msg); @@ -389,7 +398,7 @@ void GVOHub::start_hub(void) // Now start listening for the clients: 5 requests simultaneously pending // at maximum - if (listen(hub_socket, 5) < 0) { + if (listen(m_socket, 5) < 0) { std::string msg = "Unable to start listening on Hub socket. Errno="+ gammalib::str(errno); throw GException::runtime_error(G_START_HUB, msg); @@ -403,8 +412,8 @@ void GVOHub::start_hub(void) while (1) { // Accept connection from the client - socklen_t newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen); - if (newsocket < 0) { + socklen_t socket = accept(m_socket, (struct sockaddr *)&cli_addr, &clilen); + if (socket < 0) { std::string msg = "Client connection to socket not accepted."; throw GException::runtime_error(G_START_HUB, msg); } @@ -421,15 +430,13 @@ void GVOHub::start_hub(void) if (pid == 0) { // Handle request - handle_request(newsocket); + handle_request(socket); // Exit child process exit(0); } - // ... otherwise we are in the parent process. Set SIG_IGN to child - // so that the kernel can close it without sending it to zombie state - // + // ... otherwise we are in the parent process. else { // Wait for the child process to terminate @@ -437,9 +444,10 @@ void GVOHub::start_hub(void) waitpid(pid, &status, 0); // Close socket - close(newsocket); + close(socket); - // Send SIG_IGN to child process + // Send SIG_IGN to the child process so that the kernel can + // close it without sending it to zombie state std::signal(pid, SIG_IGN); // If we have received a shutdown request then exit the event @@ -453,9 +461,8 @@ void GVOHub::start_hub(void) } // endwhile: main event loop // Close socket - close(hub_socket); -std::cout << "\n\n*** Hub shutdown ***" << std::endl; -system("netstat -npta"); + close(m_socket); + m_socket = -1; // Return return; diff --git a/test/test_GVO.cpp b/test/test_GVO.cpp index 368cc42..cc2a1e9 100644 --- a/test/test_GVO.cpp +++ b/test/test_GVO.cpp @@ -90,8 +90,6 @@ void TestGVO::test_GVOHub(void) { // Declare thread pthread_t thread; -std::cout << "\n*** Before start of thread ***" << std::endl; -system("netstat -npta"); // Start Hub in a thread test_try("Start hub"); @@ -102,8 +100,6 @@ system("netstat -npta"); catch (std::exception &e) { test_try_failure(e); } -std::cout << "\n*** After start of thread ***" << std::endl; -system("netstat -npta"); // Test Hub connection GVOClient client; @@ -121,14 +117,8 @@ system("netstat -npta"); // Disconnect client client.disconnect(); - // Cancel thread. We do this here so that for the next test - // there is no Hub anymore. We do some sleeping to assure that - // the thread has been canceled. - //pthread_cancel(thread); - //sleep(1); + // We now shutdown the Hub via the client client.shutdown_hub(); -std::cout << "\n*** After thread canel ***" << std::endl; -system("netstat -npta"); // Return return; @@ -151,17 +141,15 @@ void TestGVO::test_GVOClient(void) test_try_failure(e); } -std::cout << "\n*** Before client connect ***" << std::endl; -system("netstat -npta"); // Create client GVOClient client; + // Test ping + test_assert(!client.ping_hub(), "Ping Hub (should not be alive)."); + // Connect client client.connect(); test_assert(client.is_connected(), "Check for connection."); - //std::cout << client << std::endl; -std::cout << "\n*** After client connect ***" << std::endl; -system("netstat -npta"); // Test ping test_assert(client.ping_hub(), "Ping Hub."); @@ -169,7 +157,6 @@ system("netstat -npta"); // Disconnect client client.disconnect(); test_assert(!client.is_connected(), "Check for disconnection."); - //std::cout << client << std::endl; // Test ping test_assert(client.ping_hub(), "Ping Hub."); @@ -177,6 +164,9 @@ system("netstat -npta"); // Shutdown VO Hub client.shutdown_hub(); + // Test ping + test_assert(!client.ping_hub(), "Ping Hub (should not be alive)."); + // Return return; }