From 912684d70b279369173ae594c9c94abc78e76114 Mon Sep 17 00:00:00 2001 From: Thierry Louge Date: Wed, 13 May 2015 16:36:54 +0200 Subject: [PATCH] Afetr debug of fully SAMP compliant Hub --- .plastic | 6 ++++++ include/GVOHub.hpp | 21 +++++++++++---------- pyext/GVOHub.i | 1 - src/vo/GVOHub.cpp | 514 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------- 4 files changed, 406 insertions(+), 136 deletions(-) create mode 100644 .plastic diff --git a/.plastic b/.plastic new file mode 100644 index 0000000..1576f5f --- /dev/null +++ b/.plastic @@ -0,0 +1,6 @@ +#PLASTIC server uk.ac.starlink.plastic.ServerSet +#Tue May 12 17:16:26 CEST 2015 +plastic.xmlrpc.url=http\://STAR-VORS\:2112/ +uk.ac.starlink.plastic.servid=uk.ac.starlink.plastic.ServerSet@b61fd1 +plastic.version=0.4 +plastic.rmi.port=1099 diff --git a/include/GVOHub.hpp b/include/GVOHub.hpp index 7524388..2a1d360 100644 --- a/include/GVOHub.hpp +++ b/include/GVOHub.hpp @@ -28,11 +28,13 @@ #define GVOHUB_HPP #define GVOHUB_NB_CLIENTS 5 #define GVOHUB_NB_METHODS 128 +#define GVO_HUB_testing 1 /* __ Includes ___________________________________________________________ */ #include #include #include +#include #include "GBase.hpp" #include "GXml.hpp" #include "GXmlNode.hpp" @@ -61,7 +63,6 @@ public: // Methods void clear(void); GVOHub* clone(void) const; - void start(void); std::string print(const GChatter& chatter = NORMAL) const; @@ -70,7 +71,7 @@ protected: void init_members(void); void copy_members(const GVOHub& client); void free_members(void); - void init_hub(void); + void create_samp_file(void); void start_hub(void); void register_service(const GXml& xml,const socklen_t& sock); void ping_service(const socklen_t& sock); @@ -102,7 +103,8 @@ protected: std::string& value) const; std::string get_hub_lockfile(void) const; std::string random_string( size_t length ); - void intHandler(int sig); + void activate_callbacks(std::string method,char cl_id[31]); + void post_string_toclient(const std::string& content) const; // Protected members std::string m_name; //!< Client name @@ -114,33 +116,32 @@ protected: std::string m_client_key; //!< Private client key std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others int m_socket; //!< Hub socket + int cback_socket; //!< Hub socket to callback clients int m_nb_clients; //!< Number of already registered clients typedef struct { char private_key[16]; char name[32]; char reference[32]; - char description[32]; + char description[256]; char icon[32]; char documentation[32]; char affiliation[32]; char author_name[32]; char email[32]; char homepage[32]; - char port[16]; - char registered_methods[GVOHUB_NB_METHODS][128]; //128 methods of 128 cars max. + char port[32]; + char registered_methods[GVOHUB_NB_METHODS][GVOHUB_NB_METHODS]; //128 methods of 128 cars max. } connected_shm; typedef struct { int registered; + sem_t lock; connected_shm metadatas[GVOHUB_NB_CLIENTS]; } clients_descriptor; clients_descriptor *reg_clients; connected_shm *clients; void* ptr_mem_partagee; //pointer to shm address int shm_handler; //handler to shm - static std::vector& connected() { - static std::vector m_connected_apps; //!< list of apps connected to the hub - return m_connected_apps; - } + }; #endif /* GVOHUB_HPP */ diff --git a/pyext/GVOHub.i b/pyext/GVOHub.i index f381124..5e455a6 100644 --- a/pyext/GVOHub.i +++ b/pyext/GVOHub.i @@ -44,7 +44,6 @@ public: // Methods void clear(void); GVOHub* clone(void) const; - void start(void); }; diff --git a/src/vo/GVOHub.cpp b/src/vo/GVOHub.cpp index 0703f61..1d74997 100644 --- a/src/vo/GVOHub.cpp +++ b/src/vo/GVOHub.cpp @@ -47,9 +47,11 @@ #include #include + /* __ Method name definitions ____________________________________________ */ #define G_HANDLE_REQUEST "GVOHub::handle_request(socklen_t)" #define G_START_HUB "GVOHub::start_hub()" +#define G_INIT_MEMBERS "GVOHub::init_members()" /* __ Macros _____________________________________________________________ */ @@ -101,7 +103,7 @@ GVOHub::GVOHub(const GVOHub& hub) ***************************************************************************/ GVOHub::~GVOHub(void) { - + free_members(); // Return return; } @@ -173,24 +175,6 @@ GVOHub* GVOHub::clone(void) const /***********************************************************************//** - * @brief Start Hub - ***************************************************************************/ -void GVOHub::start(void) -{ - // Creates Hub - printf("Creating the hub\n"); - init_hub(); - - // Starts the hub - printf("Starting the hub\n"); - start_hub(); - - // Return - return; -} - - -/***********************************************************************//** * @brief * * @@ -226,7 +210,7 @@ void GVOHub::register_service(const GXml& xml, const socklen_t& sock) { printf("Registration request called\n"); - + //sem_wait(®_clients->lock); // Declare message std::string msg = ""; char selfid[124]; @@ -248,7 +232,7 @@ void GVOHub::register_service(const GXml& xml, const socklen_t& sock) } } - printf("Message composition 2 \n"); + //printf("Message composition 2 \n"); sprintf(selfid,"\t\t\t\t\tsamp.self-id%s\n",cl_id); sprintf(privatekey,"\t\t\t\t\tsamp.private-key%s\n",secret.c_str()); reg_clients->registered ++; @@ -269,6 +253,8 @@ void GVOHub::register_service(const GXml& xml, const socklen_t& sock) msg.append("\n"); post_string(msg,sock); + activate_callbacks("samp.hub.event.register",cl_id); + //sem_post(®_clients->lock); //Return return; } @@ -281,7 +267,7 @@ void GVOHub::register_service(const GXml& xml, const socklen_t& sock) ***************************************************************************/ void GVOHub::unregister(const GXml& xml,const socklen_t& sock) { - + //sem_wait(®_clients->lock); std::string client_calling = ""; const GXmlNode* node = xml.element("methodCall", 0); node = node->element("params", 0); @@ -297,31 +283,33 @@ void GVOHub::unregister(const GXml& xml,const socklen_t& sock) printf("Client %s matching candidate:\n",reg_clients->metadatas[i].private_key); if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) { printf("Client %s unregistration:\n",reg_clients->metadatas[i].reference); - strcpy(reg_clients->metadatas[i].reference,"NoRef"); - sprintf(reg_clients->metadatas[i].name,"NoName"); - sprintf(reg_clients->metadatas[i].description,"NoDescription"); - sprintf(reg_clients->metadatas[i].icon,"NoIcon"); - sprintf(reg_clients->metadatas[i].documentation,"NoDoc"); - sprintf(reg_clients->metadatas[i].affiliation,"NoAffiliation"); - sprintf(reg_clients->metadatas[i].email,"NoMail"); - sprintf(reg_clients->metadatas[i].author_name,"NoAuthor"); - sprintf(reg_clients->metadatas[i].homepage,"NoPage"); + + // Declare message + std::string msg = ""; + + // Set methodResponse elts + msg.append("\n"); + msg.append("\n"); + msg.append("\t\n"); + msg.append("\t\t\n\t\t\t\n\t\t\n"); + msg.append("\t\n"); + msg.append("\n"); + post_string(msg,sock); + activate_callbacks("samp.hub.event.unregister",reg_clients->metadatas[i].reference); + strcpy(reg_clients->metadatas[i].reference,"NoRef"); + sprintf(reg_clients->metadatas[i].name,"NoName"); + sprintf(reg_clients->metadatas[i].description,"NoDescription"); + sprintf(reg_clients->metadatas[i].icon,"NoIcon"); + sprintf(reg_clients->metadatas[i].documentation,"NoDoc"); + sprintf(reg_clients->metadatas[i].affiliation,"NoAffiliation"); + sprintf(reg_clients->metadatas[i].email,"NoMail"); + sprintf(reg_clients->metadatas[i].author_name,"NoAuthor"); + sprintf(reg_clients->metadatas[i].homepage,"NoPage"); } } - // Declare message - std::string msg = ""; - - // Set methodResponse elts - msg.append("\n"); - msg.append("\n"); - msg.append("\t\n"); - msg.append("\t\t\n\t\t\t\n\t\t\n"); - msg.append("\t\n"); - msg.append("\n"); - post_string(msg,sock); - //printf("Response message sent\n"); + //sem_post(®_clients->lock); //Return return; } @@ -334,7 +322,9 @@ void GVOHub::unregister(const GXml& xml,const socklen_t& sock) void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock) { char buffer[4096]; + #if GVO_HUB_testing == 1 printf("Client Metadata received\n"); + #endif // Search for metadata values to store std::string client_calling = ""; // Search for value of methodName @@ -350,7 +340,6 @@ void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock) } for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) { - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str()); if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) { printf("Client %s ready to be filled with metadata\n",reg_clients->metadatas[i].reference); std::string client_name = get_response_value(xml, "samp.name"); @@ -369,6 +358,16 @@ void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock) sprintf(reg_clients->metadatas[i].author_name,client_authname.c_str()); std::string client_page = get_response_value(xml, "home.page"); sprintf(reg_clients->metadatas[i].homepage,client_page.c_str()); + // Declare message + std::string msg = ""; + + //The hub response to Metadata declaration has no usefull content and can be discarded by the client. + // Set methodResponse elts + msg.append("\n"); + msg.append("\n"); + msg.append("\n"); + post_string(msg,sock); + activate_callbacks("samp.hub.event.metadata",reg_clients->metadatas[i].reference); break; } else { if (i == GVOHUB_NB_CLIENTS - 1) { @@ -378,15 +377,7 @@ void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock) } - // Declare message - std::string msg = ""; - - //The hub response to Metadata declaration has no usefull content and can be discarded by the client. - // Set methodResponse elts - msg.append("\n"); - msg.append("\n"); - msg.append("\n"); - post_string(msg,sock); + //Return return; } @@ -419,7 +410,7 @@ void GVOHub::set_xml_rpc_callback(const GXml& xml,const socklen_t& sock) std::cout << client_port << std::endl; for (i = 0; i < GVOHUB_NB_CLIENTS; i++) { - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str()); + //printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str()); if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) { printf("Client %s ready to be filled with port information\n",reg_clients->metadatas[i].reference); sprintf(reg_clients->metadatas[i].port,client_port.c_str()); @@ -587,7 +578,7 @@ void GVOHub::declare_subscriptions(const GXml& xml,const socklen_t& sock) std::cout << "Client registered to following methods calls:" << std::endl; for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) { - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str()); + //printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str()); if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) { for( j=0,p=client_subscriptions.begin(); j\n\t\t\t\t\t\tsamp.status\n\t\t\t\t\t\tsamp.ok\n\t\t\t\t\t\n"); for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) { + if (strcmp("gammalib_hub",client_calling.c_str()) == 0) { + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.name\n\t\t\t\t\t\tgammalib_hub\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.description.text\n\t\t\t\t\t\tGamma lib hub.\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.icon.url\n\t\t\t\t\t\t\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.documentation.url\n\t\t\t\t\t\tgammalib github documentation\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.affiliation\n\t\t\t\t\t\tIRAP, CNRS Toulouse\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.email\n\t\t\t\t\t\tdontcontactme@please\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.name\n\t\t\t\t\t\tJ. Knödlseder, T. Louge\n\t\t\t\t\t\n"); + msg.append("\t\t\t\t\t\n\t\t\t\t\t\thome.page\n\t\t\t\t\t\tno dedicated page\n\t\t\t\t\t\n"); + break; + } if (strcmp(reg_clients->metadatas[i].reference,client_calling.c_str()) == 0) { printf("Client %s metadata retrieval:\n",reg_clients->metadatas[i].reference); msg.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.name\n\t\t\t\t\t\t"); @@ -691,7 +693,7 @@ void GVOHub::get_metadata(const GXml& xml,const socklen_t& sock) msg.append("\t\t\t\t\n\t\t\t\n\t\t\n"); msg.append("\t\n"); msg.append("\n"); - + printf("Metadata for %s : \n %s",client_calling.c_str(),msg.c_str()); post_string(msg,sock); //Return @@ -732,11 +734,16 @@ void GVOHub::get_subscriptions(const GXml& xml,const socklen_t& sock) char tempo[128]; for (i = 0; i < GVOHUB_NB_CLIENTS; i++) { if (strcmp(reg_clients->metadatas[i].reference,client_calling.c_str()) == 0) { + //printf("retrieving client %s\n",reg_clients->metadatas[i].reference); for (j = 0; j < GVOHUB_NB_METHODS; j++) { - msg.append("\t\t\t\t\t\n\t\t\t\t\t\t"); strcpy(tempo,reg_clients->metadatas[i].registered_methods[j]); - msg.append(tempo); - msg.append("\n\t\t\t\t\t\t\n\t\t\t\t\t\n"); + if (strcmp("",tempo) == 0) { + continue; + } else { + msg.append("\t\t\t\t\t\n\t\t\t\t\t\t"); + msg.append(tempo); + msg.append("\n\t\t\t\t\t\t\n\t\t\t\t\t\n"); + } } } } @@ -823,8 +830,45 @@ void GVOHub::init_members(void) //m_client_id.clear(); m_socket = -1; // Signals no socket m_nb_clients = 0; // No registered clients at initialization - connected().clear(); + + //puts the structure array of clients in shared memory + if ((shm_handler = shmget(CLEF, sizeof(clients_descriptor), 0666 | IPC_CREAT)) < 0) + { + std::string msg = "Could not open shared memory."; + throw GException::invalid_value(G_INIT_MEMBERS, msg); + } + if ((ptr_mem_partagee = shmat(shm_handler, NULL, 0)) == (void*) -1) + { + std::string msg = "Could not attach shared memory."; + throw GException::invalid_value(G_INIT_MEMBERS, msg); + } + reg_clients = (clients_descriptor *)ptr_mem_partagee; + reg_clients->registered = 0; + for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) { + strcpy(reg_clients->metadatas[i].reference,"NoRef"); + strcpy(reg_clients->metadatas[i].private_key,"Unknown"); + strcpy(reg_clients->metadatas[i].name,"Unknown"); + strcpy(reg_clients->metadatas[i].icon,"Unknown"); + strcpy(reg_clients->metadatas[i].documentation,"Unknown"); + strcpy(reg_clients->metadatas[i].affiliation,"Unknown"); + strcpy(reg_clients->metadatas[i].author_name,"Unknown"); + strcpy(reg_clients->metadatas[i].email,"Unknown"); + strcpy(reg_clients->metadatas[i].homepage,"Unknown"); + strcpy(reg_clients->metadatas[i].port,"Unknown"); + strcpy(reg_clients->metadatas[i].description,"Unknown"); + + } + for (int j = 0; j < GVOHUB_NB_CLIENTS; j++) { + printf("%s\n",reg_clients->metadatas[j].reference); + } + //sem_init(®_clients->lock, 0, 1); + // Creates Hub + printf("Creating the hub\n"); + create_samp_file(); + // Starts the hub + printf("Starting the hub\n"); + start_hub(); // Return return; } @@ -846,7 +890,6 @@ void GVOHub::copy_members(const GVOHub& hub) m_client_key = hub.m_client_key; m_hub_id = hub.m_hub_id; m_socket = hub.m_socket; - connected() = hub.connected(); // Return return; @@ -857,7 +900,16 @@ void GVOHub::copy_members(const GVOHub& hub) * @brief Delete class members ***************************************************************************/ void GVOHub::free_members(void) -{ +{ + struct shmid_ds *buf; + + std::cout << "I am here" << std::endl; + std::string lockurl = get_hub_lockfile(); + printf("Deleting file: %s\n",lockurl.c_str()); + fflush(stdout); + std::remove(lockurl.c_str()); + shmdt(ptr_mem_partagee); + shmctl(shm_handler, IPC_RMID, buf); // Return return; } @@ -869,7 +921,7 @@ void GVOHub::free_members(void) * * Implements IVOA standard REC-SAMP-1.3-20120411. ***************************************************************************/ -void GVOHub::init_hub(void) +void GVOHub::create_samp_file(void) { // Get lockfile URL std::string lockurl = get_hub_lockfile(); @@ -902,33 +954,6 @@ void GVOHub::init_hub(void) // Close SAMP lockfile fclose(fptr); - //puts the structure array of clients in shared memory - if ((shm_handler = shmget(CLEF, sizeof(clients_descriptor), 0666 | IPC_CREAT)) < 0) - { - exit(1); - } - if ((ptr_mem_partagee = shmat(shm_handler, NULL, 0)) == (void*) -1) - { - exit(1); - } - reg_clients = (clients_descriptor *)ptr_mem_partagee; - reg_clients->registered = 0; - for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) { - strcpy(reg_clients->metadatas[i].reference,"NoRef"); - } - for (int j = 0; j < GVOHUB_NB_CLIENTS; j++) { - printf("%s\n",reg_clients->metadatas[j].reference); - } - fptr = fopen("SAMP-registered.txt", "w"); - if (fptr != NULL) { - - // If successfull writes application definition - fputs ("# SAMP registered applications and Methods \n",fptr); - } - - // Closes file - fclose(fptr); - // Return return; } @@ -1008,15 +1033,6 @@ void GVOHub::handle_request(const socklen_t& sock) } printf("Here is the message: %s\n",buffer); std::string response = buffer; - /* - int j = read(sock,buffer,4096); - if (j < 0) { - std::string msg = "Could not read socket buffer."; - throw GException::invalid_value(G_HANDLE_REQUEST, msg); - } - printf("Here is the second message: %s\n",buffer); - */ - // Find start of XML text size_t start = response.find("text(); } } - // endif: connection has been established - //std::string method_called = get_response_value(xml, "methodName"); if (method_called.compare("samp.hub.ping") == 0) { ping_service(sock); } @@ -1089,28 +1103,12 @@ void GVOHub::handle_request(const socklen_t& sock) if (method_called.compare("samp.hub.reply") == 0) { ping_service(sock); } - //Test if any registered client needs to know that the method has been called - for (i = 0; i < GVOHUB_NB_CLIENTS; i++) { - for (j = 0; j < GVOHUB_NB_METHODS; j++) { - if (strcmp(method_called.c_str(),reg_clients->metadatas[i].registered_methods[j]) == 0) { - printf("Callback needed\n"); - } - } - - } + // Return return; } -/***********************************************************************//** - * @brief Closes SAMP hub shm - * - * @return - * - ***************************************************************************/ -/*void intHandler(int sig) { - shmdt(ptr_mem_partagee); -}*/ + /***********************************************************************//** * @brief Starts the SAMP hub socket and listens on it * @@ -1206,7 +1204,7 @@ void GVOHub::start_hub(void) signal(pid, SIG_IGN); } - } // endwhile + } // Return @@ -1302,7 +1300,7 @@ std::list GVOHub::get_registrations(const GXml& xml, std::cout << *(node->element("member", i)) << std::cout;*/ subnode = node->element("member", i); subnode = subnode->element("name", 0); - std::cout << node->element("member",i)->element("name", 0)->print(NORMAL,0) << std::cout; + //std::cout << node->element("member",i)->element("name", 0)->print(NORMAL,0) << std::cout; const GXmlText* text = static_cast((*subnode)[0]); if (text != NULL) { value.push_front(text->text()); @@ -1439,13 +1437,15 @@ void GVOHub::post_string(const std::string& content,const socklen_t& sock) const // Build post string std::string post = prefix + content; - //printf(post.c_str()); + #if GVO_HUB_testing == 1 + printf(post.c_str()); + #endif // Send content to socket bool done = false; do { int length = post.length(); int sent_length = send(sock, post.c_str(), length, 0); - printf("Sent through socket: \n %s",post.c_str()); + //printf("Sent through socket: \n %s",post.c_str()); if (sent_length < length) { post = post.substr(sent_length, std::string::npos); } @@ -1483,6 +1483,7 @@ std::string GVOHub::receive_string(const socklen_t& sock) const int n = 0; int m = 0; //do { + printf("reading socket\n"); n = recv(sock, buffer, 4096,0); printf("%d octets received: message %s\n",n,buffer); @@ -1539,7 +1540,9 @@ std::list GVOHub::get_clientid(const GXml& xml, const GXmlText* text = static_cast((*node)[0]); if (text != NULL) { value.push_front(text->text()); + #if GVO_HUB_testing == 1 std::cout << "Client id for which information is seeked:" + text->text() + "\n"; + #endif } } } @@ -1549,8 +1552,13 @@ std::list GVOHub::get_clientid(const GXml& xml, // Return value return value; } -/* -*/ +/***********************************************************************//** + * @brief + * + * @param[in] + * + * + ***************************************************************************/ std::string GVOHub::random_string( size_t length ) { srand(time(0)); @@ -1563,3 +1571,259 @@ std::string GVOHub::random_string( size_t length ) return str; } +/***********************************************************************//** + * @brief Test if any registered client needs to know that the method + has been called + * + * @param[in] method: name of the method, cl_id id of calling client + * + * + * + * + ***************************************************************************/ +void GVOHub::activate_callbacks(std::string method,char cl_id[31]) +{ + int i,j,k; + char port[16]; + char pkey[16]; + std::string spkey; + + for (i = 0; i < GVOHUB_NB_CLIENTS; i++) { + for (j = 0; j < GVOHUB_NB_METHODS; j++) { + if (strcmp("",reg_clients->metadatas[i].registered_methods[j]) == 0) { + continue; + } else { + //printf("Comparing %s and %s \n",method.c_str(),reg_clients->metadatas[i].registered_methods[j]); + if (strcmp(method.c_str(),reg_clients->metadatas[i].registered_methods[j]) == 0) { + strcpy(port,reg_clients->metadatas[i].port); + printf("Callback needed on port %s\n",port); + std::string port_string = std::string(port); + //Eliminate http://127.0.0.1/ from port string -----> improve this code is necessary + port_string.replace(0,17,""); + printf("Callback needed on port %s\n",port_string.c_str()); + std::size_t pos = port_string.find("/"); + std::string port_only = port_string.substr(0,pos); + printf("%s\n",port_only.c_str()); + // Set hints + struct addrinfo hints; + std::memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + // Get server information + struct addrinfo* servinfo; + if (getaddrinfo("127.0.0.1", port_only.c_str(), + &hints, &servinfo) == 0) { + + // Loop through all the results and connect to the first + // we can + for (struct addrinfo* ptr = servinfo; ptr != NULL; ptr = ptr->ai_next) { + + // Create socket + cback_socket = socket(ptr->ai_family, + ptr->ai_socktype, + ptr->ai_protocol); + + // Connect to socket if socket is valid + if (cback_socket != -1) { + if (::connect(cback_socket, + ptr->ai_addr, + ptr->ai_addrlen) == -1) { + close(cback_socket); + cback_socket = -1; + } + } + + } // endfor: looped through all results + + } // endif: server information was valid + std::string msg_cback = ""; + + if (strcmp(method.c_str(),"samp.hub.event.register") == 0) { + // Set methodResponse elts for samp.hub.event.register + msg_cback.append("\n"); + + msg_cback.append("samp.client.receiveNotification\n"); + msg_cback.append("\t\n"); + msg_cback.append("\t\t"); + msg_cback.append(reg_clients->metadatas[i].private_key); + msg_cback.append("\n"); + msg_cback.append("\t\t"+m_hub_id+"\n"); + msg_cback.append("\t\t\n\t\t\t\n\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\tsamp.mtypesamp.hub.event.register\n"); + msg_cback.append("\t\t\t\t\tsamp.params\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t"); + msg_cback.append("id"); + msg_cback.append(cl_id); + msg_cback.append("\n\t\t\t\t\t\t\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\n\t\t\t\n\t\t\n"); + msg_cback.append("\t\n"); + + msg_cback.append("\n"); + post_string_toclient(msg_cback); + } + if (strcmp(method.c_str(),"samp.hub.event.unregister") == 0) { + // Set methodResponse elts for samp.hub.event.unregister + msg_cback.append("\n"); + + msg_cback.append("samp.client.receiveNotification\n"); + msg_cback.append("\t\n"); + msg_cback.append("\t\t"); + msg_cback.append(reg_clients->metadatas[i].private_key); + msg_cback.append("\n"); + msg_cback.append("\t\t"+m_hub_id+"\n"); + msg_cback.append("\t\t\n\t\t\t\n\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\tsamp.mtypesamp.hub.event.unregister\n"); + msg_cback.append("\t\t\t\t\tsamp.params\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t"); + msg_cback.append("id"); + msg_cback.append(cl_id); + msg_cback.append("\n\t\t\t\t\t\t\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\n\t\t\t\n\t\t\n"); + msg_cback.append("\t\n"); + + msg_cback.append("\n"); + post_string_toclient(msg_cback); + + } + if (strcmp(method.c_str(),"samp.hub.event.metadata") == 0) { + // Set methodResponse elts for samp.hub.event.unregister + msg_cback.append("\n"); + + msg_cback.append("samp.client.receiveNotification\n"); + msg_cback.append("\t\n"); + msg_cback.append("\t\t"); + msg_cback.append(reg_clients->metadatas[i].private_key); + msg_cback.append("\n"); + msg_cback.append("\t\t"+m_hub_id+"\n"); + msg_cback.append("\t\t\n\t\t\t\n\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\tsamp.mtypesamp.hub.event.metadata\n"); + msg_cback.append("\t\t\t\t\tsamp.params\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t"); + msg_cback.append("\t\t\t\t\tid"); + for (k = 0; k < GVOHUB_NB_CLIENTS; k++) { + if (strcmp(cl_id,reg_clients->metadatas[k].reference) == 0) { + break; + } + } + msg_cback.append(reg_clients->metadatas[k].reference); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\tmetadata\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.name\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].name); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.description.text\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].description); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.icon.url\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].icon); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tsamp.documentation.url\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].documentation); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.affiliation\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].affiliation); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.email\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].email); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\tauthor.name\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].author_name); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\t\n\t\t\t\t\t\thome.page\n\t\t\t\t\t\t"); + msg_cback.append(reg_clients->metadatas[k].homepage); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\n"); + msg_cback.append("\n\t\t\t\t\t\n"); + msg_cback.append("\t\t\t\t\n\t\t\t\n\t\t\n"); + msg_cback.append("\t\n"); + + msg_cback.append("\n"); + post_string_toclient(msg_cback); + + } + + + + } + } + } + + } +} +/***********************************************************************//** + * @brief Post string content to Hub + * + * @param[in] content String content to post + * + * Posts the content of a string to the Hub. + * + * The method does nothing if no Hub connection has been established. + ***************************************************************************/ +void GVOHub::post_string_toclient(const std::string& content) const +{ + // Continue only if Hub connection has been established + if (cback_socket != -1) { + + // Determine content length + int length = content.length(); + + // Set prefix + std::string prefix = "POST /xmlrpc HTTP/1.1\n" + "Connection: keep-alive\n" + "User-Agent: GammaLib\n" + "Host: localhost:"+gammalib::str(cback_socket)+"\n" + "Content-Type: text/xml\n" + "Content-Length: "+gammalib::str(length)+"\n\n"; + + // Build post string + std::string post = prefix + content; + + // Send content to socket + bool done = false; + do { + int length = post.length(); + int sent_length = send(cback_socket, post.c_str(), length, 0); + #if GVO_HUB_testing == 1 + printf("Send to client: %s\n",post.c_str()); + #endif + if (sent_length < length) { + post = post.substr(sent_length, std::string::npos); + } + else { + done = true; + } + } while (!done); + + // Read buffer until it is empty + char buffer[1001]; + int n = 0; + do { + printf("receive socket instruction from callback response\n"); + fflush(stdout); + n = recv(cback_socket, buffer, 1000, 0); + printf("data received: %s\n",buffer); + fflush(stdout); + if (n > 0) { + buffer[n+1] = '\0'; + + } + } while (n > 0); + /*printf("%s",buffer); + // Declare message + std::string msg = ""; + + // Set methodResponse elts + msg.append("\n"); + msg.append("\n"); + msg.append("\t\n"); + msg.append("\t\t\n\t\t\t\n\t\t\n"); + msg.append("\t\n"); + msg.append("\n"); + + + + post_string(msg,cback_socket);*/ + } // endif: Hub connection had been established + + // Return + return; +} + -- libgit2 0.21.4