Commit 5fec60e254b7a9df60cd5f52260d8b5f753b53b7

Authored by Jürgen Knödlseder
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.
include/GVOClient.hpp
... ... @@ -72,6 +72,7 @@ public:
72 72 bool has_hub(void) const;
73 73 bool is_connected(void) const;
74 74 bool ping_hub(void) const;
  75 + void shutdown_hub(void) const;
75 76 GXml execute(const std::string& request) const;
76 77 std::string print(const GChatter& chatter = NORMAL) const;
77 78  
... ...
include/GVOHub.hpp
... ... @@ -95,6 +95,7 @@ protected:
95 95 const socklen_t& sock);
96 96 void request_get_metadata(const GXml& xml,
97 97 const socklen_t& sock);
  98 + void request_shutdown(const socklen_t& sock);
98 99 std::string get_client_key(const GXml& xml) const;
99 100 int get_client_index(const GXml& xml) const;
100 101 std::string get_response_value(const GXml& xml,
... ...
src/vo/GVOClient.cpp
... ... @@ -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 317 * @brief Execute function on server
293 318 *
294 319 * @param[in] request XML request string
... ... @@ -612,15 +637,13 @@ bool GVOClient::require_hub(void)
612 637  
613 638 // Check that Hub is alive
614 639 for (int i = 0; i < 3; ++i) {
615   - found = ping_hub();
  640 + found = find_hub() && ping_hub();
616 641 if (found) {
617 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 649 } // endif: created own VO Hub
... ...
src/vo/GVOHub.cpp
... ... @@ -423,9 +423,6 @@ void GVOHub::start_hub(void)
423 423 // Handle request
424 424 handle_request(newsocket);
425 425  
426   - // Close socket
427   - close(newsocket);
428   -
429 426 // Exit child process
430 427 exit(0);
431 428 }
... ... @@ -438,9 +435,18 @@ void GVOHub::start_hub(void)
438 435 // Wait for the child process to terminate
439 436 int status = 0;
440 437 waitpid(pid, &status, 0);
  438 +
  439 + // Close socket
  440 + close(newsocket);
441 441  
442 442 // Send SIG_IGN to child process
443 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 454  
449 455 // Close socket
450 456 close(hub_socket);
451   -
  457 +std::cout << "\n\n*** Hub shutdown ***" << std::endl;
  458 +system("netstat -npta");
  459 +
452 460 // Return
453 461 return;
454 462 }
... ... @@ -561,6 +569,9 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
561 569 else if (method_called.compare("samp.hub.reply") == 0) {
562 570 request_ping(sock);
563 571 }
  572 + else if (method_called.compare("samp.hub.shutdown") == 0) {
  573 + request_shutdown(sock);
  574 + }
564 575  
565 576 // Return
566 577 return;
... ... @@ -666,7 +677,7 @@ void GVOHub::request_register(const GXml&amp; xml, const socklen_t&amp; sock)
666 677 activate_callbacks("samp.hub.event.register", cl_id);
667 678  
668 679 // Increment number of registered clients
669   - m_clients->registered ++;
  680 + //m_clients->registered++;
670 681  
671 682 //Return
672 683 return;
... ... @@ -1168,6 +1179,42 @@ void GVOHub::request_get_metadata(const GXml&amp; xml,const socklen_t&amp; 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 1218 * @brief Extract client key from XML request
1172 1219 *
1173 1220 * @param[in] xml XML message sent by client.
... ...
test/test_GVO.cpp
... ... @@ -124,8 +124,9 @@ system(&quot;netstat -npta&quot;);
124 124 // Cancel thread. We do this here so that for the next test
125 125 // there is no Hub anymore. We do some sleeping to assure that
126 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 130 std::cout << "\n*** After thread canel ***" << std::endl;
130 131 system("netstat -npta");
131 132  
... ... @@ -173,6 +174,9 @@ system(&quot;netstat -npta&quot;);
173 174 // Test ping
174 175 test_assert(client.ping_hub(), "Ping Hub.");
175 176  
  177 + // Shutdown VO Hub
  178 + client.shutdown_hub();
  179 +
176 180 // Return
177 181 return;
178 182 }
... ...