Skip to content
Snippets Groups Projects
Commit 89a65ec6 authored by Querejeta Lomas, Leire's avatar Querejeta Lomas, Leire
Browse files

Subir nuevo archivo

parent e9b95ec2
No related branches found
No related tags found
No related merge requests found
/**
* @file class_server.hpp
* @author Alfonso Dominguez <alfonso.dominguez@tecnalia.com>
* @date 2020
*
* Copyright 2020 Tecnalia Research & Innovation.
* Distributed under the GNU GPL v3.
* For full terms see https://www.gnu.org/licenses/gpl.txt
*
* @brief Class for server which connects to CLASS device and waits for client connections
*/
#ifndef CLASSSERVER_HPP
#define CLASSSERVER_HPP
#include <string>
#include <sstream>
#include <thread>
#include <memory>
#include <mutex>
#include <fstream>
#include <logger/logger.hpp>
//#include <communication/serial.hpp>
#include <communication/async_serial.hpp>
#include <communication/udp_server.hpp>
#include <communication/udp_client.hpp>
#if defined(_WIN32) || defined(WIN32)
#include <windows.h>
#endif
class ClassServer: public UdpServerListener, public SerialListener
{
public:
//! Basic constructor
ClassServer();
//! Basic destructor
~ClassServer();
/*!
\brief Connects to the CLASS and starts the server
\param port CLASS COM port to connect with.
\param listening_port Port in which server listens for connections.
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int start(const std::string & port, const int & listening_port, boost::asio::io_service *io_service);
/*!
\brief Disconnects from the CLASS and stops the server
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int stop();
/*!
\brief Handles the reception of an UDP msg from a client
\param msg Msg
\param ip IP the message comes from
*/
void udpMsgReceived(std::string msg, std::string client_ip);
/*!
\brief Handles the reception of a msg comming from serial
\param msg Msg
*/
void serialMsgReceived(std::string msg);
private:
//! thread for looking after variables
std::thread *wait_thread_;
//! function called by thread
void waitThreadFunction();
//! serial port for communication with CLASS
//Serial serial_;
AsyncSerial *serial_;
//! thread for sending msgs to CLASS
std::thread *serial_send_thread_;
//! function called by thread
void serialSendThreadFunction();
//! list of msgs to be send to CLASS
std::vector<std::string> msgs_to_class_;
//! mutex for accessing msg list
std::mutex msgs_to_class_mutex_;
//! list of msgs received from CLASS
std::vector<std::string> msgs_from_class_;
//! mutex for accessing msg list
std::mutex msgs_from_class_mutex_;
//! UDP server
UDPServer *udp_server_;
//! List of the ips connected to the server
std::vector<std::string> udp_client_ips_;
//! List of the ips connected to the server
std::vector<UDPClient*> udp_clients_;
//! thread for sending msgs to client
std::thread *udp_send_thread_;
//! function called by thread
void udpSendThreadFunction();
//! mutex for upd_clients access
std::mutex udp_mutex_;
//! indicates whether turn of has been received
bool turn_off_;
//! mutex for upd_clients access
std::mutex turn_off_mutex_;
//! indicates whether udp thread is running
bool continue_udp_;
//! indicates whether serial thread is running
bool continue_serial_;
// !external io_service
boost::asio::io_service *io_service_;
// ! file where msgs are stored
//std::ofstream myfile_;
/*!
\brief Connects to the CLASS
\param port CLASS COM port to connect with.
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int connectToSerial(const std::string & port);
/*!
\brief Disconnects from the CLASS
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int disconnectFromSerial();
/*!
\brief Starts listening fro UDP connections
\param port Port in which the server is listening.
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int startUDPServer(const int & listening_port);
/*!
\brief Stops listening
\param port Port in which the server is listening.
\return Integer indicating the success of the connection (0-success, <0 error)
*/
int stopUDPServer();
/*!
\brief Replaces substr in a string by another
\param str Original stringl
\param from substr to be replaced
\param to substr to replace
\return True if found and done
*/
bool replace(std::string& str, const std::string& from, const std::string& to);
/*!
\brief Trims a string
\param str Original stringl
\return The trimmed string
*/
std::string trim(std::string str);
//! log function
#define log_stream_(x) log_(std::stringstream() << x);
#define log_warn_stream_(x) log_warn_(std::stringstream() << x);
#define log_error_stream_(x) log_err_(std::stringstream() << x);
void log_(const std::string &text);
void log_(const std::stringstream &text);
void log_(std::ostream & text);
void log_err_(const std::string &text);
void log_err_(std::ostream & text);
void log_warn_(const std::string &text);
void log_warn_(std::ostream & text);
};
#endif // CLASSSERVER_HPP
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment