Commit 0c47c97e23b53124c65f1688f8ef5c818b107607

Authored by Jürgen Knödlseder
1 parent 5fec60e2

Remove debut outputs.

include/GVOHub.hpp
@@ -142,7 +142,7 @@ protected: @@ -142,7 +142,7 @@ protected:
142 std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint) 142 std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint)
143 std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub 143 std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub
144 std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others 144 std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others
145 - int m_socket; //!< Hub socket 145 + socklen_t m_socket; //!< Hub socket
146 int m_cback_socket; //!< Hub socket to callback clients 146 int m_cback_socket; //!< Hub socket to callback clients
147 clients* m_clients; //!< Structure of registered clients 147 clients* m_clients; //!< Structure of registered clients
148 void* m_shmem; //!< Pointer to shared memory address 148 void* m_shmem; //!< Pointer to shared memory address
src/vo/GVOHub.cpp
@@ -246,7 +246,7 @@ void GVOHub::init_members(void) @@ -246,7 +246,7 @@ void GVOHub::init_members(void)
246 m_version = "1.3"; 246 m_version = "1.3";
247 m_hub_id = "gammalib_hub"; 247 m_hub_id = "gammalib_hub";
248 m_socket = -1; // Signals no socket 248 m_socket = -1; // Signals no socket
249 - m_cback_socket = -1; 249 + m_cback_socket = -1; // Signals no socket
250 m_clients = NULL; 250 m_clients = NULL;
251 m_shmem = NULL; 251 m_shmem = NULL;
252 m_shmem_handler = 0; 252 m_shmem_handler = 0;
@@ -314,7 +314,16 @@ void GVOHub::copy_members(const GVOHub&amp; hub) @@ -314,7 +314,16 @@ void GVOHub::copy_members(const GVOHub&amp; hub)
314 ***************************************************************************/ 314 ***************************************************************************/
315 void GVOHub::free_members(void) 315 void GVOHub::free_members(void)
316 { 316 {
317 - 317 + // Close sockets
  318 + if (m_socket != -1) {
  319 + close(m_socket);
  320 + m_socket = -1;
  321 + }
  322 + if (m_cback_socket != -1) {
  323 + close(m_cback_socket);
  324 + m_cback_socket = -1;
  325 + }
  326 +
318 // Remove lockfile 327 // Remove lockfile
319 std::string lockurl = get_hub_lockfile(); 328 std::string lockurl = get_hub_lockfile();
320 std::remove(lockurl.c_str()); 329 std::remove(lockurl.c_str());
@@ -356,10 +365,10 @@ void GVOHub::start_hub(void) @@ -356,10 +365,10 @@ void GVOHub::start_hub(void)
356 } 365 }
357 366
358 // Create Hub socket 367 // Create Hub socket
359 - socklen_t hub_socket = socket(AF_INET, SOCK_STREAM, 0); 368 + m_socket = socket(AF_INET, SOCK_STREAM, 0);
360 369
361 // Creation of hub main socket 370 // Creation of hub main socket
362 - if (hub_socket < 0) { 371 + if (m_socket < 0) {
363 std::string msg = "Unable to create Hub socket. Errno="+ 372 std::string msg = "Unable to create Hub socket. Errno="+
364 gammalib::str(errno); 373 gammalib::str(errno);
365 throw GException::runtime_error(G_START_HUB, msg); 374 throw GException::runtime_error(G_START_HUB, msg);
@@ -367,13 +376,13 @@ void GVOHub::start_hub(void) @@ -367,13 +376,13 @@ void GVOHub::start_hub(void)
367 376
368 // Set hub main socket to allow multiple connections 377 // Set hub main socket to allow multiple connections
369 int opt = 1; 378 int opt = 1;
370 - if (setsockopt(hub_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)) < 0) { 379 + if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)) < 0) {
371 std::string msg = "Unable to set Hub socket to multiple connections." 380 std::string msg = "Unable to set Hub socket to multiple connections."
372 " Errno="+gammalib::str(errno); 381 " Errno="+gammalib::str(errno);
373 throw GException::runtime_error(G_START_HUB, msg); 382 throw GException::runtime_error(G_START_HUB, msg);
374 } 383 }
375 #ifdef SO_REUSEPORT 384 #ifdef SO_REUSEPORT
376 - if (setsockopt(hub_socket, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, sizeof(opt)) < 0) { 385 + if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEPORT, (char*)&opt, sizeof(opt)) < 0) {
377 std::string msg = "Unable to set Hub socket to multiple connections." 386 std::string msg = "Unable to set Hub socket to multiple connections."
378 " Errno="+gammalib::str(errno); 387 " Errno="+gammalib::str(errno);
379 throw GException::runtime_error(G_START_HUB, msg); 388 throw GException::runtime_error(G_START_HUB, msg);
@@ -381,7 +390,7 @@ void GVOHub::start_hub(void) @@ -381,7 +390,7 @@ void GVOHub::start_hub(void)
381 #endif 390 #endif
382 391
383 // Server socket is opened. Now, bind it to the port, with family etc. 392 // Server socket is opened. Now, bind it to the port, with family etc.
384 - if (bind(hub_socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { 393 + if (bind(m_socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
385 std::string msg = "Unable to bind Hub socket to server socket. Errno="+ 394 std::string msg = "Unable to bind Hub socket to server socket. Errno="+
386 gammalib::str(errno); 395 gammalib::str(errno);
387 throw GException::runtime_error(G_START_HUB, msg); 396 throw GException::runtime_error(G_START_HUB, msg);
@@ -389,7 +398,7 @@ void GVOHub::start_hub(void) @@ -389,7 +398,7 @@ void GVOHub::start_hub(void)
389 398
390 // Now start listening for the clients: 5 requests simultaneously pending 399 // Now start listening for the clients: 5 requests simultaneously pending
391 // at maximum 400 // at maximum
392 - if (listen(hub_socket, 5) < 0) { 401 + if (listen(m_socket, 5) < 0) {
393 std::string msg = "Unable to start listening on Hub socket. Errno="+ 402 std::string msg = "Unable to start listening on Hub socket. Errno="+
394 gammalib::str(errno); 403 gammalib::str(errno);
395 throw GException::runtime_error(G_START_HUB, msg); 404 throw GException::runtime_error(G_START_HUB, msg);
@@ -403,8 +412,8 @@ void GVOHub::start_hub(void) @@ -403,8 +412,8 @@ void GVOHub::start_hub(void)
403 while (1) { 412 while (1) {
404 413
405 // Accept connection from the client 414 // Accept connection from the client
406 - socklen_t newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen);  
407 - if (newsocket < 0) { 415 + socklen_t socket = accept(m_socket, (struct sockaddr *)&cli_addr, &clilen);
  416 + if (socket < 0) {
408 std::string msg = "Client connection to socket not accepted."; 417 std::string msg = "Client connection to socket not accepted.";
409 throw GException::runtime_error(G_START_HUB, msg); 418 throw GException::runtime_error(G_START_HUB, msg);
410 } 419 }
@@ -421,15 +430,13 @@ void GVOHub::start_hub(void) @@ -421,15 +430,13 @@ void GVOHub::start_hub(void)
421 if (pid == 0) { 430 if (pid == 0) {
422 431
423 // Handle request 432 // Handle request
424 - handle_request(newsocket); 433 + handle_request(socket);
425 434
426 // Exit child process 435 // Exit child process
427 exit(0); 436 exit(0);
428 } 437 }
429 438
430 - // ... otherwise we are in the parent process. Set SIG_IGN to child  
431 - // so that the kernel can close it without sending it to zombie state  
432 - // <defunct> 439 + // ... otherwise we are in the parent process.
433 else { 440 else {
434 441
435 // Wait for the child process to terminate 442 // Wait for the child process to terminate
@@ -437,9 +444,10 @@ void GVOHub::start_hub(void) @@ -437,9 +444,10 @@ void GVOHub::start_hub(void)
437 waitpid(pid, &status, 0); 444 waitpid(pid, &status, 0);
438 445
439 // Close socket 446 // Close socket
440 - close(newsocket); 447 + close(socket);
441 448
442 - // Send SIG_IGN to child process 449 + // Send SIG_IGN to the child process so that the kernel can
  450 + // close it without sending it to zombie state <defunct>
443 std::signal(pid, SIG_IGN); 451 std::signal(pid, SIG_IGN);
444 452
445 // If we have received a shutdown request then exit the event 453 // If we have received a shutdown request then exit the event
@@ -453,9 +461,8 @@ void GVOHub::start_hub(void) @@ -453,9 +461,8 @@ void GVOHub::start_hub(void)
453 } // endwhile: main event loop 461 } // endwhile: main event loop
454 462
455 // Close socket 463 // Close socket
456 - close(hub_socket);  
457 -std::cout << "\n\n*** Hub shutdown ***" << std::endl;  
458 -system("netstat -npta"); 464 + close(m_socket);
  465 + m_socket = -1;
459 466
460 // Return 467 // Return
461 return; 468 return;
test/test_GVO.cpp
@@ -90,8 +90,6 @@ void TestGVO::test_GVOHub(void) @@ -90,8 +90,6 @@ void TestGVO::test_GVOHub(void)
90 { 90 {
91 // Declare thread 91 // Declare thread
92 pthread_t thread; 92 pthread_t thread;
93 -std::cout << "\n*** Before start of thread ***" << std::endl;  
94 -system("netstat -npta");  
95 93
96 // Start Hub in a thread 94 // Start Hub in a thread
97 test_try("Start hub"); 95 test_try("Start hub");
@@ -102,8 +100,6 @@ system(&quot;netstat -npta&quot;); @@ -102,8 +100,6 @@ system(&quot;netstat -npta&quot;);
102 catch (std::exception &e) { 100 catch (std::exception &e) {
103 test_try_failure(e); 101 test_try_failure(e);
104 } 102 }
105 -std::cout << "\n*** After start of thread ***" << std::endl;  
106 -system("netstat -npta");  
107 103
108 // Test Hub connection 104 // Test Hub connection
109 GVOClient client; 105 GVOClient client;
@@ -121,14 +117,8 @@ system(&quot;netstat -npta&quot;); @@ -121,14 +117,8 @@ system(&quot;netstat -npta&quot;);
121 // Disconnect client 117 // Disconnect client
122 client.disconnect(); 118 client.disconnect();
123 119
124 - // Cancel thread. We do this here so that for the next test  
125 - // there is no Hub anymore. We do some sleeping to assure that  
126 - // the thread has been canceled.  
127 - //pthread_cancel(thread);  
128 - //sleep(1); 120 + // We now shutdown the Hub via the client
129 client.shutdown_hub(); 121 client.shutdown_hub();
130 -std::cout << "\n*** After thread canel ***" << std::endl;  
131 -system("netstat -npta");  
132 122
133 // Return 123 // Return
134 return; 124 return;
@@ -151,17 +141,15 @@ void TestGVO::test_GVOClient(void) @@ -151,17 +141,15 @@ void TestGVO::test_GVOClient(void)
151 test_try_failure(e); 141 test_try_failure(e);
152 } 142 }
153 143
154 -std::cout << "\n*** Before client connect ***" << std::endl;  
155 -system("netstat -npta");  
156 // Create client 144 // Create client
157 GVOClient client; 145 GVOClient client;
158 146
  147 + // Test ping
  148 + test_assert(!client.ping_hub(), "Ping Hub (should not be alive).");
  149 +
159 // Connect client 150 // Connect client
160 client.connect(); 151 client.connect();
161 test_assert(client.is_connected(), "Check for connection."); 152 test_assert(client.is_connected(), "Check for connection.");
162 - //std::cout << client << std::endl;  
163 -std::cout << "\n*** After client connect ***" << std::endl;  
164 -system("netstat -npta");  
165 153
166 // Test ping 154 // Test ping
167 test_assert(client.ping_hub(), "Ping Hub."); 155 test_assert(client.ping_hub(), "Ping Hub.");
@@ -169,7 +157,6 @@ system(&quot;netstat -npta&quot;); @@ -169,7 +157,6 @@ system(&quot;netstat -npta&quot;);
169 // Disconnect client 157 // Disconnect client
170 client.disconnect(); 158 client.disconnect();
171 test_assert(!client.is_connected(), "Check for disconnection."); 159 test_assert(!client.is_connected(), "Check for disconnection.");
172 - //std::cout << client << std::endl;  
173 160
174 // Test ping 161 // Test ping
175 test_assert(client.ping_hub(), "Ping Hub."); 162 test_assert(client.ping_hub(), "Ping Hub.");
@@ -177,6 +164,9 @@ system(&quot;netstat -npta&quot;); @@ -177,6 +164,9 @@ system(&quot;netstat -npta&quot;);
177 // Shutdown VO Hub 164 // Shutdown VO Hub
178 client.shutdown_hub(); 165 client.shutdown_hub();
179 166
  167 + // Test ping
  168 + test_assert(!client.ping_hub(), "Ping Hub (should not be alive).");
  169 +
180 // Return 170 // Return
181 return; 171 return;
182 } 172 }