SNode.C
Loading...
Searching...
No Matches
FlowController.hpp
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
42#include "core/EventReceiver.h"
43#include "core/socket/stream/FlowController.h"
44#include "core/timer/Timer.h"
45#include "net/config/ConfigInstance.h"
46
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
48
49#endif // DOXYGEN_SHOULD_SKIP_THIS
50
51namespace core::socket::stream {
52
53 template <typename ConcreteFlowController>
54 uint64_t FlowController<ConcreteFlowController>::idCounter = 0;
55
56 template <typename ConcreteFlowController>
57 FlowController<ConcreteFlowController>::FlowController(net::config::ConfigInstance* configInstance)
58 : observedConfigInstance(configInstance)
60 })
62 }) {
63 }
64
65 template <typename ConcreteFlowController>
66 FlowController<ConcreteFlowController>::~FlowController() = default;
68 template <typename ConcreteFlowController>
69 std::string FlowController<ConcreteFlowController>::getInstanceName() const {
71 }
72
73 template <typename ConcreteFlowController>
74 uint64_t FlowController<ConcreteFlowController>::getId() const {
75 return id;
76 }
78 template <typename ConcreteFlowController>
79 bool FlowController<ConcreteFlowController>::terminateFlow() {
80 bool success = false;
82 if (!terminated) {
83 terminated = true;
84 retryEnabled = false;
86
90 success = true;
91 }
92
93 return success;
94 }
95
96 template <typename ConcreteFlowController>
97 bool FlowController<ConcreteFlowController>::isTerminated() const {
98 return terminated;
99 }
100
101 template <typename ConcreteFlowController>
102 void FlowController<ConcreteFlowController>::stopRetry() {
105 }
106
107 template <typename ConcreteFlowController>
108 bool FlowController<ConcreteFlowController>::isRetryEnabled() const {
109 return retryEnabled;
110 }
111
112 template <typename ConcreteFlowController>
113 ConcreteFlowController*
114 FlowController<ConcreteFlowController>::setOnFlowRetry(const std::function<void(ConcreteFlowController*)>& callback) {
115 const std::function<void(ConcreteFlowController*)> oldCallback = onFlowRetryCallback;
116 onFlowRetryCallback = [oldCallback, callback](ConcreteFlowController* flowController) {
117 oldCallback(flowController);
118 callback(flowController);
119 };
120
121 return dynamic_cast<ConcreteFlowController*>(this);
122 }
123
124 template <typename ConcreteFlowController>
125 ConcreteFlowController*
126 FlowController<ConcreteFlowController>::setOnFlowCompleted(const std::function<void(uint64_t, const std::string&)>& callback) {
127 observedConfigInstance->setOnDestroy([callback, id = getId()](net::config::ConfigInstance* configInstance) {
128 callback(id, configInstance->getInstanceName());
129 });
130
131 return dynamic_cast<ConcreteFlowController*>(this);
132 }
133
134 template <typename ConcreteFlowController>
135 ConcreteFlowController*
136 FlowController<ConcreteFlowController>::setOnFlowTerminated(const std::function<void(ConcreteFlowController*)>& callback) {
137 const std::function<void(ConcreteFlowController*)> oldCallback = onFlowTerminatedCallback;
138 onFlowTerminatedCallback = [oldCallback, callback](ConcreteFlowController* flowController) {
139 oldCallback(flowController);
140 callback(flowController);
141 };
142
143 return dynamic_cast<ConcreteFlowController*>(this);
144 }
145
146 template <typename ConcreteFlowController>
147 void FlowController<ConcreteFlowController>::startFlow(const std::function<void()>& callback) {
148 core::EventReceiver::atNextTick([this, callback] {
149 if (!terminated) {
150 callback();
151 }
152 });
153 }
154
155 template <typename ConcreteFlowController>
156 void FlowController<ConcreteFlowController>::reportFlowRetry() {
157 onFlowRetryCallback(dynamic_cast<ConcreteFlowController*>(this));
158 }
159
160 template <typename ConcreteFlowController>
161 void FlowController<ConcreteFlowController>::armRetryTimer(double timeoutSeconds, const std::function<void()>& dispatcher) {
162 if (retryEnabled) {
163 retryTimer = std::make_unique<core::timer::Timer>(core::timer::Timer::singleshotTimer(dispatcher, timeoutSeconds));
164 }
165 }
166
167 template <typename ConcreteFlowController>
168 void FlowController<ConcreteFlowController>::cancelRetryTimer() {
169 if (retryTimer) {
171 retryTimer.reset();
172 }
173 }
174
175 template <typename ConcreteFlowController>
176 void FlowController<ConcreteFlowController>::notifyFlowTerminated() {
177 onFlowTerminatedCallback(dynamic_cast<ConcreteFlowController*>(this));
178 }
179
180} // namespace core::socket::stream
static void atNextTick(const std::function< void(void)> &callBack)
void cancel()
Definition Timer.cpp:84
void observeConnectEventReceiver(core::eventreceiver::ConnectEventReceiver *connectEventReceiver)
std::set< core::eventreceiver::ConnectEventReceiver * > connectEventReceivers
std::unique_ptr< core::timer::Timer > reconnectTimer
void armReconnectTimer(double timeoutSeconds, const std::function< void()> &dispatcher)
ClientFlowController * setOnFlowReconnect(const std::function< void(ClientFlowController *)> &callback)
std::function< void(ClientFlowController *)> onFlowReconnectCallback
ClientFlowController(net::config::ConfigInstance *configInstance)
std::function< void(ConcreteFlowController *)> onFlowTerminatedCallback
ConcreteFlowController * setOnFlowCompleted(const std::function< void(uint64_t, const std::string &)> &callback)
void startFlow(const std::function< void()> &callback)
ConcreteFlowController * setOnFlowTerminated(const std::function< void(ConcreteFlowController *)> &callback)
ConcreteFlowController * setOnFlowRetry(const std::function< void(ConcreteFlowController *)> &callback)
void armRetryTimer(double timeoutSeconds, const std::function< void()> &dispatcher)
FlowController(net::config::ConfigInstance *configInstance)
net::config::ConfigInstance * observedConfigInstance
std::function< void(ConcreteFlowController *)> onFlowRetryCallback
std::unique_ptr< core::timer::Timer > retryTimer
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
ConfigInstance * setOnDestroy(const std::function< void(ConfigInstance *)> &onDestroy)
const std::string & getInstanceName() const
uint64_t FlowController< ConcreteFlowController >::idCounter
Definition Timer.h:59