From 8e337d7810bbfb5d4a4bd1b5a68e471f865a4ea1 Mon Sep 17 00:00:00 2001 From: damencho Date: Thu, 11 Jun 2020 13:12:13 -0500 Subject: [PATCH] [muc] Make providesMucService() use the KNOWN_MUC_SERVICES cache This reduces the number of disco#info queries on MUC join in some situations. --- .../jivesoftware/smackx/muc/MultiUserChat.java | 12 ++---------- .../smackx/muc/MultiUserChatManager.java | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index 40c3a85a2..f5ee3ec53 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -88,7 +88,6 @@ import org.jxmpp.jid.EntityJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.parts.Resourcepart; -import org.jxmpp.util.cache.ExpirationCache; /** * A MultiUserChat room (XEP-45), created with {@link MultiUserChatManager#getMultiUserChat(EntityBareJid)}. @@ -112,9 +111,6 @@ import org.jxmpp.util.cache.ExpirationCache; public class MultiUserChat { private static final Logger LOGGER = Logger.getLogger(MultiUserChat.class.getName()); - private static final ExpirationCache KNOWN_MUC_SERVICES = new ExpirationCache<>( - 100, 1000 * 60 * 60 * 24); - private final XMPPConnection connection; private final EntityBareJid room; private final MultiUserChatManager multiUserChatManager; @@ -340,12 +336,8 @@ public class MultiUserChat { private Presence enter(MucEnterConfiguration conf) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException, NotAMucServiceException { final DomainBareJid mucService = room.asDomainBareJid(); - if (!KNOWN_MUC_SERVICES.containsKey(mucService)) { - if (multiUserChatManager.providesMucService(mucService)) { - KNOWN_MUC_SERVICES.put(mucService, null); - } else { - throw new NotAMucServiceException(this); - } + if (!multiUserChatManager.providesMucService(mucService)) { + throw new NotAMucServiceException(this); } // We enter a room by sending a presence packet where the "to" // field is in the form "roomName@service/nickname" diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index e2654de89..7d92e9817 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -64,6 +64,7 @@ import org.jxmpp.jid.EntityFullJid; import org.jxmpp.jid.EntityJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.parts.Resourcepart; +import org.jxmpp.util.cache.ExpirationCache; /** * A manager for Multi-User Chat rooms. @@ -136,6 +137,9 @@ public final class MultiUserChatManager extends Manager { private static final StanzaFilter INVITATION_FILTER = new AndFilter(StanzaTypeFilter.MESSAGE, new StanzaExtensionFilter(new MUCUser()), new NotFilter(MessageTypeFilter.ERROR)); + private static final ExpirationCache KNOWN_MUC_SERVICES = new ExpirationCache<>( + 100, 1000 * 60 * 60 * 24); + private final Set invitationsListeners = new CopyOnWriteArraySet(); /** @@ -392,8 +396,16 @@ public final class MultiUserChatManager extends Manager { */ public boolean providesMucService(DomainBareJid domainBareJid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { - return serviceDiscoveryManager.supportsFeature(domainBareJid, - MUCInitialPresence.NAMESPACE); + boolean contains = KNOWN_MUC_SERVICES.containsKey(domainBareJid); + if (!contains) { + if (serviceDiscoveryManager.supportsFeature(domainBareJid, + MUCInitialPresence.NAMESPACE)) { + KNOWN_MUC_SERVICES.put(domainBareJid, null); + return true; + } + } + + return contains; } /**