2011-03-15 15:27:42 +01:00
|
|
|
#include "outputsocket.hh"
|
|
|
|
#include "request.hh"
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
#define PORT 9910
|
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
struct hostent *hostInfo ;
|
|
|
|
struct sockaddr_in serverAddress ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
|
|
|
OutputSocket::OutputSocket(const string &_remote_ip):
|
|
|
|
remote_ip(_remote_ip)
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-04 16:45:00 +02:00
|
|
|
init_socket() ;
|
|
|
|
}
|
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
OutputSocket::~OutputSocket()
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-04 16:45:00 +02:00
|
|
|
kill_socket() ;
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
void OutputSocket::write(const Result &result)
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-01 16:24:25 +02:00
|
|
|
string timestampXYZ;
|
2011-03-15 15:27:42 +01:00
|
|
|
ostringstream os;
|
2011-04-01 16:24:25 +02:00
|
|
|
const Request *const request = result.get_request() ;
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
Point3D position = result.get_position() ;
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
os
|
|
|
|
<< request->get_time_sent() << ';'
|
|
|
|
<< position.get_x() << ';'
|
|
|
|
<< position.get_y() << ';'
|
|
|
|
<< position.get_z() ;
|
|
|
|
timestampXYZ = os.str() ;
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
cout << timestampXYZ << '\n' ;
|
2011-04-01 16:24:25 +02:00
|
|
|
send_data(timestampXYZ);
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
void OutputSocket::init_socket()
|
|
|
|
{
|
2011-04-01 16:24:25 +02:00
|
|
|
cout << "Initialisation socket..." << endl;
|
2011-04-04 16:45:00 +02:00
|
|
|
hostInfo = gethostbyname(remote_ip.c_str());
|
2011-03-15 15:27:42 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-04-01 16:24:25 +02:00
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
void OutputSocket::kill_socket()
|
|
|
|
{
|
|
|
|
cout << "Fermeture de la socket..." << endl;
|
|
|
|
close(socketDescriptor);
|
|
|
|
}
|