Commit 7e89bba122fe575f97d5db6f0421f50ef609f75f

Authored by Jürgen Knödlseder
1 parent ecc20c92

Remove is_connected() and response() methods and introduce execute() method.

include/GVOClient.hpp
... ... @@ -67,8 +67,7 @@ public:
67 67 void connect(void);
68 68 void disconnect(void);
69 69 bool has_hub(void) const;
70   - bool is_connected(void) const;
71   - GXml response(void) const;
  70 + GXml execute(const std::string& request) const;
72 71 std::string print(const GChatter& chatter = NORMAL) const;
73 72  
74 73 protected:
... ... @@ -77,12 +76,12 @@ protected:
77 76 void copy_members(const GVOClient& client);
78 77 void free_members(void);
79 78 bool find_hub(void);
80   - void connect_to_hub(void);
81 79 void register_to_hub(void);
82 80 void unregister_from_hub(void);
83   - void send_metadata(void);
  81 + void send_metadata(void) const;
84 82  
85 83 // Low-level methods
  84 + void connect_to_hub(void) const;
86 85 void post_string(const std::string& string) const;
87 86 std::string receive_string(void) const;
88 87 std::string get_response_value(const GXml& xml, const std::string& name) const;
... ... @@ -103,7 +102,7 @@ protected:
103 102 std::string m_client_key; //!< Private client key
104 103 std::string m_hub_id; //!< Hub identifier
105 104 std::string m_client_id; //!< Client identifier
106   - int m_socket; //!< Hub socket
  105 + mutable int m_socket; //!< Hub socket
107 106 };
108 107  
109 108  
... ...
pyext/GVOClient.i
1 1 /***************************************************************************
2 2 * GVOClient.i - VO client class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2014 by Juergen Knoedlseder *
  4 + * copyright (C) 2014-2015 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -42,13 +42,13 @@ public:
42 42 virtual ~GVOClient(void);
43 43  
44 44 // Methods
45   - void clear(void);
46   - GVOClient* clone(void) const;
47   - void connect(void);
48   - void disconnect(void);
49   - bool has_hub(void) const;
50   - bool is_connected(void) const;
51   - GXml response(void) const;
  45 + void clear(void);
  46 + GVOClient* clone(void) const;
  47 + std::string classname(void) const;
  48 + void connect(void);
  49 + void disconnect(void);
  50 + bool has_hub(void) const;
  51 + GXml execute(const std::string& request) const;
52 52 };
53 53  
54 54  
... ...
src/vo/GVOClient.cpp
... ... @@ -223,33 +223,26 @@ bool GVOClient::has_hub(void) const
223 223  
224 224  
225 225 /***********************************************************************//**
226   - * @brief Signals if client is connected to Hub
  226 + * @brief Execute function on server
227 227 *
228   - * @return True if client is connected to Hub, false otherwise.
229   - *
230   - * Checks if a client is connected to Hub.
231   - ***************************************************************************/
232   -bool GVOClient::is_connected(void) const
233   -{
234   - // Return
235   - return false;
236   -}
237   -
238   -
239   -/***********************************************************************//**
240   - * @brief Returns Hub response a XML object
241   - *
242   - * @return Hub response.
  228 + * @param[in] request XML request string
  229 + * @return XML response
243 230 ***************************************************************************/
244   -GXml GVOClient::response(void) const
  231 +GXml GVOClient::execute(const std::string& request) const
245 232 {
246   - // Declare empty XML document
  233 + // Initialise response
247 234 GXml xml;
248 235  
  236 + // Connect to hub
  237 + connect_to_hub();
  238 +
249 239 // Continue only if connection has been established
250 240 if (m_socket != -1) {
251 241  
252   - // Receive a string
  242 + // Post request
  243 + post_string(request);
  244 +
  245 + // Receive response
253 246 std::string response = receive_string();
254 247  
255 248 // Find start of XML text
... ... @@ -262,7 +255,7 @@ GXml GVOClient::response(void) const
262 255  
263 256 } // endif: connection has been established
264 257  
265   - // Return XML document
  258 + // Return response
266 259 return xml;
267 260 }
268 261  
... ... @@ -521,6 +514,150 @@ bool GVOClient::find_hub(void)
521 514  
522 515  
523 516 /***********************************************************************//**
  517 + * @brief Register client at SAMP Hub
  518 + *
  519 + * @exception GException::invalid_value
  520 + * Unable to connect to Hub.
  521 + ***************************************************************************/
  522 +void GVOClient::register_to_hub(void)
  523 +{
  524 + // Declare request
  525 + std::string request;
  526 +
  527 + // Set metadata header
  528 + request.append("<?xml version=\"1.0\"?>\n");
  529 + request.append("<methodCall>\n");
  530 + request.append(" <methodName>samp.hub.register</methodName>\n");
  531 + request.append(" <params>\n");
  532 + request.append(" <param><value><string>"+m_secret+"</string></value></param>\n");
  533 + request.append(" </params>\n");
  534 + request.append("</methodCall>\n");
  535 +
  536 + // Execute request
  537 + GXml xml = execute(request);
  538 +
  539 + // If no error occured, extract hub and client identifiers
  540 + if (response_is_valid(xml)) {
  541 + m_client_key = get_response_value(xml, "samp.private-key");
  542 + m_hub_id = get_response_value(xml, "samp.hub-id");
  543 + m_client_id = get_response_value(xml, "samp.self-id");
  544 + }
  545 +
  546 + // ... otherwise signal an error
  547 + else {
  548 + std::string msg = "Unable to connect to Hub (" +
  549 + response_error_message(xml) + ", code=" +
  550 + gammalib::str(response_error_code(xml)) + ").";
  551 + throw GException::runtime_error(G_REGISTER_TO_HUB, msg);
  552 + }
  553 +
  554 + // Return
  555 + return;
  556 +}
  557 +
  558 +
  559 +/***********************************************************************//**
  560 + * @brief Unregister client from SAMP Hub
  561 + ***************************************************************************/
  562 +void GVOClient::unregister_from_hub(void)
  563 +{
  564 + // Declare request
  565 + std::string request;
  566 +
  567 + // Set metadata header
  568 + request.append("<?xml version=\"1.0\"?>\n");
  569 + request.append("<methodCall>\n");
  570 + request.append("<methodName>samp.hub.unregister</methodName>\n");
  571 + request.append("<params>\n");
  572 + request.append("<param><value><string>"+m_client_key+"</string></value></param>\n");
  573 + request.append("</params>\n");
  574 + request.append("</methodCall>\n");
  575 +
  576 + // Execute request
  577 + execute(request);
  578 +
  579 + // Reset Hub and client identifiers
  580 + m_client_key.clear();
  581 + m_hub_id.clear();
  582 + m_client_id.clear();
  583 +
  584 + // Return
  585 + return;
  586 +}
  587 +
  588 +
  589 +/***********************************************************************//**
  590 + * @brief Send client metadata to SAMP Hub
  591 + ***************************************************************************/
  592 +void GVOClient::send_metadata(void) const
  593 +{
  594 + // Declare request
  595 + std::string request;
  596 +
  597 + // Set metadata header
  598 + request.append("<?xml version=\"1.0\"?>\n");
  599 + request.append("<methodCall>\n");
  600 + request.append("<methodName>samp.hub.declareMetadata</methodName>\n");
  601 + request.append("<params>\n");
  602 + request.append("<param><value><string>"+m_client_key+"</string></value></param>\n");
  603 + request.append("<param><value><struct>\n");
  604 +
  605 + // Set SAMP name
  606 + request.append("<member>\n");
  607 + request.append("<name>samp.name</name>\n");
  608 + request.append("<value><string>"+m_name+"</string></value>\n");
  609 + request.append("</member>\n");
  610 +
  611 + // Set SAMP description text
  612 + request.append("<member>\n");
  613 + request.append("<name>samp.description.text</name>\n");
  614 + request.append("<value><string>GammaLib client</string></value>\n");
  615 + request.append("</member>\n");
  616 +
  617 + // Set SAMP icon URL
  618 + request.append("<member>\n");
  619 + request.append("<name>samp.icon.url</name>\n");
  620 + request.append("<value><string>http://a.fsdn.com/allura/p/gammalib/icon</string></value>\n");
  621 + request.append("</member>\n");
  622 +
  623 + // Set author affiliation
  624 + request.append("<member>\n");
  625 + request.append("<name>author.affiliation</name>\n");
  626 + request.append("<value><string>IRAP, Toulouse, France</string></value>\n");
  627 + request.append("</member>\n");
  628 +
  629 + // Set author e-mail
  630 + request.append("<member>\n");
  631 + request.append("<name>author.email</name>\n");
  632 + request.append("<value><string>jurgen.knodlseder@irap.omp.eu</string></value>\n");
  633 + request.append("</member>\n");
  634 +
  635 + // Set author name
  636 + request.append("<member>\n");
  637 + request.append("<name>author.name</name>\n");
  638 + request.append("<value><string>Juergen Knoedlseder</string></value>\n");
  639 + request.append("</member>\n");
  640 +
  641 + // Set metadata trailer
  642 + request.append("</struct></value></param>\n");
  643 + request.append("</params>\n");
  644 + request.append("</methodCall>\n");
  645 +
  646 + // Execute request
  647 + execute(request);
  648 +
  649 + // Return
  650 + return;
  651 +}
  652 +
  653 +
  654 +/*==========================================================================
  655 + = =
  656 + = Low-level private methods =
  657 + = =
  658 + ==========================================================================*/
  659 +
  660 +/***********************************************************************//**
524 661 * @brief Connect to SAMP Hub
525 662 *
526 663 * Connects to Hub by creating a socket and connecting to this socket. The
... ... @@ -533,7 +670,7 @@ bool GVOClient::find_hub(void)
533 670 * If the method is successful, m_socket will contain on exit a non-negative
534 671 * number. If any failure occurs, m_socket will be set to -1.
535 672 ***************************************************************************/
536   -void GVOClient::connect_to_hub(void)
  673 +void GVOClient::connect_to_hub(void) const
537 674 {
538 675 // Close any existing socket
539 676 if (m_socket != -1) {
... ... @@ -573,6 +710,9 @@ void GVOClient::connect_to_hub(void)
573 710 m_socket = -1;
574 711 }
575 712 }
  713 + else {
  714 + break;
  715 + }
576 716  
577 717 } // endfor: looped through all results
578 718  
... ... @@ -586,175 +726,95 @@ void GVOClient::connect_to_hub(void)
586 726  
587 727  
588 728 /***********************************************************************//**
589   - * @brief Register client at SAMP Hub
  729 + * @brief Post string content to Hub
590 730 *
591   - * @exception GException::invalid_value
592   - * Unable to connect to Hub.
  731 + * @param[in] content String content to post
  732 + *
  733 + * Posts the content of a string to the Hub.
  734 + *
  735 + * The method does nothing if no Hub connection has been established.
593 736 ***************************************************************************/
594   -void GVOClient::register_to_hub(void)
  737 +void GVOClient::post_string(const std::string& content) const
595 738 {
596   - // Connect to Hub
597   - connect_to_hub();
598   -
599 739 // Continue only if Hub connection has been established
600 740 if (m_socket != -1) {
601 741  
602   - // Declare message
603   - std::string msg = "";
604   -
605   - // Set metadata header
606   - msg.append("<?xml version=\"1.0\"?>\n");
607   - msg.append("<methodCall>\n");
608   - msg.append(" <methodName>samp.hub.register</methodName>\n");
609   - msg.append(" <params>\n");
610   - msg.append(" <param><value><string>"+m_secret+"</string></value></param>\n");
611   - msg.append(" </params>\n");
612   - msg.append("</methodCall>\n");
613   -
614   - // Post message
615   - post_string(msg);
616   -
617   - // Get Hub response
618   - GXml xml = response();
619   -
620   - // If no error occured, extract hub and client identifiers
621   - if (response_is_valid(xml)) {
622   - m_client_key = get_response_value(xml, "samp.private-key");
623   - m_hub_id = get_response_value(xml, "samp.hub-id");
624   - m_client_id = get_response_value(xml, "samp.self-id");
625   - }
  742 + // Determine content length
  743 + int length = content.length();
626 744  
627   - // ... otherwise signal an error
628   - else {
629   - std::string msg = "Unable to connect to Hub (" +
630   - response_error_message(xml) + ", code=" +
631   - gammalib::str(response_error_code(xml)) + ").";
632   - throw GException::runtime_error(G_REGISTER_TO_HUB, msg);
633   - }
  745 + // Set prefix
  746 + std::string prefix = "POST /"+m_hub_path+" HTTP/1.0\n"
  747 + "User-Agent: GammaLib\n"
  748 + "Content-Type: text/xml\n"
  749 + "Content-Length: "+gammalib::str(length)+"\n\n";
  750 +
  751 + // Build post string
  752 + std::string post = prefix + content;
  753 +
  754 + // Debug option: show posted message
  755 + #if defined(G_SHOW_MESSAGE)
  756 + std::cout << post << std::endl;
  757 + #endif
634 758  
635   - } // endif: Hub connection has been established
  759 + // Send content to socket
  760 + bool done = false;
  761 + do {
  762 + int length = post.length() + 1; // +1 for terminating 0
  763 + int sent_length = send(m_socket, post.c_str(), length+1, 0);
  764 + if (sent_length < length) {
  765 + post = post.substr(sent_length, std::string::npos);
  766 + }
  767 + else {
  768 + done = true;
  769 + }
  770 + } while (!done);
636 771  
  772 + } // endif: Hub connection had been established
  773 +
637 774 // Return
638 775 return;
639 776 }
640 777  
641 778  
642 779 /***********************************************************************//**
643   - * @brief Unregister client from SAMP Hub
  780 + * @brief Receive string content from Hub
  781 + *
  782 + * @return String received from Hub.
  783 + *
  784 + * Reads information sent by Hub into a string.
  785 + *
  786 + * The method does nothing if no Hub connection has been established.
644 787 ***************************************************************************/
645   -void GVOClient::unregister_from_hub(void)
  788 +std::string GVOClient::receive_string(void) const
646 789 {
647   - // Connect to Hub
648   - connect_to_hub();
  790 + // Initialise empty string
  791 + std::string result = "";
649 792  
650 793 // Continue only if Hub connection has been established
651 794 if (m_socket != -1) {
652 795  
653   - // Declare message
654   - std::string msg = "";
655   -
656   - // Set metadata header
657   - msg.append("<?xml version=\"1.0\"?>\n");
658   - msg.append("<methodCall>\n");
659   - msg.append("<methodName>samp.hub.unregister</methodName>\n");
660   - msg.append("<params>\n");
661   - msg.append("<param><value><string>"+m_client_key+"</string></value></param>\n");
662   - msg.append("</params>\n");
663   - msg.append("</methodCall>\n");
664   -
665   - // Post message
666   - post_string(msg);
667   -
668   - // Get Hub response
669   - GXml xml = response();
670   -
671   - // Reset Hub and client identifiers
672   - m_client_key.clear();
673   - m_hub_id.clear();
674   - m_client_id.clear();
675   -
676   - } // endif: Hub connection has been established
677   -
678   - // Return
679   - return;
680   -}
681   -
  796 + // Define buffer
  797 + char buffer[1001];
682 798  
683   -/***********************************************************************//**
684   - * @brief Send client metadata to Hub
685   - ***************************************************************************/
686   -void GVOClient::send_metadata(void)
687   -{
688   - // Connect to Hub. This was needed to post the metadata to the Hub.
689   - // Apparently, the connection is lost after each write/read cycle.
690   - // Maybe this is socket standard?
691   - connect_to_hub();
  799 + // Read buffer until it is empty
  800 + int n = 0;
  801 + do {
  802 + n = recv(m_socket, buffer, 1000, 0);
  803 + if (n > 0) {
  804 + buffer[n] = '\0';
  805 + result.append(std::string(buffer));
  806 + }
  807 + } while (n > 0);
692 808  
693   - // Continue only if Hub connection has been established
694   - if (m_socket != -1) {
  809 + // Debug option: show received message
  810 + #if defined(G_SHOW_MESSAGE)
  811 + std::cout << result << std::endl;
  812 + #endif
695 813  
696   - // Declare message
697   - std::string msg = "";
698   -
699   - // Set metadata header
700   - msg.append("<?xml version=\"1.0\"?>\n");
701   - msg.append("<methodCall>\n");
702   - msg.append("<methodName>samp.hub.declareMetadata</methodName>\n");
703   - msg.append("<params>\n");
704   - msg.append("<param><value><string>"+m_client_key+"</string></value></param>\n");
705   - msg.append("<param><value><struct>\n");
706   -
707   - // Set SAMP name
708   - msg.append("<member>\n");
709   - msg.append("<name>samp.name</name>\n");
710   - msg.append("<value><string>"+m_name+"</string></value>\n");
711   - msg.append("</member>\n");
712   -
713   - // Set SAMP description text
714   - msg.append("<member>\n");
715   - msg.append("<name>samp.description.text</name>\n");
716   - msg.append("<value><string>GammaLib client</string></value>\n");
717   - msg.append("</member>\n");
718   -
719   - // Set SAMP icon URL
720   - msg.append("<member>\n");
721   - msg.append("<name>samp.icon.url</name>\n");
722   - msg.append("<value><string>http://a.fsdn.com/allura/p/gammalib/icon</string></value>\n");
723   - msg.append("</member>\n");
724   -
725   - // Set author affiliation
726   - msg.append("<member>\n");
727   - msg.append("<name>author.affiliation</name>\n");
728   - msg.append("<value><string>IRAP, Toulouse, France</string></value>\n");
729   - msg.append("</member>\n");
730   -
731   - // Set author e-mail
732   - msg.append("<member>\n");
733   - msg.append("<name>author.email</name>\n");
734   - msg.append("<value><string>jurgen.knodlseder@irap.omp.eu</string></value>\n");
735   - msg.append("</member>\n");
736   -
737   - // Set author name
738   - msg.append("<member>\n");
739   - msg.append("<name>author.name</name>\n");
740   - msg.append("<value><string>Juergen Knoedlseder</string></value>\n");
741   - msg.append("</member>\n");
742   -
743   - // Set metadata trailer
744   - msg.append("</struct></value></param>\n");
745   - msg.append("</params>\n");
746   - msg.append("</methodCall>\n");
747   -
748   - // Post message
749   - post_string(msg);
750   -
751   - // Get Hub response
752   - GXml xml = response();
753   -
754   - } // endif: Hub connection has been established
  814 + } // endif: Hub connection had been established
755 815  
756   - // Return
757   - return;
  816 + // Return result
  817 + return result;
758 818 }
759 819  
760 820  
... ... @@ -842,99 +902,6 @@ void GVOClient::get_name_value_pair(const GXmlNode* node,
842 902  
843 903  
844 904 /***********************************************************************//**
845   - * @brief Post string content to Hub
846   - *
847   - * @param[in] content String content to post
848   - *
849   - * Posts the content of a string to the Hub.
850   - *
851   - * The method does nothing if no Hub connection has been established.
852   - ***************************************************************************/
853   -void GVOClient::post_string(const std::string& content) const
854   -{
855   - // Continue only if Hub connection has been established
856   - if (m_socket != -1) {
857   -
858   - // Determine content length
859   - int length = content.length();
860   -
861   - // Set prefix
862   - std::string prefix = "POST /"+m_hub_path+" HTTP/1.0\n"
863   - "User-Agent: GammaLib\n"
864   - "Content-Type: text/xml\n"
865   - "Content-Length: "+gammalib::str(length)+"\n\n";
866   -
867   - // Build post string
868   - std::string post = prefix + content;
869   -
870   - // Debug option: show posted message
871   - #if defined(G_SHOW_MESSAGE)
872   - std::cout << post << std::endl;
873   - #endif
874   -
875   - // Send content to socket
876   - bool done = false;
877   - do {
878   - int length = post.length();
879   - int sent_length = send(m_socket, post.c_str(), length, 0);
880   - if (sent_length < length) {
881   - post = post.substr(sent_length, std::string::npos);
882   - }
883   - else {
884   - done = true;
885   - }
886   - } while (!done);
887   -
888   - } // endif: Hub connection had been established
889   -
890   - // Return
891   - return;
892   -}
893   -
894   -
895   -/***********************************************************************//**
896   - * @brief Receive string content from Hub
897   - *
898   - * @return String received from Hub.
899   - *
900   - * Reads information sent by Hub into a string.
901   - *
902   - * The method does nothing if no Hub connection has been established.
903   - ***************************************************************************/
904   -std::string GVOClient::receive_string(void) const
905   -{
906   - // Initialise empty string
907   - std::string result = "";
908   -
909   - // Continue only if Hub connection has been established
910   - if (m_socket != -1) {
911   -
912   - // Define buffer
913   - char buffer[1001];
914   -
915   - // Read buffer until it is empty
916   - int n = 0;
917   - do {
918   - n = recv(m_socket, buffer, 1000, 0);
919   - if (n > 0) {
920   - buffer[n+1] = '\0';
921   - result.append(std::string(buffer));
922   - }
923   - } while (n > 0);
924   -
925   - // Debug option: show received message
926   - #if defined(G_SHOW_MESSAGE)
927   - std::cout << result << std::endl;
928   - #endif
929   -
930   - } // endif: Hub connection had been established
931   -
932   - // Return result
933   - return result;
934   -}
935   -
936   -
937   -/***********************************************************************//**
938 905 * @brief Returns SAMP Hub lockfile URL
939 906 *
940 907 * @return SAMP Hub lockfile URL (empty if no lockfile was found).
... ...