/
MucClientManager.java
396 lines (355 loc) · 12 KB
/
MucClientManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright @ 2018 - present, 8x8 Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.jitsi.xmpp.mucclient;
import org.jitsi.service.configuration.*;
import org.jitsi.utils.concurrent.*;
import org.jitsi.utils.logging2.*;
import org.jitsi.utils.logging2.Logger;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import org.jxmpp.util.*;
import java.util.*;
import java.util.concurrent.*;
/**
* Manages a set of {@link MucClient}, each of which represents an XMPP client
* connection (via an {@link XMPPConnection}) and a Multi-User Chat. Allows
* loading the configuration of a {@link MucClient} from properties in a
* {@link ConfigurationService}, as well as adding and removing
* {@link MucClient}s dynamically.
*
* @author Boris Grozev
*/
public class MucClientManager
{
/**
* The {@link Logger} used by the {@link MucClientManager} class and its
* instances for logging output.
*/
private static final Logger logger
= new LoggerImpl(MucClientManager.class.getName());
/**
* Maps a hostname to the {@link MucClient} associated with it.
*/
private final Map<String, MucClient> mucClients
= new ConcurrentHashMap<>();
/**
* Contains the list of features to use for disco#info.
*/
private final Set<String> features = new HashSet<>();
/**
* The listener which is to be called when any of our {@link MucClient}s
* receive an IQ from Smack.
*/
private IQListener iqListener;
/**
* The list of IQs which {@link #iqListener} is interested in receiving, represented by {@link IQ} instances.
*/
private Map<IQ, Boolean> registeredIqs = new HashMap<>();
/**
* The list of extensions to be added to the presence in the MUC in each
* of our {@link MucClient}s.
*/
private final Map<String, ExtensionElement> presenceExtensions
= new ConcurrentHashMap<>();
/**
* An object used to synchronize access to some of the fields in this
* instance (whichever were deemed to need it).
*/
private final Object syncRoot = new Object();
/**
* The {@link RecurringRunnableExecutor} to be utilized by the {@link MucClientManager} class and its instances.
*/
private final RecurringRunnableExecutor recurringRunnableExecutor = new RecurringRunnableExecutor();
/**
* Initializes a new {@link MucClientManager} instance.
*
*/
public MucClientManager()
{
this(new String[0]);
}
/**
* Initializes a new {@link MucClientManager} instance.
*
* @param features the features to use for disco#info.
*/
public MucClientManager(String[] features)
{
SmackConfiguration.setUnknownIqRequestReplyMode(
SmackConfiguration.UnknownIqRequestReplyMode
.replyFeatureNotImplemented);
if (features != null)
{
this.features.addAll(Arrays.asList(features));
}
}
/**
* Adds a new {@link MucClient} with a specific
* {@link MucClientConfiguration}. Initializes and starts the client
* asynchronously.
* @param config the configuration of the new {@link MucClient}.
* @return {@code true} if a new client was added, and {@code false} if a
* client with the ID described by the configuration already existed.
*/
public boolean addMucClient(MucClientConfiguration config)
{
final MucClient mucClient;
synchronized (syncRoot)
{
if (mucClients.get(config.getId()) != null)
{
logger.error("Not adding a new MUC client, ID already exists.");
return false;
}
mucClient = new MucClient(config, MucClientManager.this);
mucClients.put(config.getId(), mucClient);
recurringRunnableExecutor.registerRecurringRunnable(mucClient.getHalfOpenConnectionPeriodicCheck());
}
mucClient.start();
return true;
}
/**
* Adds an {@link ExtensionElement} to the presence of all our
* {@link MucClient}s, and removes any other extensions with the same
* element name and namespace, if any exist.
*
* @param extension the extension to add.
*/
public void setPresenceExtension(ExtensionElement extension)
{
synchronized (syncRoot)
{
logger.debug("Setting a presence extension: " + extension);
saveExtension(extension);
mucClients.values().forEach(
mucClient -> mucClient.setPresenceExtension(extension));
}
}
/**
* Saves an extension element in the local map, replacing any previous
* extension with the same element name and namespace.
* @param extension the extension to save.
*/
private void saveExtension(ExtensionElement extension)
{
synchronized (syncRoot)
{
ExtensionElement previousExtension
= presenceExtensions.put(
XmppStringUtils.generateKey(
extension.getElementName(), extension.getNamespace()),
extension);
if (previousExtension != null && logger.isDebugEnabled())
{
logger.debug(
"Replacing presence extension: " + previousExtension);
}
}
}
/**
* Removes the extension with a given element name and namespace from the
* local map.
* @param elementName the element name of the extension to remove.
* @param namespace the namespace of the extension to remove.
*/
private void removeExtension(String elementName, String namespace)
{
presenceExtensions
.remove(XmppStringUtils.generateKey(elementName, namespace));
}
/**
* @return the presence extensions as a list.
*/
Collection<ExtensionElement> getPresenceExtensions()
{
return presenceExtensions.values();
}
/**
* Removes an {@link ExtensionElement} with a particular element name and
* namespace from the presence of all our {@link MucClient}s.
*
* @param elementName the name of the element of the extension to remove.
* @param namespace the namespace of the element of the extension to remove.
*/
public void removePresenceExtension(String elementName, String namespace)
{
synchronized (syncRoot)
{
removeExtension(elementName, namespace);
mucClients.values().forEach(
mucClient -> mucClient.removePresenceExtension(
elementName, namespace));
}
}
/**
* @return the set of features to use in disco#info.
*/
Set<String> getFeatures()
{
return features;
}
/**
* Sets the IQ listener which will be called when an IQ of a registered
* type is received (see {@link #registerIQ(IQ)}).
* @param iqListener the listener to set.
*/
public void setIQListener(IQListener iqListener)
{
synchronized (syncRoot)
{
// Save it, so it is accessible to MucClients added later
if (this.iqListener != null)
{
logger.info("Replacing an existing IQ listener.");
}
this.iqListener = iqListener;
mucClients.values().forEach(m -> m.setIQListener(iqListener));
}
}
/**
* Indicates to this instance that the registered IQ listener is
* interested in IQs with a specific child element name and namespace,
* represented in an {@link IQ}.
*
* @param iq the IQ which represents a [element name, namespace] pair.
* @param requireResponse whether to send an error stanza as a response if the {@link IQListener} produces
* {@code null} for requests of this type. Using {@code false} allows asynchronous handling of the request.
*/
public void registerIQ(IQ iq, boolean requireResponse)
{
synchronized (syncRoot)
{
registeredIqs.put(iq, requireResponse);
mucClients.values()
.forEach(mucClient -> mucClient.registerIQ(iq, requireResponse));
}
}
public void registerIQ(IQ iq)
{
registerIQ(iq, true);
}
public void registerIQ(String elementName, String namespace)
{
registerIQ(elementName, namespace, true);
}
/**
* Indicates to this instance that the registered IQ listener is
* interested in IQs with a specific element name and namespace.
*
* @param elementName the child element name.
* @param namespace the child element namespace.
* @param requireResponse whether to send an error stanza as a response if the {@link IQListener} produces
* {@code null} for requests of this type. Using {@code false} allows asynchronous handling of the request.
*/
public void registerIQ(String elementName, String namespace, boolean requireResponse)
{
registerIQ(
new IQ(elementName, namespace)
{
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(
IQChildElementXmlStringBuilder xml)
{
return null;
}
},
requireResponse);
}
/**
* @return the list of registered IQ types.
*/
Map<IQ, Boolean> getRegisteredIqs()
{
return new HashMap<>(registeredIqs);
}
/**
* @return the IQ listener.
*/
IQListener getIqListener()
{
return iqListener;
}
/**
* Stops and removes a {@link MucClient} identified by its ID.
* @param id the ID of the client to remove.
* @return {@code true} if a {@link MucClient} with this specified ID exists
* and was removed, and {@code false} otherwise.
*/
public boolean removeMucClient(String id)
{
MucClient mucClient = mucClients.remove(id);
if (mucClient == null)
{
logger.info("Can not find MucClient to remove.");
return false;
}
recurringRunnableExecutor.deRegisterRecurringRunnable(mucClient.getHalfOpenConnectionPeriodicCheck());
mucClient.stop();
return true;
}
/**
* Stops and removed all MucClients.
*/
public void stop()
{
mucClients.keySet().forEach(this::removeMucClient);
}
/**
* Return the number of configured {@link MucClient}s.
*/
public long getClientCount()
{
return mucClients.size();
}
/**
* Return the number of {@link MucClient}s that are successfully connected
* to XMPP.
*/
public long getClientConnectedCount()
{
return mucClients.values().stream()
.filter(MucClient::isConnected)
.count();
}
/**
* Return a list of ids for MUC client configs that have been added.
*/
public List<String> getMucClientIds()
{
return new ArrayList<>(mucClients.keySet());
}
/**
* Return the number of configured MUCs.
*/
public long getMucCount()
{
return mucClients.values().stream()
.map(MucClient::getMucsCount)
.mapToInt(Integer::intValue)
.sum();
}
/**
* Return the number of MUCs that have been successfully joined.
*/
public long getMucJoinedCount()
{
return mucClients.values().stream()
.map(MucClient::getMucsJoinedCount)
.mapToInt(Integer::intValue)
.sum();
}
}