From cd25c4ddcd970def376ac6722ea26cc9d752a5b5 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sat, 30 Jul 2011 15:58:48 +0200 Subject: [PATCH] [Positioning] Add class OutputNetworkSocket Factorise code of OutputUDPSocket and OutputTCPSocketEvAAL. --- owlps-positioning/Makefile | 21 +++++-- owlps-positioning/TODO | 2 - owlps-positioning/src/outputnetworksocket.cc | 60 +++++++++++++++++++ owlps-positioning/src/outputnetworksocket.hh | 50 ++++++++++++++++ owlps-positioning/src/outputtcpsocketevaal.cc | 43 ++----------- owlps-positioning/src/outputtcpsocketevaal.hh | 20 ++----- owlps-positioning/src/outputudpsocket.cc | 57 +++--------------- owlps-positioning/src/outputudpsocket.hh | 31 ++-------- 8 files changed, 151 insertions(+), 133 deletions(-) create mode 100644 owlps-positioning/src/outputnetworksocket.cc create mode 100644 owlps-positioning/src/outputnetworksocket.hh diff --git a/owlps-positioning/Makefile b/owlps-positioning/Makefile index fe62d29..87dc0cc 100644 --- a/owlps-positioning/Makefile +++ b/owlps-positioning/Makefile @@ -101,6 +101,7 @@ OBJ_NOTEST_LIST = \ posexcept.o \ multilaterationalgorithm.o \ cartographyalgorithm.o \ + outputnetworksocket.o \ inputmedium.o INTERFACES_LIST = \ inputlogmedium.hh \ @@ -246,12 +247,24 @@ $(OBJ_DIR)/outputcsv.o: \ $(OBJ_DIR)/textfilewriter.o \ $(OBJ_DIR)/resultlist.o \ $(OBJ_DIR)/result.o +$(OBJ_DIR)/outputnetworksocket.o: \ + $(SRC_DIR)/outputmedium.hh \ + $(OBJ_DIR)/resultlist.o \ + $(OBJ_DIR)/result.o $(OBJ_DIR)/outputudpsocket.o: \ - $(SRC_DIR)/outputmedium.hh \ - $(OBJ_DIR)/result.o + $(OBJ_DIR)/outputnetworksocket.o \ + $(OBJ_DIR)/request.o \ + $(OBJ_DIR)/resultlist.o \ + $(OBJ_DIR)/result.o \ + $(OBJ_DIR)/posexcept.o $(OBJ_DIR)/outputtcpsocketevaal.o: \ - $(SRC_DIR)/outputmedium.hh \ - $(OBJ_DIR)/result.o + $(OBJ_DIR)/outputnetworksocket.o \ + $(OBJ_DIR)/request.o \ + $(OBJ_DIR)/resultlist.o \ + $(OBJ_DIR)/result.o \ + $(OBJ_DIR)/area.o \ + $(OBJ_DIR)/posexcept.o \ + $(OBJ_DIR)/stock.o $(OBJ_DIR)/outputcsv.o: \ $(SRC_DIR)/outputmedium.hh \ $(OBJ_DIR)/result.o diff --git a/owlps-positioning/TODO b/owlps-positioning/TODO index 04cad7f..63fdceb 100644 --- a/owlps-positioning/TODO +++ b/owlps-positioning/TODO @@ -18,8 +18,6 @@ - Refactoring ° Split Stock::regenerate_reference_points() into several functions. - ° Create virtual class OutputSocket to factorise code of - OutputUDPSocket & OutputTCPSocketEvAAL. ° Synchronise InputCSV & InputUDPSocket (calibration requests), factorise code into InputMedium. ° Write a class for Request::type? diff --git a/owlps-positioning/src/outputnetworksocket.cc b/owlps-positioning/src/outputnetworksocket.cc new file mode 100644 index 0000000..3243d71 --- /dev/null +++ b/owlps-positioning/src/outputnetworksocket.cc @@ -0,0 +1,60 @@ +#include "outputnetworksocket.hh" +#include "resultlist.hh" +#include "result.hh" +#include "posexcept.hh" + +#include // For perror() + +using namespace std ; + + + +/* *** Constructors *** */ + + +OutputNetworkSocket::~OutputNetworkSocket() +{ + close_socket() ; +} + + + +/* *** Operations *** */ + + +/** + * Normally, the socket is closed automatically by the destructor. Use + * this if you want to close the socket prematurely. + * #sockfd is set to -1, even in case of error. + * @return \em true if the socket were successfully closed or were not + * opened. + * @return \em false in case of error. + */ +bool OutputNetworkSocket::close_socket() +{ + if (sockfd >= 0) + { + if (close(sockfd)) + { + perror("Cannot close network socket") ; + return false ; + } + + sockfd = -1 ; + } + + return true ; +} + + +void OutputNetworkSocket::write(const Result &result) +{ + send_data(result.to_csv()) ; +} + + +void OutputNetworkSocket::write(const ResultList &results) +{ + if (! results.empty()) + send_data(results.to_csv()) ; +} diff --git a/owlps-positioning/src/outputnetworksocket.hh b/owlps-positioning/src/outputnetworksocket.hh new file mode 100644 index 0000000..8c363dc --- /dev/null +++ b/owlps-positioning/src/outputnetworksocket.hh @@ -0,0 +1,50 @@ +#ifndef _OWLPS_POSITIONING_OUTPUTNETWORKSOCKET_HH_ +#define _OWLPS_POSITIONING_OUTPUTNETWORKSOCKET_HH_ + +#include "outputmedium.hh" + +#include +#include // is not C++ 98 compliant +#include + +/// Parent class for all output media that use network sockets +class OutputNetworkSocket: public OutputMedium +{ +protected: + int sockfd ; + std::string remote_host ; + uint_fast16_t remote_port ; + + struct sockaddr_in server_info ; + struct sockaddr_in client_info ; + + /** @name Operations */ + //@{ + /// Initialises the socket + /** + * You are required to call this function from the constructor of any + * derived class. + */ + virtual bool init_socket(void) = 0 ; + /// Sends a string through the socket + virtual bool send_data(const std::string &data) const = 0 ; + //@} + +public: + OutputNetworkSocket(const std::string &_remote_host, + const uint_fast16_t _remote_port): + sockfd(-1), remote_host(_remote_host), remote_port(_remote_port) {} + ~OutputNetworkSocket(void) ; + + /** @name Operations */ + //@{ + /// Sends a list of results through the socket + void write(const ResultList &results) ; + /// Sends a single result through the socket + void write(const Result &result) ; + /// Closes the socket + bool close_socket(void) ; + //@} +} ; + +#endif // _OWLPS_POSITIONING_OUTPUTNETWORKSOCKET_HH_ diff --git a/owlps-positioning/src/outputtcpsocketevaal.cc b/owlps-positioning/src/outputtcpsocketevaal.cc index 11fa53e..fedc35e 100644 --- a/owlps-positioning/src/outputtcpsocketevaal.cc +++ b/owlps-positioning/src/outputtcpsocketevaal.cc @@ -17,19 +17,13 @@ using namespace std ; /* *** Constructors *** */ -OutputTCPSocketEvAAL::OutputTCPSocketEvAAL( - const string &_remote_host, - const uint_fast16_t _remote_port): - remote_host(_remote_host), remote_port(_remote_port) +OutputTCPSocketEvAAL:: +OutputTCPSocketEvAAL(const string &_remote_host, + const uint_fast16_t _remote_port): + OutputNetworkSocket(_remote_host, _remote_port) { if (! init_socket()) - throw error_opening_output_file("TCP socket") ; -} - - -OutputTCPSocketEvAAL::~OutputTCPSocketEvAAL() -{ - close_socket() ; + throw error_opening_output_file("TCP socket (EvAAL)") ; } @@ -70,31 +64,6 @@ bool OutputTCPSocketEvAAL::init_socket() } -/** - * Normally, the socket is closed automatically by the destructor. Use - * this if you want to close the socket prematurely. - * #sockfd is set to -1, even in case of error. - * @return \em true if the socket were successfully closed or were not - * opened. - * @return \em false in case of error. - */ -bool OutputTCPSocketEvAAL::close_socket() -{ - if (sockfd >= 0) - { - if (close(sockfd)) - { - perror("Cannot close TCP socket") ; - return false ; - } - - sockfd = -1 ; - } - - return true ; -} - - /** * @param results Must contain only one element, since the EvAAL format * accepts only one algorithm result. @@ -157,7 +126,7 @@ bool OutputTCPSocketEvAAL::send_data(const string &data) const ssize_t nsent = send(sockfd, data.c_str(), data.size(), 0) ; if (nsent != static_cast(data.size())) { - perror("Error sending result data") ; + perror("Error sending result data through the TCPEvAAL socket") ; return false ; } return true ; diff --git a/owlps-positioning/src/outputtcpsocketevaal.hh b/owlps-positioning/src/outputtcpsocketevaal.hh index 281b71a..cf8c30e 100644 --- a/owlps-positioning/src/outputtcpsocketevaal.hh +++ b/owlps-positioning/src/outputtcpsocketevaal.hh @@ -3,27 +3,16 @@ class Point3D ; -#include "outputmedium.hh" - -#include -#include // is not C++ 98 compliant -#include +#include "outputnetworksocket.hh" /// Sends results to a remote host by TCP (EvAAL competition format) /** * The results are sent through an TCP socket as a string value, * conforming to the EvAAL competition format. */ -class OutputTCPSocketEvAAL: public OutputMedium +class OutputTCPSocketEvAAL: public OutputNetworkSocket { protected: - int sockfd ; - std::string remote_host ; - uint_fast16_t remote_port ; - - struct sockaddr_in server_info ; - struct sockaddr_in client_info ; - /** @name Operations */ //@{ /// Initialises the socket @@ -37,16 +26,15 @@ protected: public: OutputTCPSocketEvAAL(const std::string &_remote_host, const uint_fast16_t _remote_port) ; - ~OutputTCPSocketEvAAL(void) ; /** @name Operations */ //@{ + /// Sends a list of results through the socket void write(const ResultList &results) ; + /// Sends a single result through the socket void write(const Result &result) ; /// Get the area of interest number from the position int area_of_interest_number(const Point3D &position) const ; - /// Closes the socket - bool close_socket(void) ; //@} } ; diff --git a/owlps-positioning/src/outputudpsocket.cc b/owlps-positioning/src/outputudpsocket.cc index 4e4bc02..ebc8350 100644 --- a/owlps-positioning/src/outputudpsocket.cc +++ b/owlps-positioning/src/outputudpsocket.cc @@ -1,11 +1,11 @@ #include "outputudpsocket.hh" #include "request.hh" #include "resultlist.hh" +#include "result.hh" #include "posexcept.hh" #include -#include #include // For perror() using namespace std ; @@ -17,17 +17,10 @@ using namespace std ; OutputUDPSocket::OutputUDPSocket(const string &_remote_host, const uint_fast16_t _remote_port): - remote_host(_remote_host), remote_port(_remote_port) + OutputNetworkSocket(_remote_host, _remote_port) { if (! init_socket()) throw error_opening_output_file("UDP socket") ; - -} - - -OutputUDPSocket::~OutputUDPSocket() -{ - close_socket() ; } @@ -47,52 +40,18 @@ bool OutputUDPSocket::init_socket() } -/** - * Normally, the socket is closed automatically by the destructor. Use - * this if you want to close the socket prematurely. - * #sockfd is set to -1, even in case of error. - * @return \em true if the socket were successfully closed or were not - * opened. - * @return \em false in case of error. - */ -bool OutputUDPSocket::close_socket() -{ - if (sockfd >= 0) - { - if (close(sockfd)) - { - perror("Cannot close UDP socket") ; - return false ; - } - - sockfd = -1 ; - } - - return true ; -} - - -void OutputUDPSocket::write(const Result &result) -{ - send_data(result.to_csv()) ; -} - - -void OutputUDPSocket::write(const ResultList &results) -{ - if (! results.empty()) - send_data(results.to_csv()) ; -} - - /** * Sends the text buffer 'data'. */ -void OutputUDPSocket::send_data(const string &data) +bool OutputUDPSocket::send_data(const string &data) const { ssize_t nsent = sendto(sockfd, data.c_str(), data.size(), 0, (struct sockaddr *) &server_info, sizeof(server_info)) ; if (nsent != static_cast(data.size())) - perror("Error sending result data") ; + { + perror("Error sending result data through the UDP socket") ; + return false ; + } + return true ; } diff --git a/owlps-positioning/src/outputudpsocket.hh b/owlps-positioning/src/outputudpsocket.hh index 2e8b681..990978f 100644 --- a/owlps-positioning/src/outputudpsocket.hh +++ b/owlps-positioning/src/outputudpsocket.hh @@ -1,46 +1,27 @@ #ifndef _OWLPS_POSITIONING_OUTPUTUDPSOCKET_HH_ #define _OWLPS_POSITIONING_OUTPUTUDPSOCKET_HH_ -#include "outputmedium.hh" - -#include -#include // is not C++ 98 compliant -#include +#include "outputnetworksocket.hh" /// Sends results to a remote host by UDP /** * The results are sent through an UDP socket as a CSV string value. The * format used is the same as in OutputCSV. */ -class OutputUDPSocket: public OutputMedium +class OutputUDPSocket: public OutputNetworkSocket { protected: - int sockfd ; - std::string remote_host ; - uint_fast16_t remote_port ; - - struct sockaddr_in server_info ; - struct sockaddr_in client_info ; - /** @name Operations */ //@{ /// Initialises the socket bool init_socket(void) ; - void send_data(const std::string &data) ; + /// Sends a string through the socket + bool send_data(const std::string &data) const ; //@} public: - OutputUDPSocket(const std::string &_remote_ip, - const uint_fast16_t _port) ; - ~OutputUDPSocket(void) ; - - /** @name Operations */ - //@{ - void write(const Result &result) ; - void write(const ResultList &results) ; - /// Closes the socket - bool close_socket(void) ; - //@} + OutputUDPSocket(const std::string &_remote_host, + const uint_fast16_t _remote_port) ; } ;