ORE Studio 0.0.4
Loading...
Searching...
No Matches
message_dispatcher.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 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_COMMS_MESSAGING_MESSAGE_DISPATCHER_HPP
21#define ORES_COMMS_MESSAGING_MESSAGE_DISPATCHER_HPP
22
23#include <map>
24#include <memory>
25#include <expected>
26#include <boost/asio/awaitable.hpp>
27#include "ores.utility/log/make_logger.hpp"
28#include "ores.comms/messaging/frame.hpp"
29#include "ores.comms/messaging/message_handler.hpp"
30#include "ores.comms/service/auth_session_service.hpp"
31
32namespace ores::comms::messaging {
33
45class message_dispatcher final {
46private:
47 inline static std::string_view logger_name =
48 "ores.comms.messaging.message_dispatcher";
49
50 static auto& lg() {
51 using namespace ores::utility::log;
52 static auto instance = make_logger(logger_name);
53 return instance;
54 }
55
56public:
62 explicit message_dispatcher(std::shared_ptr<service::auth_session_service> sessions);
63
74 std::shared_ptr<message_handler> handler);
75
91 boost::asio::awaitable<std::expected<frame, error_code>>
92 dispatch(const frame& request_frame, std::uint32_t sequence,
93 const std::string& remote_address,
94 compression_type response_compression = compression_type::none);
95
96private:
102 message_handler* find_handler(message_type type) const;
103
110 message_type get_response_type(message_type request_type) const;
111
112 std::shared_ptr<service::auth_session_service> sessions_;
113 std::map<message_type_range, std::shared_ptr<message_handler>> handlers_;
114};
115
116}
117
118#endif
Contains messaging related infrastructure in the comms library.
Definition compression.hpp:29
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:104
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Complete frame with header and payload.
Definition frame.hpp:77
Dispatches incoming messages to registered subsystem handlers.
Definition message_dispatcher.hpp:45
boost::asio::awaitable< std::expected< frame, error_code > > dispatch(const frame &request_frame, std::uint32_t sequence, const std::string &remote_address, compression_type response_compression=compression_type::none)
Dispatch a request frame to the appropriate handler.
Definition message_dispatcher.cpp:38
void register_handler(message_type_range range, std::shared_ptr< message_handler > handler)
Register a handler for a range of message types.
Definition message_dispatcher.cpp:30
Range of message types handled by a subsystem.
Definition message_handler.hpp:37
Abstract interface for handling messages from a subsystem.
Definition message_handler.hpp:66