SNode.C
Loading...
Searching...
No Matches
core::EventLoop Class Reference

#include <EventLoop.h>

Collaboration diagram for core::EventLoop:

Public Member Functions

 EventLoop (const EventLoop &eventLoop)=delete
EventLoopoperator= (const EventLoop &eventLoop)=delete
EventMultiplexergetEventMultiplexer ()

Static Public Member Functions

static EventLoopinstance ()
static unsigned long getTickCounter ()
static core::State getEventLoopState ()

Private Member Functions

 EventLoop ()
 ~EventLoop ()=default
TickStatus _tick (const utils::Timeval &timeOut)

Static Private Member Functions

static bool init (int argc, char *argv[])
static TickStatus tick (const utils::Timeval &timeOut)
static int start (const utils::Timeval &timeOut)
static void stop ()
static void free ()
static void stoponsig (int sig)

Private Attributes

core::EventMultiplexereventMultiplexer

Static Private Attributes

static int stopsig = 0
static unsigned long tickCounter = 0
static core::State eventLoopState = State::LOADED

Friends

class SNodeC

Detailed Description

Definition at line 62 of file EventLoop.h.

Constructor & Destructor Documentation

◆ EventLoop() [1/2]

core::EventLoop::EventLoop ( const EventLoop & eventLoop)
delete

◆ EventLoop() [2/2]

core::EventLoop::EventLoop ( )
private

Definition at line 74 of file EventLoop.cpp.

76 }
core::EventMultiplexer & EventMultiplexer()
core::EventMultiplexer & eventMultiplexer
Definition EventLoop.h:91

References eventMultiplexer.

◆ ~EventLoop()

core::EventLoop::~EventLoop ( )
privatedefault

Member Function Documentation

◆ _tick()

TickStatus core::EventLoop::_tick ( const utils::Timeval & timeOut)
private

Definition at line 135 of file EventLoop.cpp.

135 {
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 }
static unsigned long tickCounter
Definition EventLoop.h:95
static core::State eventLoopState
Definition EventLoop.h:97
TickStatus
Definition TickStatus.h:51
@ RUNNING
Definition State.h:51
@ STOPPING
Definition State.h:51

References eventLoopState, eventMultiplexer, core::RUNNING, core::STOPPING, core::SUCCESS, core::EventMultiplexer::tick(), and tickCounter.

Referenced by free(), start(), and tick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free()

void core::EventLoop::free ( )
staticprivate

Definition at line 254 of file EventLoop.cpp.

254 {
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
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 }
#define LOG(level)
Definition Logger.h:148
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:78
static int stopsig
Definition EventLoop.h:93
static void terminate()
Definition Config.cpp:787
sighandler_t signal(int sig, sighandler_t handler)
Definition signal.cpp:54
std::string sigabbrev_np(int sig)
Definition signal.cpp:59

References _tick(), eventLoopState, eventMultiplexer, instance(), logger::LogMessage::LogMessage(), utils::Timeval::operator-=(), utils::Timeval::operator>(), utils::system::sigabbrev_np(), core::EventMultiplexer::signal(), core::STOPPING, stopsig, core::SUCCESS, core::EventMultiplexer::terminate(), utils::Config::terminate(), and logger::TRACE.

Referenced by core::SNodeC::free(), start(), and tick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getEventLoopState()

State core::EventLoop::getEventLoopState ( )
static

Definition at line 92 of file EventLoop.cpp.

92 {
93 return eventLoopState;
94 }

References eventLoopState.

Referenced by core::eventLoopState(), and core::SNodeC::state().

Here is the caller graph for this function:

◆ getEventMultiplexer()

◆ getTickCounter()

unsigned long core::EventLoop::getTickCounter ( )
static

Definition at line 84 of file EventLoop.cpp.

84 {
85 return tickCounter;
86 }

References tickCounter.

Referenced by express::Controller::Controller(), core::getTickCounterAsString(), and express::Controller::next().

Here is the caller graph for this function:

◆ init()

bool core::EventLoop::init ( int argc,
char * argv[] )
staticprivate

Definition at line 97 of file EventLoop.cpp.

97 {
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 }
static void setTickResolver(TickResolver resolver)
Definition Logger.cpp:209
static bool init(int argc, char *argv[])
Definition Config.cpp:657
@ INITIALIZED
Definition State.h:51
static std::string getTickCounterAsString()
Definition EventLoop.cpp:64

References eventLoopState, core::getTickCounterAsString(), utils::Config::init(), core::INITIALIZED, logger::LogMessage::LogMessage(), logger::Logger::setTickResolver(), and logger::TRACE.

Referenced by core::SNodeC::init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instance()

EventLoop & core::EventLoop::instance ( )
static

◆ operator=()

EventLoop & core::EventLoop::operator= ( const EventLoop & eventLoop)
delete

◆ start()

int core::EventLoop::start ( const utils::Timeval & timeOut)
staticprivate

Definition at line 183 of file EventLoop.cpp.

183 {
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
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 }
#define PLOG(level)
Definition Logger.h:152
static void free()
static void stoponsig(int sig)
static bool bootstrap()
Definition Config.cpp:779

References _tick(), utils::Config::bootstrap(), eventLoopState, logger::FATAL, free(), core::INITIALIZED, instance(), core::INTERRUPTED, logger::LogMessage::LogMessage(), core::NOOBSERVER, core::RUNNING, stoponsig(), stopsig, core::SUCCESS, core::TRACE, and logger::TRACE.

Referenced by core::SNodeC::start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void core::EventLoop::stop ( )
staticprivate

Definition at line 250 of file EventLoop.cpp.

250 {
252 }

References eventLoopState, and core::STOPPING.

Referenced by core::SNodeC::stop(), and stoponsig().

Here is the caller graph for this function:

◆ stoponsig()

void core::EventLoop::stoponsig ( int sig)
staticprivate

Definition at line 297 of file EventLoop.cpp.

297 {
298 LOG(TRACE) << "Core: Received signal '" << strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = " << sig << ")";
299 stopsig = sig;
300 stop();
301 }
static void stop()

References logger::LogMessage::LogMessage(), utils::system::sigabbrev_np(), stop(), stopsig, and logger::TRACE.

Referenced by start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tick()

TickStatus core::EventLoop::tick ( const utils::Timeval & timeOut)
staticprivate

Definition at line 158 of file EventLoop.cpp.

158 {
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 }

References _tick(), core::EventMultiplexer::clearEventQueue(), eventLoopState, eventMultiplexer, logger::FATAL, free(), core::INITIALIZED, instance(), logger::LogMessage::LogMessage(), and core::TRACE.

Referenced by core::SNodeC::tick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SNodeC

friend class SNodeC
friend

Definition at line 99 of file EventLoop.h.

Member Data Documentation

◆ eventLoopState

core::State core::EventLoop::eventLoopState = State::LOADED
staticprivate

Definition at line 97 of file EventLoop.h.

Referenced by _tick(), free(), getEventLoopState(), init(), start(), stop(), and tick().

◆ eventMultiplexer

core::EventMultiplexer& core::EventLoop::eventMultiplexer
private

Definition at line 91 of file EventLoop.h.

Referenced by _tick(), EventLoop(), free(), getEventMultiplexer(), and tick().

◆ stopsig

int core::EventLoop::stopsig = 0
staticprivate

Definition at line 93 of file EventLoop.h.

Referenced by free(), start(), and stoponsig().

◆ tickCounter

unsigned long core::EventLoop::tickCounter = 0
staticprivate

Definition at line 95 of file EventLoop.h.

Referenced by _tick(), and getTickCounter().


The documentation for this class was generated from the following files: