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#include "core/DescriptorEventPublisher.h"
21
22#include "core/DescriptorEventReceiver.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include "utils/Timeval.h"
27
28#include <algorithm>
29#include <iterator>
30#include <utility>
31
32#endif /* DOXYGEN_SHOULD_SKIP_THIS */
33
34namespace core {
35
36 DescriptorEventPublisher::DescriptorEventPublisher(std::string name)
37 : name(std::move(name)) {
38 }
39
42
44 const int fd = descriptorEventReceiver->getRegisteredFd();
45
46 observedEventReceiverLists[fd].push_front(descriptorEventReceiver);
47 muxAdd(descriptorEventReceiver);
48 if (descriptorEventReceiver->isSuspended()) {
49 muxOff(descriptorEventReceiver);
50 }
51 descriptorEventReceiver->setEnabled(utils::Timeval::currentTime());
52 }
53
55 const int fd = descriptorEventReceiver->getRegisteredFd();
56
57 dirtyEventReceiverLists[&observedEventReceiverLists[fd]].push_back(descriptorEventReceiver);
58 }
59
61 muxOff(descriptorEventReceiver);
62 }
63
65 muxOn(descriptorEventReceiver);
66 }
67
68 void DescriptorEventPublisher::checkTimedOutEvents(const utils::Timeval& currentTime) {
69 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
70 eventReceivers.front()->checkTimeout(currentTime);
71 }
72 }
73
74 void DescriptorEventPublisher::releaseDisabledEvents(const utils::Timeval& currentTime) {
75 for (auto& [dirtyDescriptEventReceiverList, disabledDescriptorEventReceivers] : dirtyEventReceiverLists) {
76 for (DescriptorEventReceiver* disabledDescriptorEventReceiver : disabledDescriptorEventReceivers) {
77 dirtyDescriptEventReceiverList->remove(disabledDescriptorEventReceiver);
78
79 if (dirtyDescriptEventReceiverList->empty()) {
80 const int fd = disabledDescriptorEventReceiver->getRegisteredFd();
81
82 muxDel(fd);
83 observedEventReceiverLists.erase(fd);
84 } else {
85 DescriptorEventReceiver* activeDescriptorEventReceiver = dirtyDescriptEventReceiverList->front();
86
87 activeDescriptorEventReceiver->triggered(currentTime);
88 if (!activeDescriptorEventReceiver->isSuspended()) {
89 muxOn(activeDescriptorEventReceiver);
90 } else {
91 muxOff(activeDescriptorEventReceiver);
92 }
93 }
94
95 disabledDescriptorEventReceiver->setDisabled();
96 }
97 }
98
99 dirtyEventReceiverLists.clear();
100 }
101
103 return static_cast<int>(observedEventReceiverLists.size());
104 }
105
107 int maxFd = -1;
108
109 if (!observedEventReceiverLists.empty()) {
110 maxFd = observedEventReceiverLists.rbegin()->first;
111 }
112
113 return maxFd;
114 }
115
116 utils::Timeval DescriptorEventPublisher::getNextTimeout(const utils::Timeval& currentTime) const {
118
119 if (dirtyEventReceiverLists.empty()) {
120 for (const auto& [fd, eventReceivers] : observedEventReceiverLists) {
121 nextTimeout = std::min(eventReceivers.front()->getTimeout(currentTime), nextTimeout);
122 }
123 } else {
124 nextTimeout = 0;
125 }
126
127 return nextTimeout;
128 }
129
131 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
132 for (DescriptorEventReceiver* eventReceiver : eventReceivers) {
133 eventReceiver->onSignal(sigNum);
134 }
135 }
136 }
137
139 for (auto& [fd, eventReceivers] : observedEventReceiverLists) {
140 for (DescriptorEventReceiver* eventReceiver : eventReceivers) {
141 eventReceiver->disable();
142 }
143 }
144 }
145
146 const std::string& DescriptorEventPublisher::getName() const {
147 return name;
148 }
149
150} // namespace core
virtual void muxAdd(DescriptorEventReceiver *descriptorEventReceiver)=0
utils::Timeval getNextTimeout(const utils::Timeval &currentTime) const
void checkTimedOutEvents(const utils::Timeval &currentTime)
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)
void suspend(DescriptorEventReceiver *descriptorEventReceiver)
void resume(DescriptorEventReceiver *descriptorEventReceiver)
void setEnabled(const utils::Timeval &currentTime)
static Timeval currentTime()
Definition Timeval.cpp:54
Timeval & operator=(const Timeval &timeVal)
Definition Timeval.cpp:61