Commit 5fec60e254b7a9df60cd5f52260d8b5f753b53b7
1 parent
4e44ffd3
Implement samp.hub.shutdown request.
The samp.hub.shutdown request allows a client to shutdown the VO Hub. This is not part of the SAMP protocol but does not harm. It is basically needed for unit testing.
Showing
5 changed files
with
87 additions
and
11 deletions
include/GVOClient.hpp
| @@ -72,6 +72,7 @@ public: | @@ -72,6 +72,7 @@ public: | ||
| 72 | bool has_hub(void) const; | 72 | bool has_hub(void) const; |
| 73 | bool is_connected(void) const; | 73 | bool is_connected(void) const; |
| 74 | bool ping_hub(void) const; | 74 | bool ping_hub(void) const; |
| 75 | + void shutdown_hub(void) const; | ||
| 75 | GXml execute(const std::string& request) const; | 76 | GXml execute(const std::string& request) const; |
| 76 | std::string print(const GChatter& chatter = NORMAL) const; | 77 | std::string print(const GChatter& chatter = NORMAL) const; |
| 77 | 78 |
include/GVOHub.hpp
| @@ -95,6 +95,7 @@ protected: | @@ -95,6 +95,7 @@ protected: | ||
| 95 | const socklen_t& sock); | 95 | const socklen_t& sock); |
| 96 | void request_get_metadata(const GXml& xml, | 96 | void request_get_metadata(const GXml& xml, |
| 97 | const socklen_t& sock); | 97 | const socklen_t& sock); |
| 98 | + void request_shutdown(const socklen_t& sock); | ||
| 98 | std::string get_client_key(const GXml& xml) const; | 99 | std::string get_client_key(const GXml& xml) const; |
| 99 | int get_client_index(const GXml& xml) const; | 100 | int get_client_index(const GXml& xml) const; |
| 100 | std::string get_response_value(const GXml& xml, | 101 | std::string get_response_value(const GXml& xml, |
src/vo/GVOClient.cpp
| @@ -289,6 +289,31 @@ bool GVOClient::ping_hub(void) const | @@ -289,6 +289,31 @@ bool GVOClient::ping_hub(void) const | ||
| 289 | 289 | ||
| 290 | 290 | ||
| 291 | /***********************************************************************//** | 291 | /***********************************************************************//** |
| 292 | + * @brief Shutdown VO Hub | ||
| 293 | + ***************************************************************************/ | ||
| 294 | +void GVOClient::shutdown_hub(void) const | ||
| 295 | +{ | ||
| 296 | + // Declare request | ||
| 297 | + std::string request = ""; | ||
| 298 | + | ||
| 299 | + // Set request | ||
| 300 | + request.append("<?xml version=\"1.0\"?>\n"); | ||
| 301 | + request.append("<methodCall>\n"); | ||
| 302 | + request.append(" <methodName>samp.hub.shutdown</methodName>\n"); | ||
| 303 | + request.append(" <params>\n"); | ||
| 304 | + request.append(" <param><value/></param>\n"); | ||
| 305 | + request.append(" </params>\n"); | ||
| 306 | + request.append("</methodCall>\n"); | ||
| 307 | + | ||
| 308 | + // Execute request | ||
| 309 | + execute(request); | ||
| 310 | + | ||
| 311 | + // Return | ||
| 312 | + return; | ||
| 313 | +} | ||
| 314 | + | ||
| 315 | + | ||
| 316 | +/***********************************************************************//** | ||
| 292 | * @brief Execute function on server | 317 | * @brief Execute function on server |
| 293 | * | 318 | * |
| 294 | * @param[in] request XML request string | 319 | * @param[in] request XML request string |
| @@ -612,15 +637,13 @@ bool GVOClient::require_hub(void) | @@ -612,15 +637,13 @@ bool GVOClient::require_hub(void) | ||
| 612 | 637 | ||
| 613 | // Check that Hub is alive | 638 | // Check that Hub is alive |
| 614 | for (int i = 0; i < 3; ++i) { | 639 | for (int i = 0; i < 3; ++i) { |
| 615 | - found = ping_hub(); | 640 | + found = find_hub() && ping_hub(); |
| 616 | if (found) { | 641 | if (found) { |
| 617 | break; | 642 | break; |
| 618 | } | 643 | } |
| 644 | + sleep(1); | ||
| 619 | } | 645 | } |
| 620 | 646 | ||
| 621 | - // Get Hub information | ||
| 622 | - found = find_hub(); | ||
| 623 | - | ||
| 624 | } | 647 | } |
| 625 | 648 | ||
| 626 | } // endif: created own VO Hub | 649 | } // endif: created own VO Hub |
src/vo/GVOHub.cpp
| @@ -423,9 +423,6 @@ void GVOHub::start_hub(void) | @@ -423,9 +423,6 @@ void GVOHub::start_hub(void) | ||
| 423 | // Handle request | 423 | // Handle request |
| 424 | handle_request(newsocket); | 424 | handle_request(newsocket); |
| 425 | 425 | ||
| 426 | - // Close socket | ||
| 427 | - close(newsocket); | ||
| 428 | - | ||
| 429 | // Exit child process | 426 | // Exit child process |
| 430 | exit(0); | 427 | exit(0); |
| 431 | } | 428 | } |
| @@ -438,9 +435,18 @@ void GVOHub::start_hub(void) | @@ -438,9 +435,18 @@ void GVOHub::start_hub(void) | ||
| 438 | // Wait for the child process to terminate | 435 | // Wait for the child process to terminate |
| 439 | int status = 0; | 436 | int status = 0; |
| 440 | waitpid(pid, &status, 0); | 437 | waitpid(pid, &status, 0); |
| 438 | + | ||
| 439 | + // Close socket | ||
| 440 | + close(newsocket); | ||
| 441 | 441 | ||
| 442 | // Send SIG_IGN to child process | 442 | // Send SIG_IGN to child process |
| 443 | std::signal(pid, SIG_IGN); | 443 | std::signal(pid, SIG_IGN); |
| 444 | + | ||
| 445 | + // If we have received a shutdown request then exit the event | ||
| 446 | + // loop | ||
| 447 | + if (m_clients->registered == -1) { | ||
| 448 | + break; | ||
| 449 | + } | ||
| 444 | 450 | ||
| 445 | } | 451 | } |
| 446 | 452 | ||
| @@ -448,7 +454,9 @@ void GVOHub::start_hub(void) | @@ -448,7 +454,9 @@ void GVOHub::start_hub(void) | ||
| 448 | 454 | ||
| 449 | // Close socket | 455 | // Close socket |
| 450 | close(hub_socket); | 456 | close(hub_socket); |
| 451 | - | 457 | +std::cout << "\n\n*** Hub shutdown ***" << std::endl; |
| 458 | +system("netstat -npta"); | ||
| 459 | + | ||
| 452 | // Return | 460 | // Return |
| 453 | return; | 461 | return; |
| 454 | } | 462 | } |
| @@ -561,6 +569,9 @@ void GVOHub::handle_request(const socklen_t& sock) | @@ -561,6 +569,9 @@ void GVOHub::handle_request(const socklen_t& sock) | ||
| 561 | else if (method_called.compare("samp.hub.reply") == 0) { | 569 | else if (method_called.compare("samp.hub.reply") == 0) { |
| 562 | request_ping(sock); | 570 | request_ping(sock); |
| 563 | } | 571 | } |
| 572 | + else if (method_called.compare("samp.hub.shutdown") == 0) { | ||
| 573 | + request_shutdown(sock); | ||
| 574 | + } | ||
| 564 | 575 | ||
| 565 | // Return | 576 | // Return |
| 566 | return; | 577 | return; |
| @@ -666,7 +677,7 @@ void GVOHub::request_register(const GXml& xml, const socklen_t& sock) | @@ -666,7 +677,7 @@ void GVOHub::request_register(const GXml& xml, const socklen_t& sock) | ||
| 666 | activate_callbacks("samp.hub.event.register", cl_id); | 677 | activate_callbacks("samp.hub.event.register", cl_id); |
| 667 | 678 | ||
| 668 | // Increment number of registered clients | 679 | // Increment number of registered clients |
| 669 | - m_clients->registered ++; | 680 | + //m_clients->registered++; |
| 670 | 681 | ||
| 671 | //Return | 682 | //Return |
| 672 | return; | 683 | return; |
| @@ -1168,6 +1179,42 @@ void GVOHub::request_get_metadata(const GXml& xml,const socklen_t& sock) | @@ -1168,6 +1179,42 @@ void GVOHub::request_get_metadata(const GXml& xml,const socklen_t& sock) | ||
| 1168 | 1179 | ||
| 1169 | 1180 | ||
| 1170 | /***********************************************************************//** | 1181 | /***********************************************************************//** |
| 1182 | + * @brief Handles Hub shutdown requests | ||
| 1183 | + * | ||
| 1184 | + * @param[in] sock Socket. | ||
| 1185 | + * | ||
| 1186 | + * Handles all incoming Hub shutdown requests. | ||
| 1187 | + ***************************************************************************/ | ||
| 1188 | +void GVOHub::request_shutdown(const socklen_t& sock) | ||
| 1189 | +{ | ||
| 1190 | + // Header | ||
| 1191 | + #if defined(G_CONSOLE_DUMP) | ||
| 1192 | + std::cout << "GVOHub::request_shutdown" << std::endl; | ||
| 1193 | + #endif | ||
| 1194 | + | ||
| 1195 | + // Set shutdown flag | ||
| 1196 | + m_clients->registered = -1; | ||
| 1197 | + | ||
| 1198 | + // Declare message | ||
| 1199 | + std::string msg = ""; | ||
| 1200 | + | ||
| 1201 | + // Set response | ||
| 1202 | + msg.append("<?xml version=\"1.0\"?>\n"); | ||
| 1203 | + msg.append("<methodResponse>\n"); | ||
| 1204 | + msg.append("<params>\n"); | ||
| 1205 | + msg.append(" <param><value/></param>\n"); | ||
| 1206 | + msg.append("</params>\n"); | ||
| 1207 | + msg.append("</methodResponse>\n"); | ||
| 1208 | + | ||
| 1209 | + // Post response | ||
| 1210 | + post_string(msg,sock); | ||
| 1211 | + | ||
| 1212 | + // Return | ||
| 1213 | + return; | ||
| 1214 | +} | ||
| 1215 | + | ||
| 1216 | + | ||
| 1217 | +/***********************************************************************//** | ||
| 1171 | * @brief Extract client key from XML request | 1218 | * @brief Extract client key from XML request |
| 1172 | * | 1219 | * |
| 1173 | * @param[in] xml XML message sent by client. | 1220 | * @param[in] xml XML message sent by client. |
test/test_GVO.cpp
| @@ -124,8 +124,9 @@ system("netstat -npta"); | @@ -124,8 +124,9 @@ system("netstat -npta"); | ||
| 124 | // Cancel thread. We do this here so that for the next test | 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 | 125 | // there is no Hub anymore. We do some sleeping to assure that |
| 126 | // the thread has been canceled. | 126 | // the thread has been canceled. |
| 127 | - pthread_cancel(thread); | ||
| 128 | - sleep(1); | 127 | + //pthread_cancel(thread); |
| 128 | + //sleep(1); | ||
| 129 | + client.shutdown_hub(); | ||
| 129 | std::cout << "\n*** After thread canel ***" << std::endl; | 130 | std::cout << "\n*** After thread canel ***" << std::endl; |
| 130 | system("netstat -npta"); | 131 | system("netstat -npta"); |
| 131 | 132 | ||
| @@ -173,6 +174,9 @@ system("netstat -npta"); | @@ -173,6 +174,9 @@ system("netstat -npta"); | ||
| 173 | // Test ping | 174 | // Test ping |
| 174 | test_assert(client.ping_hub(), "Ping Hub."); | 175 | test_assert(client.ping_hub(), "Ping Hub."); |
| 175 | 176 | ||
| 177 | + // Shutdown VO Hub | ||
| 178 | + client.shutdown_hub(); | ||
| 179 | + | ||
| 176 | // Return | 180 | // Return |
| 177 | return; | 181 | return; |
| 178 | } | 182 | } |