SNode.C
Loading...
Searching...
No Matches
ConfigPhysicalSocket.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
43
44#include "net/config/ConfigSection.hpp"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "log/Logger.h"
49
50#include <functional>
51
52#endif // DOXYGEN_SHOULD_SKIP_THIS
53
54#define XSTR(s) STR(s)
55#define STR(s) #s
56
57namespace net::config {
58
60 : ConfigSection(instance, "socket", "Configuration of socket behavior") {
62 "--retry{true}",
63 "Automatically retry listen|connect",
64 "bool",
66 CLI::IsMember({"true", "false"}));
67
69 "--retry-on-fatal{true}",
70 [this]() {
71 if (retryOnFatalOpt->as<bool>() && !retryOpt->as<bool>()) {
72 throw CLI::RequiresError(retryOnFatalOpt->get_name(), retryOpt->get_name().append("=true"));
73 }
74 },
75 "Retry also on fatal errors",
76 "bool",
78 CLI::IsMember({"true", "false"}));
80
81 retryTimeoutOpt = addOption( //
82 "--retry-timeout",
83 "Timeout of the retry timer",
84 "sec",
85 RETRY_TIMEOUT,
86 CLI::NonNegativeNumber);
88
89 retryTriesOpt = addOption( //
90 "--retry-tries",
91 "Number of retry attempts before giving up (0 = unlimited)",
92 "tries",
93 RETRY_TRIES,
94 CLI::TypeValidator<unsigned int>());
95 retryTriesOpt->needs(retryOpt);
96
97 retryBaseOpt = addOption( //
98 "--retry-base",
99 "Base of exponential backoff",
100 "base",
101 RETRY_BASE,
102 CLI::PositiveNumber);
103 retryBaseOpt->needs(retryOpt);
104
105 retryJitterOpt = addOption( //
106 "--retry-jitter",
107 "Maximum jitter in percent to apply randomly to calculated retry timeout (0 to disable)",
108 "jitter",
109 RETRY_JITTER,
110 CLI::Range(0., 100.));
111 retryJitterOpt->needs(retryOpt);
112
113 retryLimitOpt = addOption( //
114 "--retry-limit",
115 "Upper limit in seconds of retry timeout (0 for infinite)",
116 "sec",
117 RETRY_LIMIT,
118 CLI::NonNegativeNumber);
119 retryLimitOpt->needs(retryOpt);
120 }
121
122 const std::map<int, std::map<int, const net::phy::PhysicalSocketOption>>& ConfigPhysicalSocket::getSocketOptions() {
123 return socketOptionsMapMap;
124 }
125
126 CLI::Option* ConfigPhysicalSocket::addSocketOption(const std::string& name,
127 int optLevel,
128 int optName,
129 const std::string& description,
130 const std::string& typeName,
131 const std::string& defaultValue,
132 const CLI::Validator& validator) {
133 return addFlagFunction(
134 name,
135 [this, strippedName = name.substr(0, name.find('{')), optLevel, optName]() {
136 try {
137 try {
138 if (section->get_option(strippedName)->as<bool>()) {
139 socketOptionsMapMap[optLevel].emplace(optName, net::phy::PhysicalSocketOption(optLevel, optName, 1));
140 } else {
141 socketOptionsMapMap[optLevel].emplace(optName, net::phy::PhysicalSocketOption(optLevel, optName, 0));
142 }
143 } catch ([[maybe_unused]] CLI::ConversionError& err) {
144 if (socketOptionsMapMap.contains(optLevel)) {
145 socketOptionsMapMap[optLevel].erase(optName);
146 if (socketOptionsMapMap[optLevel].empty()) {
147 socketOptionsMapMap.erase(optLevel);
148 }
149 }
150 }
151 } catch (CLI::OptionNotFound& err) {
152 LOG(ERROR) << err.what();
153 }
154 },
155 description,
156 typeName,
157 defaultValue,
158 validator)
159 ->force_callback();
160 }
161
162 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, int optValue) {
163 socketOptionsMapMap[optLevel].emplace(optName, net::phy::PhysicalSocketOption(optLevel, optName, optValue));
164
165 return *this;
166 }
167
168 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::string& optValue) {
169 socketOptionsMapMap[optLevel].emplace(optName, net::phy::PhysicalSocketOption(optLevel, optName, optValue));
170
171 return *this;
172 }
173
174 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::vector<char>& optValue) {
175 socketOptionsMapMap[optLevel].emplace(optName, net::phy::PhysicalSocketOption(optLevel, optName, optValue));
176
177 return *this;
178 }
179
181 socketOptionsMapMap[optLevel].erase(optName);
182 if (socketOptionsMapMap[optLevel].empty()) {
183 socketOptionsMapMap.erase(optLevel);
184 }
185
186 return *this;
187 }
188
190 retryOpt //
191 ->default_val(retry ? "true" : "false")
192 ->clear();
193
194 if (retry) {
195 retryLimitOpt->remove_needs(retryOpt);
196 retryJitterOpt->remove_needs(retryOpt);
197 retryBaseOpt->remove_needs(retryOpt);
198 retryTriesOpt->remove_needs(retryOpt);
199 retryTimeoutOpt->remove_needs(retryOpt);
200 retryOnFatalOpt->remove_needs(retryOpt);
201 } else {
202 retryLimitOpt->needs(retryOpt);
203 retryJitterOpt->needs(retryOpt);
204 retryBaseOpt->needs(retryOpt);
205 retryTriesOpt->needs(retryOpt);
208 }
209
210 return *this;
211 }
212
214 return retryOpt->as<bool>();
215 }
216
219 ->default_val(retry ? "true" : "false")
220 ->clear();
221
222 return *this;
223 }
224
226 return retryOnFatalOpt->as<bool>();
227 }
228
231 ->default_val(sec)
232 ->clear();
233
234 return *this;
235 }
236
238 return retryTimeoutOpt->as<double>();
239 }
240
243 ->default_val(tries)
244 ->clear();
245
246 return *this;
247 }
248
249 unsigned int ConfigPhysicalSocket::getRetryTries() const {
250 return retryTriesOpt->as<unsigned int>();
251 }
252
254 retryBaseOpt //
255 ->default_val(base)
256 ->clear();
257
258 return *this;
259 }
260
262 return retryBaseOpt->as<double>();
263 }
264
267 ->default_val(limit)
268 ->clear();
269
270 return *this;
271 }
272
273 unsigned int ConfigPhysicalSocket::getRetryLimit() const {
274 return retryLimitOpt->as<unsigned int>();
275 }
276
279 ->default_val(percent)
280 ->clear();
281
282 return *this;
283 }
284
286 return retryJitterOpt->as<double>();
287 }
288
289} // namespace net::config
#define XSTR(s)
ConfigPhysicalSocket & setRetryTimeout(double sec)
ConfigPhysicalSocket & setRetryJitter(double percent)
ConfigPhysicalSocket & addSocketOption(int optLevel, int optName, int optValue)
ConfigPhysicalSocket & addSocketOption(int optLevel, int optName, const std::string &optValue)
ConfigPhysicalSocket(ConfigInstance *instance)
ConfigPhysicalSocket & removeSocketOption(int optLevel, int optName)
ConfigPhysicalSocket & setRetryOnFatal(bool retry=true)
ConfigPhysicalSocket & addSocketOption(int optLevel, int optName, const std::vector< char > &optValue)
ConfigPhysicalSocket & setRetryLimit(unsigned int limit)
std::map< int, std::map< int, const net::phy::PhysicalSocketOption > > socketOptionsMapMap
ConfigPhysicalSocket & setRetry(bool retry=true)
const std::map< int, std::map< int, const net::phy::PhysicalSocketOption > > & getSocketOptions()
ConfigPhysicalSocket & setRetryBase(double base)
CLI::Option * addSocketOption(const std::string &name, int optLevel, int optName, const std::string &description, const std::string &typeName, const std::string &defaultValue, const CLI::Validator &validator)
ConfigPhysicalSocket & setRetryTries(unsigned int tries=0)
ConfigSection(ConfigInstance *instance, const std::string &name, const std::string &description)
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &additionalValidator)
CLI::Option * addFlagFunction(const std::string &name, const std::function< void()> &callback, const std::string &description, const std::string &typeName, const std::string &defaultValue, const CLI::Validator &validator)
PhysicalSocketOption(int optLevel, int optName, int optValue)
PhysicalSocketOption(int optLevel, int optName, const std::vector< char > &optValue)
PhysicalSocketOption(int optLevel, int optName, const std::string &optValue)
#define STR(a)
Definition clients.h:48