2011-03-15 15:27:42 +01:00
|
|
|
#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>
|
|
|
|
|
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 ;
|
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
2011-04-01 16:24:25 +02:00
|
|
|
/*OutputSocket::OutputSocket(const string m_ip)
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-01 16:24:25 +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-01 16:24:25 +02:00
|
|
|
kill_socket();
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
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-01 16:24:25 +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-01 16:24:25 +02:00
|
|
|
cout << timestampXYZ << endl;
|
|
|
|
send_data(timestampXYZ);
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSocket::init_socket()
|
|
|
|
{
|
2011-04-01 16:24:25 +02:00
|
|
|
cout << "Initialisation socket..." << endl;
|
|
|
|
hostInfo = gethostbyname(m_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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
void OutputSocket::kill_socket()
|
|
|
|
{
|
|
|
|
cout << "Fermeture de la socket..." << endl;
|
|
|
|
close(socketDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSocket::send_screen()
|
|
|
|
{
|
|
|
|
cout << "Hello world !!" << endl;
|
|
|
|
}
|