Commit ae33eadc8f7c6079cc267b130c431b9421e98f86
1 parent
ecd33dfb
Adapt to GammaLib coding conventions, derive from GBase, implement clear(), clon…
…e(), start() and print() methods and add a C++ unit test to test_GVO.
Showing
5 changed files
with
380 additions
and
123 deletions
include/GVOHub.hpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | - * GVOHub.hpp - VO SAMP Hub class * | 2 | + * GVOHub.hpp - VO SAMP Hub class * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2014 by Juergen Knoedlseder & Thierry Louge * | 4 | + * copyright (C) 2014 by Thierry Louge * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -21,7 +21,7 @@ | @@ -21,7 +21,7 @@ | ||
| 21 | /** | 21 | /** |
| 22 | * @file GVOHub.hpp | 22 | * @file GVOHub.hpp |
| 23 | * @brief SAMP hub class interface definition | 23 | * @brief SAMP hub class interface definition |
| 24 | - * @author Juergen Knoedlseder, Thierry Louge | 24 | + * @author Thierry Louge |
| 25 | */ | 25 | */ |
| 26 | 26 | ||
| 27 | #ifndef GVOHUB_HPP | 27 | #ifndef GVOHUB_HPP |
| @@ -43,38 +43,40 @@ | @@ -43,38 +43,40 @@ | ||
| 43 | * This class implements a SAMP hub for exchanges | 43 | * This class implements a SAMP hub for exchanges |
| 44 | * through VO-compatible applications. | 44 | * through VO-compatible applications. |
| 45 | ***************************************************************************/ | 45 | ***************************************************************************/ |
| 46 | -class GVOHub { | 46 | +class GVOHub : public GBase { |
| 47 | 47 | ||
| 48 | public: | 48 | public: |
| 49 | // Constructors and destructors | 49 | // Constructors and destructors |
| 50 | GVOHub(void); | 50 | GVOHub(void); |
| 51 | - //GVOHub(const GVOHub& hub); | 51 | + GVOHub(const GVOHub& hub); |
| 52 | virtual ~GVOHub(void); | 52 | virtual ~GVOHub(void); |
| 53 | 53 | ||
| 54 | // Operators | 54 | // Operators |
| 55 | GVOHub& operator=(const GVOHub& hub); | 55 | GVOHub& operator=(const GVOHub& hub); |
| 56 | 56 | ||
| 57 | // Methods | 57 | // Methods |
| 58 | - //void clear(void); | ||
| 59 | - //GVOHub* clone(void) const; | 58 | + void clear(void); |
| 59 | + GVOHub* clone(void) const; | ||
| 60 | + void start(void); | ||
| 61 | + std::string print(const GChatter& chatter = NORMAL) const; | ||
| 60 | //void connect(void); | 62 | //void connect(void); |
| 61 | //void disconnect(void); | 63 | //void disconnect(void); |
| 62 | //bool has_hub(void) const; | 64 | //bool has_hub(void) const; |
| 63 | //bool is_connected(void) const; | 65 | //bool is_connected(void) const; |
| 64 | //GXml response(void) const; | 66 | //GXml response(void) const; |
| 65 | - //std::string print(const GChatter& chatter = NORMAL) const; | ||
| 66 | 67 | ||
| 67 | protected: | 68 | protected: |
| 68 | // Protected methods | 69 | // Protected methods |
| 69 | void init_members(void); | 70 | void init_members(void); |
| 70 | - void init_hub(void); | ||
| 71 | - int start_hub(void); | ||
| 72 | - //void free_members(void); | 71 | + void copy_members(const GVOHub& client); |
| 72 | + void free_members(void); | ||
| 73 | + void init_hub(void); | ||
| 74 | + void start_hub(void); | ||
| 73 | //bool find_hub(void); | 75 | //bool find_hub(void); |
| 74 | //void connect_to_hub(void); | 76 | //void connect_to_hub(void); |
| 75 | void register_service(void); | 77 | void register_service(void); |
| 76 | void unregister(void); | 78 | void unregister(void); |
| 77 | - void handle_request(socklen_t); | 79 | + void handle_request(const socklen_t& sock); |
| 78 | //void send_metadata(void); | 80 | //void send_metadata(void); |
| 79 | 81 | ||
| 80 | // Low-level methods | 82 | // Low-level methods |
pyext/GVOHub.i
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | - * GVOHub.i - VO Hub class * | 2 | + * GVOHub.i - VO SAMP Hub class * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2014 by Juergen Knoedlseder * | 4 | + * copyright (C) 2014 by Thierry Louge * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -20,8 +20,8 @@ | @@ -20,8 +20,8 @@ | ||
| 20 | ***************************************************************************/ | 20 | ***************************************************************************/ |
| 21 | /** | 21 | /** |
| 22 | * @file GVOHub.i | 22 | * @file GVOHub.i |
| 23 | - * @brief VO Hub class interface definition | ||
| 24 | - * @author Juergen Knoedlseder | 23 | + * @brief SAMP hub class interface definition |
| 24 | + * @author Thierry Louge | ||
| 25 | */ | 25 | */ |
| 26 | %{ | 26 | %{ |
| 27 | /* Put headers and other declarations here that are needed for compilation */ | 27 | /* Put headers and other declarations here that are needed for compilation */ |
| @@ -39,12 +39,12 @@ public: | @@ -39,12 +39,12 @@ public: | ||
| 39 | // Constructors and destructors | 39 | // Constructors and destructors |
| 40 | // Constructors and destructors | 40 | // Constructors and destructors |
| 41 | GVOHub(void); | 41 | GVOHub(void); |
| 42 | - //GVOHub(const GVOHub& hub); | 42 | + GVOHub(const GVOHub& hub); |
| 43 | virtual ~GVOHub(void); | 43 | virtual ~GVOHub(void); |
| 44 | 44 | ||
| 45 | // Methods | 45 | // Methods |
| 46 | - //void clear(void); | ||
| 47 | - //GVOHub* clone(void) const; | 46 | + void clear(void); |
| 47 | + GVOHub* clone(void) const; | ||
| 48 | //void connect(void); | 48 | //void connect(void); |
| 49 | //void disconnect(void); | 49 | //void disconnect(void); |
| 50 | //bool has_hub(void) const; | 50 | //bool has_hub(void) const; |
src/vo/GVOHub.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | - * GVOHub.hpp - VO SAMP Hub class * | 2 | + * GVOHub.hpp - VO SAMP Hub class * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2014 by Juergen Knoedlseder & Thierry Louge * | 4 | + * copyright (C) 2014 by Thierry Louge * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -21,7 +21,7 @@ | @@ -21,7 +21,7 @@ | ||
| 21 | /** | 21 | /** |
| 22 | * @file GVOHub.cpp | 22 | * @file GVOHub.cpp |
| 23 | * @brief VO SAMP Hub class implementation | 23 | * @brief VO SAMP Hub class implementation |
| 24 | - * @author Juergen Knoedlseder, Thierry Louge | 24 | + * @author Thierry Louge |
| 25 | */ | 25 | */ |
| 26 | 26 | ||
| 27 | /* __ Includes ___________________________________________________________ */ | 27 | /* __ Includes ___________________________________________________________ */ |
| @@ -38,6 +38,18 @@ | @@ -38,6 +38,18 @@ | ||
| 38 | #include "GVOClient.hpp" | 38 | #include "GVOClient.hpp" |
| 39 | #include "GException.hpp" | 39 | #include "GException.hpp" |
| 40 | #include "GTools.hpp" | 40 | #include "GTools.hpp" |
| 41 | + | ||
| 42 | +/* __ Method name definitions ____________________________________________ */ | ||
| 43 | +#define G_HANDLE_REQUEST "GVOHub::handle_request(socklen_t)" | ||
| 44 | +#define G_START_HUB "GVOHub::start_hub()" | ||
| 45 | + | ||
| 46 | +/* __ Macros _____________________________________________________________ */ | ||
| 47 | + | ||
| 48 | +/* __ Coding definitions _________________________________________________ */ | ||
| 49 | + | ||
| 50 | +/* __ Debug definitions __________________________________________________ */ | ||
| 51 | + | ||
| 52 | + | ||
| 41 | /*========================================================================== | 53 | /*========================================================================== |
| 42 | = = | 54 | = = |
| 43 | = Constructors/destructors = | 55 | = Constructors/destructors = |
| @@ -50,21 +62,30 @@ | @@ -50,21 +62,30 @@ | ||
| 50 | GVOHub::GVOHub(void) | 62 | GVOHub::GVOHub(void) |
| 51 | { | 63 | { |
| 52 | // Initialise members | 64 | // Initialise members |
| 53 | - printf("Initializing members\n"); | ||
| 54 | init_members(); | 65 | init_members(); |
| 55 | 66 | ||
| 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 | 67 | // Return |
| 64 | return; | 68 | return; |
| 65 | } | 69 | } |
| 66 | 70 | ||
| 67 | 71 | ||
| 72 | +/***********************************************************************//** | ||
| 73 | + * @brief Copy constructor | ||
| 74 | + * | ||
| 75 | + * @param[in] hub VO Hub. | ||
| 76 | + ***************************************************************************/ | ||
| 77 | +GVOHub::GVOHub(const GVOHub& hub) | ||
| 78 | +{ | ||
| 79 | + // Initialise members | ||
| 80 | + init_members(); | ||
| 81 | + | ||
| 82 | + // Copy members | ||
| 83 | + copy_members(hub); | ||
| 84 | + | ||
| 85 | + // Return | ||
| 86 | + return; | ||
| 87 | +} | ||
| 88 | + | ||
| 68 | 89 | ||
| 69 | /***********************************************************************//** | 90 | /***********************************************************************//** |
| 70 | * @brief Destructor | 91 | * @brief Destructor |
| @@ -79,11 +100,88 @@ GVOHub::~GVOHub(void) | @@ -79,11 +100,88 @@ GVOHub::~GVOHub(void) | ||
| 79 | 100 | ||
| 80 | /*========================================================================== | 101 | /*========================================================================== |
| 81 | = = | 102 | = = |
| 103 | + = Operators = | ||
| 104 | + = = | ||
| 105 | + ==========================================================================*/ | ||
| 106 | + | ||
| 107 | +/***********************************************************************//** | ||
| 108 | + * @brief Assignment operator | ||
| 109 | + * | ||
| 110 | + * @param[in] hub VO hub. | ||
| 111 | + * @return VO hub. | ||
| 112 | + ***************************************************************************/ | ||
| 113 | +GVOHub& GVOHub::operator=(const GVOHub& hub) | ||
| 114 | +{ | ||
| 115 | + // Execute only if object is not identical | ||
| 116 | + if (this != &hub) { | ||
| 117 | + | ||
| 118 | + // Free members | ||
| 119 | + free_members(); | ||
| 120 | + | ||
| 121 | + // Initialise members | ||
| 122 | + init_members(); | ||
| 123 | + | ||
| 124 | + // Copy members | ||
| 125 | + copy_members(hub); | ||
| 126 | + | ||
| 127 | + } // endif: object was not identical | ||
| 128 | + | ||
| 129 | + // Return | ||
| 130 | + return *this; | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | + | ||
| 134 | +/*========================================================================== | ||
| 135 | + = = | ||
| 82 | = Public methods = | 136 | = Public methods = |
| 83 | = = | 137 | = = |
| 84 | ==========================================================================*/ | 138 | ==========================================================================*/ |
| 85 | 139 | ||
| 86 | /***********************************************************************//** | 140 | /***********************************************************************//** |
| 141 | + * @brief Clear object. | ||
| 142 | + * | ||
| 143 | + * Reset object to a clean initial state. | ||
| 144 | + ***************************************************************************/ | ||
| 145 | +void GVOHub::clear(void) | ||
| 146 | +{ | ||
| 147 | + // Free memory and initialise members | ||
| 148 | + free_members(); | ||
| 149 | + init_members(); | ||
| 150 | + | ||
| 151 | + // Return | ||
| 152 | + return; | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | + | ||
| 156 | +/***********************************************************************//** | ||
| 157 | + * @brief Clone object | ||
| 158 | + ***************************************************************************/ | ||
| 159 | +GVOHub* GVOHub::clone(void) const | ||
| 160 | +{ | ||
| 161 | + // Clone client | ||
| 162 | + return new GVOHub(*this); | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | + | ||
| 166 | +/***********************************************************************//** | ||
| 167 | + * @brief Start Hub | ||
| 168 | + ***************************************************************************/ | ||
| 169 | +void GVOHub::start(void) | ||
| 170 | +{ | ||
| 171 | + // Creates Hub | ||
| 172 | + printf("Creating the hub\n"); | ||
| 173 | + init_hub(); | ||
| 174 | + | ||
| 175 | + // Starts the hub | ||
| 176 | + printf("Starting the hub\n"); | ||
| 177 | + start_hub(); | ||
| 178 | + | ||
| 179 | + // Return | ||
| 180 | + return; | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | + | ||
| 184 | +/***********************************************************************//** | ||
| 87 | * @brief | 185 | * @brief |
| 88 | * | 186 | * |
| 89 | * | 187 | * |
| @@ -95,6 +193,7 @@ void GVOHub::register_service(void) | @@ -95,6 +193,7 @@ void GVOHub::register_service(void) | ||
| 95 | return; | 193 | return; |
| 96 | } | 194 | } |
| 97 | 195 | ||
| 196 | + | ||
| 98 | /***********************************************************************//** | 197 | /***********************************************************************//** |
| 99 | * @brief | 198 | * @brief |
| 100 | * | 199 | * |
| @@ -107,31 +206,119 @@ void GVOHub::unregister(void) | @@ -107,31 +206,119 @@ void GVOHub::unregister(void) | ||
| 107 | return; | 206 | return; |
| 108 | } | 207 | } |
| 109 | 208 | ||
| 209 | + | ||
| 210 | +/***********************************************************************//** | ||
| 211 | + * @brief Print VO hub information | ||
| 212 | + * | ||
| 213 | + * @param[in] chatter Chattiness (defaults to NORMAL). | ||
| 214 | + * @return String containing VO hub information | ||
| 215 | + ***************************************************************************/ | ||
| 216 | +std::string GVOHub::print(const GChatter& chatter) const | ||
| 217 | +{ | ||
| 218 | + // Initialise result string | ||
| 219 | + std::string result; | ||
| 220 | + | ||
| 221 | + // Continue only if chatter is not silent | ||
| 222 | + if (chatter != SILENT) { | ||
| 223 | + | ||
| 224 | + // Append header | ||
| 225 | + result.append("=== GVOHub ==="); | ||
| 226 | + | ||
| 227 | + // Append client information | ||
| 228 | + result.append("\n"+gammalib::parformat("Name")+m_name); | ||
| 229 | + | ||
| 230 | + // Append Hub information | ||
| 231 | + result.append("\n"+gammalib::parformat("Hub key")+m_secret); | ||
| 232 | + result.append("\n"+gammalib::parformat("Hub URL")+m_hub_url); | ||
| 233 | + result.append("\n"+gammalib::parformat("Hub host (port)")); | ||
| 234 | + result.append(m_hub_host+" ("+m_hub_port+")"); | ||
| 235 | + result.append("\n"+gammalib::parformat("SAMP protocol version")); | ||
| 236 | + result.append(m_version); | ||
| 237 | + result.append("\n"+gammalib::parformat("Hub connection")); | ||
| 238 | + if (m_socket == -1) { | ||
| 239 | + result.append("no"); | ||
| 240 | + } | ||
| 241 | + else { | ||
| 242 | + if (!m_client_key.empty()) { | ||
| 243 | + result.append("registered as \""+m_client_id); | ||
| 244 | + result.append("\" on Hub \""+m_hub_id); | ||
| 245 | + result.append("\" via socket "+gammalib::str(m_socket)); | ||
| 246 | + } | ||
| 247 | + else { | ||
| 248 | + result.append("established on socket "); | ||
| 249 | + result.append(gammalib::str(m_socket)); | ||
| 250 | + } | ||
| 251 | + } | ||
| 252 | + | ||
| 253 | + } // endif: chatter was not silent | ||
| 254 | + | ||
| 255 | + // Return result | ||
| 256 | + return result; | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | + | ||
| 110 | /*========================================================================== | 260 | /*========================================================================== |
| 111 | = = | 261 | = = |
| 112 | = Private methods = | 262 | = Private methods = |
| 113 | = = | 263 | = = |
| 114 | ==========================================================================*/ | 264 | ==========================================================================*/ |
| 265 | + | ||
| 115 | /***********************************************************************//** | 266 | /***********************************************************************//** |
| 116 | * @brief Initialise class members | 267 | * @brief Initialise class members |
| 117 | ***************************************************************************/ | 268 | ***************************************************************************/ |
| 118 | void GVOHub::init_members(void) | 269 | void GVOHub::init_members(void) |
| 119 | { | 270 | { |
| 120 | // Initialise members | 271 | // Initialise members |
| 121 | - m_name = "GammaLib"; | ||
| 122 | - m_secret = "mysupersecret#0032557sentence"; | ||
| 123 | - m_hub_url = "http://localhost:8001"; | 272 | + m_name = "GammaLib"; |
| 273 | + m_secret = "mysupersecret#0032557sentence"; | ||
| 274 | + m_hub_url = "http://localhost:8001"; | ||
| 124 | m_hub_host = "127.0.0.1"; | 275 | m_hub_host = "127.0.0.1"; |
| 125 | m_hub_port = "8001"; | 276 | 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 | 277 | + m_version = "1.3"; |
| 278 | + m_client_key.clear(); | ||
| 279 | + m_hub_id = "b79884e0"; | ||
| 280 | + m_client_id.clear(); | ||
| 281 | + m_socket = -1; // Signals no socket | ||
| 282 | + | ||
| 283 | + // Return | ||
| 284 | + return; | ||
| 285 | +} | ||
| 286 | + | ||
| 287 | + | ||
| 288 | +/***********************************************************************//** | ||
| 289 | + * @brief Copy class members | ||
| 290 | + * | ||
| 291 | + * @param[in] hub VO hub. | ||
| 292 | + ***************************************************************************/ | ||
| 293 | +void GVOHub::copy_members(const GVOHub& hub) | ||
| 294 | +{ | ||
| 295 | + // Copy members | ||
| 296 | + m_name = hub.m_name; | ||
| 297 | + m_secret = hub.m_secret; | ||
| 298 | + m_hub_url = hub.m_hub_url; | ||
| 299 | + m_hub_host = hub.m_hub_host; | ||
| 300 | + m_hub_port = hub.m_hub_port; | ||
| 301 | + m_version = hub.m_version; | ||
| 302 | + m_client_key = hub.m_client_key; | ||
| 303 | + m_hub_id = hub.m_hub_id; | ||
| 304 | + m_client_id = hub.m_client_id; | ||
| 305 | + m_socket = hub.m_socket; | ||
| 306 | + | ||
| 307 | + // Return | ||
| 308 | + return; | ||
| 309 | +} | ||
| 131 | 310 | ||
| 311 | + | ||
| 312 | +/***********************************************************************//** | ||
| 313 | + * @brief Delete class members | ||
| 314 | + ***************************************************************************/ | ||
| 315 | +void GVOHub::free_members(void) | ||
| 316 | +{ | ||
| 132 | // Return | 317 | // Return |
| 133 | return; | 318 | return; |
| 134 | } | 319 | } |
| 320 | + | ||
| 321 | + | ||
| 135 | /***********************************************************************//** | 322 | /***********************************************************************//** |
| 136 | * @brief Creates the lockfile, fill it | 323 | * @brief Creates the lockfile, fill it |
| 137 | * | 324 | * |
| @@ -140,36 +327,42 @@ void GVOHub::init_members(void) | @@ -140,36 +327,42 @@ void GVOHub::init_members(void) | ||
| 140 | ***************************************************************************/ | 327 | ***************************************************************************/ |
| 141 | void GVOHub::init_hub(void) | 328 | void GVOHub::init_hub(void) |
| 142 | { | 329 | { |
| 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); | 330 | + // Get lockfile URL |
| 331 | + std::string lockurl = get_hub_lockfile(); | ||
| 332 | + | ||
| 333 | + // Open SAMP lockfile. Continue only if opening was successful | ||
| 334 | + FILE* fptr = fopen(lockurl.c_str(), "w"); | ||
| 335 | + if (fptr != NULL) { | ||
| 336 | + | ||
| 337 | + // If successfull writes basic hub configuration to the lockfile | ||
| 338 | + fputs ("# SAMP lockfile \n# Required keys:\n",fptr); | ||
| 339 | + char sampsecret[256] = "samp.secret= "; | ||
| 340 | + strcat(sampsecret,m_secret.c_str()); | ||
| 341 | + strcat(sampsecret,"\n"); | ||
| 342 | + fputs (sampsecret,fptr); | ||
| 343 | + char sampurl[256] = "samp.hub.xmlrpc.url="; | ||
| 344 | + strcat(sampurl,m_hub_url.c_str()); | ||
| 345 | + strcat(sampurl,"\n"); | ||
| 346 | + fputs (sampurl,fptr); | ||
| 347 | + char samprofile[256] = "samp.profile.version="; | ||
| 348 | + strcat(samprofile,m_version.c_str()); | ||
| 349 | + strcat(samprofile,"\n"); | ||
| 350 | + fputs (samprofile,fptr); | ||
| 351 | + fputs ("# Info stored by hub for some private reason:\n",fptr); | ||
| 352 | + char sampid[256] = "gammalib.hubid="; | ||
| 353 | + strcat(sampid,m_hub_id.c_str()); | ||
| 354 | + strcat(sampid,"\n"); | ||
| 355 | + fputs (sampid,fptr); | ||
| 356 | + } | ||
| 357 | + | ||
| 358 | + // Close SAMP lockfile | ||
| 359 | + fclose(fptr); | ||
| 360 | + | ||
| 361 | + // Return | ||
| 362 | + return; | ||
| 171 | } | 363 | } |
| 172 | 364 | ||
| 365 | + | ||
| 173 | /***********************************************************************//** | 366 | /***********************************************************************//** |
| 174 | * @brief Returns SAMP Hub lockfile URL | 367 | * @brief Returns SAMP Hub lockfile URL |
| 175 | * | 368 | * |
| @@ -220,48 +413,52 @@ std::string GVOHub::get_hub_lockfile(void) const | @@ -220,48 +413,52 @@ std::string GVOHub::get_hub_lockfile(void) const | ||
| 220 | return url; | 413 | return url; |
| 221 | } | 414 | } |
| 222 | 415 | ||
| 416 | + | ||
| 223 | /***********************************************************************//** | 417 | /***********************************************************************//** |
| 224 | - * @brief reads the client message and runs appropriate function | ||
| 225 | - * | ||
| 226 | - * @return 0 if no problem, 1 if error comes out. | 418 | + * @brief Reads the client message and runs appropriate function |
| 227 | * | 419 | * |
| 420 | + * @exception GExpection::invalid_value | ||
| 421 | + * Buffer could not be read or written | ||
| 228 | ***************************************************************************/ | 422 | ***************************************************************************/ |
| 229 | -void GVOHub::handle_request (socklen_t sock) | 423 | +void GVOHub::handle_request(const socklen_t& sock) |
| 230 | { | 424 | { |
| 231 | - int n; | 425 | + // Initialize buffer |
| 232 | char buffer[256]; | 426 | char buffer[256]; |
| 233 | - | ||
| 234 | bzero(buffer,256); | 427 | bzero(buffer,256); |
| 235 | 428 | ||
| 236 | - n = read(sock,buffer,255); | ||
| 237 | - /*if (n < 0) | ||
| 238 | - { | ||
| 239 | - return(1); | ||
| 240 | - }*/ | 429 | + // Read buffer |
| 430 | + int n = read(sock,buffer,255); | ||
| 431 | + if (n < 0) { | ||
| 432 | + std::string msg = "Could not read socket buffer."; | ||
| 433 | + throw GException::invalid_value(G_HANDLE_REQUEST, msg); | ||
| 434 | + } | ||
| 241 | printf("Here is the message: %s\n",buffer); | 435 | printf("Here is the message: %s\n",buffer); |
| 242 | 436 | ||
| 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. | 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. | ||
| 245 | if (strcmp(buffer,"register") == 0) { | 439 | if (strcmp(buffer,"register") == 0) { |
| 246 | - register_service(); | 440 | + register_service(); |
| 247 | } | 441 | } |
| 248 | - //Same for writing, we'll use GXml | 442 | + |
| 443 | + // Same for writing, we'll use GXml | ||
| 249 | n = write(sock,"I got your message",18); | 444 | n = write(sock,"I got your message",18); |
| 250 | - /* | ||
| 251 | -if (n < 0) | ||
| 252 | - { | ||
| 253 | - return(1); | 445 | + if (n < 0) { |
| 446 | + std::string msg = "Could not write socket buffer."; | ||
| 447 | + throw GException::invalid_value(G_HANDLE_REQUEST, msg); | ||
| 254 | } | 448 | } |
| 255 | - return(0); | ||
| 256 | - */ | 449 | + |
| 450 | + // Return | ||
| 451 | + return; | ||
| 257 | } | 452 | } |
| 453 | + | ||
| 454 | + | ||
| 258 | /***********************************************************************//** | 455 | /***********************************************************************//** |
| 259 | * @brief Starts the SAMP hub socket and listens on it | 456 | * @brief Starts the SAMP hub socket and listens on it |
| 260 | * | 457 | * |
| 261 | * @return 1 if error comes out, nothing if everything is right (infinite listening loop) | 458 | * @return 1 if error comes out, nothing if everything is right (infinite listening loop) |
| 262 | * | 459 | * |
| 263 | ***************************************************************************/ | 460 | ***************************************************************************/ |
| 264 | -int GVOHub::start_hub(void) | 461 | +void GVOHub::start_hub(void) |
| 265 | { | 462 | { |
| 266 | struct sockaddr_in serv_addr,cli_addr; | 463 | struct sockaddr_in serv_addr,cli_addr; |
| 267 | int portno,pid; | 464 | int portno,pid; |
| @@ -269,60 +466,56 @@ int GVOHub::start_hub(void) | @@ -269,60 +466,56 @@ int GVOHub::start_hub(void) | ||
| 269 | 466 | ||
| 270 | // Prepare TCP/IP structure | 467 | // Prepare TCP/IP structure |
| 271 | bzero((char *) &serv_addr, sizeof(serv_addr)); | 468 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 272 | - portno = 8001; | ||
| 273 | - serv_addr.sin_family = AF_INET; | 469 | + portno = 8001; |
| 470 | + serv_addr.sin_family = AF_INET; | ||
| 274 | serv_addr.sin_addr.s_addr = INADDR_ANY; | 471 | 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); | 472 | + serv_addr.sin_port = htons(portno); |
| 473 | + hub_socket = socket(AF_INET, SOCK_STREAM, 0); | ||
| 474 | + if (hub_socket < 0) { | ||
| 475 | + std::string msg = "Socket could not be created."; | ||
| 476 | + throw GException::invalid_value(G_START_HUB, msg); | ||
| 280 | } | 477 | } |
| 281 | 478 | ||
| 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); | 479 | + // Server socket is opened. Now, bind it to the port, with family etc. |
| 480 | + if (bind(hub_socket, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { | ||
| 481 | + std::string msg = "Could not bind to server socket."; | ||
| 482 | + throw GException::invalid_value(G_START_HUB, msg); | ||
| 286 | } | 483 | } |
| 287 | 484 | ||
| 288 | - //Now start listening for the clients: 5 requests simultaneously maximum | 485 | + // Now start listening for the clients: 5 requests simultaneously maximum |
| 289 | listen(hub_socket,5); | 486 | listen(hub_socket,5); |
| 290 | clilen = sizeof(cli_addr); | 487 | clilen = sizeof(cli_addr); |
| 291 | - while (1) | ||
| 292 | - { | 488 | + while (1) { |
| 489 | + | ||
| 293 | // Accept actual connection from the client | 490 | // Accept actual connection from the client |
| 491 | +//std::cout << "Now accept connection the socket" << std::endl; | ||
| 294 | newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen); | 492 | newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen); |
| 295 | - if (newsocket < 0) | ||
| 296 | - { | ||
| 297 | - return(1); | 493 | +//std::cout << "Connection accepted" << std::endl; |
| 494 | + if (newsocket < 0) { | ||
| 495 | + std::string msg = "Client connection to socket not accepted."; | ||
| 496 | + throw GException::invalid_value(G_START_HUB, msg); | ||
| 298 | } | 497 | } |
| 498 | + | ||
| 299 | // Create child process to handle the request | 499 | // Create child process to handle the request |
| 300 | pid = fork(); | 500 | pid = fork(); |
| 301 | - if (pid < 0) | ||
| 302 | - { | ||
| 303 | - return(1); | 501 | +//std::cout << "Called fork " << pid << std::endl; |
| 502 | + if (pid < 0) { | ||
| 503 | + std::string msg = "Not child process created."; | ||
| 504 | + throw GException::invalid_value(G_START_HUB, msg); | ||
| 304 | } | 505 | } |
| 305 | - if (pid == 0) | ||
| 306 | - { | 506 | + if (pid == 0) { |
| 307 | // Child process: client process | 507 | // Child process: client process |
| 308 | handle_request(newsocket); | 508 | handle_request(newsocket); |
| 309 | exit(0); | 509 | exit(0); |
| 310 | } | 510 | } |
| 311 | - else | ||
| 312 | - { | 511 | + else { |
| 512 | +//std::cout << "Now close the socket" << std::endl; | ||
| 313 | close(newsocket); | 513 | close(newsocket); |
| 514 | +//std::cout << "Socket closed" << std::endl; | ||
| 314 | } | 515 | } |
| 516 | + | ||
| 315 | } | 517 | } |
| 316 | -} | ||
| 317 | 518 | ||
| 318 | -/***********************************************************************//** | ||
| 319 | - * @brief For testing purposes only | ||
| 320 | - * | ||
| 321 | - * | ||
| 322 | - ***************************************************************************/ | ||
| 323 | -int main() | ||
| 324 | -{ | ||
| 325 | - GVOHub* myhub = new GVOHub; | ||
| 326 | - return 0; | 519 | + // Return |
| 520 | + return; | ||
| 327 | } | 521 | } |
| 328 | - |
test/test_GVO.cpp
| @@ -28,6 +28,7 @@ | @@ -28,6 +28,7 @@ | ||
| 28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include <config.h> | 29 | #include <config.h> |
| 30 | #endif | 30 | #endif |
| 31 | +#include <pthread.h> | ||
| 31 | #include "test_GVO.hpp" | 32 | #include "test_GVO.hpp" |
| 32 | #include "GTools.hpp" | 33 | #include "GTools.hpp" |
| 33 | 34 | ||
| @@ -35,12 +36,29 @@ | @@ -35,12 +36,29 @@ | ||
| 35 | /***********************************************************************//** | 36 | /***********************************************************************//** |
| 36 | * @brief Set parameters and tests | 37 | * @brief Set parameters and tests |
| 37 | **************************************************************************/ | 38 | **************************************************************************/ |
| 39 | +void* vo_thread(void* ptr) | ||
| 40 | +{ | ||
| 41 | + // Create VO Hub | ||
| 42 | + GVOHub hub; | ||
| 43 | + | ||
| 44 | + // Start Hub. We will never return from this ... | ||
| 45 | + hub.start(); | ||
| 46 | + | ||
| 47 | + // Return | ||
| 48 | + return NULL; | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | + | ||
| 52 | +/***********************************************************************//** | ||
| 53 | + * @brief Set parameters and tests | ||
| 54 | + **************************************************************************/ | ||
| 38 | void TestGVO::set(void) | 55 | void TestGVO::set(void) |
| 39 | { | 56 | { |
| 40 | // Test name | 57 | // Test name |
| 41 | name("GVO"); | 58 | name("GVO"); |
| 42 | 59 | ||
| 43 | // Append tests | 60 | // Append tests |
| 61 | + append(static_cast<pfunction>(&TestGVO::test_GVOHub), "Test GHub class"); | ||
| 44 | append(static_cast<pfunction>(&TestGVO::test_GVOClient), "Test GVOClient class"); | 62 | append(static_cast<pfunction>(&TestGVO::test_GVOClient), "Test GVOClient class"); |
| 45 | 63 | ||
| 46 | // Return | 64 | // Return |
| @@ -61,6 +79,49 @@ TestGVO* TestGVO::clone(void) const | @@ -61,6 +79,49 @@ TestGVO* TestGVO::clone(void) const | ||
| 61 | 79 | ||
| 62 | 80 | ||
| 63 | /***********************************************************************//** | 81 | /***********************************************************************//** |
| 82 | + * @brief Test VO hub class | ||
| 83 | + **************************************************************************/ | ||
| 84 | +void TestGVO::test_GVOHub(void) | ||
| 85 | +{ | ||
| 86 | + // Declare thread | ||
| 87 | + pthread_t thread; | ||
| 88 | + | ||
| 89 | + // Start Hub | ||
| 90 | + test_try("Start hub"); | ||
| 91 | + try { | ||
| 92 | + int rc = pthread_create(&thread, NULL, vo_thread, NULL); | ||
| 93 | + test_try_success(); | ||
| 94 | + } | ||
| 95 | + catch (std::exception &e) { | ||
| 96 | + test_try_failure(e); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + // Test Hub connection | ||
| 100 | + test_try("GVOClient hub connection and disconnection"); | ||
| 101 | + try { | ||
| 102 | + for (int i = 0; i < 3; ++i) { | ||
| 103 | + GVOClient client; | ||
| 104 | + client.connect(); | ||
| 105 | + if (client.is_connected()) { | ||
| 106 | + break; | ||
| 107 | + } | ||
| 108 | + std::cout << client << std::endl; | ||
| 109 | + sleep(1); | ||
| 110 | + } | ||
| 111 | + test_try_failure(); | ||
| 112 | + //test_try_success(); | ||
| 113 | + } | ||
| 114 | + catch (std::exception &e) { | ||
| 115 | + test_try_success(); | ||
| 116 | + //test_try_failure(e); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + // Return | ||
| 120 | + return; | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | + | ||
| 124 | +/***********************************************************************//** | ||
| 64 | * @brief Test VO client class | 125 | * @brief Test VO client class |
| 65 | **************************************************************************/ | 126 | **************************************************************************/ |
| 66 | void TestGVO::test_GVOClient(void) | 127 | void TestGVO::test_GVOClient(void) |
test/test_GVO.hpp
| @@ -46,6 +46,7 @@ public: | @@ -46,6 +46,7 @@ public: | ||
| 46 | // Methods | 46 | // Methods |
| 47 | virtual void set(void); | 47 | virtual void set(void); |
| 48 | virtual TestGVO* clone(void) const; | 48 | virtual TestGVO* clone(void) const; |
| 49 | + void test_GVOHub(void); | ||
| 49 | void test_GVOClient(void); | 50 | void test_GVOClient(void); |
| 50 | 51 | ||
| 51 | private: | 52 | private: |