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

249 {
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
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
290
291 LOG(TRACE) << "Core:: All resources released";
292
293 LOG(TRACE) << "SNode.C: Ended ... BYE";
294 }
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:640
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
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 }
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 245 of file EventLoop.cpp.

245 {
247 }

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

296 {
297 LOG(TRACE) << "Core: Received signal '" << strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = " << sig << ")";
298 stopsig = sig;
299 stop();
300 }
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: