Commit 4533046c9ba80c102f8c3c0799692abedba558e8

Authored by Thierry Louge
1 parent a4413814

After GVOApp creation in order to keep track of every VO Application registered in the Hub

include/GVOApp.hpp 0 → 100644
  1 +/***************************************************************************
  2 + * GVOApp.hpp - VO SAMP Hub class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2014 by Thierry Louge *
  5 + * ----------------------------------------------------------------------- *
  6 + * *
  7 + * This program is free software: you can redistribute it and/or modify *
  8 + * it under the terms of the GNU General Public License as published by *
  9 + * the Free Software Foundation, either version 3 of the License, or *
  10 + * (at your option) any later version. *
  11 + * *
  12 + * This program is distributed in the hope that it will be useful, *
  13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15 + * GNU General Public License for more details. *
  16 + * *
  17 + * You should have received a copy of the GNU General Public License *
  18 + * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19 + * *
  20 + ***************************************************************************/
  21 +/**
  22 + * @file GVOApp.hpp
  23 + * @brief SAMP hub class interface definition
  24 + * @author Thierry Louge
  25 + */
  26 +
  27 +#ifndef GVOAPP_HPP
  28 +#define GVOAPP_HPP
  29 +
  30 +/* __ Includes ___________________________________________________________ */
  31 +#include <string>
  32 +#include <list>
  33 +#include "GBase.hpp"
  34 +#include "GXml.hpp"
  35 +#include "GXmlNode.hpp"
  36 +
  37 +
  38 +/***********************************************************************//**
  39 + * @class GVOApp
  40 + *
  41 + * @brief VO SAMP Hub class
  42 + *
  43 + * This class implements a SAMP hub for exchanges
  44 + * through VO-compatible applications.
  45 + ***************************************************************************/
  46 +class GVOApp : public GBase {
  47 +
  48 +public:
  49 + // Constructors and destructors
  50 + GVOApp(void);
  51 + GVOApp(const GVOApp& hub);
  52 + virtual ~GVOApp(void);
  53 +
  54 + // Operators
  55 + GVOApp& operator=(const GVOApp& hub);
  56 +
  57 + // Methods
  58 + void clear(void);
  59 + GVOApp* clone(void) const;
  60 + void start(void);
  61 + std::string print(const GChatter& chatter = NORMAL) const;
  62 + //void connect(void);
  63 + //void disconnect(void);
  64 + //bool has_hub(void) const;
  65 + //bool is_connected(void) const;
  66 + //GXml response(void) const;
  67 +
  68 +protected:
  69 + // Protected methods
  70 + void init_members(void);
  71 + void copy_members(const GVOApp& client);
  72 + void free_members(void);
  73 + void init_app(void);
  74 + void start_app(void);
  75 + void post_string(const std::string& string) const;
  76 + std::string receive_string(void) const;
  77 + std::string get_response_value(const GXml& xml, const std::string& name) const;
  78 + void get_name_value_pair(const GXmlNode* node, std::string& name, std::string& value) const;
  79 +
  80 + // Protected data area
  81 + std::string m_name; //!< Client name
  82 + std::string m__id; //!< Id assigned to client by the hub
  83 + std::list <std::string> registered_calls; //!< calls for which is registered the application.
  84 +
  85 +};
  86 +
  87 +#endif /* GVOAPP_HPP */
include/GVOHub.hpp
@@ -29,10 +29,12 @@ @@ -29,10 +29,12 @@
29 29
30 /* __ Includes ___________________________________________________________ */ 30 /* __ Includes ___________________________________________________________ */
31 #include <string> 31 #include <string>
  32 +#include <list>
32 #include <sys/socket.h> 33 #include <sys/socket.h>
33 #include "GBase.hpp" 34 #include "GBase.hpp"
34 #include "GXml.hpp" 35 #include "GXml.hpp"
35 #include "GXmlNode.hpp" 36 #include "GXmlNode.hpp"
  37 +#include "GVOApp.hpp"
36 38
37 39
38 /***********************************************************************//** 40 /***********************************************************************//**
@@ -79,7 +81,7 @@ protected: @@ -79,7 +81,7 @@ protected:
79 void unregister(const socklen_t& sock); 81 void unregister(const socklen_t& sock);
80 void handle_request(const socklen_t& sock); 82 void handle_request(const socklen_t& sock);
81 void register_metadata(const GXml& xml,const socklen_t& sock); 83 void register_metadata(const GXml& xml,const socklen_t& sock);
82 - 84 + void declare_subscriptions(const GXml& xml,const socklen_t& sock);
83 // Low-level methods 85 // Low-level methods
84 void post_string(const std::string& string) const; 86 void post_string(const std::string& string) const;
85 std::string receive_string(void) const; 87 std::string receive_string(void) const;
@@ -95,15 +97,13 @@ protected: @@ -95,15 +97,13 @@ protected:
95 std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint) 97 std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint)
96 std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub 98 std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub
97 std::string m_client_key; //!< Private client key 99 std::string m_client_key; //!< Private client key
98 - std::string m_hub_id; //!< Hub identifier  
99 - std::string m_client_id; //!< Client identifier 100 + std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others
  101 + //std::string m_client_id; //!< Client identifier
100 int m_socket; //!< Hub socket 102 int m_socket; //!< Hub socket
101 int m_nb_clients; //!< Number of already registered clients 103 int m_nb_clients; //!< Number of already registered clients
102 - struct client_metadata {  
103 - std::string client_id;  
104 - std::string client_name;  
105 - };  
106 - static client_metadata * Clients; //Array of structure containing clients id and names 104 + //std::vector<int> m_client_id; //!< Id assigned to client by the hub.
  105 + //std::vector<std::string> m_client_name;
  106 + std::list<GVOApp*> m_connected_apps; //!< list of apps connected to the hub
107 }; 107 };
108 108
109 #endif /* GVOHUB_HPP */ 109 #endif /* GVOHUB_HPP */
src/vo/GVOApp.cpp 0 → 100644
  1 +/***************************************************************************
  2 + * GVOApp.hpp - VO SAMP Hub class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2014 by Thierry Louge *
  5 + * ----------------------------------------------------------------------- *
  6 + * *
  7 + * This program is free software: you can redistribute it and/or modify *
  8 + * it under the terms of the GNU General Public License as published by *
  9 + * the Free Software Foundation, either version 3 of the License, or *
  10 + * (at your option) any later version. *
  11 + * *
  12 + * This program is distributed in the hope that it will be useful, *
  13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15 + * GNU General Public License for more details. *
  16 + * *
  17 + * You should have received a copy of the GNU General Public License *
  18 + * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19 + * *
  20 + ***************************************************************************/
  21 +/**
  22 + * @file GVOApp.cpp
  23 + * @brief VO Application connected to SAMP HUB class implementation
  24 + * @author Thierry Louge
  25 + */
  26 +
  27 +/* __ Includes ___________________________________________________________ */
  28 +#ifdef HAVE_CONFIG_H
  29 +#include <config.h>
  30 +#include <errno.h>
  31 +#endif
  32 +#include <cstdlib> // std::getenv() function
  33 +#include <cstdio> // std::fopen(), etc. functions
  34 +#include <cstring> // std::memset() function
  35 +#include <unistd.h> // close() function
  36 +#include "GVOApp.hpp"
  37 +
  38 +/* __ Method name definitions ____________________________________________ */
  39 +#define G_HANDLE_REQUEST "GVOApp::handle_request(socklen_t)"
  40 +#define G_START_HUB "GVOApp::start_hub()"
  41 +
  42 +/* __ Macros _____________________________________________________________ */
  43 +
  44 +/* __ Coding definitions _________________________________________________ */
  45 +
  46 +/* __ Debug definitions __________________________________________________ */
  47 +
  48 +
  49 +/*==========================================================================
  50 + = =
  51 + = Constructors/destructors =
  52 + = =
  53 + ==========================================================================*/
  54 +
  55 +/***********************************************************************//**
  56 + * @brief Void constructor
  57 + ***************************************************************************/
  58 +GVOApp::GVOApp(void)
  59 +{
  60 + // Initialise members
  61 + init_members();
  62 +
  63 + // Return
  64 + return;
  65 +}
  66 +
  67 +
  68 +/***********************************************************************//**
  69 + * @brief Copy constructor
  70 + *
  71 + * @param[in] hub VO Hub.
  72 + ***************************************************************************/
  73 +GVOApp::GVOApp(const GVOApp& hub)
  74 +{
  75 + // Initialise members
  76 + init_members();
  77 +
  78 + // Copy members
  79 + copy_members(hub);
  80 +
  81 + // Return
  82 + return;
  83 +}
  84 +
  85 +
  86 +/***********************************************************************//**
  87 + * @brief Destructor
  88 + ***************************************************************************/
  89 +GVOApp::~GVOApp(void)
  90 +{
  91 +
  92 + // Return
  93 + return;
  94 +}
  95 +
  96 +
  97 +/*==========================================================================
  98 + = =
  99 + = Operators =
  100 + = =
  101 + ==========================================================================*/
  102 +
  103 +/***********************************************************************//**
  104 + * @brief Assignment operator
  105 + *
  106 + * @param[in] hub VO hub.
  107 + * @return VO hub.
  108 + ***************************************************************************/
  109 +GVOApp& GVOApp::operator=(const GVOApp& hub)
  110 +{
  111 + // Execute only if object is not identical
  112 + if (this != &hub) {
  113 +
  114 + // Free members
  115 + free_members();
  116 +
  117 + // Initialise members
  118 + init_members();
  119 +
  120 + // Copy members
  121 + copy_members(hub);
  122 +
  123 + } // endif: object was not identical
  124 +
  125 + // Return
  126 + return *this;
  127 +}
  128 +
  129 +
  130 +/*==========================================================================
  131 + = =
  132 + = Public methods =
  133 + = =
  134 + ==========================================================================*/
  135 +
  136 +/***********************************************************************//**
  137 + * @brief Clear object.
  138 + *
  139 + * Reset object to a clean initial state.
  140 + ***************************************************************************/
  141 +void GVOApp::clear(void)
  142 +{
  143 + // Free memory and initialise members
  144 + free_members();
  145 + init_members();
  146 +
  147 + // Return
  148 + return;
  149 +}
  150 +
  151 +
  152 +/***********************************************************************//**
  153 + * @brief Clone object
  154 + ***************************************************************************/
  155 +GVOApp* GVOApp::clone(void) const
  156 +{
  157 + // Clone client
  158 + return new GVOApp(*this);
  159 +}
  160 +
  161 +
  162 +/***********************************************************************//**
  163 + * @brief Creates nex application container
  164 + ***************************************************************************/
  165 +void GVOApp::start_app(void)
  166 +{
  167 + // Creates Hub
  168 + printf("Creating new application Container\n");
  169 + init_app();
  170 +
  171 + // Return
  172 + return;
  173 +}
  174 +
src/vo/GVOHub.cpp
@@ -189,7 +189,7 @@ void GVOHub::start(void) @@ -189,7 +189,7 @@ void GVOHub::start(void)
189 ***************************************************************************/ 189 ***************************************************************************/
190 void GVOHub::ping_service(const socklen_t& sock) 190 void GVOHub::ping_service(const socklen_t& sock)
191 { 191 {
192 - printf("Registration request received\n"); 192 + printf("ping call received\n");
193 // Declare message 193 // Declare message
194 std::string msg = ""; 194 std::string msg = "";
195 195
@@ -218,15 +218,20 @@ void GVOHub::ping_service(const socklen_t&amp; sock) @@ -218,15 +218,20 @@ void GVOHub::ping_service(const socklen_t&amp; sock)
218 ***************************************************************************/ 218 ***************************************************************************/
219 void GVOHub::register_service(const GXml& xml, const socklen_t& sock) 219 void GVOHub::register_service(const GXml& xml, const socklen_t& sock)
220 { 220 {
221 - printf("Registration request received\n"); 221 +
  222 + printf("Registration request called\n");
  223 +
222 // Declare message 224 // Declare message
223 std::string msg = ""; 225 std::string msg = "";
  226 + char buffer[124];
224 std::string translator = "http://localhost:8001/"; 227 std::string translator = "http://localhost:8001/";
225 - //std::string client_name = get_response_value(xml, "samp.name"); 228 + printf("Message composition\n");
  229 + //m_client_id.push_back(m_nb_clients);
226 230
227 - //Clients[m_nb_clients].client_name = get_response_value(xml, "samp.name");  
228 - //printf("Client name %s",Clients[m_nb_clients].client_name.c_str());  
229 - //m_nb_clients ++; 231 + printf("Message composition 2 \n");
  232 + sprintf(buffer,"<member><name>samp.self-id</name><value><string>client-id:%d</string></value></member>\n",m_nb_clients);
  233 + printf(buffer);
  234 + m_nb_clients ++;
230 235
231 // Set methodResponse elts 236 // Set methodResponse elts
232 msg.append("<?xml version=\"1.0\"?>\n"); 237 msg.append("<?xml version=\"1.0\"?>\n");
@@ -234,15 +239,16 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock) @@ -234,15 +239,16 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
234 msg.append("\t<params>\n"); 239 msg.append("\t<params>\n");
235 msg.append("\t\t<param>\n\t\t\t<value>\n\t\t\t\t<struct>\n"); 240 msg.append("\t\t<param>\n\t\t\t<value>\n\t\t\t\t<struct>\n");
236 msg.append("<member><name>samp.private-key</name><value><string>client-key:" + m_hub_id + "</string></value></member>\n"); 241 msg.append("<member><name>samp.private-key</name><value><string>client-key:" + m_hub_id + "</string></value></member>\n");
237 - msg.append("<member><name>samp.hub-id</name><value><string>hub-id:" + m_hub_id + "</string></value></member>\n");  
238 - msg.append("<member><name>samp.self-id</name><value><string>client-id:" + m_client_id + "</string></value></member>\n"); 242 + msg.append("<member><name>samp.hub-id</name><value><string>client-id:" + m_hub_id + "</string></value></member>\n");
  243 + msg.append(buffer);
239 msg.append("<member><name>samp.url-translator</name><value><string>" + translator + "</string></value></member>\n"); 244 msg.append("<member><name>samp.url-translator</name><value><string>" + translator + "</string></value></member>\n");
240 msg.append("</struct></value></param>\n"); 245 msg.append("</struct></value></param>\n");
241 msg.append("</params>\n"); 246 msg.append("</params>\n");
242 msg.append("</methodResponse>\n"); 247 msg.append("</methodResponse>\n");
243 248
244 int length = msg.length(); 249 int length = msg.length();
245 - 250 + printf("Sending message %s\n",msg.c_str());
  251 + fflush(stdout);
246 int n = write(sock,msg.c_str(),length); 252 int n = write(sock,msg.c_str(),length);
247 if (n < 0) { 253 if (n < 0) {
248 printf("Could not write socket buffer.\n"); 254 printf("Could not write socket buffer.\n");
@@ -275,15 +281,20 @@ void GVOHub::unregister(const socklen_t&amp; sock) @@ -275,15 +281,20 @@ void GVOHub::unregister(const socklen_t&amp; sock)
275 ***************************************************************************/ 281 ***************************************************************************/
276 void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock) 282 void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock)
277 { 283 {
  284 + char buffer[124];
278 printf("Client Metadata received\n"); 285 printf("Client Metadata received\n");
279 // Search for metadata values to store 286 // Search for metadata values to store
280 std::string client_name = get_response_value(xml, "samp.name"); 287 std::string client_name = get_response_value(xml, "samp.name");
281 //std::cout("Client Metadata : \n" + client_name.c_str()); 288 //std::cout("Client Metadata : \n" + client_name.c_str());
282 printf("\nClient Metadata : Name %s\n", client_name.c_str()); 289 printf("\nClient Metadata : Name %s\n", client_name.c_str());
  290 + sprintf(buffer,"Client number: %d\n",m_nb_clients);
  291 + printf(buffer);
283 fflush(stdout); 292 fflush(stdout);
  293 + //m_client_name.push_back(client_name.c_str());
284 // Declare message 294 // Declare message
285 std::string msg = ""; 295 std::string msg = "";
286 296
  297 + //The hub response to Metadata declaration has no usefull content and can be discarded by the client.
287 // Set methodResponse elts 298 // Set methodResponse elts
288 msg.append("<?xml version=\"1.0\"?>\n"); 299 msg.append("<?xml version=\"1.0\"?>\n");
289 msg.append("<methodResponse>\n"); 300 msg.append("<methodResponse>\n");
@@ -302,7 +313,37 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock) @@ -302,7 +313,37 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
302 //Return 313 //Return
303 return; 314 return;
304 } 315 }
  316 +/***********************************************************************//**
  317 + * @brief
  318 + *
  319 + *
  320 + ***************************************************************************/
  321 +void GVOHub::declare_subscriptions(const GXml& xml,const socklen_t& sock)
  322 +{
  323 + printf("Declare Subscriptions\n");
  324 + std::string client_subscriptions = get_response_value(xml, "samp.hub.event.subscriptions");
  325 + printf("\nClient subscriptions: %s\n", client_subscriptions.c_str());
  326 + // Declare message
  327 + std::string msg = "";
305 328
  329 + // Set methodResponse elts
  330 + msg.append("<?xml version=\"1.0\"?>\n");
  331 + msg.append("<methodResponse>\n");
  332 + msg.append("</methodResponse>\n");
  333 +
  334 + int length = msg.length();
  335 +
  336 + int n = write(sock,msg.c_str(),length);
  337 + if (n < 0) {
  338 + printf("Could not write socket buffer.\n");
  339 + std::string msg = "Could not write socket buffer.";
  340 + throw GException::invalid_value(G_HANDLE_REQUEST, msg);
  341 + }
  342 +
  343 + printf("Response message sent\n");
  344 + //Return
  345 + return;
  346 +}
306 /***********************************************************************//** 347 /***********************************************************************//**
307 * @brief Print VO hub information 348 * @brief Print VO hub information
308 * 349 *
@@ -336,7 +377,7 @@ std::string GVOHub::print(const GChatter&amp; chatter) const @@ -336,7 +377,7 @@ std::string GVOHub::print(const GChatter&amp; chatter) const
336 } 377 }
337 else { 378 else {
338 if (!m_client_key.empty()) { 379 if (!m_client_key.empty()) {
339 - result.append("registered as \""+m_client_id); 380 + //result.append("registered as \""+m_client_id);
340 result.append("\" on Hub \""+m_hub_id); 381 result.append("\" on Hub \""+m_hub_id);
341 result.append("\" via socket "+gammalib::str(m_socket)); 382 result.append("\" via socket "+gammalib::str(m_socket));
342 } 383 }
@@ -373,12 +414,10 @@ void GVOHub::init_members(void) @@ -373,12 +414,10 @@ void GVOHub::init_members(void)
373 m_version = "1.3"; 414 m_version = "1.3";
374 m_client_key.clear(); 415 m_client_key.clear();
375 m_hub_id = "b79884e0"; 416 m_hub_id = "b79884e0";
376 - m_client_id.clear(); 417 + //m_client_id.clear();
377 m_socket = -1; // Signals no socket 418 m_socket = -1; // Signals no socket
378 m_nb_clients = 0; // No registered clients at initialization 419 m_nb_clients = 0; // No registered clients at initialization
379 - //client_metadata * Clients = new client_metadata[5];  
380 // Return 420 // Return
381 - Clients = new client_metadata[5];  
382 return; 421 return;
383 } 422 }
384 423
@@ -399,7 +438,7 @@ void GVOHub::copy_members(const GVOHub&amp; hub) @@ -399,7 +438,7 @@ void GVOHub::copy_members(const GVOHub&amp; hub)
399 m_version = hub.m_version; 438 m_version = hub.m_version;
400 m_client_key = hub.m_client_key; 439 m_client_key = hub.m_client_key;
401 m_hub_id = hub.m_hub_id; 440 m_hub_id = hub.m_hub_id;
402 - m_client_id = hub.m_client_id; 441 + //m_client_id = hub.m_client_id;
403 m_socket = hub.m_socket; 442 m_socket = hub.m_socket;
404 443
405 // Return 444 // Return
@@ -575,7 +614,7 @@ void GVOHub::handle_request(const socklen_t&amp; sock) @@ -575,7 +614,7 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
575 ping_service(sock); 614 ping_service(sock);
576 } 615 }
577 if (method_called.compare("samp.hub.declareSubscriptions") == 0) { 616 if (method_called.compare("samp.hub.declareSubscriptions") == 0) {
578 - ping_service(sock); 617 + declare_subscriptions(xml,sock);
579 } 618 }
580 if (method_called.compare("samp.hub.getSubscriptions") == 0) { 619 if (method_called.compare("samp.hub.getSubscriptions") == 0) {
581 ping_service(sock); 620 ping_service(sock);
src/vo/Makefile.am
@@ -7,7 +7,8 @@ INCLUDES = -I$(top_srcdir)/include \ @@ -7,7 +7,8 @@ INCLUDES = -I$(top_srcdir)/include \
7 7
8 # Define sources for this directory 8 # Define sources for this directory
9 sources = GVOClient.cpp \ 9 sources = GVOClient.cpp \
10 - GVOHub.cpp 10 + GVOHub.cpp \
  11 + GVOApp.cpp
11 12
12 # Build libtool library 13 # Build libtool library
13 noinst_LTLIBRARIES = libvo.la 14 noinst_LTLIBRARIES = libvo.la