2012-02-06 16:44:09 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Owl Positioning System (OwlPS).
|
2012-11-03 14:52:33 +01:00
|
|
|
* OwlPS is a project of the University of Franche-Comte
|
2012-02-06 16:44:09 +01:00
|
|
|
* (Université de Franche-Comté), France.
|
2012-11-03 14:52:33 +01:00
|
|
|
*
|
|
|
|
* Copyright © Université de Franche-Comté 2007-2012.
|
|
|
|
*
|
|
|
|
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
|
|
|
|
*
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
* This software is governed by the CeCILL license under French law and
|
|
|
|
* abiding by the rules of distribution of free software. You can use,
|
|
|
|
* modify and/or redistribute the software under the terms of the CeCILL
|
|
|
|
* license as circulated by CEA, CNRS and INRIA at the following URL:
|
|
|
|
* http://www.cecill.info
|
|
|
|
*
|
|
|
|
* As a counterpart to the access to the source code and rights to copy,
|
|
|
|
* modify and redistribute granted by the license, users are provided
|
|
|
|
* only with a limited warranty and the software's authors, the holder
|
|
|
|
* of the economic rights, and the successive licensors have only
|
|
|
|
* limited liability.
|
|
|
|
*
|
|
|
|
* In this respect, the user's attention is drawn to the risks
|
|
|
|
* associated with loading, using, modifying and/or developing or
|
|
|
|
* reproducing the software by the user in light of its specific status
|
|
|
|
* of free software, that may mean that it is complicated to manipulate,
|
|
|
|
* and that also therefore means that it is reserved for developers and
|
|
|
|
* experienced professionals having in-depth computer knowledge. Users
|
|
|
|
* are therefore encouraged to load and test the software's suitability
|
|
|
|
* as regards their requirements in conditions enabling the security of
|
|
|
|
* their systems and/or data to be ensured and, more generally, to use
|
|
|
|
* and operate it in the same conditions as regards security.
|
|
|
|
*
|
|
|
|
* The fact that you are presently reading this means that you have had
|
|
|
|
* knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
*
|
|
|
|
***********************************************************************
|
2012-02-06 16:44:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-04-04 17:37:19 +02:00
|
|
|
#include "outputudpsocket.hh"
|
2011-03-15 15:27:42 +01:00
|
|
|
#include "request.hh"
|
2011-04-05 12:05:46 +02:00
|
|
|
#include "resultlist.hh"
|
2011-07-30 15:58:48 +02:00
|
|
|
#include "result.hh"
|
2011-04-05 15:06:46 +02:00
|
|
|
#include "posexcept.hh"
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
#include <owlps.h>
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
#include <cstdio> // For perror()
|
2011-04-05 12:05:46 +02:00
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
2011-04-05 11:22:28 +02:00
|
|
|
OutputUDPSocket::OutputUDPSocket(const string &_remote_host,
|
|
|
|
const uint_fast16_t _remote_port):
|
2011-07-30 15:58:48 +02:00
|
|
|
OutputNetworkSocket(_remote_host, _remote_port)
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-05 15:06:46 +02:00
|
|
|
if (! init_socket())
|
|
|
|
throw error_opening_output_file("UDP socket") ;
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
/**
|
2011-06-14 16:44:17 +02:00
|
|
|
* @return \em true if the socket were successfully opened.
|
|
|
|
* @return \em false in case of error.
|
2011-04-05 15:06:46 +02:00
|
|
|
*/
|
|
|
|
bool OutputUDPSocket::init_socket()
|
2011-04-05 12:05:46 +02:00
|
|
|
{
|
2011-04-05 15:06:46 +02:00
|
|
|
sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port,
|
|
|
|
&server_info, &client_info) ;
|
|
|
|
return sockfd >= 0 ;
|
2011-04-05 12:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
/**
|
|
|
|
* Sends the text buffer 'data'.
|
|
|
|
*/
|
2011-07-30 15:58:48 +02:00
|
|
|
bool OutputUDPSocket::send_data(const string &data) const
|
2011-04-01 16:24:25 +02:00
|
|
|
{
|
2011-08-18 21:36:42 +02:00
|
|
|
unsigned int data_len = data.size() + 1 ; // +1 for the '\0'
|
|
|
|
ssize_t nsent = sendto(sockfd, data.c_str(), data_len, 0,
|
2011-04-05 15:06:46 +02:00
|
|
|
(struct sockaddr *) &server_info,
|
|
|
|
sizeof(server_info)) ;
|
2011-08-18 21:36:42 +02:00
|
|
|
if (nsent != static_cast<ssize_t>(data_len))
|
2011-07-30 15:58:48 +02:00
|
|
|
{
|
|
|
|
perror("Error sending result data through the UDP socket") ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
return true ;
|
2011-04-01 16:24:25 +02:00
|
|
|
}
|