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
55#endif /* DOXYGEN_SHOULD_SKIP_THIS */
56
57namespace core {
58
59 int EventLoop::stopsig = 0;
60 unsigned long EventLoop::tickCounter = 0;
62
63 static std::string getTickCounterAsString() {
64 std::string tick = std::to_string(EventLoop::getTickCounter());
65
66 if (tick.length() < 13) {
67 tick.insert(0, 13 - tick.length(), '0');
68 }
69
70 return tick;
71 }
72
74 : eventMultiplexer(::EventMultiplexer()) {
75 }
76
78 static EventLoop eventLoop;
79
80 return eventLoop;
81 }
82
83 unsigned long EventLoop::getTickCounter() {
84 return tickCounter;
85 }
86
90
94
95 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays, modernize-avoid-c-arrays)
96 bool EventLoop::init(int argc, char* argv[]) {
97 struct sigaction sact{};
98 sigemptyset(&sact.sa_mask);
99 sact.sa_flags = 0;
100 sact.sa_handler = SIG_IGN;
101
102 struct sigaction oldPipeAct{};
103 sigaction(SIGPIPE, &sact, &oldPipeAct);
104
105 struct sigaction oldIntAct{};
106 sigaction(SIGINT, &sact, &oldIntAct);
107
108 struct sigaction oldTermAct{};
109 sigaction(SIGTERM, &sact, &oldTermAct);
110
111 struct sigaction oldAlarmAct{};
112 sigaction(SIGALRM, &sact, &oldAlarmAct);
113
114 struct sigaction oldHupAct{};
115 sigaction(SIGHUP, &sact, &oldHupAct);
116
118
119 if (utils::Config::init(argc, argv)) {
121
122 LOG(TRACE) << "SNode.C: Starting ... HELLO";
123 }
124
125 sigaction(SIGPIPE, &oldPipeAct, nullptr);
126 sigaction(SIGINT, &oldIntAct, nullptr);
127 sigaction(SIGTERM, &oldTermAct, nullptr);
128 sigaction(SIGALRM, &oldAlarmAct, nullptr);
129 sigaction(SIGHUP, &oldHupAct, nullptr);
130
132 }
133
134 TickStatus EventLoop::_tick(const utils::Timeval& timeOut) {
135 TickStatus tickStatus = TickStatus::SUCCESS;
136
137 tickCounter++;
138
139 sigset_t newSet{};
140 sigaddset(&newSet, SIGINT);
141 sigaddset(&newSet, SIGTERM);
142 sigaddset(&newSet, SIGALRM);
143 sigaddset(&newSet, SIGHUP);
144
145 sigset_t oldSet{};
146 sigprocmask(SIG_BLOCK, &newSet, &oldSet);
147
149 tickStatus = eventMultiplexer.tick(timeOut, oldSet);
150 }
151
152 sigprocmask(SIG_SETMASK, &oldSet, nullptr);
153
154 return tickStatus;
155 }
156
157 TickStatus EventLoop::tick(const utils::Timeval& timeOut) {
158 TickStatus tickStatus = TickStatus::TRACE;
159
161 struct sigaction sact{};
162 sigemptyset(&sact.sa_mask);
163 sact.sa_flags = 0;
164 sact.sa_handler = SIG_IGN;
165
166 struct sigaction oldPipeAct{};
167 sigaction(SIGPIPE, &sact, &oldPipeAct);
168
169 tickStatus = EventLoop::instance()._tick(timeOut);
170
171 sigaction(SIGPIPE, &oldPipeAct, nullptr);
172 } else {
174 free();
175
176 PLOG(FATAL) << "Core: not initialized: No events will be processed\nCall SNodeC::init(argc, argv) before SNodeC::tick().";
177 }
178
179 return tickStatus;
180 }
181
182 int EventLoop::start(const utils::Timeval& timeOut) {
183 struct sigaction sact{};
184 sigemptyset(&sact.sa_mask);
185 sact.sa_flags = 0;
186 sact.sa_handler = SIG_IGN;
187
188 struct sigaction oldPipeAct{};
189 sigaction(SIGPIPE, &sact, &oldPipeAct);
190
191 sact.sa_handler = EventLoop::stoponsig;
192
193 struct sigaction oldIntAct{};
194 sigaction(SIGINT, &sact, &oldIntAct);
195
196 struct sigaction oldTermAct{};
197 sigaction(SIGTERM, &sact, &oldTermAct);
198
199 struct sigaction oldAlarmAct{};
200 sigaction(SIGALRM, &sact, &oldAlarmAct);
201
202 struct sigaction oldHupAct{};
203 sigaction(SIGHUP, &sact, &oldHupAct);
204
206 if (utils::Config::bootstrap()) {
208 core::TickStatus tickStatus = TickStatus::SUCCESS;
209
210 LOG(TRACE) << "Core::EventLoop: started";
211
212 do {
213 tickStatus = EventLoop::instance()._tick(timeOut);
214 } while ((tickStatus == TickStatus::SUCCESS || tickStatus == TickStatus::INTERRUPTED) && eventLoopState == State::RUNNING);
215
216 switch (tickStatus) {
218 LOG(TRACE) << "Core::EventLoop: Stopped";
219 break;
221 LOG(TRACE) << "Core::EventLoop: No Observer";
222 break;
224 LOG(TRACE) << "Core::EventLoop: Interrupted";
225 break;
227 PLOG(FATAL) << "Core::EventLoop: _tick()";
228 break;
229 }
230 } else {
231 stopsig = -2;
232 }
233 } else {
234 stopsig = -1;
235 }
236
237 sigaction(SIGPIPE, &oldPipeAct, nullptr);
238 sigaction(SIGTERM, &oldTermAct, nullptr);
239 sigaction(SIGALRM, &oldAlarmAct, nullptr);
240 sigaction(SIGHUP, &oldHupAct, nullptr);
241
242 free();
243
244 sigaction(SIGINT, &oldIntAct, nullptr);
245
246 return -stopsig;
247 }
248
252
253 void EventLoop::free() {
254 std::string signal = "SIG" + utils::system::sigabbrev_np(stopsig);
255
256 if (signal == "SIGUNKNOWN") {
257 signal = std::to_string(stopsig);
258 }
259
260 if (stopsig != 0) {
261 LOG(TRACE) << "Core: Sending signal " << signal << " to all DescriptorEventReceivers";
262
264 }
265
267
268 utils::Timeval timeout = 2;
269
270 LOG(TRACE) << "Core: Terminate all stalled DescriptorEventReceivers";
271
273
274 core::TickStatus tickStatus = TickStatus::SUCCESS;
275 do {
276 auto t1 = std::chrono::system_clock::now();
277 const utils::Timeval timeoutOp = timeout;
278
279 tickStatus = EventLoop::instance()._tick(timeoutOp);
280
281 auto t2 = std::chrono::system_clock::now();
282 const std::chrono::duration<double> seconds = t2 - t1;
283
284 timeout -= seconds.count();
285 } while (timeout > 0 && (tickStatus == TickStatus::SUCCESS));
286
287 LOG(TRACE) << "Core: Shutdown config system";
288
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 '" << utils::system::strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = "
298 << 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:87
static core::State getEventLoopState()
Definition EventLoop.cpp:91
static void free()
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:77
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:96
static unsigned long getTickCounter()
Definition EventLoop.cpp:83
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:787
static bool init(int argc, char *argv[])
Definition Config.cpp:657
static bool bootstrap()
Definition Config.cpp:779
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:63
std::string strsignal(int sig)
Definition signal.cpp:71
std::string sigabbrev_np(int sig)
Definition signal.cpp:60