cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
26
#include "
msgpack_rpc/logging/logger.h
"
27
#include "
msgpack_rpc/messages/message_serializer.h
"
28
#include "
msgpack_rpc/messages/method_name_view.h
"
29
#include "
msgpack_rpc/messages/parsed_notification.h
"
30
#include "
msgpack_rpc/messages/parsed_request.h
"
31
#include "
msgpack_rpc/messages/serialized_message.h
"
32
#include "
msgpack_rpc/methods/i_method.h
"
33
#include "
msgpack_rpc/methods/i_method_processor.h
"
34
#include "
msgpack_rpc/methods/method_dict.h
"
35
36
namespace
msgpack_rpc::methods
{
37
41
class
MethodProcessor
final :
public
IMethodProcessor {
42
public
:
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
57
[[nodiscard]]
messages::SerializedMessage
call
(
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());
64
return
messages::MessageSerializer::serialize_error_response
(
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) {
74
MSGPACK_RPC_DEBUG
(
logger_
,
75
"Error when notifying to a method {}: {}"
,
76
notification.
method_name
(), e.what());
77
}
78
}
79
80
private
:
82
std::shared_ptr<logging::Logger>
logger_
;
83
85
MethodDict
methods_
{};
86
};
87
88
}
// namespace msgpack_rpc::methods
msgpack_rpc::messages::MessageSerializer::serialize_error_response
static SerializedMessage serialize_error_response(MessageID request_id, const T &error)
Serialize an error response.
Definition
message_serializer.h:91
msgpack_rpc::messages::ParsedNotification
Class of parsed notification.
Definition
parsed_notification.h:32
msgpack_rpc::messages::ParsedNotification::method_name
MethodNameView method_name() const noexcept
Get the method name.
Definition
parsed_notification.h:48
msgpack_rpc::messages::ParsedRequest
Class of parsed requests.
Definition
parsed_request.h:33
msgpack_rpc::messages::ParsedRequest::method_name
MethodNameView method_name() const noexcept
Get the method name.
Definition
parsed_request.h:60
msgpack_rpc::messages::ParsedRequest::id
MessageID id() const noexcept
Get the message ID.
Definition
parsed_request.h:53
msgpack_rpc::messages::SerializedMessage
Class of serialized message data.
Definition
serialized_message.h:34
msgpack_rpc::methods::IMethod::notify
virtual void notify(const messages::ParsedNotification ¬ification)=0
Notify this method.
msgpack_rpc::methods::IMethod::call
virtual messages::SerializedMessage call(const messages::ParsedRequest &request)=0
Call this method.
msgpack_rpc::methods::MethodDict
Class of dictionaries of methods.
Definition
method_dict.h:39
msgpack_rpc::methods::MethodDict::get
IMethod * get(messages::MethodNameView name) const
Get the method with a name. Throws an exception when not found.
Definition
method_dict.h:68
msgpack_rpc::methods::MethodDict::append
void append(std::unique_ptr< IMethod > method)
Append a method.
Definition
method_dict.h:51
msgpack_rpc::methods::MethodProcessor::notify
void notify(const messages::ParsedNotification ¬ification) override
Notify a method.
Definition
method_processor_impl.h:70
msgpack_rpc::methods::MethodProcessor::logger_
std::shared_ptr< logging::Logger > logger_
Logger.
Definition
method_processor_impl.h:82
msgpack_rpc::methods::MethodProcessor::methods_
MethodDict methods_
Dictionary of methods.
Definition
method_processor_impl.h:85
msgpack_rpc::methods::MethodProcessor::MethodProcessor
MethodProcessor(std::shared_ptr< logging::Logger > logger)
Constructor.
Definition
method_processor_impl.h:48
msgpack_rpc::methods::MethodProcessor::append
void append(std::unique_ptr< IMethod > method) override
Append a method.
Definition
method_processor_impl.h:52
msgpack_rpc::methods::MethodProcessor::call
messages::SerializedMessage call(const messages::ParsedRequest &request) override
Call a method.
Definition
method_processor_impl.h:57
i_method.h
Definition of IMethod class.
i_method_processor.h
Definition of IMethodProcessor class.
logger.h
Definition of Logger class.
MSGPACK_RPC_DEBUG
#define MSGPACK_RPC_DEBUG(LOGGER_PTR,...)
Write a debug log.
Definition
logger.h:186
message_serializer.h
Definition of MessageSerializer class.
method_dict.h
Definition of MethodDict class.
method_name_view.h
Definition of MethodNameView class.
msgpack_rpc::methods
Namespace of methods in MessagePack-RPC.
Definition
functional_method.h:41
std
STL namespace.
parsed_notification.h
Definition of ParsedNotification class.
parsed_request.h
Definition of ParsedRequest class.
serialized_message.h
Definition of SerializedMessage class.
src
msgpack_rpc
methods
method_processor_impl.h
Generated on
for cpp-msgpack-rpc by
1.18.0