owlps/owlps-positioner/src/input.cc

228 lines
6.5 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* 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.
*
***********************************************************************
*/
#include "input.hh"
#include "inputcsv.hh"
#include "inputudpsocket.hh"
#include "inputlogcsv.hh"
#include "calibrationrequest.hh"
#include "configuration.hh"
#include "posexcept.hh"
#include "stock.hh"
#include <iostream>
#include <string>
using namespace std ;
/* *** Constructors *** */
Input::Input():
medium(NULL)
{
initialise_input_medium() ;
initialise_log_media() ;
}
Input::~Input()
{
delete medium ;
for (vector<InputLogMedium*>::const_iterator i = log_media.begin() ;
i != log_media.end() ; ++i)
delete *i ;
log_media.clear() ;
}
/* *** Operations *** */
void Input::initialise_input_medium()
{
if (! Configuration::is_configured("input.medium"))
throw missing_configuration(
"No input medium specified in configuration!") ;
const string &medium_name =
Configuration::string_value("input.medium") ;
if (medium_name == "CSV")
{
if (! Configuration::is_configured("input.csv-file"))
throw missing_configuration(
"No input CSV file specified in the configuration!") ;
medium = new
InputCSV(Configuration::string_value("input.csv-file")) ;
}
else if (medium_name == "UDP")
{
if (! Configuration::is_configured("input.udp-port"))
throw missing_configuration(
"No input UDP port specified in the configuration!") ;
medium = new
InputUDPSocket(Configuration::int_value("input.udp-port")) ;
}
else
throw bad_configuration(
"The specified input medium \""+ medium_name +"\" is unknown!") ;
}
void Input::initialise_log_media()
{
if (! Configuration::is_configured("log.medium"))
return ;
if (Configuration::value_exists_in_string_vector("log.medium", "none"))
return ;
const vector<string> &media_names =
Configuration::string_vector_value("log.medium") ;
for (vector<string>::const_iterator i = media_names.begin() ;
i != media_names.end() ; ++i)
{
if (*i == "CSV")
initialise_log_csv() ;
else
throw bad_configuration(
"The input medium \""+ *i +
"\" specified for request logging is unknown!") ;
}
}
void Input::initialise_log_csv()
{
if (! Configuration::is_configured("log.csv-file"))
throw missing_configuration(
"No log CSV file specified in the configuration!") ;
log_media.push_back(
new InputLogCSV(
Configuration::string_value("log.csv-file"))) ;
}
const Request& Input::get_next_request() const
{
if (eof())
return medium->get_current_request() ;
const Request& request = medium->get_next_request() ;
log_current_request() ;
if (! request)
return medium->get_current_request() ;
/* Update the current time */
if (Configuration::bool_value("replay"))
Timestamp::update_current_time(request.get_time_sent()) ;
/* Clean the old requests */
unsigned int cr_timeout =
Configuration::uint_value(
"positioning.calibration-requests-timeout") ;
if (cr_timeout > 0)
Stock::delete_calibration_requests_older_than(cr_timeout) ;
/* If the request is a calibration request, add it to the Stock */
CalibrationRequest *calibration_request =
dynamic_cast<CalibrationRequest*>(const_cast<Request*>(&request)) ;
if (calibration_request != NULL)
{
if (Configuration::bool_value(
"positioning.accept-new-calibration-requests"))
{
if (Configuration::is_configured("verbose"))
cerr
<< "Got a new calibration request (total "
<< Stock::nb_calibration_requests() << " in "
<< Stock::nb_reference_points()
<< " reference points).\n" ;
if (Configuration::bool_value(
"positioning.unique-calibration-requests"))
calibration_request->reference_point_delete_requests() ;
Stock::store_calibration_request(*calibration_request) ;
if (Configuration::bool_value(
"positioning.generate-reference-points"))
Stock::regenerate_reference_points() ;
}
else if (Configuration::is_configured("verbose"))
cerr
<< "Ignored new calibration request (positioning."
<< "accept-new-calibration-requests = false).\n" ;
if (! Configuration::bool_value(
"positioning.position-calibration-requests"))
medium->clear_current_request() ;
}
return medium->get_current_request() ;
}
bool Input::eof() const
{
if (medium == NULL)
throw null_input_medium() ;
return medium->eof() ;
}
void Input::log_current_request() const
{
for (vector<InputLogMedium*>::const_iterator i = log_media.begin() ;
i != log_media.end() ; ++i)
(*i)->log_request(medium->get_current_request()) ;
}