ORE Studio 0.0.4
Loading...
Searching...
No Matches
fsm_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_DQ_CORE_MESSAGING_FSM_HANDLER_HPP
21#define ORES_DQ_CORE_MESSAGING_FSM_HANDLER_HPP
22
23#include <optional>
24#include <stdexcept>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.dq.api/messaging/fsm_protocol.hpp"
33#include "ores.dq.core/service/fsm_service.hpp"
34
35namespace ores::dq::messaging {
36
37using ores::service::messaging::reply;
38using ores::service::messaging::decode;
39using ores::service::messaging::error_reply;
40using namespace ores::logging;
41
42namespace {
43inline auto& fsm_handler_lg() {
44 static auto instance = ores::logging::make_logger(
45 "ores.dq.messaging.fsm_handler");
46 return instance;
47}
48} // namespace
49
50class fsm_handler {
51public:
52 fsm_handler(ores::nats::service::client& nats,
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
56
57 void list(ores::nats::message msg) {
58 BOOST_LOG_SEV(fsm_handler_lg(), debug) << "Handling " << msg.subject;
59 auto req = decode<get_fsm_states_request>(msg);
60 if (!req) {
61 BOOST_LOG_SEV(fsm_handler_lg(), warn)
62 << "Failed to decode: " << msg.subject;
63 reply(nats_, msg, get_fsm_states_response{
64 .success = false, .message = "Failed to decode request"});
65 return;
66 }
67 auto ctx_expected = ores::service::service::make_request_context(
68 ctx_, msg, verifier_);
69 if (!ctx_expected) {
70 error_reply(nats_, msg, ctx_expected.error());
71 return;
72 }
73 service::fsm_service svc(*ctx_expected);
74 try {
75 get_fsm_states_response resp;
76 if (req->machine_name.empty())
77 resp.states = svc.list_all_states();
78 else
79 resp.states = svc.list_states_for_machine(req->machine_name);
80 resp.success = true;
81 reply(nats_, msg, resp);
82 } catch (const std::exception& e) {
83 BOOST_LOG_SEV(fsm_handler_lg(), error)
84 << msg.subject << " failed: " << e.what();
85 reply(nats_, msg, get_fsm_states_response{
86 .success = false, .message = e.what()});
87 }
88 BOOST_LOG_SEV(fsm_handler_lg(), debug) << "Completed " << msg.subject;
89 }
90
91private:
94 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
95};
96
97}
98
99#endif
STL namespace.
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73