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

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

134 {
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 }
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 253 of file EventLoop.cpp.

253 {
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
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 }
#define LOG(level)
Definition Logger.h:148
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:77
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:55
std::string sigabbrev_np(int sig)
Definition signal.cpp:60

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

91 {
92 return eventLoopState;
93 }

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

83 {
84 return tickCounter;
85 }

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

96 {
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 }
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:63

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

182 {
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
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 }
#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 249 of file EventLoop.cpp.

249 {
251 }

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 '" << utils::system::strsignal(sig) << "' (SIG" << utils::system::sigabbrev_np(sig) << " = "
298 << sig << ")";
299 stopsig = sig;
300 stop();
301 }
static void stop()
std::string strsignal(int sig)
Definition signal.cpp:71

References logger::LogMessage::LogMessage(), utils::system::sigabbrev_np(), stop(), stopsig, utils::system::strsignal(), 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 157 of file EventLoop.cpp.

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

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: