Commit 0ebbbedc37a69d542e8a0528504c0afdc82c58e5

Authored by Thierry Louge
2 parents e531ca5e dab81eb3

Merge branch '1229-GVOHub' of https://cta-git.irap.omp.eu/gammalib into 1229-GVOHub

Conflicts:
	include/GVOHub.hpp
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 Thierry Louge * 4 + * copyright (C) 2014-2015 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 *
@@ -68,23 +68,28 @@ protected: @@ -68,23 +68,28 @@ protected:
68 void init_members(void); 68 void init_members(void);
69 void copy_members(const GVOHub& client); 69 void copy_members(const GVOHub& client);
70 void free_members(void); 70 void free_members(void);
71 - void init_hub(void); 71 + void init_hub(void);
72 void start_hub(void); 72 void start_hub(void);
73 void register_service(const GXml& xml,const socklen_t& sock); 73 void register_service(const GXml& xml,const socklen_t& sock);
74 void ping_service(const socklen_t& sock); 74 void ping_service(const socklen_t& sock);
75 void unregister(const socklen_t& sock); 75 void unregister(const socklen_t& sock);
76 - void handle_request(const socklen_t& sock); 76 + void handle_request(const socklen_t& sock);
77 void register_metadata(const GXml& xml,const socklen_t& sock); 77 void register_metadata(const GXml& xml,const socklen_t& sock);
78 void declare_subscriptions(const GXml& xml,const socklen_t& sock); 78 void declare_subscriptions(const GXml& xml,const socklen_t& sock);
  79 +
79 // Low-level methods 80 // Low-level methods
80 - void post_string(const std::string& string) const;  
81 - std::string receive_string(void) const;  
82 - std::string get_response_value(const GXml& xml, const std::string& name) const;  
83 - std::list<std::string> get_registrations(const GXml& xml, const std::string& name) const;  
84 - void get_name_value_pair(const GXmlNode* node, std::string& name, std::string& value) const;  
85 - std::string get_hub_lockfile(void) const; 81 + void post_string(const std::string& string) const;
  82 + std::string receive_string(void) const;
  83 + std::string get_response_value(const GXml& xml,
  84 + const std::string& name) const;
  85 + std::list<std::string> get_registrations(const GXml& xml,
  86 + const std::string& name) const;
  87 + void get_name_value_pair(const GXmlNode* node,
  88 + std::string& name,
  89 + std::string& value) const;
  90 + std::string get_hub_lockfile(void) const;
86 91
87 - // Protected data area 92 + // Protected members
88 std::string m_name; //!< Client name 93 std::string m_name; //!< Client name
89 std::string m_secret; //!< Secret Hub key 94 std::string m_secret; //!< Secret Hub key
90 std::string m_hub_url; //!< The XML-RPC endpoint for communication with the hub 95 std::string m_hub_url; //!< The XML-RPC endpoint for communication with the hub
@@ -94,8 +99,13 @@ protected: @@ -94,8 +99,13 @@ protected:
94 std::string m_client_key; //!< Private client key 99 std::string m_client_key; //!< Private client key
95 std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others 100 std::string m_hub_id; //!< Hub identifier used by the hub when it sends message itself rather than forwarding from others
96 int m_socket; //!< Hub socket 101 int m_socket; //!< Hub socket
  102 +<<<<<<< HEAD
97 int m_nb_clients; //!< Number of already registered clients 103 int m_nb_clients; //!< Number of already registered clients
98 static std::list<GVOApp*> m_connected_apps; //!< list of apps connected to the hub 104 static std::list<GVOApp*> m_connected_apps; //!< list of apps connected to the hub
  105 +=======
  106 + int m_nb_clients; //!< Number of already registered clients
  107 + std::list<GVOApp> m_connected_apps; //!< list of apps connected to the hub
  108 +>>>>>>> dab81eb3c037358cd6c4dc35ed8cc4e35a5646c0
99 }; 109 };
100 110
101 #endif /* GVOHUB_HPP */ 111 #endif /* GVOHUB_HPP */
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 Thierry Louge * 4 + * copyright (C) 2014-2015 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 *
@@ -228,11 +228,21 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock) @@ -228,11 +228,21 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
228 char buffer[124]; 228 char buffer[124];
229 char privatekey[124]; 229 char privatekey[124];
230 int i; 230 int i;
231 - std::list<GVOApp*>::iterator q; 231 + std::list<GVOApp>::iterator q;
232 std::string content; 232 std::string content;
233 std::string translator = "http://localhost:8001/"; 233 std::string translator = "http://localhost:8001/";
234 printf("Message composition\n"); 234 printf("Message composition\n");
235 - 235 +
  236 + // Add application
  237 + GVOApp new_app;
  238 + sprintf(new_app.m_id,"client-key:%d", m_nb_clients);
  239 +std::cout << "Nb. of current applications registered:" << std::endl;
  240 +std::cout << m_connected_apps.size()<< std::endl;
  241 + m_connected_apps.push_front(new_app);
  242 +std::cout << "Nb. of current applications registered:" << std::endl;
  243 +std::cout << m_connected_apps.size()<< std::endl;
  244 +
  245 + /*
236 GVOApp* new_app = new GVOApp(); 246 GVOApp* new_app = new GVOApp();
237 sprintf(new_app -> m_id,"client-key:%d",m_nb_clients); 247 sprintf(new_app -> m_id,"client-key:%d",m_nb_clients);
238 std::cout<< "Nb. of current applications registered:" << std::endl; 248 std::cout<< "Nb. of current applications registered:" << std::endl;
@@ -241,10 +251,17 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock) @@ -241,10 +251,17 @@ void GVOHub::register_service(const GXml&amp; xml, const socklen_t&amp; sock)
241 std::cout<< new_app->m_id << std::endl; 251 std::cout<< new_app->m_id << std::endl;
242 std::cout<< "Nb. of current applications registered:" << std::endl; 252 std::cout<< "Nb. of current applications registered:" << std::endl;
243 std::cout<< m_connected_apps.size()<< std::endl; 253 std::cout<< m_connected_apps.size()<< std::endl;
244 - 254 + */
  255 +
  256 + /*
  257 + for (int i = 0; i < m_connected_apps.size(); ++i) {
  258 + std::cout << "Displaying element of applications list:" << std::endl;
  259 + std::cout << m_connected_apps[i].print() << std::endl;
  260 + }
  261 + */
245 for( i=0,q=m_connected_apps.begin(); i<m_connected_apps.size(); i++,q++ ) { 262 for( i=0,q=m_connected_apps.begin(); i<m_connected_apps.size(); i++,q++ ) {
246 std::cout << "Displaying element of applications list:" << std::endl; 263 std::cout << "Displaying element of applications list:" << std::endl;
247 - content = (*q)->print(); 264 + content = (q)->print();
248 std::cout << content << std::endl; 265 std::cout << content << std::endl;
249 } 266 }
250 267
@@ -341,7 +358,7 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock) @@ -341,7 +358,7 @@ void GVOHub::register_metadata(const GXml&amp; xml,const socklen_t&amp; sock)
341 void GVOHub::declare_subscriptions(const GXml& xml,const socklen_t& sock) 358 void GVOHub::declare_subscriptions(const GXml& xml,const socklen_t& sock)
342 { 359 {
343 std::list<std::string>::iterator p; 360 std::list<std::string>::iterator p;
344 - std::list<GVOApp*>::iterator q; 361 + std::list<GVOApp>::iterator q;
345 int i; 362 int i;
346 std::ofstream myfile; 363 std::ofstream myfile;
347 std::string content; 364 std::string content;
@@ -372,7 +389,7 @@ void GVOHub::declare_subscriptions(const GXml&amp; xml,const socklen_t&amp; sock) @@ -372,7 +389,7 @@ void GVOHub::declare_subscriptions(const GXml&amp; xml,const socklen_t&amp; sock)
372 std::cout<< m_connected_apps.size()<< std::endl; 389 std::cout<< m_connected_apps.size()<< std::endl;
373 for( i=0,q=m_connected_apps.begin(); i<m_connected_apps.size(); i++,q++ ) { 390 for( i=0,q=m_connected_apps.begin(); i<m_connected_apps.size(); i++,q++ ) {
374 std::cout << "Displaying element of applications list:" << std::endl; 391 std::cout << "Displaying element of applications list:" << std::endl;
375 - content = (*q)->print(); 392 + content = (q)->print();
376 std::cout << content << std::endl; 393 std::cout << content << std::endl;
377 } 394 }
378 395
@@ -460,17 +477,19 @@ std::string GVOHub::print(const GChatter&amp; chatter) const @@ -460,17 +477,19 @@ std::string GVOHub::print(const GChatter&amp; chatter) const
460 void GVOHub::init_members(void) 477 void GVOHub::init_members(void)
461 { 478 {
462 // Initialise members 479 // Initialise members
463 - m_name = "GammaLib";  
464 - m_secret = "mysupersecret#0032557sentence";  
465 - m_hub_url = "http://localhost:8001";  
466 - m_hub_host = "127.0.0.1";  
467 - m_hub_port = "8001";  
468 - m_version = "1.3"; 480 + m_name = "GammaLib";
  481 + m_secret = "mysupersecret#0032557sentence";
  482 + m_hub_url = "http://localhost:8001";
  483 + m_hub_host = "127.0.0.1";
  484 + m_hub_port = "8001";
  485 + m_version = "1.3";
469 m_client_key.clear(); 486 m_client_key.clear();
470 - m_hub_id = "b79884e0"; 487 + m_hub_id = "b79884e0";
471 //m_client_id.clear(); 488 //m_client_id.clear();
472 - m_socket = -1; // Signals no socket  
473 - m_nb_clients = 0; // No registered clients at initialization 489 + m_socket = -1; // Signals no socket
  490 + m_nb_clients = 0; // No registered clients at initialization
  491 + m_connected_apps.clear();
  492 +
474 // Return 493 // Return
475 return; 494 return;
476 } 495 }
@@ -484,16 +503,17 @@ void GVOHub::init_members(void) @@ -484,16 +503,17 @@ void GVOHub::init_members(void)
484 void GVOHub::copy_members(const GVOHub& hub) 503 void GVOHub::copy_members(const GVOHub& hub)
485 { 504 {
486 // Copy members 505 // Copy members
487 - m_name = hub.m_name;  
488 - m_secret = hub.m_secret;  
489 - m_hub_url = hub.m_hub_url;  
490 - m_hub_host = hub.m_hub_host;  
491 - m_hub_port = hub.m_hub_port;  
492 - m_version = hub.m_version;  
493 - m_client_key = hub.m_client_key;  
494 - m_hub_id = hub.m_hub_id;  
495 - //m_client_id = hub.m_client_id;  
496 - m_socket = hub.m_socket; 506 + m_name = hub.m_name;
  507 + m_secret = hub.m_secret;
  508 + m_hub_url = hub.m_hub_url;
  509 + m_hub_host = hub.m_hub_host;
  510 + m_hub_port = hub.m_hub_port;
  511 + m_version = hub.m_version;
  512 + m_client_key = hub.m_client_key;
  513 + m_hub_id = hub.m_hub_id;
  514 + //m_client_id = hub.m_client_id;
  515 + m_socket = hub.m_socket;
  516 + m_connected_apps = hub.m_connected_apps;
497 517
498 // Return 518 // Return
499 return; 519 return;
@@ -715,7 +735,7 @@ void GVOHub::start_hub(void) @@ -715,7 +735,7 @@ void GVOHub::start_hub(void)
715 { 735 {
716 struct sockaddr_in serv_addr,cli_addr; 736 struct sockaddr_in serv_addr,cli_addr;
717 int max_sd, portno,pid,max_clients = 10, i,activity,addrlen; 737 int max_sd, portno,pid,max_clients = 10, i,activity,addrlen;
718 - socklen_t hub_socket,newsocket,sockfd,clilen, client_socket[10]; 738 + socklen_t sockfd, client_socket[10];
719 int opt = 1; 739 int opt = 1;
720 // Prepare TCP/IP structure 740 // Prepare TCP/IP structure
721 bzero((char *) &serv_addr, sizeof(serv_addr)); 741 bzero((char *) &serv_addr, sizeof(serv_addr));
@@ -727,48 +747,46 @@ void GVOHub::start_hub(void) @@ -727,48 +747,46 @@ void GVOHub::start_hub(void)
727 //set of socket descriptors 747 //set of socket descriptors
728 fd_set readfds; 748 fd_set readfds;
729 749
730 - //initialise all client_socket[] to 0 so not checked  
731 - for (i = 0; i < max_clients; i++)  
732 - { 750 + // Initialise all client_socket[] to 0 so not checked
  751 + for (i = 0; i < max_clients; ++i) {
733 client_socket[i] = 0; 752 client_socket[i] = 0;
734 } 753 }
  754 +
  755 + // ...
  756 + socklen_t hub_socket = socket(AF_INET, SOCK_STREAM, 0);
735 757
736 - hub_socket = socket(AF_INET, SOCK_STREAM, 0);  
737 -  
738 - //Creation of hub main socket 758 + // Creation of hub main socket
739 if (hub_socket < 0) { 759 if (hub_socket < 0) {
740 - std::string msg = "Socket could not be created."; 760 + std::string msg = "Hub socket could not be created.";
741 throw GException::invalid_value(G_START_HUB, msg); 761 throw GException::invalid_value(G_START_HUB, msg);
742 } 762 }
743 763
744 - //set hub main socket to allow multiple connections  
745 - if( setsockopt(hub_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 )  
746 - {  
747 - std::string msg = "Socket could not be set to multiple connetcions."; 764 + // Set hub main socket to allow multiple connections
  765 + if (setsockopt(hub_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
  766 + std::string msg = "Hub socket could not be set to multiple connections.";
748 throw GException::invalid_value(G_START_HUB, msg); 767 throw GException::invalid_value(G_START_HUB, msg);
749 } 768 }
750 769
751 // Server socket is opened. Now, bind it to the port, with family etc. 770 // Server socket is opened. Now, bind it to the port, with family etc.
752 if (bind(hub_socket, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { 771 if (bind(hub_socket, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
753 - std::string msg = "Could not bind to server socket."; 772 + std::string msg = "Hub hub socket could not bind to server socket.";
754 throw GException::invalid_value(G_START_HUB, msg); 773 throw GException::invalid_value(G_START_HUB, msg);
755 } 774 }
756 775
757 // Now start listening for the clients: 5 requests simultaneously pending maximum 776 // Now start listening for the clients: 5 requests simultaneously pending maximum
758 - if (listen(hub_socket,5) < 0)  
759 - {  
760 - std::string msg = "Main hub socket couldn't start litening"; 777 + if (listen(hub_socket, 5) < 0) {
  778 + std::string msg = "Hub socket could not start listening.";
761 throw GException::invalid_value(G_START_HUB, msg); 779 throw GException::invalid_value(G_START_HUB, msg);
762 } 780 }
763 -  
764 - clilen = sizeof(cli_addr);  
765 - 781 +
  782 + // ...
  783 + socklen_t clilen = sizeof(cli_addr);
  784 +
  785 + // Main event handling loop
766 while (1) { 786 while (1) {
767 -  
768 787
769 // Accept connection from the client 788 // Accept connection from the client
770 -  
771 - newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen); 789 + socklen_t newsocket = accept(hub_socket, (struct sockaddr *)&cli_addr, &clilen);
772 if (newsocket < 0) { 790 if (newsocket < 0) {
773 std::string msg = "Client connection to socket not accepted."; 791 std::string msg = "Client connection to socket not accepted.";
774 throw GException::invalid_value(G_START_HUB, msg); 792 throw GException::invalid_value(G_START_HUB, msg);
@@ -776,9 +794,8 @@ void GVOHub::start_hub(void) @@ -776,9 +794,8 @@ void GVOHub::start_hub(void)
776 794
777 // Create child process to handle the request 795 // Create child process to handle the request
778 pid = fork(); 796 pid = fork();
779 -  
780 if (pid < 0) { 797 if (pid < 0) {
781 - std::string msg = "Not child process created."; 798 + std::string msg = "No child process created.";
782 throw GException::invalid_value(G_START_HUB, msg); 799 throw GException::invalid_value(G_START_HUB, msg);
783 } 800 }
784 if (pid == 0) { 801 if (pid == 0) {
@@ -787,16 +804,17 @@ void GVOHub::start_hub(void) @@ -787,16 +804,17 @@ void GVOHub::start_hub(void)
787 exit(0); 804 exit(0);
788 } 805 }
789 else { 806 else {
790 -  
791 close(newsocket); 807 close(newsocket);
792 808
793 } 809 }
794 810
795 - } 811 + } // endwhile
796 812
797 // Return 813 // Return
798 return; 814 return;
799 } 815 }
  816 +
  817 +
800 /***********************************************************************//** 818 /***********************************************************************//**
801 * @brief Returns value for a SAMP client query parameter 819 * @brief Returns value for a SAMP client query parameter
802 * 820 *