[Positioning] Add OutputSocket
This commit is contained in:
parent
c980d05baa
commit
6fbb0874e7
|
@ -76,6 +76,7 @@ OBJ_LIST = \
|
|||
output.o \
|
||||
outputterminal.o \
|
||||
outputcsv.o \
|
||||
outputsocket.o \
|
||||
positioning.o \
|
||||
input.o \
|
||||
inputcsv.o \
|
||||
|
@ -213,6 +214,9 @@ $(OBJ_DIR)/outputcsv.o: \
|
|||
$(SRC_DIR)/outputmedium.hh \
|
||||
$(OBJ_DIR)/textfilewriter.o \
|
||||
$(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)/configuration.o \
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include "outputterminal.hh"
|
||||
#include "outputcsv.hh"
|
||||
#include "outputsocket.hh"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -53,6 +54,9 @@ void Output::initialise_output_media()
|
|||
else if (*i == "CSV")
|
||||
initialise_output_csv() ;
|
||||
|
||||
else if (*i == "Socket")
|
||||
initialise_output_socket() ;
|
||||
|
||||
else
|
||||
throw bad_configuration(
|
||||
"The specified output medium « "+ *i +" » is unknown!") ;
|
||||
|
@ -65,6 +69,10 @@ void Output::initialise_output_terminal()
|
|||
output_media.push_back(new OutputTerminal) ;
|
||||
}
|
||||
|
||||
void Output::initialise_output_socket()
|
||||
{
|
||||
output_media.push_back(new OutputSocket) ;
|
||||
}
|
||||
|
||||
void Output::initialise_output_csv()
|
||||
{
|
||||
|
|
|
@ -18,6 +18,7 @@ protected:
|
|||
void initialise_output_media(void) ;
|
||||
void initialise_output_terminal(void) ;
|
||||
void initialise_output_csv(void) ;
|
||||
void initialise_output_socket(void) ;
|
||||
//@}
|
||||
|
||||
public:
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
#include "outputsocket.hh"
|
||||
#include "request.hh"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <netdb.h>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
//#include "mobile.hh"
|
||||
//#include <netinet/in.h>
|
||||
//#include <cstdlib>
|
||||
//#include <unistd.h>
|
||||
//#include <limits>
|
||||
|
||||
#define IP_R "127.0.0.1"
|
||||
#define PORT 1600
|
||||
using namespace std ;
|
||||
|
||||
struct hostent *hostInfo ;
|
||||
struct sockaddr_in serverAddress ;
|
||||
|
||||
|
||||
/* *** Operations *** */
|
||||
|
||||
|
||||
void OutputSocket::write(const Result &result)
|
||||
{
|
||||
string timestampXYZ;
|
||||
const Request *const request = result.get_request() ;
|
||||
|
||||
Point3D position = result.get_position() ;
|
||||
|
||||
timestampXYZ= uint2string(request->get_time_sent()) + ";" + float2string(position.get_x()) + ";" + float2string(position.get_y()) + ";" + float2string(position.get_z());
|
||||
|
||||
init_socket();
|
||||
//cout << timestampXYZ << endl;
|
||||
send_data(timestampXYZ);
|
||||
}
|
||||
|
||||
|
||||
string OutputSocket::float2string(float f)
|
||||
{
|
||||
ostringstream os;
|
||||
os << f;
|
||||
return os.str();
|
||||
|
||||
}
|
||||
|
||||
string OutputSocket::uint2string(uint64_t f)
|
||||
{
|
||||
ostringstream os;
|
||||
os << f;
|
||||
return os.str();
|
||||
|
||||
}
|
||||
|
||||
string OutputSocket::int2string(int f)
|
||||
{
|
||||
ostringstream os;
|
||||
os << f;
|
||||
return os.str();
|
||||
|
||||
}
|
||||
|
||||
void OutputSocket::init_socket()
|
||||
{
|
||||
hostInfo = gethostbyname(IP_R);
|
||||
serverPort = PORT;
|
||||
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(serverPort);
|
||||
|
||||
}
|
||||
|
||||
void OutputSocket::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);
|
||||
exit(1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef _OWLPS_POSITIONING_OUTPUTSOCKET_HH_
|
||||
#define _OWLPS_POSITIONING_OUTPUTSOCKET_HH_
|
||||
#include "outputmedium.hh"
|
||||
#include <string>
|
||||
#include <stdint.h> // <cstdint> is not C++ 98 compliant
|
||||
|
||||
//#include <netinet/in.h>
|
||||
//#include <unistd.h>
|
||||
|
||||
/// Send a result to socket
|
||||
class OutputSocket: public OutputMedium
|
||||
{
|
||||
private:
|
||||
int socketDescriptor ;
|
||||
unsigned short int serverPort ;
|
||||
|
||||
public:
|
||||
OutputSocket() {}
|
||||
|
||||
void write(const Result &result) ;
|
||||
|
||||
std::string float2string(float f) ;
|
||||
std::string uint2string(uint64_t f) ;
|
||||
std::string int2string(int f) ;
|
||||
void init_socket() ;
|
||||
void send_data(std::string msg) ;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
#endif // _OWLPS_POSITIONING_OUTPUTSOCKET_HH_
|
||||
|
Loading…
Reference in New Issue