88 lines
1.8 KiB
C++
88 lines
1.8 KiB
C++
|
#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);
|
||
|
}
|
||
|
}
|