Commit 9a5fe40b9e23b561fe1d684eb7b30ee8d2a61d99

Authored by Thierry Louge
1 parent ab88041b

Incorporating parsing of client queries in the Hub code

Showing 1 changed file with 72 additions and 5 deletions
src/vo/GVOHub.cpp
... ... @@ -188,7 +188,7 @@ void GVOHub::start(void)
188 188 ***************************************************************************/
189 189 void GVOHub::register_service(void)
190 190 {
191   -
  191 + printf("Registration request received");
192 192 // Return
193 193 return;
194 194 }
... ... @@ -425,7 +425,10 @@ void GVOHub::handle_request(const socklen_t& sock)
425 425 // Initialize buffer
426 426 char buffer[256];
427 427 bzero(buffer,256);
428   -
  428 +
  429 + // Declare empty XML document
  430 + GXml xml;
  431 +
429 432 // Read buffer
430 433 int n = read(sock,buffer,255);
431 434 if (n < 0) {
... ... @@ -433,10 +436,28 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
433 436 throw GException::invalid_value(G_HANDLE_REQUEST, msg);
434 437 }
435 438 printf("Here is the message: %s\n",buffer);
  439 + std::string response = buffer;
436 440  
437   - // Here, obviously not simple string matching but xml parsing to be done with
438   - // help of GXml object. This is only for architectural purpose.
439   - if (strcmp(buffer,"register") == 0) {
  441 + // Find start of XML text
  442 + size_t start = response.find("<?xml");
  443 + // If found then convert text into XML document
  444 + if (start != std::string::npos) {
  445 + xml = GXml(response.substr(start, std::string::npos));
  446 + }
  447 +
  448 + std::string method_called = "";
  449 + // Search for value of methodName
  450 + const GXmlNode* node = xml.element("methodCall", 0);
  451 + const GXmlNode* ptr = node->element("methodName", 0);
  452 + if (ptr != NULL) {
  453 + const GXmlText* text = static_cast<const GXmlText*>((*ptr)[0]);
  454 + if (text != NULL) {
  455 + method_called = text->text();
  456 + }
  457 + }
  458 + // endif: connection has been established
  459 + //std::string method_called = get_response_value(xml, "methodName");
  460 + if (method_called.compare("samp.hub.register") == 0) {
440 461 register_service();
441 462 }
442 463  
... ... @@ -519,3 +540,49 @@ void GVOHub::start_hub(void)
519 540 // Return
520 541 return;
521 542 }
  543 +
  544 +
  545 +/***********************************************************************//**
  546 + * @brief Extract name / value pair from XML node
  547 + *
  548 + * @param[in] node Pointer to XML node.
  549 + * @param[out] name Name string.
  550 + * @param[out] value Value string.
  551 + *
  552 + * Extracts a name / value pair from a XML node. If the XML node pointer is
  553 + * NULL, the name and value strings will be empty.
  554 + ***************************************************************************/
  555 +void GVOHub::get_name_value_pair(const GXmlNode* node,
  556 + std::string& name,
  557 + std::string& value) const
  558 +{
  559 + // Clear name and value strings
  560 + name.clear();
  561 + value.clear();
  562 +
  563 + // Continue only if node is valid
  564 + if (node != NULL) {
  565 +
  566 + // Get name node and extract text content
  567 + const GXmlNode* ptr = node->element("name", 0);
  568 + if (ptr != NULL) {
  569 + const GXmlText* text = static_cast<const GXmlText*>((*ptr)[0]);
  570 + if (text != NULL) {
  571 + name = text->text();
  572 + }
  573 + }
  574 +
  575 + // Get value node and extract text content
  576 + ptr = node->element("value", 0);
  577 + if (ptr != NULL) {
  578 + const GXmlText* text = static_cast<const GXmlText*>((*ptr)[0]);
  579 + if (text != NULL) {
  580 + value = text->text();
  581 + }
  582 + }
  583 + }
  584 +
  585 + // Return
  586 + return;
  587 +}
  588 +
... ...