diff --git a/owlps-positioning/Makefile b/owlps-positioning/Makefile index afc3f22..ad2121f 100644 --- a/owlps-positioning/Makefile +++ b/owlps-positioning/Makefile @@ -77,6 +77,7 @@ OBJ_LIST = \ output.o \ outputterminal.o \ outputcsv.o \ + outputudpsocket.o \ positioning.o \ input.o \ inputcsv.o \ @@ -220,8 +221,16 @@ $(OBJ_DIR)/outputcsv.o: \ $(OBJ_DIR)/textfilewriter.o \ $(OBJ_DIR)/resultlist.o \ $(OBJ_DIR)/result.o +$(OBJ_DIR)/outputudpsocket.o: \ + $(SRC_DIR)/outputmedium.hh \ + $(OBJ_DIR)/result.o +$(OBJ_DIR)/outputcsv.o: \ + $(SRC_DIR)/outputmedium.hh \ + $(OBJ_DIR)/result.o $(OBJ_DIR)/output.o: \ $(OBJ_DIR)/outputterminal.o \ + $(OBJ_DIR)/outputcsv.o \ + $(OBJ_DIR)/outputudpsocket.o \ $(OBJ_DIR)/configuration.o \ $(OBJ_DIR)/posexcept.o $(OBJ_DIR)/multilaterationalgorithm.o: \ diff --git a/owlps-positioning/src/output.cc b/owlps-positioning/src/output.cc index 75eb29d..0496953 100644 --- a/owlps-positioning/src/output.cc +++ b/owlps-positioning/src/output.cc @@ -4,6 +4,7 @@ #include "outputterminal.hh" #include "outputcsv.hh" +#include "outputudpsocket.hh" #include @@ -53,6 +54,9 @@ void Output::initialise_output_media() else if (*i == "CSV") initialise_output_csv() ; + else if (*i == "UDP") + initialise_output_udp_socket() ; + else throw bad_configuration( "The specified output medium « "+ *i +" » is unknown!") ; @@ -66,6 +70,18 @@ void Output::initialise_output_terminal() } +void Output::initialise_output_udp_socket() +{ + if (! Configuration::is_configured("output.udp-host")) + throw missing_configuration( + "No remote UDP host specified in the configuration!") ; + + output_media.push_back( + new OutputUDPSocket(Configuration::string_value("output.udp-host"), + Configuration::int_value("output.udp-port"))) ; +} + + void Output::initialise_output_csv() { if (! Configuration::is_configured("output.csv-file")) diff --git a/owlps-positioning/src/output.hh b/owlps-positioning/src/output.hh index 5884f07..29d4d15 100644 --- a/owlps-positioning/src/output.hh +++ b/owlps-positioning/src/output.hh @@ -18,6 +18,7 @@ protected: void initialise_output_media(void) ; void initialise_output_terminal(void) ; void initialise_output_csv(void) ; + void initialise_output_udp_socket(void) ; //@} public: diff --git a/owlps-positioning/src/outputudpsocket.cc b/owlps-positioning/src/outputudpsocket.cc new file mode 100644 index 0000000..b605209 --- /dev/null +++ b/owlps-positioning/src/outputudpsocket.cc @@ -0,0 +1,91 @@ +#include "outputudpsocket.hh" +#include "request.hh" +#include "resultlist.hh" + +#include +#include + +#include +#include + +using namespace std ; + +struct hostent *hostInfo ; +struct sockaddr_in serverAddress ; + + + +/* *** Constructors *** */ + + +OutputUDPSocket::OutputUDPSocket(const string &_remote_host, + const uint_fast16_t _remote_port): + remote_host(_remote_host), remote_port(_remote_port) +{ + init_socket() ; +} + + +OutputUDPSocket::~OutputUDPSocket() +{ + kill_socket() ; +} + + + +/* *** Operations *** */ + + +void OutputUDPSocket::write(const ResultList &results) +{ + // Not implemented +} + + +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); +} + + +void OutputUDPSocket::init_socket() +{ + 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); +} + + +void OutputUDPSocket::send_data(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); +} diff --git a/owlps-positioning/src/outputudpsocket.hh b/owlps-positioning/src/outputudpsocket.hh new file mode 100644 index 0000000..9502f75 --- /dev/null +++ b/owlps-positioning/src/outputudpsocket.hh @@ -0,0 +1,40 @@ +#ifndef _OWLPS_POSITIONING_OUTPUTUDPSOCKET_HH_ +#define _OWLPS_POSITIONING_OUTPUTUDPSOCKET_HH_ + +#include "outputmedium.hh" + +#include +#include // is not C++ 98 compliant + +/// Sends results to an UDP socket +class OutputUDPSocket: public OutputMedium +{ +private: + int socketDescriptor ; + std::string remote_host ; + uint_fast16_t remote_port ; + + /** @name Operations */ + //@{ + /// Initialises the socket + void init_socket(void) ; + void kill_socket(void) ; + void send_data(std::string msg) ; + //@} + +public: + OutputUDPSocket(const std::string &_remote_ip, + const uint_fast16_t _port) ; + ~OutputUDPSocket(void) ; + + /** @name Operations */ + //@{ + /// Initialises the socket + void write(const Result &result) ; + void write(const ResultList &results) ; + //@} +} ; + + + +#endif // _OWLPS_POSITIONING_OUTPUTUDPSOCKET_HH_ diff --git a/owlps-positioning/src/userinterface.cc b/owlps-positioning/src/userinterface.cc index 82d67cd..0d777ec 100644 --- a/owlps-positioning/src/userinterface.cc +++ b/owlps-positioning/src/userinterface.cc @@ -183,10 +183,15 @@ void UserInterface::fill_output_options() options.add_options() ("output.medium,O", po::value< vector >()->composing(), "Medium to which the results will be wrote. You can specify \ -this option more than once. Allowed: Terminal, CSV. \ +this option more than once. Allowed: Terminal, CSV, UDP. \ If this option is absent, results will be printed on the terminal.") ("output.csv-file", po::value(), "CSV file to use for output (when output.medium = CSV).") + ("output.udp-host", po::value(), + "Host to which the UDP data is sent (when output.medium = UDP).") + ("output.udp-port", po::value() + ->default_value(MOBILE_DEFAULT_PORT), + "Port on which the UDP data is sent (when output.medium = UDP).") ; file_options->add(options) ;