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 75 of file EventLoop.cpp.

76 : eventMultiplexer(::EventMultiplexer()) {
77 }
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 136 of file EventLoop.cpp.

136 {
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 }
static unsigned long tickCounter
Definition EventLoop.h:95
static core::State eventLoopState
Definition EventLoop.h:97
TickStatus tick(const utils::Timeval &tickTimeOut, const sigset_t &sigMask)
TickStatus
Definition TickStatus.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 255 of file EventLoop.cpp.

255 {
256 std::string signal = "SIG" + utils::system::sigabbrev_np(stopsig);
257
258 if (signal == "SIGUNKNOWN") {
259 signal = std::to_string(stopsig);
260 }
261
262 if (stopsig != 0) {
263 LOG(TRACE) << "Core: Sending signal " << signal << " to all DescriptorEventReceivers";
264
266 }
267
269
270 utils::Timeval timeout = 2;
271
273 do {
274 auto t1 = std::chrono::system_clock::now();
275 const utils::Timeval timeoutOp = timeout;
276
277 tickStatus = EventLoop::instance()._tick(timeoutOp);
278
279 auto t2 = std::chrono::system_clock::now();
280 const std::chrono::duration<double> seconds = t2 - t1;
281
282 timeout -= seconds.count();
283 } while (timeout > 0 && (tickStatus == TickStatus::SUCCESS));
284
285 LOG(TRACE) << "Core: Terminate all stalled DescriptorEventReceivers";
286
288
289 LOG(TRACE) << "Core: Close all libraries opened during runtime";
290
292
293 LOG(TRACE) << "Core:: Clean up the filesystem";
294
296
297 LOG(TRACE) << "Core:: All resources released";
298
299 LOG(TRACE) << "SNode.C: Ended ... BYE";
300 }
static void execDlCloseAll()
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:79
static int stopsig
Definition EventLoop.h:93
static void terminate()
Definition Config.cpp:636
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, core::DynamicLoader::execDlCloseAll(), instance(), utils::Timeval::operator-=(), utils::Timeval::operator>(), utils::system::sigabbrev_np(), core::EventMultiplexer::signal(), core::STOPPING, stopsig, core::SUCCESS, and core::EventMultiplexer::terminate().

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 93 of file EventLoop.cpp.

93 {
94 return eventLoopState;
95 }

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 85 of file EventLoop.cpp.

85 {
86 return tickCounter;
87 }

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 98 of file EventLoop.cpp.

98 {
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
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 }
static void setCustomFormatSpec(const char *format, const el::FormatSpecifierValueResolver &resolver)
Definition Logger.cpp:78
static bool init(int argc, char *argv[])
Definition Config.cpp:128
static std::string getTickCounterAsString(const el::LogMessage *logMessage)
Definition EventLoop.cpp:65

References eventLoopState, core::getTickCounterAsString(), and core::INITIALIZED.

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

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

◆ instance()

◆ operator=()

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

◆ start()

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

Definition at line 184 of file EventLoop.cpp.

184 {
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
211
212 LOG(TRACE) << "Core::EventLoop: started";
213
214 do {
215 tickStatus = EventLoop::instance()._tick(timeOut);
216 } while ((tickStatus == TickStatus::SUCCESS || tickStatus == TickStatus::INTERRUPTED) && eventLoopState == State::RUNNING);
217
218 switch (tickStatus) {
220 LOG(TRACE) << "Core::EventLoop: Stopped";
221 break;
223 LOG(TRACE) << "Core::EventLoop: No Observer";
224 break;
226 LOG(TRACE) << "Core::EventLoop: Interrupted";
227 break;
229 PLOG(FATAL) << "Core::EventLoop: _tick()";
230 break;
231 }
232 } else {
233 stopsig = -2;
234 }
235 } else {
236 stopsig = -1;
237 }
238
239 sigaction(SIGPIPE, &oldPipeAct, nullptr);
240 sigaction(SIGTERM, &oldTermAct, nullptr);
241 sigaction(SIGALRM, &oldAlarmAct, nullptr);
242 sigaction(SIGHUP, &oldHupAct, nullptr);
243
244 free();
245
246 sigaction(SIGINT, &oldIntAct, nullptr);
247
248 return -stopsig;
249 }
static void free()
static void stoponsig(int sig)
static bool bootstrap()
Definition Config.cpp:365

References _tick(), eventLoopState, free(), core::INITIALIZED, instance(), core::INTERRUPTED, core::NOOBSERVER, core::RUNNING, stoponsig(), stopsig, core::SUCCESS, and core::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 251 of file EventLoop.cpp.

251 {
253 }

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 302 of file EventLoop.cpp.

302 {
303 LOG(TRACE) << "Core: Received signal '" << strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = " << sig << ")";
304 stopsig = sig;
305 stop();
306 }
static void stop()

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

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 159 of file EventLoop.cpp.

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

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

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

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

Friends And Related Symbol Documentation

◆ 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: