From 3719f58b65fc78280e09ef7bd862a282c8f44051 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Tue, 5 Apr 2011 15:06:46 +0200 Subject: [PATCH] [Positioning] OutputUDPSocket: finish cleaning Use libowlps for socket creation. Use Result::to_csv() and ResultList::to_csv(). --- owlps-positioning/src/outputudpsocket.cc | 100 ++++++++++++----------- owlps-positioning/src/outputudpsocket.hh | 22 +++-- 2 files changed, 68 insertions(+), 54 deletions(-) diff --git a/owlps-positioning/src/outputudpsocket.cc b/owlps-positioning/src/outputudpsocket.cc index b605209..6093286 100644 --- a/owlps-positioning/src/outputudpsocket.cc +++ b/owlps-positioning/src/outputudpsocket.cc @@ -1,18 +1,15 @@ #include "outputudpsocket.hh" #include "request.hh" #include "resultlist.hh" +#include "posexcept.hh" + +#include #include -#include - -#include -#include +#include // For perror() using namespace std ; -struct hostent *hostInfo ; -struct sockaddr_in serverAddress ; - /* *** Constructors *** */ @@ -22,13 +19,15 @@ OutputUDPSocket::OutputUDPSocket(const string &_remote_host, const uint_fast16_t _remote_port): remote_host(_remote_host), remote_port(_remote_port) { - init_socket() ; + if (! init_socket()) + throw error_opening_output_file("UDP socket") ; + } OutputUDPSocket::~OutputUDPSocket() { - kill_socket() ; + close_socket() ; } @@ -36,56 +35,63 @@ OutputUDPSocket::~OutputUDPSocket() /* *** Operations *** */ -void OutputUDPSocket::write(const ResultList &results) +/** + * @return true if the socket were successfully opened, false in case of + * error. + */ +bool OutputUDPSocket::init_socket() { - // Not implemented + sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port, + &server_info, &client_info) ; + return sockfd >= 0 ; +} + + +/** + * 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) { - string timestampXYZ; - ostringstream os; - - Point3D position = result.get_position() ; - - os - << position.get_x() << ';' - << position.get_y() << ';' - << position.get_z() ; - timestampXYZ = os.str() ; - - cout << timestampXYZ << '\n' ; - send_data(timestampXYZ); + send_data(result.to_csv()) ; } -void OutputUDPSocket::init_socket() +void OutputUDPSocket::write(const ResultList &results) { - cout << "Initialisation socket..." << endl; - hostInfo = gethostbyname(remote_host.c_str()); - socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0); - serverAddress.sin_family = hostInfo->h_addrtype; - memcpy((char *) &serverAddress.sin_addr.s_addr, - hostInfo->h_addr_list[0], hostInfo->h_length); - serverAddress.sin_port = htons(remote_port); + send_data(results.to_csv()) ; } -void OutputUDPSocket::send_data(string data) +/** + * Sends the text buffer 'data'. + */ +void OutputUDPSocket::send_data(const string &data) { - if (sendto(socketDescriptor, data.c_str(), data.size(), 0, - (struct sockaddr *) &serverAddress, - sizeof(serverAddress)) < 0) - { - cerr << "Émission du message impossible\n"; - close(socketDescriptor); - } -} - - -void OutputUDPSocket::kill_socket() -{ - cout << "Fermeture de la socket..." << endl; - close(socketDescriptor); + 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") ; } diff --git a/owlps-positioning/src/outputudpsocket.hh b/owlps-positioning/src/outputudpsocket.hh index 9502f75..2e8b681 100644 --- a/owlps-positioning/src/outputudpsocket.hh +++ b/owlps-positioning/src/outputudpsocket.hh @@ -5,21 +5,28 @@ #include #include // is not C++ 98 compliant +#include -/// Sends results to an UDP socket +/// 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 { -private: - int socketDescriptor ; +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 - void init_socket(void) ; - void kill_socket(void) ; - void send_data(std::string msg) ; + bool init_socket(void) ; + void send_data(const std::string &data) ; //@} public: @@ -29,9 +36,10 @@ public: /** @name Operations */ //@{ - /// Initialises the socket void write(const Result &result) ; void write(const ResultList &results) ; + /// Closes the socket + bool close_socket(void) ; //@} } ;