C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin_viewer_node.hpp
Go to the documentation of this file.
1// Copyright (C) 2026 Maik Knof
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN_VIEWER__YASMIN_VIEWER_NODE_HPP_
17#define YASMIN_VIEWER__YASMIN_VIEWER_NODE_HPP_
18
19#include <atomic>
20#include <chrono>
21#include <map>
22#include <mutex>
23#include <string>
24#include <thread>
25#include <unordered_map>
26#include <vector>
27
28#include <rclcpp/rclcpp.hpp>
29
30#include "yasmin_msgs/msg/state.hpp"
31#include "yasmin_msgs/msg/state_machine.hpp"
32#include "yasmin_msgs/msg/transition.hpp"
33
34namespace yasmin_viewer {
35
41class YasminViewerNode : public rclcpp::Node {
42public:
48
52 ~YasminViewerNode() override;
53
56
61 std::string get_fsms_json() const;
62
67 std::string get_web_root() const;
68
69private:
74 struct CachedFsm {
75 std::string json;
76 std::chrono::steady_clock::time_point timestamp;
77 };
78
79 using StateMachineMsg = yasmin_msgs::msg::StateMachine;
80 using StateMsg = yasmin_msgs::msg::State;
81 using TransitionMsg = yasmin_msgs::msg::Transition;
82
87 void fsm_viewer_cb(const StateMachineMsg::SharedPtr msg);
88
92 void start_server();
93
97 void stop_server();
98
102 void run_server();
103
108 void
109 prune_expired_locked(const std::chrono::steady_clock::time_point &now) const;
110
116 static std::string escape_json(const std::string &value);
117
123 static std::string
124 transitions_to_json(const std::vector<TransitionMsg> &transitions);
125
131 static std::string state_to_json(const StateMsg &state);
132
138 static std::string state_machine_to_json(const StateMachineMsg &msg);
139
141 mutable std::mutex fsms_mutex_;
143 mutable std::unordered_map<std::string, CachedFsm> fsms_;
144
146 rclcpp::Subscription<StateMachineMsg>::SharedPtr fsm_sub_;
147
149 std::string host_;
151 std::string web_root_;
153 int64_t port_;
156
158 std::atomic_bool server_running_;
160 std::thread server_thread_;
161};
162
163} // namespace yasmin_viewer
164
165#endif // YASMIN_VIEWER__YASMIN_VIEWER_NODE_HPP_
static std::string escape_json(const std::string &value)
Escapes a string for safe JSON serialization.
Definition yasmin_viewer_node.cpp:328
void stop_server()
Stops the embedded web server thread.
Definition yasmin_viewer_node.cpp:265
std::string host_
Host address used by the embedded web server.
Definition yasmin_viewer_node.hpp:149
std::mutex fsms_mutex_
Mutex protecting access to the finite state machine cache.
Definition yasmin_viewer_node.hpp:141
~YasminViewerNode() override
Destroys the YasminViewerNode and stops the web server.
Definition yasmin_viewer_node.cpp:221
void run_server()
Runs the embedded web server loop.
Definition yasmin_viewer_node.cpp:286
void start_server()
Starts the embedded web server thread.
Definition yasmin_viewer_node.cpp:260
std::string get_web_root() const
Returns the configured web root directory.
Definition yasmin_viewer_node.cpp:244
yasmin_msgs::msg::State StateMsg
Definition yasmin_viewer_node.hpp:80
std::unordered_map< std::string, CachedFsm > fsms_
Cache of serialized finite state machines indexed by name.
Definition yasmin_viewer_node.hpp:143
rclcpp::Subscription< StateMachineMsg >::SharedPtr fsm_sub_
Subscription for incoming state machine descriptions.
Definition yasmin_viewer_node.hpp:146
double max_age_seconds_
Maximum cache age in seconds before a finite state machine is removed.
Definition yasmin_viewer_node.hpp:155
std::string get_fsms_json() const
Returns the cached finite state machines as a JSON string.
Definition yasmin_viewer_node.cpp:223
void fsm_viewer_cb(const StateMachineMsg::SharedPtr msg)
Callback for incoming state machine messages.
Definition yasmin_viewer_node.cpp:246
YasminViewerNode()
Constructs the YasminViewerNode and initializes subscriptions, parameters, and the web server.
Definition yasmin_viewer_node.cpp:184
std::thread server_thread_
Background thread executing the embedded web server.
Definition yasmin_viewer_node.hpp:160
YasminViewerNode(const YasminViewerNode &)=delete
std::string web_root_
Root directory containing the web viewer files.
Definition yasmin_viewer_node.hpp:151
static std::string state_machine_to_json(const StateMachineMsg &msg)
Serializes a full state machine message into JSON.
Definition yasmin_viewer_node.cpp:410
yasmin_msgs::msg::Transition TransitionMsg
Definition yasmin_viewer_node.hpp:81
static std::string state_to_json(const StateMsg &state)
Serializes a state into JSON.
Definition yasmin_viewer_node.cpp:386
void prune_expired_locked(const std::chrono::steady_clock::time_point &now) const
Removes expired finite state machines from the cache.
Definition yasmin_viewer_node.cpp:315
yasmin_msgs::msg::StateMachine StateMachineMsg
Definition yasmin_viewer_node.hpp:79
static std::string transitions_to_json(const std::vector< TransitionMsg > &transitions)
Serializes a list of transitions into JSON.
Definition yasmin_viewer_node.cpp:368
int64_t port_
Port used by the embedded web server.
Definition yasmin_viewer_node.hpp:153
std::atomic_bool server_running_
Indicates whether the embedded web server is running.
Definition yasmin_viewer_node.hpp:158
YasminViewerNode & operator=(const YasminViewerNode &)=delete
Definition yasmin_viewer_node.hpp:34
Stores a serialized finite state machine and its last update time.
Definition yasmin_viewer_node.hpp:74
std::string json
Definition yasmin_viewer_node.hpp:75
std::chrono::steady_clock::time_point timestamp
Definition yasmin_viewer_node.hpp:76