0.5.0
Loading...
Searching...
No Matches
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#include "UdpUtil.hpp"
18
19#ifdef _WIN32
20 // Set the proper SDK version before including boost/Asio
21 #include <SDKDDKVer.h>
22 // Note boost/ASIO includes Windows.h.
23 #include <boost/asio.hpp>
24#else
25 #include <boost/asio.hpp>
26#endif
27
28#include <array>
29#include <string>
30
31namespace NAV
32{
33/// UDP Client
34class UdpRecv : public Node
35{
36 public:
37 /// @brief Default constructor
38 UdpRecv();
39 /// @brief Destructor
40 ~UdpRecv() override;
41 /// @brief Copy constructor
42 UdpRecv(const UdpRecv&) = delete;
43 /// @brief Move constructor
44 UdpRecv(UdpRecv&&) = delete;
45 /// @brief Copy assignment operator
46 UdpRecv& operator=(const UdpRecv&) = delete;
47 /// @brief Move assignment operator
49
50 /// @brief String representation of the Class Type
51 [[nodiscard]] static std::string typeStatic();
52
53 /// @brief String representation of the Class Type
54 [[nodiscard]] std::string type() const override;
55
56 /// @brief String representation of the Class Category
57 [[nodiscard]] static std::string category();
58
59 /// @brief ImGui config window which is shown on double click
60 /// @attention Don't forget to set _hasConfig to true in the constructor of the node
61 void guiConfig() override;
62
63 /// @brief Saves the node into a json object
64 [[nodiscard]] json save() const override;
65
66 /// @brief Restores the node from a json object
67 /// @param[in] j Json object with the node state
68 void restore(const json& j) override;
69
70 /// @brief Resets the node. Moves the read cursor to the start
71 bool resetNode() override;
72
73 private:
74 constexpr static size_t OUTPUT_PORT_INDEX_NODE_DATA = 0; ///< @brief Object (NodeData)
75
76 /// @brief Initialize the node
77 bool initialize() override;
78
79 /// @brief Deinitialize the node
80 void deinitialize() override;
81
82 /// @brief Polls the next data
83 void asyncReceive();
84
85 /// UDP port number
86 int _port = 4567;
87
88 /// The selected output type in the GUI
90
91 /// Asynchronous receive fct
92 boost::asio::io_context _io_context;
93 /// Boost udp socket
94 boost::asio::ip::udp::socket _socket;
95 /// Boost udp endpoint
96 boost::asio::ip::udp::endpoint _sender_endpoint;
97
98 /// Receiver thread
99 std::thread _recvThread;
100
101 /// Flag that indicates the running data link
102 bool _running = false;
103 /// Startup handler: used in 'initialize()' to differentiate between startup and re-initialization
104 bool _isStartup = true;
105
106 /// Time point where the first package has been received
107 std::chrono::steady_clock::time_point _startPoint;
108
109 /// The array that contains the data from the UDP stream
110 std::array<char, UdpUtil::MAXIMUM_BYTES> _charArray{};
111};
112
113/// @brief Converts the enum to a string
114/// @param[in] value Enum value to convert into text
115/// @return String representation of the enum
116const char* to_string(NAV::UdpUtil::MessageType value);
117
118} // namespace NAV
nlohmann::json json
json namespace
Node Class.
Utility for the UDP Send and Receive nodes.
Node(std::string name)
Constructor.
Definition Node.cpp:30
static std::string category()
String representation of the Class Category.
Definition udpRecv.cpp:59
UdpRecv()
Default constructor.
Definition udpRecv.cpp:32
void asyncReceive()
Polls the next data.
Definition udpRecv.cpp:208
void guiConfig() override
ImGui config window which is shown on double click.
Definition udpRecv.cpp:64
UdpUtil::MessageType _outputType
The selected output type in the GUI.
Definition udpRecv.hpp:89
bool initialize() override
Initialize the node.
Definition udpRecv.cpp:161
~UdpRecv() override
Destructor.
Definition udpRecv.cpp:44
boost::asio::io_context _io_context
Asynchronous receive fct.
Definition udpRecv.hpp:92
UdpRecv(UdpRecv &&)=delete
Move constructor.
bool _isStartup
Startup handler: used in 'initialize()' to differentiate between startup and re-initialization.
Definition udpRecv.hpp:104
std::array< char, UdpUtil::MAXIMUM_BYTES > _charArray
The array that contains the data from the UDP stream.
Definition udpRecv.hpp:110
bool resetNode() override
Resets the node. Moves the read cursor to the start.
Definition udpRecv.cpp:108
UdpRecv & operator=(const UdpRecv &)=delete
Copy assignment operator.
std::thread _recvThread
Receiver thread.
Definition udpRecv.hpp:99
bool _running
Flag that indicates the running data link.
Definition udpRecv.hpp:102
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:107
void restore(const json &j) override
Restores the node from a json object.
Definition udpRecv.cpp:124
static constexpr size_t OUTPUT_PORT_INDEX_NODE_DATA
Object (NodeData)
Definition udpRecv.hpp:74
UdpRecv(const UdpRecv &)=delete
Copy constructor.
json save() const override
Saves the node into a json object.
Definition udpRecv.cpp:113
std::string type() const override
String representation of the Class Type.
Definition udpRecv.cpp:54
boost::asio::ip::udp::socket _socket
Boost udp socket.
Definition udpRecv.hpp:94
static std::string typeStatic()
String representation of the Class Type.
Definition udpRecv.cpp:49
int _port
UDP port number.
Definition udpRecv.hpp:86
void deinitialize() override
Deinitialize the node.
Definition udpRecv.cpp:198
boost::asio::ip::udp::endpoint _sender_endpoint
Boost udp endpoint.
Definition udpRecv.hpp:96
MessageType
Enum specifying the type of the output message.
Definition UdpUtil.hpp:28
@ PosVelAtt
Extract PosVelAtt data.
Definition UdpUtil.hpp:29
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.