Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open_class_api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CLASS_API
open_class_api
Commits
89a65ec6
Commit
89a65ec6
authored
Aug 30, 2022
by
Querejeta Lomas, Leire
Browse files
Options
Downloads
Patches
Plain Diff
Subir nuevo archivo
parent
e9b95ec2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/class_server/include/class_server/class_server.hpp
+178
-0
178 additions, 0 deletions
src/class_server/include/class_server/class_server.hpp
with
178 additions
and
0 deletions
src/class_server/include/class_server/class_server.hpp
0 → 100644
+
178
−
0
View file @
89a65ec6
/**
* @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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment