Commit 912684d70b279369173ae594c9c94abc78e76114

Authored by Thierry Louge
1 parent 5733c3a3

Afetr debug of fully SAMP compliant Hub

.plastic 0 → 100644
  1 +#PLASTIC server uk.ac.starlink.plastic.ServerSet
  2 +#Tue May 12 17:16:26 CEST 2015
  3 +plastic.xmlrpc.url=http\://STAR-VORS\:2112/
  4 +uk.ac.starlink.plastic.servid=uk.ac.starlink.plastic.ServerSet@b61fd1
  5 +plastic.version=0.4
  6 +plastic.rmi.port=1099
... ...
include/GVOHub.hpp
... ... @@ -28,11 +28,13 @@
28 28 #define GVOHUB_HPP
29 29 #define GVOHUB_NB_CLIENTS 5
30 30 #define GVOHUB_NB_METHODS 128
  31 +#define GVO_HUB_testing 1
31 32  
32 33 /* __ Includes ___________________________________________________________ */
33 34 #include <string>
34 35 #include <list>
35 36 #include <sys/socket.h>
  37 +#include <semaphore.h>
36 38 #include "GBase.hpp"
37 39 #include "GXml.hpp"
38 40 #include "GXmlNode.hpp"
... ... @@ -61,7 +63,6 @@ public:
61 63 // Methods
62 64 void clear(void);
63 65 GVOHub* clone(void) const;
64   - void start(void);
65 66 std::string print(const GChatter& chatter = NORMAL) const;
66 67  
67 68  
... ... @@ -70,7 +71,7 @@ protected:
70 71 void init_members(void);
71 72 void copy_members(const GVOHub& client);
72 73 void free_members(void);
73   - void init_hub(void);
  74 + void create_samp_file(void);
74 75 void start_hub(void);
75 76 void register_service(const GXml& xml,const socklen_t& sock);
76 77 void ping_service(const socklen_t& sock);
... ... @@ -102,7 +103,8 @@ protected:
102 103 std::string& value) const;
103 104 std::string get_hub_lockfile(void) const;
104 105 std::string random_string( size_t length );
105   - void intHandler(int sig);
  106 + void activate_callbacks(std::string method,char cl_id[31]);
  107 + void post_string_toclient(const std::string& content) const;
106 108  
107 109 // Protected members
108 110 std::string m_name; //!< Client name
... ... @@ -114,33 +116,32 @@ protected:
114 116 std::string m_client_key; //!< Private client key
115 117 std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others
116 118 int m_socket; //!< Hub socket
  119 + int cback_socket; //!< Hub socket to callback clients
117 120 int m_nb_clients; //!< Number of already registered clients
118 121 typedef struct {
119 122 char private_key[16];
120 123 char name[32];
121 124 char reference[32];
122   - char description[32];
  125 + char description[256];
123 126 char icon[32];
124 127 char documentation[32];
125 128 char affiliation[32];
126 129 char author_name[32];
127 130 char email[32];
128 131 char homepage[32];
129   - char port[16];
130   - char registered_methods[GVOHUB_NB_METHODS][128]; //128 methods of 128 cars max.
  132 + char port[32];
  133 + char registered_methods[GVOHUB_NB_METHODS][GVOHUB_NB_METHODS]; //128 methods of 128 cars max.
131 134 } connected_shm;
132 135 typedef struct {
133 136 int registered;
  137 + sem_t lock;
134 138 connected_shm metadatas[GVOHUB_NB_CLIENTS];
135 139 } clients_descriptor;
136 140 clients_descriptor *reg_clients;
137 141 connected_shm *clients;
138 142 void* ptr_mem_partagee; //pointer to shm address
139 143 int shm_handler; //handler to shm
140   - static std::vector<GVOApp>& connected() {
141   - static std::vector<GVOApp> m_connected_apps; //!< list of apps connected to the hub
142   - return m_connected_apps;
143   - }
  144 +
144 145 };
145 146  
146 147 #endif /* GVOHUB_HPP */
... ...
pyext/GVOHub.i
... ... @@ -44,7 +44,6 @@ public:
44 44 // Methods
45 45 void clear(void);
46 46 GVOHub* clone(void) const;
47   - void start(void);
48 47 };
49 48  
50 49  
... ...
src/vo/GVOHub.cpp
... ... @@ -47,9 +47,11 @@
47 47 #include <sys/ipc.h>
48 48 #include <sys/shm.h>
49 49  
  50 +
50 51 /* __ Method name definitions ____________________________________________ */
51 52 #define G_HANDLE_REQUEST "GVOHub::handle_request(socklen_t)"
52 53 #define G_START_HUB "GVOHub::start_hub()"
  54 +#define G_INIT_MEMBERS "GVOHub::init_members()"
53 55  
54 56 /* __ Macros _____________________________________________________________ */
55 57  
... ... @@ -101,7 +103,7 @@ GVOHub::GVOHub(const GVOHub&amp; hub)
101 103 ***************************************************************************/
102 104 GVOHub::~GVOHub(void)
103 105 {
104   -
  106 + free_members();
105 107 // Return
106 108 return;
107 109 }
... ... @@ -173,24 +175,6 @@ GVOHub* GVOHub::clone(void) const
173 175  
174 176  
175 177 /***********************************************************************//**
176   - * @brief Start Hub
177   - ***************************************************************************/
178   -void GVOHub::start(void)
179   -{
180   - // Creates Hub
181   - printf("Creating the hub\n");
182   - init_hub();
183   -
184   - // Starts the hub
185   - printf("Starting the hub\n");
186   - start_hub();
187   -
188   - // Return
189   - return;
190   -}
191   -
192   -
193   -/***********************************************************************//**
194 178 * @brief
195 179 *
196 180 *
... ... @@ -226,7 +210,7 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
226 210 {
227 211  
228 212 printf("Registration request called\n");
229   -
  213 + //sem_wait(&reg_clients->lock);
230 214 // Declare message
231 215 std::string msg = "";
232 216 char selfid[124];
... ... @@ -248,7 +232,7 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
248 232 }
249 233 }
250 234  
251   - printf("Message composition 2 \n");
  235 + //printf("Message composition 2 \n");
252 236 sprintf(selfid,"\t\t\t\t\t<member><name>samp.self-id</name><value>%s</value></member>\n",cl_id);
253 237 sprintf(privatekey,"\t\t\t\t\t<member><name>samp.private-key</name><value>%s</value></member>\n",secret.c_str());
254 238 reg_clients->registered ++;
... ... @@ -269,6 +253,8 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
269 253 msg.append("</methodResponse>\n");
270 254 post_string(msg,sock);
271 255  
  256 + activate_callbacks("samp.hub.event.register",cl_id);
  257 + //sem_post(&reg_clients->lock);
272 258 //Return
273 259 return;
274 260 }
... ... @@ -281,7 +267,7 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
281 267 ***************************************************************************/
282 268 void GVOHub::unregister(const GXml& xml,const socklen_t& sock)
283 269 {
284   -
  270 + //sem_wait(&reg_clients->lock);
285 271 std::string client_calling = "";
286 272 const GXmlNode* node = xml.element("methodCall", 0);
287 273 node = node->element("params", 0);
... ... @@ -297,31 +283,33 @@ void GVOHub::unregister(const GXml&amp; xml,const socklen_t&amp; sock)
297 283 printf("Client %s matching candidate:\n",reg_clients->metadatas[i].private_key);
298 284 if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) {
299 285 printf("Client %s unregistration:\n",reg_clients->metadatas[i].reference);
300   - strcpy(reg_clients->metadatas[i].reference,"NoRef");
301   - sprintf(reg_clients->metadatas[i].name,"NoName");
302   - sprintf(reg_clients->metadatas[i].description,"NoDescription");
303   - sprintf(reg_clients->metadatas[i].icon,"NoIcon");
304   - sprintf(reg_clients->metadatas[i].documentation,"NoDoc");
305   - sprintf(reg_clients->metadatas[i].affiliation,"NoAffiliation");
306   - sprintf(reg_clients->metadatas[i].email,"NoMail");
307   - sprintf(reg_clients->metadatas[i].author_name,"NoAuthor");
308   - sprintf(reg_clients->metadatas[i].homepage,"NoPage");
309 286  
310 287  
  288 +
  289 + // Declare message
  290 + std::string msg = "";
  291 +
  292 + // Set methodResponse elts
  293 + msg.append("<?xml version=\"1.0\"?>\n");
  294 + msg.append("<methodResponse>\n");
  295 + msg.append("\t<params>\n");
  296 + msg.append("\t\t<param>\n\t\t\t<value/>\n\t\t</param>\n");
  297 + msg.append("\t</params>\n");
  298 + msg.append("</methodResponse>\n");
  299 + post_string(msg,sock);
  300 + activate_callbacks("samp.hub.event.unregister",reg_clients->metadatas[i].reference);
  301 + strcpy(reg_clients->metadatas[i].reference,"NoRef");
  302 + sprintf(reg_clients->metadatas[i].name,"NoName");
  303 + sprintf(reg_clients->metadatas[i].description,"NoDescription");
  304 + sprintf(reg_clients->metadatas[i].icon,"NoIcon");
  305 + sprintf(reg_clients->metadatas[i].documentation,"NoDoc");
  306 + sprintf(reg_clients->metadatas[i].affiliation,"NoAffiliation");
  307 + sprintf(reg_clients->metadatas[i].email,"NoMail");
  308 + sprintf(reg_clients->metadatas[i].author_name,"NoAuthor");
  309 + sprintf(reg_clients->metadatas[i].homepage,"NoPage");
311 310 }
312 311 }
313   - // Declare message
314   - std::string msg = "";
315   -
316   - // Set methodResponse elts
317   - msg.append("<?xml version=\"1.0\"?>\n");
318   - msg.append("<methodResponse>\n");
319   - msg.append("\t<params>\n");
320   - msg.append("\t\t<param>\n\t\t\t<value/>\n\t\t</param>\n");
321   - msg.append("\t</params>\n");
322   - msg.append("</methodResponse>\n");
323   - post_string(msg,sock);
324   - //printf("Response message sent\n");
  312 + //sem_post(&reg_clients->lock);
325 313 //Return
326 314 return;
327 315 }
... ... @@ -334,7 +322,9 @@ void GVOHub::unregister(const GXml&amp; xml,const socklen_t&amp; sock)
334 322 void GVOHub::register_metadata(const GXml& xml,const socklen_t& sock)
335 323 {
336 324 char buffer[4096];
  325 + #if GVO_HUB_testing == 1
337 326 printf("Client Metadata received\n");
  327 + #endif
338 328 // Search for metadata values to store
339 329 std::string client_calling = "";
340 330 // Search for value of methodName
... ... @@ -350,7 +340,6 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
350 340 }
351 341  
352 342 for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) {
353   - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str());
354 343 if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) {
355 344 printf("Client %s ready to be filled with metadata\n",reg_clients->metadatas[i].reference);
356 345 std::string client_name = get_response_value(xml, "samp.name");
... ... @@ -369,6 +358,16 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
369 358 sprintf(reg_clients->metadatas[i].author_name,client_authname.c_str());
370 359 std::string client_page = get_response_value(xml, "home.page");
371 360 sprintf(reg_clients->metadatas[i].homepage,client_page.c_str());
  361 + // Declare message
  362 + std::string msg = "";
  363 +
  364 + //The hub response to Metadata declaration has no usefull content and can be discarded by the client.
  365 + // Set methodResponse elts
  366 + msg.append("<?xml version=\"1.0\"?>\n");
  367 + msg.append("<methodResponse><params><param><value/></param></params>\n");
  368 + msg.append("</methodResponse>\n");
  369 + post_string(msg,sock);
  370 + activate_callbacks("samp.hub.event.metadata",reg_clients->metadatas[i].reference);
372 371 break;
373 372 } else {
374 373 if (i == GVOHUB_NB_CLIENTS - 1) {
... ... @@ -378,15 +377,7 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
378 377 }
379 378  
380 379  
381   - // Declare message
382   - std::string msg = "";
383   -
384   - //The hub response to Metadata declaration has no usefull content and can be discarded by the client.
385   - // Set methodResponse elts
386   - msg.append("<?xml version=\"1.0\"?>\n");
387   - msg.append("<methodResponse><params><param><value/></param></params>\n");
388   - msg.append("</methodResponse>\n");
389   - post_string(msg,sock);
  380 +
390 381 //Return
391 382 return;
392 383 }
... ... @@ -419,7 +410,7 @@ void GVOHub::set_xml_rpc_callback(const GXml&amp; xml,const socklen_t&amp; sock)
419 410 std::cout << client_port << std::endl;
420 411  
421 412 for (i = 0; i < GVOHUB_NB_CLIENTS; i++) {
422   - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str());
  413 + //printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str());
423 414 if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) {
424 415 printf("Client %s ready to be filled with port information\n",reg_clients->metadatas[i].reference);
425 416 sprintf(reg_clients->metadatas[i].port,client_port.c_str());
... ... @@ -587,7 +578,7 @@ void GVOHub::declare_subscriptions(const GXml&amp; xml,const socklen_t&amp; sock)
587 578 std::cout << "Client registered to following methods calls:" << std::endl;
588 579  
589 580 for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) {
590   - printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str());
  581 + //printf("comparing Reference of client %s with seeked %s\n",reg_clients->metadatas[i].private_key,client_calling.c_str());
591 582 if (strcmp(reg_clients->metadatas[i].private_key,client_calling.c_str()) == 0) {
592 583 for( j=0,p=client_subscriptions.begin(); j<client_subscriptions.size()-1; j++,p++ ) {
593 584 std::cout << *p << std::endl;
... ... @@ -654,6 +645,17 @@ void GVOHub::get_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
654 645 msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.status</name>\n\t\t\t\t\t\t<value>samp.ok</value>\n\t\t\t\t\t</member>\n");
655 646  
656 647 for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) {
  648 + if (strcmp("gammalib_hub",client_calling.c_str()) == 0) {
  649 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.name</name>\n\t\t\t\t\t\t<value>gammalib_hub</value>\n\t\t\t\t\t</member>\n");
  650 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.description.text</name>\n\t\t\t\t\t\t<value>Gamma lib hub.</value>\n\t\t\t\t\t</member>\n");
  651 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.icon.url</name>\n\t\t\t\t\t\t<value></value>\n\t\t\t\t\t</member>\n");
  652 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.documentation.url</name>\n\t\t\t\t\t\t<value>gammalib github documentation</value>\n\t\t\t\t\t</member>\n");
  653 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.affiliation</name>\n\t\t\t\t\t\t<value>IRAP, CNRS Toulouse</value>\n\t\t\t\t\t</member>\n");
  654 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.email</name>\n\t\t\t\t\t\t<value>dontcontactme@please</value>\n\t\t\t\t\t</member>\n");
  655 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.name</name>\n\t\t\t\t\t\t<value>J. Knödlseder, T. Louge</value>\n\t\t\t\t\t</member>\n");
  656 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>home.page</name>\n\t\t\t\t\t\t<value>no dedicated page</value>\n\t\t\t\t\t</member>\n");
  657 + break;
  658 + }
657 659 if (strcmp(reg_clients->metadatas[i].reference,client_calling.c_str()) == 0) {
658 660 printf("Client %s metadata retrieval:\n",reg_clients->metadatas[i].reference);
659 661 msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.name</name>\n\t\t\t\t\t\t<value>");
... ... @@ -691,7 +693,7 @@ void GVOHub::get_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
691 693 msg.append("\t\t\t\t</struct>\n\t\t\t</value>\n\t\t</param>\n");
692 694 msg.append("\t</params>\n");
693 695 msg.append("</methodResponse>\n");
694   -
  696 + printf("Metadata for %s : \n %s",client_calling.c_str(),msg.c_str());
695 697 post_string(msg,sock);
696 698  
697 699 //Return
... ... @@ -732,11 +734,16 @@ void GVOHub::get_subscriptions(const GXml&amp; xml,const socklen_t&amp; sock)
732 734 char tempo[128];
733 735 for (i = 0; i < GVOHUB_NB_CLIENTS; i++) {
734 736 if (strcmp(reg_clients->metadatas[i].reference,client_calling.c_str()) == 0) {
  737 + //printf("retrieving client %s\n",reg_clients->metadatas[i].reference);
735 738 for (j = 0; j < GVOHUB_NB_METHODS; j++) {
736   - msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>");
737 739 strcpy(tempo,reg_clients->metadatas[i].registered_methods[j]);
738   - msg.append(tempo);
739   - msg.append("</name>\n\t\t\t\t\t\t<value></value>\n\t\t\t\t\t</member>\n");
  740 + if (strcmp("",tempo) == 0) {
  741 + continue;
  742 + } else {
  743 + msg.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>");
  744 + msg.append(tempo);
  745 + msg.append("</name>\n\t\t\t\t\t\t<value></value>\n\t\t\t\t\t</member>\n");
  746 + }
740 747 }
741 748 }
742 749 }
... ... @@ -823,8 +830,45 @@ void GVOHub::init_members(void)
823 830 //m_client_id.clear();
824 831 m_socket = -1; // Signals no socket
825 832 m_nb_clients = 0; // No registered clients at initialization
826   - connected().clear();
  833 +
  834 + //puts the structure array of clients in shared memory
  835 + if ((shm_handler = shmget(CLEF, sizeof(clients_descriptor), 0666 | IPC_CREAT)) < 0)
  836 + {
  837 + std::string msg = "Could not open shared memory.";
  838 + throw GException::invalid_value(G_INIT_MEMBERS, msg);
  839 + }
  840 + if ((ptr_mem_partagee = shmat(shm_handler, NULL, 0)) == (void*) -1)
  841 + {
  842 + std::string msg = "Could not attach shared memory.";
  843 + throw GException::invalid_value(G_INIT_MEMBERS, msg);
  844 + }
  845 + reg_clients = (clients_descriptor *)ptr_mem_partagee;
  846 + reg_clients->registered = 0;
  847 + for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) {
  848 + strcpy(reg_clients->metadatas[i].reference,"NoRef");
  849 + strcpy(reg_clients->metadatas[i].private_key,"Unknown");
  850 + strcpy(reg_clients->metadatas[i].name,"Unknown");
  851 + strcpy(reg_clients->metadatas[i].icon,"Unknown");
  852 + strcpy(reg_clients->metadatas[i].documentation,"Unknown");
  853 + strcpy(reg_clients->metadatas[i].affiliation,"Unknown");
  854 + strcpy(reg_clients->metadatas[i].author_name,"Unknown");
  855 + strcpy(reg_clients->metadatas[i].email,"Unknown");
  856 + strcpy(reg_clients->metadatas[i].homepage,"Unknown");
  857 + strcpy(reg_clients->metadatas[i].port,"Unknown");
  858 + strcpy(reg_clients->metadatas[i].description,"Unknown");
  859 +
  860 + }
  861 + for (int j = 0; j < GVOHUB_NB_CLIENTS; j++) {
  862 + printf("%s\n",reg_clients->metadatas[j].reference);
  863 + }
  864 + //sem_init(&reg_clients->lock, 0, 1);
  865 + // Creates Hub
  866 + printf("Creating the hub\n");
  867 + create_samp_file();
827 868  
  869 + // Starts the hub
  870 + printf("Starting the hub\n");
  871 + start_hub();
828 872 // Return
829 873 return;
830 874 }
... ... @@ -846,7 +890,6 @@ void GVOHub::copy_members(const GVOHub&amp; hub)
846 890 m_client_key = hub.m_client_key;
847 891 m_hub_id = hub.m_hub_id;
848 892 m_socket = hub.m_socket;
849   - connected() = hub.connected();
850 893  
851 894 // Return
852 895 return;
... ... @@ -857,7 +900,16 @@ void GVOHub::copy_members(const GVOHub&amp; hub)
857 900 * @brief Delete class members
858 901 ***************************************************************************/
859 902 void GVOHub::free_members(void)
860   -{
  903 +{
  904 + struct shmid_ds *buf;
  905 +
  906 + std::cout << "I am here" << std::endl;
  907 + std::string lockurl = get_hub_lockfile();
  908 + printf("Deleting file: %s\n",lockurl.c_str());
  909 + fflush(stdout);
  910 + std::remove(lockurl.c_str());
  911 + shmdt(ptr_mem_partagee);
  912 + shmctl(shm_handler, IPC_RMID, buf);
861 913 // Return
862 914 return;
863 915 }
... ... @@ -869,7 +921,7 @@ void GVOHub::free_members(void)
869 921 *
870 922 * Implements IVOA standard REC-SAMP-1.3-20120411.
871 923 ***************************************************************************/
872   -void GVOHub::init_hub(void)
  924 +void GVOHub::create_samp_file(void)
873 925 {
874 926 // Get lockfile URL
875 927 std::string lockurl = get_hub_lockfile();
... ... @@ -902,33 +954,6 @@ void GVOHub::init_hub(void)
902 954 // Close SAMP lockfile
903 955 fclose(fptr);
904 956  
905   - //puts the structure array of clients in shared memory
906   - if ((shm_handler = shmget(CLEF, sizeof(clients_descriptor), 0666 | IPC_CREAT)) < 0)
907   - {
908   - exit(1);
909   - }
910   - if ((ptr_mem_partagee = shmat(shm_handler, NULL, 0)) == (void*) -1)
911   - {
912   - exit(1);
913   - }
914   - reg_clients = (clients_descriptor *)ptr_mem_partagee;
915   - reg_clients->registered = 0;
916   - for (int i = 0; i < GVOHUB_NB_CLIENTS; i++) {
917   - strcpy(reg_clients->metadatas[i].reference,"NoRef");
918   - }
919   - for (int j = 0; j < GVOHUB_NB_CLIENTS; j++) {
920   - printf("%s\n",reg_clients->metadatas[j].reference);
921   - }
922   - fptr = fopen("SAMP-registered.txt", "w");
923   - if (fptr != NULL) {
924   -
925   - // If successfull writes application definition
926   - fputs ("# SAMP registered applications and Methods \n",fptr);
927   - }
928   -
929   - // Closes file
930   - fclose(fptr);
931   -
932 957 // Return
933 958 return;
934 959 }
... ... @@ -1008,15 +1033,6 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
1008 1033 }
1009 1034 printf("Here is the message: %s\n",buffer);
1010 1035 std::string response = buffer;
1011   - /*
1012   - int j = read(sock,buffer,4096);
1013   - if (j < 0) {
1014   - std::string msg = "Could not read socket buffer.";
1015   - throw GException::invalid_value(G_HANDLE_REQUEST, msg);
1016   - }
1017   - printf("Here is the second message: %s\n",buffer);
1018   - */
1019   -
1020 1036  
1021 1037 // Find start of XML text
1022 1038 size_t start = response.find("<?xml");
... ... @@ -1039,8 +1055,6 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
1039 1055 method_called = text->text();
1040 1056 }
1041 1057 }
1042   - // endif: connection has been established
1043   - //std::string method_called = get_response_value(xml, "methodName");
1044 1058 if (method_called.compare("samp.hub.ping") == 0) {
1045 1059 ping_service(sock);
1046 1060 }
... ... @@ -1089,28 +1103,12 @@ void GVOHub::handle_request(const socklen_t&amp; sock)
1089 1103 if (method_called.compare("samp.hub.reply") == 0) {
1090 1104 ping_service(sock);
1091 1105 }
1092   - //Test if any registered client needs to know that the method has been called
1093   - for (i = 0; i < GVOHUB_NB_CLIENTS; i++) {
1094   - for (j = 0; j < GVOHUB_NB_METHODS; j++) {
1095   - if (strcmp(method_called.c_str(),reg_clients->metadatas[i].registered_methods[j]) == 0) {
1096   - printf("Callback needed\n");
1097   - }
1098   - }
1099   -
1100   - }
  1106 +
1101 1107  
1102 1108 // Return
1103 1109 return;
1104 1110 }
1105   -/***********************************************************************//**
1106   - * @brief Closes SAMP hub shm
1107   - *
1108   - * @return
1109   - *
1110   - ***************************************************************************/
1111   -/*void intHandler(int sig) {
1112   - shmdt(ptr_mem_partagee);
1113   -}*/
  1111 +
1114 1112 /***********************************************************************//**
1115 1113 * @brief Starts the SAMP hub socket and listens on it
1116 1114 *
... ... @@ -1206,7 +1204,7 @@ void GVOHub::start_hub(void)
1206 1204 signal(pid, SIG_IGN);
1207 1205 }
1208 1206  
1209   - } // endwhile
  1207 + }
1210 1208  
1211 1209  
1212 1210 // Return
... ... @@ -1302,7 +1300,7 @@ std::list&lt;std::string&gt; GVOHub::get_registrations(const GXml&amp; xml,
1302 1300 std::cout << *(node->element("member", i)) << std::cout;*/
1303 1301 subnode = node->element("member", i);
1304 1302 subnode = subnode->element("name", 0);
1305   - std::cout << node->element("member",i)->element("name", 0)->print(NORMAL,0) << std::cout;
  1303 + //std::cout << node->element("member",i)->element("name", 0)->print(NORMAL,0) << std::cout;
1306 1304 const GXmlText* text = static_cast<const GXmlText*>((*subnode)[0]);
1307 1305 if (text != NULL) {
1308 1306 value.push_front(text->text());
... ... @@ -1439,13 +1437,15 @@ void GVOHub::post_string(const std::string&amp; content,const socklen_t&amp; sock) const
1439 1437  
1440 1438 // Build post string
1441 1439 std::string post = prefix + content;
1442   - //printf(post.c_str());
  1440 + #if GVO_HUB_testing == 1
  1441 + printf(post.c_str());
  1442 + #endif
1443 1443 // Send content to socket
1444 1444 bool done = false;
1445 1445 do {
1446 1446 int length = post.length();
1447 1447 int sent_length = send(sock, post.c_str(), length, 0);
1448   - printf("Sent through socket: \n %s",post.c_str());
  1448 + //printf("Sent through socket: \n %s",post.c_str());
1449 1449 if (sent_length < length) {
1450 1450 post = post.substr(sent_length, std::string::npos);
1451 1451 }
... ... @@ -1483,6 +1483,7 @@ std::string GVOHub::receive_string(const socklen_t&amp; sock) const
1483 1483 int n = 0;
1484 1484 int m = 0;
1485 1485 //do {
  1486 +
1486 1487 printf("reading socket\n");
1487 1488 n = recv(sock, buffer, 4096,0);
1488 1489 printf("%d octets received: message %s\n",n,buffer);
... ... @@ -1539,7 +1540,9 @@ std::list&lt;std::string&gt; GVOHub::get_clientid(const GXml&amp; xml,
1539 1540 const GXmlText* text = static_cast<const GXmlText*>((*node)[0]);
1540 1541 if (text != NULL) {
1541 1542 value.push_front(text->text());
  1543 + #if GVO_HUB_testing == 1
1542 1544 std::cout << "Client id for which information is seeked:" + text->text() + "\n";
  1545 + #endif
1543 1546 }
1544 1547 }
1545 1548 }
... ... @@ -1549,8 +1552,13 @@ std::list&lt;std::string&gt; GVOHub::get_clientid(const GXml&amp; xml,
1549 1552 // Return value
1550 1553 return value;
1551 1554 }
1552   -/*
1553   -*/
  1555 +/***********************************************************************//**
  1556 + * @brief
  1557 + *
  1558 + * @param[in]
  1559 + *
  1560 + *
  1561 + ***************************************************************************/
1554 1562 std::string GVOHub::random_string( size_t length )
1555 1563 {
1556 1564 srand(time(0));
... ... @@ -1563,3 +1571,259 @@ std::string GVOHub::random_string( size_t length )
1563 1571 return str;
1564 1572 }
1565 1573  
  1574 +/***********************************************************************//**
  1575 + * @brief Test if any registered client needs to know that the method
  1576 + has been called
  1577 + *
  1578 + * @param[in] method: name of the method, cl_id id of calling client
  1579 + *
  1580 + *
  1581 + *
  1582 + *
  1583 + ***************************************************************************/
  1584 +void GVOHub::activate_callbacks(std::string method,char cl_id[31])
  1585 +{
  1586 + int i,j,k;
  1587 + char port[16];
  1588 + char pkey[16];
  1589 + std::string spkey;
  1590 +
  1591 + for (i = 0; i < GVOHUB_NB_CLIENTS; i++) {
  1592 + for (j = 0; j < GVOHUB_NB_METHODS; j++) {
  1593 + if (strcmp("",reg_clients->metadatas[i].registered_methods[j]) == 0) {
  1594 + continue;
  1595 + } else {
  1596 + //printf("Comparing %s and %s \n",method.c_str(),reg_clients->metadatas[i].registered_methods[j]);
  1597 + if (strcmp(method.c_str(),reg_clients->metadatas[i].registered_methods[j]) == 0) {
  1598 + strcpy(port,reg_clients->metadatas[i].port);
  1599 + printf("Callback needed on port %s\n",port);
  1600 + std::string port_string = std::string(port);
  1601 + //Eliminate http://127.0.0.1/ from port string -----> improve this code is necessary
  1602 + port_string.replace(0,17,"");
  1603 + printf("Callback needed on port %s\n",port_string.c_str());
  1604 + std::size_t pos = port_string.find("/");
  1605 + std::string port_only = port_string.substr(0,pos);
  1606 + printf("%s\n",port_only.c_str());
  1607 + // Set hints
  1608 + struct addrinfo hints;
  1609 + std::memset(&hints, 0, sizeof(hints));
  1610 + hints.ai_family = AF_INET;
  1611 + hints.ai_socktype = SOCK_STREAM;
  1612 +
  1613 + // Get server information
  1614 + struct addrinfo* servinfo;
  1615 + if (getaddrinfo("127.0.0.1", port_only.c_str(),
  1616 + &hints, &servinfo) == 0) {
  1617 +
  1618 + // Loop through all the results and connect to the first
  1619 + // we can
  1620 + for (struct addrinfo* ptr = servinfo; ptr != NULL; ptr = ptr->ai_next) {
  1621 +
  1622 + // Create socket
  1623 + cback_socket = socket(ptr->ai_family,
  1624 + ptr->ai_socktype,
  1625 + ptr->ai_protocol);
  1626 +
  1627 + // Connect to socket if socket is valid
  1628 + if (cback_socket != -1) {
  1629 + if (::connect(cback_socket,
  1630 + ptr->ai_addr,
  1631 + ptr->ai_addrlen) == -1) {
  1632 + close(cback_socket);
  1633 + cback_socket = -1;
  1634 + }
  1635 + }
  1636 +
  1637 + } // endfor: looped through all results
  1638 +
  1639 + } // endif: server information was valid
  1640 + std::string msg_cback = "";
  1641 +
  1642 + if (strcmp(method.c_str(),"samp.hub.event.register") == 0) {
  1643 + // Set methodResponse elts for samp.hub.event.register
  1644 + msg_cback.append("<?xml version=\"1.0\"?>\n");
  1645 +
  1646 + msg_cback.append("<methodCall><methodName>samp.client.receiveNotification</methodName>\n");
  1647 + msg_cback.append("\t<params>\n");
  1648 + msg_cback.append("\t\t<param><value>");
  1649 + msg_cback.append(reg_clients->metadatas[i].private_key);
  1650 + msg_cback.append("</value></param>\n");
  1651 + msg_cback.append("\t\t<param><value>"+m_hub_id+"</value></param>\n");
  1652 + msg_cback.append("\t\t<param>\n\t\t\t<value>\n\t\t\t\t<struct>\n");
  1653 + msg_cback.append("\t\t\t\t\t<member><name>samp.mtype</name><value>samp.hub.event.register</value></member>\n");
  1654 + msg_cback.append("\t\t\t\t\t<member><name>samp.params</name><value>\n\t\t\t\t\t\t<struct>\n\t\t\t\t\t\t\t<member>");
  1655 + msg_cback.append("<name>id</name><value>");
  1656 + msg_cback.append(cl_id);
  1657 + msg_cback.append("</value></member>\n\t\t\t\t\t\t</struct>\n\t\t\t\t\t</value></member>\n");
  1658 + msg_cback.append("\t\t\t\t</struct>\n\t\t\t</value>\n\t\t</param>\n");
  1659 + msg_cback.append("\t</params>\n");
  1660 +
  1661 + msg_cback.append("</methodCall>\n");
  1662 + post_string_toclient(msg_cback);
  1663 + }
  1664 + if (strcmp(method.c_str(),"samp.hub.event.unregister") == 0) {
  1665 + // Set methodResponse elts for samp.hub.event.unregister
  1666 + msg_cback.append("<?xml version=\"1.0\"?>\n");
  1667 +
  1668 + msg_cback.append("<methodCall><methodName>samp.client.receiveNotification</methodName>\n");
  1669 + msg_cback.append("\t<params>\n");
  1670 + msg_cback.append("\t\t<param><value>");
  1671 + msg_cback.append(reg_clients->metadatas[i].private_key);
  1672 + msg_cback.append("</value></param>\n");
  1673 + msg_cback.append("\t\t<param><value>"+m_hub_id+"</value></param>\n");
  1674 + msg_cback.append("\t\t<param>\n\t\t\t<value>\n\t\t\t\t<struct>\n");
  1675 + msg_cback.append("\t\t\t\t\t<member><name>samp.mtype</name><value>samp.hub.event.unregister</value></member>\n");
  1676 + msg_cback.append("\t\t\t\t\t<member><name>samp.params</name><value>\n\t\t\t\t\t\t<struct>\n\t\t\t\t\t\t\t<member>");
  1677 + msg_cback.append("<name>id</name><value>");
  1678 + msg_cback.append(cl_id);
  1679 + msg_cback.append("</value></member>\n\t\t\t\t\t\t</struct>\n\t\t\t\t\t</value></member>\n");
  1680 + msg_cback.append("\t\t\t\t</struct>\n\t\t\t</value>\n\t\t</param>\n");
  1681 + msg_cback.append("\t</params>\n");
  1682 +
  1683 + msg_cback.append("</methodCall>\n");
  1684 + post_string_toclient(msg_cback);
  1685 +
  1686 + }
  1687 + if (strcmp(method.c_str(),"samp.hub.event.metadata") == 0) {
  1688 + // Set methodResponse elts for samp.hub.event.unregister
  1689 + msg_cback.append("<?xml version=\"1.0\"?>\n");
  1690 +
  1691 + msg_cback.append("<methodCall><methodName>samp.client.receiveNotification</methodName>\n");
  1692 + msg_cback.append("\t<params>\n");
  1693 + msg_cback.append("\t\t<param><value>");
  1694 + msg_cback.append(reg_clients->metadatas[i].private_key);
  1695 + msg_cback.append("</value></param>\n");
  1696 + msg_cback.append("\t\t<param><value>"+m_hub_id+"</value></param>\n");
  1697 + msg_cback.append("\t\t<param>\n\t\t\t<value>\n\t\t\t\t<struct>\n");
  1698 + msg_cback.append("\t\t\t\t\t<member><name>samp.mtype</name><value>samp.hub.event.metadata</value></member>\n");
  1699 + msg_cback.append("\t\t\t\t\t<member><name>samp.params</name><value>\n\t\t\t\t\t\t<struct>\n\t\t\t\t\t\t\t");
  1700 + msg_cback.append("\t\t\t\t\t<member><name>id</name><value>");
  1701 + for (k = 0; k < GVOHUB_NB_CLIENTS; k++) {
  1702 + if (strcmp(cl_id,reg_clients->metadatas[k].reference) == 0) {
  1703 + break;
  1704 + }
  1705 + }
  1706 + msg_cback.append(reg_clients->metadatas[k].reference);
  1707 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1708 + msg_cback.append("\t\t\t\t\t<member><name>metadata</name><value>\n\t\t\t\t\t\t<struct>\n\t\t\t\t\t\t\t");
  1709 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.name</name>\n\t\t\t\t\t\t<value>");
  1710 + msg_cback.append(reg_clients->metadatas[k].name);
  1711 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1712 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.description.text</name>\n\t\t\t\t\t\t<value>");
  1713 + msg_cback.append(reg_clients->metadatas[k].description);
  1714 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1715 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.icon.url</name>\n\t\t\t\t\t\t<value>");
  1716 + msg_cback.append(reg_clients->metadatas[k].icon);
  1717 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1718 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>samp.documentation.url</name>\n\t\t\t\t\t\t<value>");
  1719 + msg_cback.append(reg_clients->metadatas[k].documentation);
  1720 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1721 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.affiliation</name>\n\t\t\t\t\t\t<value>");
  1722 + msg_cback.append(reg_clients->metadatas[k].affiliation);
  1723 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1724 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.email</name>\n\t\t\t\t\t\t<value>");
  1725 + msg_cback.append(reg_clients->metadatas[k].email);
  1726 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1727 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>author.name</name>\n\t\t\t\t\t\t<value>");
  1728 + msg_cback.append(reg_clients->metadatas[k].author_name);
  1729 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1730 + msg_cback.append("\t\t\t\t\t<member>\n\t\t\t\t\t\t<name>home.page</name>\n\t\t\t\t\t\t<value>");
  1731 + msg_cback.append(reg_clients->metadatas[k].homepage);
  1732 + msg_cback.append("</value>\n\t\t\t\t\t</member>\n");
  1733 + msg_cback.append("</struct></value></member>\n");
  1734 + msg_cback.append("</struct>\n\t\t\t\t\t</value></member>\n");
  1735 + msg_cback.append("\t\t\t\t</struct>\n\t\t\t</value>\n\t\t</param>\n");
  1736 + msg_cback.append("\t</params>\n");
  1737 +
  1738 + msg_cback.append("</methodCall>\n");
  1739 + post_string_toclient(msg_cback);
  1740 +
  1741 + }
  1742 +
  1743 +
  1744 +
  1745 + }
  1746 + }
  1747 + }
  1748 +
  1749 + }
  1750 +}
  1751 +/***********************************************************************//**
  1752 + * @brief Post string content to Hub
  1753 + *
  1754 + * @param[in] content String content to post
  1755 + *
  1756 + * Posts the content of a string to the Hub.
  1757 + *
  1758 + * The method does nothing if no Hub connection has been established.
  1759 + ***************************************************************************/
  1760 +void GVOHub::post_string_toclient(const std::string& content) const
  1761 +{
  1762 + // Continue only if Hub connection has been established
  1763 + if (cback_socket != -1) {
  1764 +
  1765 + // Determine content length
  1766 + int length = content.length();
  1767 +
  1768 + // Set prefix
  1769 + std::string prefix = "POST /xmlrpc HTTP/1.1\n"
  1770 + "Connection: keep-alive\n"
  1771 + "User-Agent: GammaLib\n"
  1772 + "Host: localhost:"+gammalib::str(cback_socket)+"\n"
  1773 + "Content-Type: text/xml\n"
  1774 + "Content-Length: "+gammalib::str(length)+"\n\n";
  1775 +
  1776 + // Build post string
  1777 + std::string post = prefix + content;
  1778 +
  1779 + // Send content to socket
  1780 + bool done = false;
  1781 + do {
  1782 + int length = post.length();
  1783 + int sent_length = send(cback_socket, post.c_str(), length, 0);
  1784 + #if GVO_HUB_testing == 1
  1785 + printf("Send to client: %s\n",post.c_str());
  1786 + #endif
  1787 + if (sent_length < length) {
  1788 + post = post.substr(sent_length, std::string::npos);
  1789 + }
  1790 + else {
  1791 + done = true;
  1792 + }
  1793 + } while (!done);
  1794 +
  1795 + // Read buffer until it is empty
  1796 + char buffer[1001];
  1797 + int n = 0;
  1798 + do {
  1799 + printf("receive socket instruction from callback response\n");
  1800 + fflush(stdout);
  1801 + n = recv(cback_socket, buffer, 1000, 0);
  1802 + printf("data received: %s\n",buffer);
  1803 + fflush(stdout);
  1804 + if (n > 0) {
  1805 + buffer[n+1] = '\0';
  1806 +
  1807 + }
  1808 + } while (n > 0);
  1809 + /*printf("%s",buffer);
  1810 + // Declare message
  1811 + std::string msg = "";
  1812 +
  1813 + // Set methodResponse elts
  1814 + msg.append("<?xml version=\"1.0\"?>\n");
  1815 + msg.append("<methodResponse>\n");
  1816 + msg.append("\t<params>\n");
  1817 + msg.append("\t\t<param>\n\t\t\t<value/>\n\t\t</param>\n");
  1818 + msg.append("\t</params>\n");
  1819 + msg.append("</methodResponse>\n");
  1820 +
  1821 +
  1822 +
  1823 + post_string(msg,cback_socket);*/
  1824 + } // endif: Hub connection had been established
  1825 +
  1826 + // Return
  1827 + return;
  1828 +}
  1829 +
... ...