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, 2026
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#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "log/Logger.h"
47
48#include <cstdint>
49#include <functional>
50
51#endif // DOXYGEN_SHOULD_SKIP_THIS
52
53#define XSTR(s) STR(s)
54#define STR(s) #s
55
56namespace net::config {
57
60
61 const std::map<int, std::map<int, net::phy::PhysicalSocketOption>>& ConfigPhysicalSocket::getSocketOptions() const {
63 }
64
65 CLI::Option* ConfigPhysicalSocket::addSocketOption(const std::string& name,
66 int optLevel,
67 int optName,
68 const std::string& description,
69 const std::string& typeName,
70 const std::string& defaultValue,
71 const CLI::Validator& validator) {
72 return addFlagFunction(
73 name,
74 [this, strippedName = name.substr(0, name.find('{')), optLevel, optName]() {
75 try {
76 try {
77 if (getOption(strippedName)->as<bool>()) {
78 addSocketOption(optLevel, optName, getOption(strippedName)->as<bool>() ? 1 : 0);
79 } else {
80 addSocketOption(optLevel, optName, 0);
81 }
82 } catch ([[maybe_unused]] CLI::ConversionError& err) {
83 removeSocketOption(optLevel, optName);
84 }
85 } catch (CLI::OptionNotFound& err) {
86 LOG(ERROR) << err.what();
87 }
88 },
89 description,
90 typeName,
91 defaultValue,
92 validator)
93 ->force_callback();
94 }
95
96 ConfigPhysicalSocket* ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, int optValue) {
97 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
98
99 return this;
100 }
101
102 ConfigPhysicalSocket* ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::string& optValue) {
103 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
104
105 return this;
106 }
107
108 ConfigPhysicalSocket* ConfigPhysicalSocket::addSocketOption(int optLevel, int optName, const std::vector<char>& optValue) {
109 socketOptionsMapMap[optLevel][optName] = net::phy::PhysicalSocketOption(optLevel, optName, optValue);
110
111 return this;
112 }
113
115 socketOptionsMapMap[optLevel].erase(optName);
116 if (socketOptionsMapMap[optLevel].empty()) {
117 socketOptionsMapMap.erase(optLevel);
118 }
119
120 return this;
121 }
122
124 setDefaultValue(retryOpt, retry ? "true" : "false");
125
126 if (retry) {
127 retryLimitOpt->remove_needs(retryOpt);
128 retryJitterOpt->remove_needs(retryOpt);
129 retryBaseOpt->remove_needs(retryOpt);
130 retryTriesOpt->remove_needs(retryOpt);
131 retryTimeoutOpt->remove_needs(retryOpt);
132 retryOnFatalOpt->remove_needs(retryOpt);
133 } else {
134 retryLimitOpt->needs(retryOpt);
135 retryJitterOpt->needs(retryOpt);
136 retryBaseOpt->needs(retryOpt);
137 retryTriesOpt->needs(retryOpt);
140 }
141
142 return this;
143 }
144
146 return retryOpt->as<bool>();
147 }
148
150 setDefaultValue(retryOnFatalOpt, retry ? "true" : "false");
151
152 return this;
153 }
154
156 return retryOnFatalOpt->as<bool>();
157 }
158
164
166 return retryTimeoutOpt->as<double>();
167 }
168
171
172 return this;
173 }
174
175 unsigned int ConfigPhysicalSocket::getRetryTries() const {
176 return retryTriesOpt->as<unsigned int>();
177 }
178
181
182 return this;
183 }
184
186 return retryBaseOpt->as<double>();
187 }
188
191
192 return this;
193 }
194
195 unsigned int ConfigPhysicalSocket::getRetryLimit() const {
196 return retryLimitOpt->as<unsigned int>();
197 }
198
201
202 return this;
203 }
204
206 return retryJitterOpt->as<double>();
207 }
208
209 const std::string ConfigPhysicalSocket::retry = XSTR(RETRY);
216
217} // namespace net::config
#define XSTR(s)
#define LOG(level)
Definition Logger.h:148
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
ConfigPhysicalSocket * setRetry(bool retry=true)
ConfigPhysicalSocket * addSocketOption(int optLevel, int optName, const std::vector< char > &optValue)
ConfigPhysicalSocket * setRetryTimeout(double sec)
ConfigPhysicalSocket * removeSocketOption(int optLevel, int optName)
ConfigPhysicalSocket * addSocketOption(int optLevel, int optName, int optValue)
ConfigPhysicalSocket * setRetryTries(unsigned int tries=0)
ConfigPhysicalSocket * addSocketOption(int optLevel, int optName, const std::string &optValue)
std::map< int, std::map< int, net::phy::PhysicalSocketOption > > socketOptionsMapMap
ConfigPhysicalSocket * setRetryLimit(unsigned int limit)
ConfigPhysicalSocket * setRetryOnFatal(bool retry=true)
ConfigPhysicalSocket * setRetryBase(double base)
const std::map< int, std::map< int, net::phy::PhysicalSocketOption > > & getSocketOptions() const
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 * setRetryJitter(double percent)
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)
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) const
CLI::Option * setDefaultValue(CLI::Option *option, const ValueTypeT &value, bool clear=true) const
Definition SubCommand.h:367
CLI::Option * getOption(const std::string &name) const
#define STR(a)
Definition clients.h:48