Commit ecd33dfb26c5a28296fa510b7555063c788ed3f2

Authored by Thierry Louge
1 parent c7cf0410

Initial version of GVOHub

include/GVOHub.hpp 0 → 100644
  1 +/***************************************************************************
  2 + * GVOHub.hpp - VO SAMP Hub class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2014 by Juergen Knoedlseder & 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 GVOHub.hpp
  23 + * @brief SAMP hub class interface definition
  24 + * @author Juergen Knoedlseder, Thierry Louge
  25 + */
  26 +
  27 +#ifndef GVOHUB_HPP
  28 +#define GVOHUB_HPP
  29 +
  30 +/* __ Includes ___________________________________________________________ */
  31 +#include <string>
  32 +#include <sys/socket.h>
  33 +#include "GBase.hpp"
  34 +#include "GXml.hpp"
  35 +#include "GXmlNode.hpp"
  36 +
  37 +
  38 +/***********************************************************************//**
  39 + * @class GVOHub
  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 GVOHub {
  47 +
  48 +public:
  49 + // Constructors and destructors
  50 + GVOHub(void);
  51 + //GVOHub(const GVOHub& hub);
  52 + virtual ~GVOHub(void);
  53 +
  54 + // Operators
  55 + GVOHub& operator=(const GVOHub& hub);
  56 +
  57 + // Methods
  58 + //void clear(void);
  59 + //GVOHub* clone(void) const;
  60 + //void connect(void);
  61 + //void disconnect(void);
  62 + //bool has_hub(void) const;
  63 + //bool is_connected(void) const;
  64 + //GXml response(void) const;
  65 + //std::string print(const GChatter& chatter = NORMAL) const;
  66 +
  67 +protected:
  68 + // Protected methods
  69 + void init_members(void);
  70 + void init_hub(void);
  71 + int start_hub(void);
  72 + //void free_members(void);
  73 + //bool find_hub(void);
  74 + //void connect_to_hub(void);
  75 + void register_service(void);
  76 + void unregister(void);
  77 + void handle_request(socklen_t);
  78 + //void send_metadata(void);
  79 +
  80 + // Low-level methods
  81 + void post_string(const std::string& string) const;
  82 + std::string receive_string(void) const;
  83 + std::string get_response_value(const GXml& xml, const std::string& name) const;
  84 + void get_name_value_pair(const GXmlNode* node, std::string& name, std::string& value) const;
  85 + std::string get_hub_lockfile(void) const;
  86 +
  87 + // Protected data area
  88 + std::string m_name; //!< Client name
  89 + std::string m_secret; //!< Secret Hub key
  90 + std::string m_hub_url; //!< The XML-RPC endpoint for communication with the hub
  91 + std::string m_hub_host; //!< Hub host (extracted from XML-RPC endpoint)
  92 + std::string m_hub_port; //!< Hub port (extracted from XML-RPC endpoint)
  93 + std::string m_version; //!< The version of the SAMP Standard Profile implemented by the hub
  94 + std::string m_client_key; //!< Private client key
  95 + std::string m_hub_id; //!< Hub identifier
  96 + std::string m_client_id; //!< Client identifier
  97 + int m_socket; //!< Hub socket
  98 +};
  99 +
  100 +#endif /* GVOHUB_HPP */
... ...
include/GammaLib.hpp
... ... @@ -106,6 +106,7 @@
106 106  
107 107 /* __ VO module __________________________________________________________ */
108 108 #include "GVOClient.hpp"
  109 +#include "GVOHub.hpp"
109 110  
110 111 /* __ XSPEC module _______________________________________________________ */
111 112 #include "GArf.hpp"
... ...
include/Makefile.am
... ... @@ -61,6 +61,7 @@ pkginclude_HEADERS = GammaLib.hpp \
61 61 GXmlAttribute.hpp \
62 62 GXmlPI.hpp \
63 63 GVOClient.hpp \
  64 + GVOHub.hpp \
64 65 GPha.hpp \
65 66 GArf.hpp \
66 67 GRmf.hpp \
... ...
pyext/GVOHub.i 0 → 100644
  1 +/***************************************************************************
  2 + * GVOHub.i - VO Hub class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2014 by Juergen Knoedlseder *
  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 GVOHub.i
  23 + * @brief VO Hub class interface definition
  24 + * @author Juergen Knoedlseder
  25 + */
  26 +%{
  27 +/* Put headers and other declarations here that are needed for compilation */
  28 +#include "GVOHub.hpp"
  29 +%}
  30 +
  31 +
  32 +/***********************************************************************//**
  33 + * @class GVOHub
  34 + *
  35 + * @brief VO Hub class
  36 + ***************************************************************************/
  37 +class GVOHub {
  38 +public:
  39 + // Constructors and destructors
  40 + // Constructors and destructors
  41 + GVOHub(void);
  42 + //GVOHub(const GVOHub& hub);
  43 + virtual ~GVOHub(void);
  44 +
  45 + // Methods
  46 + //void clear(void);
  47 + //GVOHub* clone(void) const;
  48 + //void connect(void);
  49 + //void disconnect(void);
  50 + //bool has_hub(void) const;
  51 + //bool is_connected(void) const;
  52 + //GXml response(void) const;
  53 +};
  54 +
  55 +
  56 +/***********************************************************************//**
  57 + * @brief GVOHub class extension
  58 + ***************************************************************************/
  59 +/*
  60 +%extend GVOHub {
  61 + GVOHub copy() {
  62 + return (*self);
  63 + }
  64 +};
  65 +*/
  66 +
... ...
pyext/gammalib/vo.i
... ... @@ -48,3 +48,4 @@
48 48  
49 49 /* __ Virtual Observatory module _________________________________________ */
50 50 %include "GVOClient.i"
  51 +%include "GVOHub.i"
... ...
src/vo/GVOHub.cpp 0 → 100644
  1 +/***************************************************************************
  2 + * GVOHub.hpp - VO SAMP Hub class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2014 by Juergen Knoedlseder & 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 GVOHub.cpp
  23 + * @brief VO SAMP Hub class implementation
  24 + * @author Juergen Knoedlseder, Thierry Louge
  25 + */
  26 +
  27 +/* __ Includes ___________________________________________________________ */
  28 +#ifdef HAVE_CONFIG_H
  29 +#include <config.h>
  30 +#endif
  31 +#include <cstdlib> // std::getenv() function
  32 +#include <cstdio> // std::fopen(), etc. functions
  33 +#include <cstring> // std::memset() function
  34 +#include <unistd.h> // close() function
  35 +#include <netdb.h> // getaddrinfo() function
  36 +#include <sys/socket.h> // socket(), connect() functions
  37 +#include "GVOHub.hpp"
  38 +#include "GVOClient.hpp"
  39 +#include "GException.hpp"
  40 +#include "GTools.hpp"
  41 +/*==========================================================================
  42 + = =
  43 + = Constructors/destructors =
  44 + = =
  45 + ==========================================================================*/
  46 +
  47 +/***********************************************************************//**
  48 + * @brief Void constructor
  49 + ***************************************************************************/
  50 +GVOHub::GVOHub(void)
  51 +{
  52 + // Initialise members
  53 + printf("Initializing members\n");
  54 + init_members();
  55 +
  56 + // Creates Hub
  57 + printf("Creating the hub\n");
  58 + init_hub();
  59 +
  60 + // Starts the hub
  61 + printf("Starting the hub");
  62 + start_hub();
  63 + // Return
  64 + return;
  65 +}
  66 +
  67 +
  68 +
  69 +/***********************************************************************//**
  70 + * @brief Destructor
  71 + ***************************************************************************/
  72 +GVOHub::~GVOHub(void)
  73 +{
  74 +
  75 + // Return
  76 + return;
  77 +}
  78 +
  79 +
  80 +/*==========================================================================
  81 + = =
  82 + = Public methods =
  83 + = =
  84 + ==========================================================================*/
  85 +
  86 +/***********************************************************************//**
  87 + * @brief
  88 + *
  89 + *
  90 + ***************************************************************************/
  91 +void GVOHub::register_service(void)
  92 +{
  93 +
  94 + // Return
  95 + return;
  96 +}
  97 +
  98 +/***********************************************************************//**
  99 + * @brief
  100 + *
  101 + *
  102 + ***************************************************************************/
  103 +void GVOHub::unregister(void)
  104 +{
  105 +
  106 + // Return
  107 + return;
  108 +}
  109 +
  110 +/*==========================================================================
  111 + = =
  112 + = Private methods =
  113 + = =
  114 + ==========================================================================*/
  115 +/***********************************************************************//**
  116 + * @brief Initialise class members
  117 + ***************************************************************************/
  118 +void GVOHub::init_members(void)
  119 +{
  120 + // Initialise members
  121 + m_name = "GammaLib";
  122 + m_secret = "mysupersecret#0032557sentence";
  123 + m_hub_url = "http://localhost:8001";
  124 + m_hub_host = "127.0.0.1";
  125 + m_hub_port = "8001";
  126 + m_version = "1.3";
  127 + //m_client_key.clear();
  128 + m_hub_id = "b79884e0";
  129 + //m_client_id.clear();
  130 + //m_socket = -1; // Signals no socket
  131 +
  132 + // Return
  133 + return;
  134 +}
  135 +/***********************************************************************//**
  136 + * @brief Creates the lockfile, fill it
  137 + *
  138 + *
  139 + * Implements IVOA standard REC-SAMP-1.3-20120411.
  140 + ***************************************************************************/
  141 +void GVOHub::init_hub(void)
  142 +{
  143 + // Get lockfile URL
  144 + std::string lockurl = get_hub_lockfile();
  145 +
  146 + // Open SAMP lockfile. Continue only if opening was successful
  147 + FILE* fptr = fopen(lockurl.c_str(), "w");
  148 + if (fptr != NULL) {
  149 + //If successfull writes basic hub configuration to the lockfile
  150 + fputs ("# SAMP lockfile \n# Required keys:\n",fptr);
  151 + char sampsecret[256] = "samp.secret= ";
  152 + strcat(sampsecret,m_secret.c_str());
  153 + strcat(sampsecret,"\n");
  154 + fputs (sampsecret,fptr);
  155 + char sampurl[256] = "samp.hub.xmlrpc.url=";
  156 + strcat(sampurl,m_hub_url.c_str());
  157 + strcat(sampurl,"\n");
  158 + fputs (sampurl,fptr);
  159 + char samprofile[256] = "samp.profile.version=";
  160 + strcat(samprofile,m_version.c_str());
  161 + strcat(samprofile,"\n");
  162 + fputs (samprofile,fptr);
  163 + fputs ("# Info stored by hub for some private reason:\n",fptr);
  164 + char sampid[256] = "gammalib.hubid=";
  165 + strcat(sampid,m_hub_id.c_str());
  166 + strcat(sampid,"\n");
  167 + fputs (sampid,fptr);
  168 + }
  169 + // Close SAMP lockfile
  170 + fclose(fptr);
  171 +}
  172 +
  173 +/***********************************************************************//**
  174 + * @brief Returns SAMP Hub lockfile URL
  175 + *
  176 + * @return SAMP Hub lockfile URL (empty if no lockfile was found).
  177 + *
  178 + * Implements IVOA standard REC-SAMP-1.3-20120411.
  179 + ***************************************************************************/
  180 +std::string GVOHub::get_hub_lockfile(void) const
  181 +{
  182 + // Initialise result
  183 + std::string url = "";
  184 +
  185 + // Check for existence of the SAMP_HUB environment variable first
  186 + char* hub_ptr = std::getenv("SAMP_HUB");
  187 + if (hub_ptr != NULL) {
  188 +
  189 + // Check for mandatory std-lockurl: prefix (no other prefixe is
  190 + // supported so far)
  191 + std::string lockurl = std::string(hub_ptr);
  192 + if (lockurl.compare(0, 12, "std-lockurl:") == 0) {
  193 +
  194 + // Extract URL
  195 + url = lockurl.substr(12, std::string::npos);
  196 +
  197 + } // endif: std-lockurl: prefix found
  198 +
  199 + }
  200 +
  201 + // ... otherwise the lockfile should be $HOME/.samp
  202 + else {
  203 +
  204 + // Get user's HOME directory path as the prefix of the full
  205 + // path. If the HOME environment variable is not set we
  206 + // expect that .samp is in the local directory. This is non
  207 + // standard, but prevents for creating an exception here.
  208 + std::string prefix = "";
  209 + char* home_ptr = std::getenv("HOME");
  210 + if (home_ptr != NULL) {
  211 + prefix = std::string(home_ptr) + "/";
  212 + }
  213 +
  214 + // Set filename
  215 + url = prefix + ".samp";
  216 +
  217 + } // endelse: no SAMP_HUB environment variable found
  218 +
  219 + // Return URL
  220 + return url;
  221 +}
  222 +
  223 +/***********************************************************************//**
  224 + * @brief reads the client message and runs appropriate function
  225 + *
  226 + * @return 0 if no problem, 1 if error comes out.
  227 + *
  228 + ***************************************************************************/
  229 +void GVOHub::handle_request (socklen_t sock)
  230 +{
  231 + int n;
  232 + char buffer[256];
  233 +
  234 + bzero(buffer,256);
  235 +
  236 + n = read(sock,buffer,255);
  237 + /*if (n < 0)
  238 + {
  239 + return(1);
  240 + }*/
  241 + printf("Here is the message: %s\n",buffer);
  242 +
  243 + //Here, obviously not simple string matching but xml parsing to be done with
  244 + //help of GXml object. This is only for architectural purpose.
  245 + if (strcmp(buffer,"register") == 0) {
  246 + register_service();
  247 + }
  248 + //Same for writing, we'll use GXml
  249 + n = write(sock,"I got your message",18);
  250 + /*
  251 +if (n < 0)
  252 + {
  253 + return(1);
  254 + }
  255 + return(0);
  256 + */
  257 +}
  258 +/***********************************************************************//**
  259 + * @brief Starts the SAMP hub socket and listens on it
  260 + *
  261 + * @return 1 if error comes out, nothing if everything is right (infinite listening loop)
  262 + *
  263 + ***************************************************************************/
  264 +int GVOHub::start_hub(void)
  265 +{
  266 + struct sockaddr_in serv_addr,cli_addr;
  267 + int portno,pid;
  268 + socklen_t hub_socket,newsocket,sockfd,clilen;
  269 +
  270 + // Prepare TCP/IP structure
  271 + bzero((char *) &serv_addr, sizeof(serv_addr));
  272 + portno = 8001;
  273 + serv_addr.sin_family = AF_INET;
  274 + serv_addr.sin_addr.s_addr = INADDR_ANY;
  275 + serv_addr.sin_port = htons(portno);
  276 + hub_socket = socket(AF_INET, SOCK_STREAM, 0);
  277 + if (hub_socket < 0)
  278 + {
  279 + return(1);
  280 + }
  281 +
  282 + //Server socket is opened. Now, bind it to the port, with family etc.
  283 + if (bind(hub_socket, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  284 + {
  285 + return(1);
  286 + }
  287 +
  288 + //Now start listening for the clients: 5 requests simultaneously maximum
  289 + listen(hub_socket,5);
  290 + clilen = sizeof(cli_addr);
  291 + while (1)
  292 + {
  293 + // Accept actual connection from the client
  294 + newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen);
  295 + if (newsocket < 0)
  296 + {
  297 + return(1);
  298 + }
  299 + // Create child process to handle the request
  300 + pid = fork();
  301 + if (pid < 0)
  302 + {
  303 + return(1);
  304 + }
  305 + if (pid == 0)
  306 + {
  307 + // Child process: client process
  308 + handle_request(newsocket);
  309 + exit(0);
  310 + }
  311 + else
  312 + {
  313 + close(newsocket);
  314 + }
  315 + }
  316 +}
  317 +
  318 +/***********************************************************************//**
  319 + * @brief For testing purposes only
  320 + *
  321 + *
  322 + ***************************************************************************/
  323 +int main()
  324 +{
  325 + GVOHub* myhub = new GVOHub;
  326 + return 0;
  327 +}
  328 +
... ...
src/vo/Makefile.am
... ... @@ -6,7 +6,8 @@ INCLUDES = -I$(top_srcdir)/include \
6 6 -I$(top_srcdir)/src/support
7 7  
8 8 # Define sources for this directory
9   -sources = GVOClient.cpp
  9 +sources = GVOClient.cpp \
  10 + GVOHub.cpp
10 11  
11 12 # Build libtool library
12 13 noinst_LTLIBRARIES = libvo.la
... ...