SNode.C
Loading...
Searching...
No Matches
EventMultiplexer.h
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
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#ifndef CORE_EVENTMULTIPLEXER_H
43#define CORE_EVENTMULTIPLEXER_H
44
45#include "core/TickStatus.h"
46
47namespace core {
48 class Event;
51} // namespace core
52
53namespace utils {
54 class Timeval;
55} // namespace utils
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59#include "utils/system/signal.h"
60
61#include <array>
62#include <list>
63
64#endif /* DOXYGEN_SHOULD_SKIP_THIS */
65
66namespace core {
67
69 public:
70 EventMultiplexer(DescriptorEventPublisher* readDescriptorEventPublisher,
71 DescriptorEventPublisher* writeDescriptorEventPublisher,
72 DescriptorEventPublisher* exceptionDescriptorEventPublisher);
73 virtual ~EventMultiplexer();
74
76
78
79 private:
80 class EventQueue {
81 public:
82 EventQueue();
83 ~EventQueue();
84
85 EventQueue(const EventQueue&) = delete;
87
88 EventQueue& operator=(const EventQueue&) = delete;
90
91 void insert(Event* event);
92 void remove(Event* event);
93 void execute(const utils::Timeval& currentTime);
94 bool empty() const;
95 void clear();
96
97 private:
98 std::list<Event*>* executeQueue;
99 std::list<Event*>* publishQueue;
100 };
101
102 public:
103#define DISP_COUNT 3
104
105 enum DISP_TYPE { RD = 0, WR = 1, EX = 2 };
106
109
110 void span(core::Event* event);
111 void relax(core::Event* event);
112
113 void signal(int sigNum);
114 void terminate();
115 void clearEventQueue();
116
117 TickStatus tick(const utils::Timeval& tickTimeOut, const sigset_t& sigMask);
118
119 private:
120 TickStatus waitForEvents(const utils::Timeval& tickTimeOut,
121 const utils::Timeval& currentTime,
122 const sigset_t& sigMask,
123 int& activeDescriptorCount);
124
125 void spanActiveEvents(const utils::Timeval& currentTime, int activeDescriptorCount);
126 void executeEventQueue(const utils::Timeval& currentTime);
127 void checkTimedOutEvents(const utils::Timeval& currentTime);
128 void releaseExpiredResources(const utils::Timeval& currentTime);
129
130 utils::Timeval getNextTimeout(const utils::Timeval& currentTime);
131
132 virtual void spanActiveEvents(int activeDescriptorCount) = 0;
133
134 virtual int monitorDescriptors(utils::Timeval& tickTimeOut, const sigset_t& sigMask) = 0;
135
136 protected:
137 int maxFd();
138
140
141 private:
143
145
147 };
148
149} // namespace core
150
151core::EventMultiplexer& EventMultiplexer();
152
153#endif // CORE_EVENTMULTIPLEXER_H
#define DISP_COUNT
static unsigned long tickCounter
Definition EventLoop.h:95
EventMultiplexer & getEventMultiplexer()
Definition EventLoop.cpp:89
EventLoop(const EventLoop &eventLoop)=delete
static core::State getEventLoopState()
Definition EventLoop.cpp:93
static void free()
TickStatus _tick(const utils::Timeval &timeOut)
static EventLoop & instance()
Definition EventLoop.cpp:79
core::EventMultiplexer & eventMultiplexer
Definition EventLoop.h:91
static core::State eventLoopState
Definition EventLoop.h:97
EventLoop & operator=(const EventLoop &eventLoop)=delete
static void stoponsig(int sig)
static int start(const utils::Timeval &timeOut)
static void stop()
~EventLoop()=default
static int stopsig
Definition EventLoop.h:93
static TickStatus tick(const utils::Timeval &timeOut)
static bool init(int argc, char *argv[])
Definition EventLoop.cpp:98
static unsigned long getTickCounter()
Definition EventLoop.cpp:85
std::list< Event * > * publishQueue
EventQueue & operator=(const EventQueue &)=delete
std::list< Event * > * executeQueue
EventQueue & operator=(EventQueue &&)=delete
EventQueue(EventQueue &&)=delete
EventQueue(const EventQueue &)=delete
void execute(const utils::Timeval &currentTime)
virtual int monitorDescriptors(utils::Timeval &tickTimeOut, const sigset_t &sigMask)=0
void releaseExpiredResources(const utils::Timeval &currentTime)
utils::Timeval getNextTimeout(const utils::Timeval &currentTime)
void checkTimedOutEvents(const utils::Timeval &currentTime)
TickStatus waitForEvents(const utils::Timeval &tickTimeOut, const utils::Timeval &currentTime, const sigset_t &sigMask, int &activeDescriptorCount)
EventMultiplexer(const EventMultiplexer &)=delete
TimerEventPublisher & getTimerEventPublisher()
TickStatus tick(const utils::Timeval &tickTimeOut, const sigset_t &sigMask)
void spanActiveEvents(const utils::Timeval &currentTime, int activeDescriptorCount)
void span(core::Event *event)
std::array< DescriptorEventPublisher *, 3 > descriptorEventPublishers
EventMultiplexer & operator=(const EventMultiplexer &)=delete
virtual void spanActiveEvents(int activeDescriptorCount)=0
DescriptorEventPublisher & getDescriptorEventPublisher(DISP_TYPE dispType)
void relax(core::Event *event)
EventMultiplexer(DescriptorEventPublisher *readDescriptorEventPublisher, DescriptorEventPublisher *writeDescriptorEventPublisher, DescriptorEventPublisher *exceptionDescriptorEventPublisher)
void executeEventQueue(const utils::Timeval &currentTime)
core::TimerEventPublisher *const timerEventPublisher
virtual void onEvent(const utils::Timeval &currentTime)=0
bool published
Definition Event.h:83
void span()
Definition Event.cpp:64
const std::string & getName() const
Definition Event.cpp:78
void dispatch(const utils::Timeval &currentTime)
Definition Event.cpp:82
EventMultiplexer & eventMultiplexer
Definition Event.h:81
EventReceiver * eventReceiver
Definition Event.h:80
std::string name
Definition Event.h:78
Event(EventReceiver *eventReceiver, const std::string &name)
Definition Event.cpp:54
EventReceiver * getEventReceiver() const
Definition Event.cpp:87
void relax()
Definition Event.cpp:71
TickStatus
Definition TickStatus.h:51
State
Definition State.h:51