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, 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 addSocketOption(optLevel, optName, section->get_option(strippedName)->as<bool>() ? 1 : 0);
140 } else {
141 addSocketOption(optLevel, optName, 0);
142 }
143 } catch ([[maybe_unused]] CLI::ConversionError& err) {
144 removeSocketOption(optLevel, optName);
145 }
146 } catch (CLI::OptionNotFound& err) {
147 LOG(ERROR) << err.what();
148 }
149 },
150 description,
151 typeName,
152 defaultValue,
153 validator)
154 ->force_callback();
155 }
156
157 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, int optValue) {
158 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
159
160 return *this;
161 }
162
163 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::string& optValue) {
164 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
165
166 return *this;
167 }
168
169 ConfigPhysicalSocket& ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::vector<char>& optValue) {
170 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
171
172 return *this;
173 }
174
176 socketOptionsMapMap[optLevel].erase(optName);
177 if (socketOptionsMapMap[optLevel].empty()) {
178 socketOptionsMapMap.erase(optLevel);
179 }
180
181 return *this;
182 }
183
185 retryOpt //
186 ->default_val(retry ? "true" : "false")
187 ->clear();
188
189 if (retry) {
190 retryLimitOpt->remove_needs(retryOpt);
191 retryJitterOpt->remove_needs(retryOpt);
192 retryBaseOpt->remove_needs(retryOpt);
193 retryTriesOpt->remove_needs(retryOpt);
194 retryTimeoutOpt->remove_needs(retryOpt);
195 retryOnFatalOpt->remove_needs(retryOpt);
196 } else {
197 retryLimitOpt->needs(retryOpt);
198 retryJitterOpt->needs(retryOpt);
199 retryBaseOpt->needs(retryOpt);
200 retryTriesOpt->needs(retryOpt);
203 }
204
205 return *this;
206 }
207
209 return retryOpt->as<bool>();
210 }
211
214 ->default_val(retry ? "true" : "false")
215 ->clear();
216
217 return *this;
218 }
219
221 return retryOnFatalOpt->as<bool>();
222 }
223
226 ->default_val(sec)
227 ->clear();
228
229 return *this;
230 }
231
233 return retryTimeoutOpt->as<double>();
234 }
235
238 ->default_val(tries)
239 ->clear();
240
241 return *this;
242 }
243
244 unsigned int ConfigPhysicalSocket::getRetryTries() const {
245 return retryTriesOpt->as<unsigned int>();
246 }
247
249 retryBaseOpt //
250 ->default_val(base)
251 ->clear();
252
253 return *this;
254 }
255
257 return retryBaseOpt->as<double>();
258 }
259
262 ->default_val(limit)
263 ->clear();
264
265 return *this;
266 }
267
268 unsigned int ConfigPhysicalSocket::getRetryLimit() const {
269 return retryLimitOpt->as<unsigned int>();
270 }
271
274 ->default_val(percent)
275 ->clear();
276
277 return *this;
278 }
279
281 return retryJitterOpt->as<double>();
282 }
283
284} // 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)
std::map< int, std::map< int, net::phy::PhysicalSocketOption > > socketOptionsMapMap
ConfigPhysicalSocket(ConfigInstance *instance)
const std::map< int, std::map< int, net::phy::PhysicalSocketOption > > & getSocketOptions()
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)
ConfigPhysicalSocket & setRetry(bool retry=true)
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