0.4.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
udpRecv.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9/// @file udpRecv.hpp
10/// @brief Asynchronous data link - receiver node
11/// @author M. Maier (marcel.maier@ins.uni-stuttgart.de)
12/// @date 2023-07-26
13
14#pragma once
15
16#ifdef _WIN32
17 // Set the proper SDK version before including boost/Asio
18 #include <SDKDDKVer.h>
19 // Note boost/ASIO includes Windows.h.
20 #include <boost/asio.hpp>
21#else // _WIN32
22 #include <boost/asio.hpp>
23#endif //_WIN32
24
26
28#include <string>
29
30namespace NAV
31{
32/// UDP Client
33class UdpRecv : public Node
34{
35 public:
36 /// @brief Default constructor
37 UdpRecv();
38 /// @brief Destructor
39 ~UdpRecv() override;
40 /// @brief Copy constructor
41 UdpRecv(const UdpRecv&) = delete;
42 /// @brief Move constructor
43 UdpRecv(UdpRecv&&) = delete;
44 /// @brief Copy assignment operator
45 UdpRecv& operator=(const UdpRecv&) = delete;
46 /// @brief Move assignment operator
48
49 /// @brief String representation of the Class Type
50 [[nodiscard]] static std::string typeStatic();
51
52 /// @brief String representation of the Class Type
53 [[nodiscard]] std::string type() const override;
54
55 /// @brief String representation of the Class Category
56 [[nodiscard]] static std::string category();
57
58 /// @brief ImGui config window which is shown on double click
59 /// @attention Don't forget to set _hasConfig to true in the constructor of the node
60 void guiConfig() override;
61
62 /// @brief Saves the node into a json object
63 [[nodiscard]] json save() const override;
64
65 /// @brief Restores the node from a json object
66 /// @param[in] j Json object with the node state
67 void restore(const json& j) override;
68
69 /// @brief Resets the node. Moves the read cursor to the start
70 bool resetNode() override;
71
72 private:
73 constexpr static size_t OUTPUT_PORT_INDEX_NODE_DATA = 0; ///< @brief Object (NodeData)
74
75 /// @brief Initialize the node
76 bool initialize() override;
77
78 /// @brief Deinitialize the node
79 void deinitialize() override;
80
81 /// @brief Polls the next data
82 void asyncReceive();
83
84 /// UDP port number
85 int _port = 4567;
86
87 /// Range a port can be in [0, 2^16-1]
88 static constexpr std::array<int, 2> PORT_LIMITS = { 0, 65535 };
89
90 /// Asynchronous receive fct
91 boost::asio::io_context _io_context;
92 /// Boost udp socket
93 boost::asio::ip::udp::socket _socket;
94 /// Boost udp endpoint
95 boost::asio::ip::udp::endpoint _sender_endpoint;
96
97 /// Receiver thread
98 std::thread _recvThread;
99
100 /// Flag that indicates the running data link
101 bool _running = false;
102 /// Startup handler: used in 'initialize()' to differentiate between startup and re-initialization
103 bool _isStartup = true;
104
105 /// Time point where the first package has been received
106 std::chrono::steady_clock::time_point _startPoint;
107
108 /// Network data stream maximum buffer size in [bytes]
109 constexpr static unsigned int _maxBytes = 104;
110
111 /// Network data stream array
112 std::array<double, 13> _data{};
113};
114} // namespace NAV
nlohmann::json json
json namespace
Node Class.
Position, Velocity and Attitude Storage Class.
Node(std::string name)
Constructor.
Definition Node.cpp:30
static std::string category()
String representation of the Class Category.
Definition udpRecv.cpp:50
UdpRecv()
Default constructor.
Definition udpRecv.cpp:23
void asyncReceive()
Polls the next data.
Definition udpRecv.cpp:135
void guiConfig() override
ImGui config window which is shown on double click.
Definition udpRecv.cpp:55
bool initialize() override
Initialize the node.
Definition udpRecv.cpp:88
static constexpr std::array< int, 2 > PORT_LIMITS
Range a port can be in [0, 2^16-1].
Definition udpRecv.hpp:88
~UdpRecv() override
Destructor.
Definition udpRecv.cpp:35
boost::asio::io_context _io_context
Asynchronous receive fct.
Definition udpRecv.hpp:91
UdpRecv(UdpRecv &&)=delete
Move constructor.
bool _isStartup
Startup handler: used in 'initialize()' to differentiate between startup and re-initialization.
Definition udpRecv.hpp:103
bool resetNode() override
Resets the node. Moves the read cursor to the start.
Definition udpRecv.cpp:64
UdpRecv & operator=(const UdpRecv &)=delete
Copy assignment operator.
std::array< double, 13 > _data
Network data stream array.
Definition udpRecv.hpp:112
std::thread _recvThread
Receiver thread.
Definition udpRecv.hpp:98
bool _running
Flag that indicates the running data link.
Definition udpRecv.hpp:101
UdpRecv & operator=(UdpRecv &&)=delete
Move assignment operator.
std::chrono::steady_clock::time_point _startPoint
Time point where the first package has been received.
Definition udpRecv.hpp:106
void restore(const json &j) override
Restores the node from a json object.
Definition udpRecv.cpp:79
static constexpr size_t OUTPUT_PORT_INDEX_NODE_DATA
Object (NodeData)
Definition udpRecv.hpp:73
UdpRecv(const UdpRecv &)=delete
Copy constructor.
json save() const override
Saves the node into a json object.
Definition udpRecv.cpp:69
std::string type() const override
String representation of the Class Type.
Definition udpRecv.cpp:45
boost::asio::ip::udp::socket _socket
Boost udp socket.
Definition udpRecv.hpp:93
static std::string typeStatic()
String representation of the Class Type.
Definition udpRecv.cpp:40
static constexpr unsigned int _maxBytes
Network data stream maximum buffer size in [bytes].
Definition udpRecv.hpp:109
int _port
UDP port number.
Definition udpRecv.hpp:85
void deinitialize() override
Deinitialize the node.
Definition udpRecv.cpp:125
boost::asio::ip::udp::endpoint _sender_endpoint
Boost udp endpoint.
Definition udpRecv.hpp:95