cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
method_processor_impl.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <exception>
23#include <memory>
24#include <utility>
25
35
36namespace msgpack_rpc::methods {
37
41class MethodProcessor final : public IMethodProcessor {
42public:
48 explicit MethodProcessor(std::shared_ptr<logging::Logger> logger)
49 : logger_(std::move(logger)) {}
50
52 void append(std::unique_ptr<IMethod> method) override {
53 methods_.append(std::move(method));
54 }
55
58 const messages::ParsedRequest& request) override {
59 try {
60 return methods_.get(request.method_name())->call(request);
61 } catch (const std::exception& e) {
62 MSGPACK_RPC_DEBUG(logger_, "Error when calling a method {}: {}",
63 request.method_name(), e.what());
65 request.id(), e.what());
66 }
67 }
68
70 void notify(const messages::ParsedNotification& notification) override {
71 try {
72 methods_.get(notification.method_name())->notify(notification);
73 } catch (const std::exception& e) {
75 "Error when notifying to a method {}: {}",
76 notification.method_name(), e.what());
77 }
78 }
79
80private:
82 std::shared_ptr<logging::Logger> logger_;
83
86};
87
88} // namespace msgpack_rpc::methods
static SerializedMessage serialize_error_response(MessageID request_id, const T &error)
Serialize an error response.
MethodNameView method_name() const noexcept
Get the method name.
MethodNameView method_name() const noexcept
Get the method name.
MessageID id() const noexcept
Get the message ID.
Class of serialized message data.
Class of dictionaries of methods.
Definition method_dict.h:39
void notify(const messages::ParsedNotification &notification) override
Notify a method.
std::shared_ptr< logging::Logger > logger_
Logger.
MethodDict methods_
Dictionary of methods.
MethodProcessor(std::shared_ptr< logging::Logger > logger)
Constructor.
void append(std::unique_ptr< IMethod > method) override
Append a method.
messages::SerializedMessage call(const messages::ParsedRequest &request) override
Call a method.
Definition of IMethod class.
Definition of IMethodProcessor class.
Definition of Logger class.
#define MSGPACK_RPC_DEBUG(LOGGER_PTR,...)
Write a debug log.
Definition logger.h:186
Definition of MessageSerializer class.
Definition of MethodDict class.
Definition of MethodNameView class.
Namespace of methods in MessagePack-RPC.
STL namespace.
Definition of ParsedNotification class.
Definition of ParsedRequest class.
Definition of SerializedMessage class.