SNode.C
Loading...
Searching...
No Matches
DescriptorEventPublisher.cpp
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#include "core/DescriptorEventPublisher.h"
43
44#include "core/DescriptorEventReceiver.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "utils/Timeval.h"
49
50#include <algorithm>
51#include <iterator>
52#include <utility>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56namespace core {
57
59 : name(std::move(name)) {
60 }
61
63 }
64
66 const int fd = descriptorEventReceiver->getRegisteredFd();
67
68 observedEventReceiverLists[fd].push_front(descriptorEventReceiver);
69 muxAdd(descriptorEventReceiver);
70 if (descriptorEventReceiver->isSuspended()) {
71 muxOff(descriptorEventReceiver);
72 }
73 descriptorEventReceiver->setEnabled(utils::Timeval::currentTime());
74 }
75
77 const int fd = descriptorEventReceiver->getRegisteredFd();
78
79 dirtyEventReceiverLists[&observedEventReceiverLists[fd]].push_back(descriptorEventReceiver);
80 }
81
83 muxOff(descriptorEventReceiver);
84 }
85
87 muxOn(descriptorEventReceiver);
88 }
89
90 void DescriptorEventPublisher::checkTimedOutEvents(const utils::Timeval& currentTime) {
91 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
92 eventReceivers.front()->checkTimeout(currentTime);
93 }
94 }
95
96 void DescriptorEventPublisher::releaseDisabledEvents(const utils::Timeval& currentTime) {
97 for (auto& [dirtyDescriptEventReceiverList, disabledDescriptorEventReceivers] : dirtyEventReceiverLists) {
98 for (DescriptorEventReceiver* disabledDescriptorEventReceiver : disabledDescriptorEventReceivers) {
99 dirtyDescriptEventReceiverList->remove(disabledDescriptorEventReceiver);
100
101 if (dirtyDescriptEventReceiverList->empty()) {
102 const int fd = disabledDescriptorEventReceiver->getRegisteredFd();
103
104 muxDel(fd);
106 } else {
107 DescriptorEventReceiver* activeDescriptorEventReceiver = dirtyDescriptEventReceiverList->front();
108
109 activeDescriptorEventReceiver->triggered(currentTime);
110 if (!activeDescriptorEventReceiver->isSuspended()) {
111 muxOn(activeDescriptorEventReceiver);
112 } else {
113 muxOff(activeDescriptorEventReceiver);
114 }
115 }
116
117 disabledDescriptorEventReceiver->setDisabled();
118 }
119 }
120
122 }
123
125 return static_cast<int>(observedEventReceiverLists.size());
126 }
127
129 int maxFd = -1;
130
131 if (!observedEventReceiverLists.empty()) {
132 maxFd = observedEventReceiverLists.rbegin()->first;
133 }
134
135 return maxFd;
136 }
137
138 utils::Timeval DescriptorEventPublisher::getNextTimeout(const utils::Timeval& currentTime) const {
140
141 if (dirtyEventReceiverLists.empty()) {
142 for (const auto& [fd, eventReceivers] : observedEventReceiverLists) {
143 nextTimeout = std::min(eventReceivers.front()->getTimeout(currentTime), nextTimeout);
144 }
145 } else {
146 nextTimeout = 0;
147 }
148
149 return nextTimeout;
150 }
151
153 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
154 for (DescriptorEventReceiver* eventReceiver : eventReceivers) {
155 eventReceiver->onSignal(sigNum);
156 }
157 }
158 }
159
161 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
162 for (DescriptorEventReceiver* eventReceiver : eventReceivers) {
163 eventReceiver->disable();
164 }
165 }
166 }
167
168 const std::string& DescriptorEventPublisher::getName() const {
169 return name;
170 }
171
172} // namespace core
virtual void muxAdd(DescriptorEventReceiver *descriptorEventReceiver)=0
utils::Timeval getNextTimeout(const utils::Timeval &currentTime) const
void checkTimedOutEvents(const utils::Timeval &currentTime)
virtual void muxDel(int fd)=0
virtual void muxOff(DescriptorEventReceiver *descriptorEventReceiver)=0
void releaseDisabledEvents(const utils::Timeval &currentTime)
virtual void muxOn(DescriptorEventReceiver *descriptorEventReceiver)=0
void disable(DescriptorEventReceiver *descriptorEventReceiver)
void enable(DescriptorEventReceiver *descriptorEventReceiver)
std::map< int, std::list< DescriptorEventReceiver * > > observedEventReceiverLists
void suspend(DescriptorEventReceiver *descriptorEventReceiver)
std::map< std::list< DescriptorEventReceiver * > *, std::list< DescriptorEventReceiver * > > dirtyEventReceiverLists
void resume(DescriptorEventReceiver *descriptorEventReceiver)
void triggered(const utils::Timeval &currentTime)
void setEnabled(const utils::Timeval &currentTime)
void checkTimeout(const utils::Timeval &currentTime)
utils::Timeval getTimeout(const utils::Timeval &currentTime) const
static Timeval currentTime()
Definition Timeval.cpp:76
Timeval & operator=(const Timeval &timeVal)
Definition Timeval.cpp:83