cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
parse_toml_client_server.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 <cstddef>
23
#include <exception>
24
#include <string>
25
#include <string_view>
26
#include <unordered_map>
27
#include <utility>
28
29
#include <toml++/toml.h>
30
31
#include "
msgpack_rpc/config/client_config.h
"
32
#include "
msgpack_rpc/config/executor_config.h
"
33
#include "
msgpack_rpc/config/message_parser_config.h
"
34
#include "
msgpack_rpc/config/reconnection_config.h
"
35
#include "
msgpack_rpc/config/server_config.h
"
36
#include "
msgpack_rpc/config/toml/parse_toml_common.h
"
37
38
namespace
msgpack_rpc::config::toml::impl {
39
46
inline
void
parse_toml
(
47
const ::toml::table& table,
MessageParserConfig
&
config
) {
48
for
(
const
auto
& [key, value] : table) {
49
const
auto
key_str = key.str();
50
if
(key_str ==
"read_buffer_size"
) {
51
MSGPACK_RPC_PARSE_TOML_VALUE
(
52
"read_buffer_size"
, read_buffer_size, std::size_t);
53
}
54
}
55
}
56
63
inline
void
parse_toml
(const ::toml::table& table,
ExecutorConfig
&
config
) {
64
for
(
const
auto
& [key, value] : table) {
65
const
auto
key_str = key.str();
66
if
(key_str ==
"num_transport_threads"
) {
67
MSGPACK_RPC_PARSE_TOML_VALUE
(
68
"num_transport_threads"
, num_transport_threads, std::size_t);
69
}
else
if
(key_str ==
"num_callback_threads"
) {
70
MSGPACK_RPC_PARSE_TOML_VALUE
(
71
"num_callback_threads"
, num_callback_threads, std::size_t);
72
}
73
}
74
}
75
82
inline
void
parse_toml
(const ::toml::table& table,
ReconnectionConfig
&
config
) {
83
for
(
const
auto
& [key, value] : table) {
84
const
auto
key_str = key.str();
85
if
(key_str ==
"initial_waiting_time_sec"
) {
86
MSGPACK_RPC_PARSE_TOML_VALUE_DURATION
(
87
"initial_waiting_time_sec"
, initial_waiting_time);
88
}
else
if
(key_str ==
"max_waiting_time_sec"
) {
89
MSGPACK_RPC_PARSE_TOML_VALUE_DURATION
(
90
"max_waiting_time_sec"
, max_waiting_time);
91
}
else
if
(key_str ==
"max_jitter_waiting_time_sec"
) {
92
MSGPACK_RPC_PARSE_TOML_VALUE_DURATION
(
93
"max_jitter_waiting_time_sec"
, max_jitter_waiting_time);
94
}
95
}
96
}
97
104
inline
void
parse_toml
(const ::toml::table& table,
ClientConfig
&
config
) {
105
for
(
const
auto
& [key, value] : table) {
106
const
auto
key_str = key.str();
107
if
(key_str ==
"uris"
) {
108
const
auto
* uris_node = value.as_array();
109
if
(uris_node ==
nullptr
) {
110
throw_error
(value.source(),
"uris"
);
111
}
112
for
(
const
auto
& elem : *uris_node) {
113
const
auto
* uri_node = elem.as_string();
114
if
(uri_node ==
nullptr
) {
115
throw_error
(elem.source(),
"uris"
);
116
}
117
try
{
118
config
.add_uri(uri_node->get());
119
}
catch
(
const
std::exception& e) {
120
throw_error
(elem.source(),
"uris"
, e.what());
121
}
122
}
123
}
else
if
(key_str ==
"call_timeout_sec"
) {
124
MSGPACK_RPC_PARSE_TOML_VALUE_DURATION
(
125
"call_timeout_sec"
, call_timeout);
126
}
else
if
(key_str ==
"message_parser"
) {
127
const
auto
* child_table = value.as_table();
128
if
(child_table ==
nullptr
) {
129
throw_error
(value.source(),
"message_parser"
);
130
}
131
parse_toml
(*child_table,
config
.message_parser());
132
}
else
if
(key_str ==
"executor"
) {
133
const
auto
* child_table = value.as_table();
134
if
(child_table ==
nullptr
) {
135
throw_error
(value.source(),
"executor"
);
136
}
137
parse_toml
(*child_table,
config
.executor());
138
}
else
if
(key_str ==
"reconnection"
) {
139
const
auto
* child_table = value.as_table();
140
if
(child_table ==
nullptr
) {
141
throw_error
(value.source(),
"reconnection"
);
142
}
143
parse_toml
(*child_table,
config
.reconnection());
144
}
145
}
146
}
147
154
inline
void
parse_toml
(const ::toml::table& table,
155
std::unordered_map<std::string, ClientConfig>& configs) {
156
for
(
const
auto
& [key, value] : table) {
157
const
auto
* table_ptr = value.as_table();
158
if
(table_ptr ==
nullptr
) {
159
throw_error
(value.source(),
"client"
,
160
"\"client\" must be a table of tables."
);
161
}
162
ClientConfig
config
;
163
parse_toml
(*table_ptr,
config
);
164
configs.try_emplace(std::string(key.str()), std::move(
config
));
165
}
166
}
167
174
inline
void
parse_toml
(const ::toml::table& table,
ServerConfig
&
config
) {
175
for
(
const
auto
& [key, value] : table) {
176
const
auto
key_str = key.str();
177
if
(key_str ==
"uris"
) {
178
const
auto
* uris_node = value.as_array();
179
if
(uris_node ==
nullptr
) {
180
throw_error
(value.source(),
"uris"
);
181
}
182
for
(
const
auto
& elem : *uris_node) {
183
const
auto
* uri_node = elem.as_string();
184
if
(uri_node ==
nullptr
) {
185
throw_error
(elem.source(),
"uris"
);
186
}
187
try
{
188
config
.add_uri(uri_node->get());
189
}
catch
(
const
std::exception& e) {
190
throw_error
(elem.source(),
"uris"
, e.what());
191
}
192
}
193
}
else
if
(key_str ==
"message_parser"
) {
194
const
auto
* child_table = value.as_table();
195
if
(child_table ==
nullptr
) {
196
throw_error
(value.source(),
"message_parser"
);
197
}
198
parse_toml
(*child_table,
config
.message_parser());
199
}
else
if
(key_str ==
"executor"
) {
200
const
auto
* child_table = value.as_table();
201
if
(child_table ==
nullptr
) {
202
throw_error
(value.source(),
"executor"
);
203
}
204
parse_toml
(*child_table,
config
.executor());
205
}
206
}
207
}
208
215
inline
void
parse_toml
(const ::toml::table& table,
216
std::unordered_map<std::string, ServerConfig>& configs) {
217
for
(
const
auto
& [key, value] : table) {
218
const
auto
* table_ptr = value.as_table();
219
if
(table_ptr ==
nullptr
) {
220
throw_error
(value.source(),
"server"
,
221
"\"server\" must be a table of tables."
);
222
}
223
ServerConfig
config
;
224
parse_toml
(*table_ptr,
config
);
225
configs.try_emplace(std::string(key.str()), std::move(
config
));
226
}
227
}
228
229
}
// namespace msgpack_rpc::config::toml::impl
msgpack_rpc::config::ClientConfig
Class of configuration of clients.
Definition
client_config.h:38
msgpack_rpc::config::ExecutorConfig
Class of configuration of executors.
Definition
executor_config.h:31
msgpack_rpc::config::MessageParserConfig
Class of configuration of parsers of messages.
Definition
message_parser_config.h:31
msgpack_rpc::config::ReconnectionConfig
Class of configurations of reconnection.
Definition
reconnection_config.h:31
msgpack_rpc::config::ServerConfig
Class of configurations of servers.
Definition
server_config.h:35
client_config.h
Definition of ClientConfig class.
executor_config.h
Definition of ExecutorConfig class.
message_parser_config.h
Definition of MessageParserConfig class.
msgpack_rpc::config
Namespace of configurations.
Definition
client_config.h:32
msgpack_rpc::config::toml::impl::parse_toml
void parse_toml(const ::toml::table &table, MessageParserConfig &config)
Parse a configuration of parsers of messages from TOML.
Definition
parse_toml_client_server.h:46
parse_toml_common.h
Definition of common functions and macros to parse TOML files.
MSGPACK_RPC_PARSE_TOML_VALUE
#define MSGPACK_RPC_PARSE_TOML_VALUE(KEY_STR, CONFIG_FUNCTION, TYPE)
Internal macro to parse a value from TOML.
Definition
parse_toml_common.h:69
msgpack_rpc::config::toml::impl::throw_error
void throw_error(const ::toml::source_region &source, std::string_view config_key)
Throw an exception for an error of TOML.
Definition
parse_toml_common.h:43
MSGPACK_RPC_PARSE_TOML_VALUE_DURATION
#define MSGPACK_RPC_PARSE_TOML_VALUE_DURATION(KEY_STR, CONFIG_FUNCTION)
Internal macro to parse a value from TOML.
Definition
parse_toml_common.h:81
reconnection_config.h
Definition of ReconnectionConfig class.
server_config.h
Definition of ServerConfig class.
src
msgpack_rpc
config
toml
parse_toml_client_server.h
Generated on
for cpp-msgpack-rpc by
1.18.0