Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
notification_bus.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <functional>
6#include <nlohmann/json.hpp>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <thread>
11#include <vector>
12
15
16namespace mm::node {
17
22inline constexpr std::string_view kNotificationTopic = "notifications";
23
57 public:
59 struct Source {
62 std::function<std::uint64_t()> revision;
69 std::function<std::optional<nlohmann::json>()> render;
81 std::chrono::milliseconds interval{20};
82 };
83
87 using PublishFn = std::function<void(std::string topic, std::string json)>;
88
90
93
98
102 void setPublish(PublishFn publish);
103
108 void addSource(Source source);
109
114 void start();
115
118 void stop();
119
120 private:
121 void run(std::stop_token stopToken);
122
124 static std::string envelope(const nlohmann::json& data);
125
127 std::chrono::steady_clock::time_point nextDeadline() const;
128
129 PublishFn publish_;
130 std::vector<Source> sources_;
131 std::vector<std::uint64_t> lastSeen_;
132 std::vector<std::chrono::steady_clock::time_point> nextDue_;
133 // Last on purpose: everything above is fully built before the thread that reads it starts, and
134 // the thread is joined before any of it goes away.
135 std::jthread thread_;
136};
137
138} // namespace mm::node
Polls a fixed set of sources and broadcasts a notification when one of them changes.
Definition notification_bus.h:56
NotificationBus(const NotificationBus &)=delete
NotificationBus(NotificationBus &&)=delete
void setPublish(PublishFn publish)
Sets where finished messages go. Call before start.
Definition notification_bus.cc:17
void start()
Starts the poll thread.
Definition notification_bus.cc:21
~NotificationBus()
Requests the stop and joins.
Definition notification_bus.cc:15
NotificationBus & operator=(const NotificationBus &)=delete
std::function< void(std::string topic, std::string json)> PublishFn
Receives a finished message and the topic to put it on. Wired to WebSocketServer::publish at the root...
Definition notification_bus.h:87
NotificationBus & operator=(NotificationBus &&)=delete
void addSource(Source source)
Registers a source. Call before start.
Definition notification_bus.cc:19
void stop()
Stops the poll thread and joins it. Returns immediately if it is not running, and does not wait out t...
Definition notification_bus.cc:37
Definition bus_health_source.h:7
constexpr std::string_view kNotificationTopic
The one topic every notification is published to.
Definition notification_bus.h:22
One feature's contribution: when to speak, and what to say.
Definition notification_bus.h:59
std::function< std::optional< nlohmann::json >()> render
Definition notification_bus.h:69
std::function< std::uint64_t()> revision
Definition notification_bus.h:62
std::chrono::milliseconds interval
Definition notification_bus.h:81