[Positioning] OutputUDPSocket: finish cleaning

Use libowlps for socket creation.
Use Result::to_csv() and ResultList::to_csv().
This commit is contained in:
Matteo Cypriani 2011-04-05 15:06:46 +02:00
parent 2324c8e5d1
commit 3719f58b65
2 changed files with 68 additions and 54 deletions

View File

@ -1,18 +1,15 @@
#include "outputudpsocket.hh"
#include "request.hh"
#include "resultlist.hh"
#include "posexcept.hh"
#include <owlps.h>
#include <sstream>
#include <iostream>
#include <netdb.h>
#include <cstring>
#include <cstdio> // For perror()
using namespace std ;
struct hostent *hostInfo ;
struct sockaddr_in serverAddress ;
/* *** Constructors *** */
@ -22,13 +19,15 @@ OutputUDPSocket::OutputUDPSocket(const string &_remote_host,
const uint_fast16_t _remote_port):
remote_host(_remote_host), remote_port(_remote_port)
{
init_socket() ;
if (! init_socket())
throw error_opening_output_file("UDP socket") ;
}
OutputUDPSocket::~OutputUDPSocket()
{
kill_socket() ;
close_socket() ;
}
@ -36,56 +35,63 @@ OutputUDPSocket::~OutputUDPSocket()
/* *** Operations *** */
void OutputUDPSocket::write(const ResultList &results)
/**
* @return true if the socket were successfully opened, false in case of
* error.
*/
bool OutputUDPSocket::init_socket()
{
// Not implemented
sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port,
&server_info, &client_info) ;
return sockfd >= 0 ;
}
/**
* Normally, the socket is closed automatically by the destructor. Use
* this if you want to close the socket prematurely.
* #sockfd is set to -1, even in case of error.
* @return \em true if the socket were successfully closed or were not
* opened.
* @return \em false in case of error.
*/
bool OutputUDPSocket::close_socket()
{
if (sockfd >= 0)
{
if (close(sockfd))
{
perror("Cannot close UDP socket") ;
return false ;
}
sockfd = -1 ;
}
return true ;
}
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);
send_data(result.to_csv()) ;
}
void OutputUDPSocket::init_socket()
void OutputUDPSocket::write(const ResultList &results)
{
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);
send_data(results.to_csv()) ;
}
void OutputUDPSocket::send_data(string data)
/**
* Sends the text buffer 'data'.
*/
void OutputUDPSocket::send_data(const 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);
ssize_t nsent = sendto(sockfd, data.c_str(), data.size(), 0,
(struct sockaddr *) &server_info,
sizeof(server_info)) ;
if (nsent != static_cast<ssize_t>(data.size()))
perror("Error sending result data") ;
}

View File

@ -5,21 +5,28 @@
#include <string>
#include <stdint.h> // <cstdint> is not C++ 98 compliant
#include <arpa/inet.h>
/// Sends results to an UDP socket
/// Sends results to a remote host by UDP
/**
* The results are sent through an UDP socket as a CSV string value. The
* format used is the same as in OutputCSV.
*/
class OutputUDPSocket: public OutputMedium
{
private:
int socketDescriptor ;
protected:
int sockfd ;
std::string remote_host ;
uint_fast16_t remote_port ;
struct sockaddr_in server_info ;
struct sockaddr_in client_info ;
/** @name Operations */
//@{
/// Initialises the socket
void init_socket(void) ;
void kill_socket(void) ;
void send_data(std::string msg) ;
bool init_socket(void) ;
void send_data(const std::string &data) ;
//@}
public:
@ -29,9 +36,10 @@ public:
/** @name Operations */
//@{
/// Initialises the socket
void write(const Result &result) ;
void write(const ResultList &results) ;
/// Closes the socket
bool close_socket(void) ;
//@}
} ;