SNode.C
Loading...
Searching...
No Matches
GroupsManager.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 "web/websocket/server/GroupsManager.h"
43
44#include "web/websocket/server/SubProtocol.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#endif /* DOXYGEN_SHOULD_SKIP_THIS */
49
50namespace web::websocket::server {
51
53
56 }
57
60 }
61
62 void GroupsManager::subscribe(SubProtocol* subProtocol, std::string group) {
63 if (group.empty()) {
64 group = subProtocol->getName();
65 }
66
67 if (subProtocol->group != group) {
68 const std::string newChannel = subProtocol->getName() + "/" + group;
69
70 groups[newChannel].insert(subProtocol);
71
72 if (!subProtocol->group.empty()) {
73 unsubscribe(subProtocol);
74 }
75
76 subProtocol->group = newChannel;
77 }
78 }
79
81 if (groups.contains(subProtocol->group)) {
82 groups[subProtocol->group].erase(subProtocol);
83
84 if (groups[subProtocol->group].empty()) {
85 groups.erase(subProtocol->group);
86 if (groups.empty()) {
87 delete this;
88 }
89 }
90 }
91 }
92
93 void GroupsManager::sendBroadcast(const std::string& group,
94 const char* message,
95 std::size_t messageLength,
96 const SubProtocol* excludedClient) {
97 if (groups.contains(group)) {
98 for (const SubProtocol* client : groups[group]) {
99 if (client != excludedClient) {
100 client->sendMessage(message, messageLength);
101 }
102 }
103 }
104 }
105
106 void GroupsManager::sendBroadcast(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
107 if (groups.contains(group)) {
108 for (const SubProtocol* client : groups[group]) {
109 if (client != excludedClient) {
110 client->sendMessage(message);
111 }
112 }
113 }
114 }
115
116 void GroupsManager::sendBroadcastStart(const std::string& group,
117 const char* message,
118 std::size_t messageLength,
119 const SubProtocol* excludedClient) {
120 if (groups.contains(group)) {
121 for (const SubProtocol* client : groups[group]) {
122 if (client != excludedClient) {
123 client->sendMessageStart(message, messageLength);
124 }
125 }
126 }
127 }
128
129 void GroupsManager::sendBroadcastStart(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
130 sendBroadcastStart(group, message.data(), message.length(), excludedClient);
131 }
132
133 void GroupsManager::sendBroadcastFrame(const std::string& group,
134 const char* message,
135 std::size_t messageLength,
136 const SubProtocol* excludedClient) {
137 if (groups.contains(group)) {
138 for (const SubProtocol* client : groups[group]) {
139 if (client != excludedClient) {
140 client->sendMessageFrame(message, messageLength);
141 }
142 }
143 }
144 }
145
146 void GroupsManager::sendBroadcastFrame(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
147 sendBroadcastFrame(group, message.data(), message.length(), excludedClient);
148 }
149
150 void GroupsManager::sendBroadcastEnd(const std::string& group,
151 const char* message,
152 std::size_t messageLength,
153 const SubProtocol* excludedClient) {
154 if (groups.contains(group)) {
155 for (const SubProtocol* client : groups[group]) {
156 if (client != excludedClient) {
157 client->sendMessageEnd(message, messageLength);
158 }
159 }
160 }
161 }
162
163 void GroupsManager::sendBroadcastEnd(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
164 sendBroadcastEnd(group, message.data(), message.length(), excludedClient);
165 }
166
167 void GroupsManager::forEachClient(const std::string& group,
168 const std::function<void(const SubProtocol*)>& sendToClient,
169 const SubProtocol* excludedClient) {
170 if (groups.contains(group)) {
171 for (const SubProtocol* client : groups[group]) {
172 if (client != excludedClient) {
173 sendToClient(client);
174 }
175 }
176 }
177 }
178
179} // namespace web::websocket::server
void sendMessage(const char *message, std::size_t messageLength) const
void sendMessageEnd(const char *message, std::size_t messageLength) const
void sendMessage(const std::string &message) const
const std::string & getName()
void sendMessageStart(const char *message, std::size_t messageLength) const
void sendMessageFrame(const char *message, std::size_t messageLength) const
std::map< std::string, std::set< SubProtocol * > > groups
static GroupsManager * groupsManager
void sendBroadcast(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void sendBroadcastEnd(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void sendBroadcastFrame(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void sendBroadcastStart(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void forEachClient(const std::string &group, const std::function< void(const SubProtocol *)> &sendToClient, const SubProtocol *excludedClient)
void sendBroadcastStart(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void subscribe(SubProtocol *subProtocol, std::string group="")
void sendBroadcastEnd(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void sendBroadcast(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void unsubscribe(SubProtocol *subProtocol)
void sendBroadcastFrame(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)