SNode.C
Loading...
Searching...
No Matches
EventLoop.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
42#include "core/EventLoop.h"
43
44#include "core/EventMultiplexer.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "log/Logger.h"
49#include "utils/Config.h"
50#include "utils/Timeval.h"
51#include "utils/system/signal.h"
52
53#include <chrono>
54#include <cstring>
55
56#endif /* DOXYGEN_SHOULD_SKIP_THIS */
57
58namespace core {
59
60 int EventLoop::stopsig = 0;
61 unsigned long EventLoop::tickCounter = 0;
63
64 static std::string getTickCounterAsString() {
65 std::string tick = std::to_string(EventLoop::getTickCounter());
66
67 if (tick.length() < 13) {
68 tick.insert(0, 13 - tick.length(), '0');
69 }
70
71 return tick;
72 }
73
75 : eventMultiplexer(::EventMultiplexer()) {
76 }
77
79 static EventLoop eventLoop;
80
81 return eventLoop;
82 }
83
84 unsigned long EventLoop::getTickCounter() {
85 return tickCounter;
86 }
87
91
95
96 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays, modernize-avoid-c-arrays)
97 bool EventLoop::init(int argc, char* argv[]) {
98 struct sigaction sact{};
99 sigemptyset(&sact.sa_mask);
100 sact.sa_flags = 0;
101 sact.sa_handler = SIG_IGN;
102
103 struct sigaction oldPipeAct{};
104 sigaction(SIGPIPE, &sact, &oldPipeAct);
105
106 struct sigaction oldIntAct{};
107 sigaction(SIGINT, &sact, &oldIntAct);
108
109 struct sigaction oldTermAct{};
110 sigaction(SIGTERM, &sact, &oldTermAct);
111
112 struct sigaction oldAlarmAct{};
113 sigaction(SIGALRM, &sact, &oldAlarmAct);
114
115 struct sigaction oldHupAct{};
116 sigaction(SIGHUP, &sact, &oldHupAct);
117
119
120 if (utils::Config::init(argc, argv)) {
122
123 LOG(TRACE) << "SNode.C: Starting ... HELLO";
124 }
125
126 sigaction(SIGPIPE, &oldPipeAct, nullptr);
127 sigaction(SIGINT, &oldIntAct, nullptr);
128 sigaction(SIGTERM, &oldTermAct, nullptr);
129 sigaction(SIGALRM, &oldAlarmAct, nullptr);
130 sigaction(SIGHUP, &oldHupAct, nullptr);
131
133 }
134
135 TickStatus EventLoop::_tick(const utils::Timeval& timeOut) {
136 TickStatus tickStatus = TickStatus::SUCCESS;
137
138 tickCounter++;
139
140 sigset_t newSet{};
141 sigaddset(&newSet, SIGINT);
142 sigaddset(&newSet, SIGTERM);
143 sigaddset(&newSet, SIGALRM);
144 sigaddset(&newSet, SIGHUP);
145
146 sigset_t oldSet{};
147 sigprocmask(SIG_BLOCK, &newSet, &oldSet);
148
150 tickStatus = eventMultiplexer.tick(timeOut, oldSet);
151 }
152
153 sigprocmask(SIG_SETMASK, &oldSet, nullptr);
154
155 return tickStatus;
156 }
157
158 TickStatus EventLoop::tick(const utils::Timeval& timeOut) {
159 TickStatus tickStatus = TickStatus::TRACE;
160
162 struct sigaction sact{};
163 sigemptyset(&sact.sa_mask);
164 sact.sa_flags = 0;
165 sact.sa_handler = SIG_IGN;
166
167 struct sigaction oldPipeAct{};
168 sigaction(SIGPIPE, &sact, &oldPipeAct);
169
170 tickStatus = EventLoop::instance()._tick(timeOut);
171
172 sigaction(SIGPIPE, &oldPipeAct, nullptr);
173 } else {
175 free();
176
177 PLOG(FATAL) << "Core: not initialized: No events will be processed\nCall SNodeC::init(argc, argv) before SNodeC::tick().";
178 }
179
180 return tickStatus;
181 }
182
183 int EventLoop::start(const utils::Timeval& timeOut) {
184 struct sigaction sact{};
185 sigemptyset(&sact.sa_mask);
186 sact.sa_flags = 0;
187 sact.sa_handler = SIG_IGN;
188
189 struct sigaction oldPipeAct{};
190 sigaction(SIGPIPE, &sact, &oldPipeAct);
191
192 sact.sa_handler = EventLoop::stoponsig;
193
194 struct sigaction oldIntAct{};
195 sigaction(SIGINT, &sact, &oldIntAct);
196
197 struct sigaction oldTermAct{};
198 sigaction(SIGTERM, &sact, &oldTermAct);
199
200 struct sigaction oldAlarmAct{};
201 sigaction(SIGALRM, &sact, &oldAlarmAct);
202
203 struct sigaction oldHupAct{};
204 sigaction(SIGHUP, &sact, &oldHupAct);
205
207 if (utils::Config::bootstrap()) {
209 core::TickStatus tickStatus = TickStatus::SUCCESS;
210
211 LOG(TRACE) << "Core::EventLoop: started";
212
213 do {
214 tickStatus = EventLoop::instance()._tick(timeOut);
215 } while ((tickStatus == TickStatus::SUCCESS || tickStatus == TickStatus::INTERRUPTED) && eventLoopState == State::RUNNING);
216
217 switch (tickStatus) {
219 LOG(TRACE) << "Core::EventLoop: Stopped";
220 break;
222 LOG(TRACE) << "Core::EventLoop: No Observer";
223 break;
225 LOG(TRACE) << "Core::EventLoop: Interrupted";
226 break;
228 PLOG(FATAL) << "Core::EventLoop: _tick()";
229 break;
230 }
231 } else {
232 stopsig = -2;
233 }
234 } else {
235 stopsig = -1;
236 }
237
238 sigaction(SIGPIPE, &oldPipeAct, nullptr);
239 sigaction(SIGTERM, &oldTermAct, nullptr);
240 sigaction(SIGALRM, &oldAlarmAct, nullptr);
241 sigaction(SIGHUP, &oldHupAct, nullptr);
242
243 free();
244
245 sigaction(SIGINT, &oldIntAct, nullptr);
246
247 return -stopsig;
248 }
249
253
254 void EventLoop::free() {
255 std::string signal = "SIG" + utils::system::sigabbrev_np(stopsig);
256
257 if (signal == "SIGUNKNOWN") {
258 signal = std::to_string(stopsig);
259 }
260
261 if (stopsig != 0) {
262 LOG(TRACE) << "Core: Sending signal " << signal << " to all DescriptorEventReceivers";
263
265 }
266
268
269 utils::Timeval timeout = 2;
270
271 core::TickStatus tickStatus = TickStatus::SUCCESS;
272 do {
273 auto t1 = std::chrono::system_clock::now();
274 const utils::Timeval timeoutOp = timeout;
275
276 tickStatus = EventLoop::instance()._tick(timeoutOp);
277
278 auto t2 = std::chrono::system_clock::now();
279 const std::chrono::duration<double> seconds = t2 - t1;
280
281 timeout -= seconds.count();
282 } while (timeout > 0 && (tickStatus == TickStatus::SUCCESS));
283
284 LOG(TRACE) << "Core: Terminate all stalled DescriptorEventReceivers";
285
287
288 LOG(TRACE) << "Core: Shutdown config system";
289
291
292 LOG(TRACE) << "Core: All resources released";
293
294 LOG(TRACE) << "SNode.C: Ended ... BYE";
295 }
296
297 void EventLoop::stoponsig(int sig) {
298 LOG(TRACE) << "Core: Received signal '" << strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = " << sig << ")";
299 stopsig = sig;
300 stop();
301 }
302
303} // namespace core
#define LOG(level)
Definition Logger.h:148
#define PLOG(level)
Definition Logger.h:152
static unsigned long tickCounter
Definition EventLoop.h:95
EventMultiplexer & getEventMultiplexer()
Definition EventLoop.cpp:88
static core::State getEventLoopState()
Definition EventLoop.cpp:92
static void free()
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:78
core::EventMultiplexer & eventMultiplexer
Definition EventLoop.h:91
static core::State eventLoopState
Definition EventLoop.h:97
static void stoponsig(int sig)
static int start(const utils::Timeval &timeOut)
static void stop()
static int stopsig
Definition EventLoop.h:93
static TickStatus tick(const utils::Timeval &timeOut)
static bool init(int argc, char *argv[])
Definition EventLoop.cpp:97
static unsigned long getTickCounter()
Definition EventLoop.cpp:84
TickStatus tick(const utils::Timeval &tickTimeOut, const sigset_t &sigMask)
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
static void setTickResolver(TickResolver resolver)
Definition Logger.cpp:209
static void terminate()
Definition Config.cpp:781
static bool init(int argc, char *argv[])
Definition Config.cpp:651
static bool bootstrap()
Definition Config.cpp:773
bool operator>(const Timeval &timeVal) const
Definition Timeval.cpp:149
Timeval & operator-=(const Timeval &timeVal)
Definition Timeval.cpp:130
TickStatus
Definition TickStatus.h:51
State
Definition State.h:51
@ RUNNING
Definition State.h:51
@ INITIALIZED
Definition State.h:51
@ STOPPING
Definition State.h:51
static std::string getTickCounterAsString()
Definition EventLoop.cpp:64
std::string sigabbrev_np(int sig)
Definition signal.cpp:59