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
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/DynamicLoader.h"
45#include "core/EventMultiplexer.h"
46
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
48
49#include "log/Logger.h"
50#include "utils/Config.h"
51#include "utils/Timeval.h"
52#include "utils/system/signal.h"
53
54#include <chrono>
55#include <cstring>
56
57#endif /* DOXYGEN_SHOULD_SKIP_THIS */
58
59namespace core {
60
61 int EventLoop::stopsig = 0;
62 unsigned long EventLoop::tickCounter = 0;
64
65 static std::string getTickCounterAsString([[maybe_unused]] const el::LogMessage* logMessage) {
66 std::string tick = std::to_string(EventLoop::getTickCounter());
67
68 if (tick.length() < 13) {
69 tick.insert(0, 13 - tick.length(), '0');
70 }
71
72 return tick;
73 }
74
76 : eventMultiplexer(::EventMultiplexer()) {
77 }
78
80 static EventLoop eventLoop;
81
82 return eventLoop;
83 }
84
85 unsigned long EventLoop::getTickCounter() {
86 return tickCounter;
87 }
88
90 return eventMultiplexer;
91 }
92
94 return eventLoopState;
95 }
96
97 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays, modernize-avoid-c-arrays)
98 bool EventLoop::init(int argc, char* argv[]) {
99 struct sigaction sact{};
100 sigemptyset(&sact.sa_mask);
101 sact.sa_flags = 0;
102 sact.sa_handler = SIG_IGN;
103
104 struct sigaction oldPipeAct{};
105 sigaction(SIGPIPE, &sact, &oldPipeAct);
106
107 struct sigaction oldIntAct{};
108 sigaction(SIGINT, &sact, &oldIntAct);
109
110 struct sigaction oldTermAct{};
111 sigaction(SIGTERM, &sact, &oldTermAct);
112
113 struct sigaction oldAlarmAct{};
114 sigaction(SIGALRM, &sact, &oldAlarmAct);
115
116 struct sigaction oldHupAct{};
117 sigaction(SIGHUP, &sact, &oldHupAct);
118
119 logger::Logger::setCustomFormatSpec("%tick", core::getTickCounterAsString);
120
121 if (utils::Config::init(argc, argv)) {
123
124 LOG(TRACE) << "SNode.C: Starting ... HELLO";
125 }
126
127 sigaction(SIGPIPE, &oldPipeAct, nullptr);
128 sigaction(SIGINT, &oldIntAct, nullptr);
129 sigaction(SIGTERM, &oldTermAct, nullptr);
130 sigaction(SIGALRM, &oldAlarmAct, nullptr);
131 sigaction(SIGHUP, &oldHupAct, nullptr);
132
134 }
135
136 TickStatus EventLoop::_tick(const utils::Timeval& tickTimeOut) {
137 TickStatus tickStatus = TickStatus::SUCCESS;
138
139 tickCounter++;
140
141 sigset_t newSet{};
142 sigaddset(&newSet, SIGINT);
143 sigaddset(&newSet, SIGTERM);
144 sigaddset(&newSet, SIGALRM);
145 sigaddset(&newSet, SIGHUP);
146
147 sigset_t oldSet{};
148 sigprocmask(SIG_BLOCK, &newSet, &oldSet);
149
151 tickStatus = eventMultiplexer.tick(tickTimeOut, oldSet);
152 }
153
154 sigprocmask(SIG_SETMASK, &oldSet, nullptr);
155
156 return tickStatus;
157 }
158
159 TickStatus EventLoop::tick(const utils::Timeval& timeOut) {
160 TickStatus tickStatus = TickStatus::TRACE;
161
163 struct sigaction sact{};
164 sigemptyset(&sact.sa_mask);
165 sact.sa_flags = 0;
166 sact.sa_handler = SIG_IGN;
167
168 struct sigaction oldPipeAct{};
169 sigaction(SIGPIPE, &sact, &oldPipeAct);
170
171 tickStatus = EventLoop::instance()._tick(timeOut);
172
173 sigaction(SIGPIPE, &oldPipeAct, nullptr);
174 } else {
176 free();
177
178 PLOG(FATAL) << "Core: not initialized: No events will be processed\nCall SNodeC::init(argc, argv) before SNodeC::tick().";
179 }
180
181 return tickStatus;
182 }
183
184 int EventLoop::start(const utils::Timeval& timeOut) {
185 struct sigaction sact{};
186 sigemptyset(&sact.sa_mask);
187 sact.sa_flags = 0;
188 sact.sa_handler = SIG_IGN;
189
190 struct sigaction oldPipeAct{};
191 sigaction(SIGPIPE, &sact, &oldPipeAct);
192
193 sact.sa_handler = EventLoop::stoponsig;
194
195 struct sigaction oldIntAct{};
196 sigaction(SIGINT, &sact, &oldIntAct);
197
198 struct sigaction oldTermAct{};
199 sigaction(SIGTERM, &sact, &oldTermAct);
200
201 struct sigaction oldAlarmAct{};
202 sigaction(SIGALRM, &sact, &oldAlarmAct);
203
204 struct sigaction oldHupAct{};
205 sigaction(SIGHUP, &sact, &oldHupAct);
206
207 if (eventLoopState == State::INITIALIZED && 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 }
232
233 sigaction(SIGPIPE, &oldPipeAct, nullptr);
234 sigaction(SIGTERM, &oldTermAct, nullptr);
235 sigaction(SIGALRM, &oldAlarmAct, nullptr);
236 sigaction(SIGHUP, &oldHupAct, nullptr);
237
238 free();
239
240 sigaction(SIGINT, &oldIntAct, nullptr);
241
242 return stopsig;
243 }
244
245 void EventLoop::stop() {
247 }
248
249 void EventLoop::free() {
250 std::string signal = "SIG" + utils::system::sigabbrev_np(stopsig);
251
252 if (signal == "SIGUNKNOWN") {
253 signal = std::to_string(stopsig);
254 }
255
256 if (stopsig != 0) {
257 LOG(TRACE) << "Core: Sending signal " << signal << " to all DescriptorEventReceivers";
258
260 }
261
263
264 utils::Timeval timeout = 2;
265
266 core::TickStatus tickStatus = TickStatus::SUCCESS;
267 do {
268 auto t1 = std::chrono::system_clock::now();
269 const utils::Timeval timeoutOp = timeout;
270
271 tickStatus = EventLoop::instance()._tick(timeoutOp);
272
273 auto t2 = std::chrono::system_clock::now();
274 const std::chrono::duration<double> seconds = t2 - t1;
275
276 timeout -= seconds.count();
277 } while (timeout > 0 && (tickStatus == TickStatus::SUCCESS));
278
279 LOG(TRACE) << "Core: Terminate all stalled DescriptorEventReceivers";
280
282
283 LOG(TRACE) << "Core: Close all libraries opened during runtime";
284
286
287 LOG(TRACE) << "Core:: Clean up the filesystem";
288
289 utils::Config::terminate();
290
291 LOG(TRACE) << "Core:: All resources released";
292
293 LOG(TRACE) << "SNode.C: Ended ... BYE";
294 }
295
296 void EventLoop::stoponsig(int sig) {
297 LOG(TRACE) << "Core: Received signal '" << strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = " << sig << ")";
298 stopsig = sig;
299 stop();
300 }
301
302} // namespace core
static void execDlCloseAll()
static unsigned long tickCounter
Definition EventLoop.h:95
EventMultiplexer & getEventMultiplexer()
Definition EventLoop.cpp:89
static core::State getEventLoopState()
Definition EventLoop.cpp:93
static void free()
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:79
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:98
static unsigned long getTickCounter()
Definition EventLoop.cpp:85
TickStatus tick(const utils::Timeval &tickTimeOut, const sigset_t &sigMask)
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
static std::string getTickCounterAsString(const el::LogMessage *logMessage)
Definition EventLoop.cpp:65
std::string sigabbrev_np(int sig)
Definition signal.cpp:59