From 2679c72f0fa4eefbe0458cdcfedbb07899c75d01 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 17 May 2020 20:50:25 +0200 Subject: [PATCH] [disco] Move logic that was previously in EntityCapsManager in SDM --- .../smackx/caps/EntityCapsManager.java | 54 ++----------------- .../EntityCapabilitiesChangedListener.java | 6 ++- .../smackx/disco/ServiceDiscoveryManager.java | 45 +++++++++++++++- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/EntityCapsManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/EntityCapsManager.java index d7ee5b45a..5352c85cc 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/EntityCapsManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/caps/EntityCapsManager.java @@ -33,8 +33,6 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.logging.Level; -import java.util.logging.Logger; import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionListener; @@ -51,7 +49,6 @@ import org.jivesoftware.smack.filter.PresenceTypeFilter; import org.jivesoftware.smack.filter.StanzaExtensionFilter; import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.filter.StanzaTypeFilter; -import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.PresenceBuilder; import org.jivesoftware.smack.packet.Stanza; @@ -87,7 +84,6 @@ import org.jxmpp.util.cache.LruCache; * @see XEP-0115: Entity Capabilities */ public final class EntityCapsManager extends Manager { - private static final Logger LOGGER = Logger.getLogger(EntityCapsManager.class.getName()); public static final String NAMESPACE = CapsExtension.NAMESPACE; public static final String ELEMENT = CapsExtension.ELEMENT; @@ -307,7 +303,6 @@ public final class EntityCapsManager extends Manager { private boolean entityCapsEnabled; private CapsVersionAndHash currentCapsVersion; - private volatile Presence presenceSend; /** * The entity node String used by this EntityCapsManager instance. @@ -342,11 +337,6 @@ public final class EntityCapsManager extends Manager { // feature, so we try to process it after we are connected and // once after we are authenticated. processCapsStreamFeatureIfAvailable(connection); - - // Reset presenceSend when the connection was not resumed - if (!resumed) { - presenceSend = null; - } } private void processCapsStreamFeatureIfAvailable(XMPPConnection connection) { CapsExtension capsExtension = connection.getFeature( @@ -359,9 +349,6 @@ public final class EntityCapsManager extends Manager { } }); - // This calculates the local entity caps version - updateLocalEntityCaps(); - if (autoEnableEntityCaps) enableEntityCaps(); @@ -387,26 +374,16 @@ public final class EntityCapsManager extends Manager { } }); - connection.addStanzaSendingListener(new StanzaListener() { - @Override - public void processStanza(Stanza packet) { - presenceSend = (Presence) packet; - } - }, PresenceTypeFilter.OUTGOING_PRESENCE_BROADCAST); - - - enableEntityCaps(); - // It's important to do this as last action. Since it changes the // behavior of the SDM in some ways sdm.addEntityCapabilitiesChangedListener(new EntityCapabilitiesChangedListener() { @Override - public void onEntityCapailitiesChanged() { + public void onEntityCapabilitiesChanged(DiscoverInfo synthesizedDiscoveryInfo) { if (!entityCapsEnabled()) { return; } - updateLocalEntityCaps(); + updateLocalEntityCaps(synthesizedDiscoveryInfo); } }); } @@ -431,7 +408,6 @@ public final class EntityCapsManager extends Manager { // Add Entity Capabilities (XEP-0115) feature node. sdm.addFeature(NAMESPACE); - updateLocalEntityCaps(); entityCapsEnabled = true; } @@ -446,11 +422,6 @@ public final class EntityCapsManager extends Manager { return entityCapsEnabled; } - public void setEntityNode(String entityNode) { - this.entityNode = entityNode; - updateLocalEntityCaps(); - } - /** * Remove a record telling what entity caps node a user has. * @@ -522,13 +493,10 @@ public final class EntityCapsManager extends Manager { * presence is send to inform others about your new Entity Caps node string. * */ - private void updateLocalEntityCaps() { + private void updateLocalEntityCaps(DiscoverInfo synthesizedDiscoveryInfo) { XMPPConnection connection = connection(); - DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder("synthetized-disco-info-response") - .ofType(IQ.Type.result); - sdm.addDiscoverInfoTo(discoverInfoBuilder); - + DiscoverInfoBuilder discoverInfoBuilder = synthesizedDiscoveryInfo.asBuilder(); // getLocalNodeVer() will return a result only after currentCapsVersion is set. Therefore // set it first and then call getLocalNodeVer() currentCapsVersion = generateVerificationString(discoverInfoBuilder); @@ -564,20 +532,6 @@ public final class EntityCapsManager extends Manager { return packetExtensions; } }); - - // Re-send the last sent presence, and let the stanza interceptor - // add a node to it. - // See http://xmpp.org/extensions/xep-0115.html#advertise - // We only send a presence packet if there was already one send - // to respect ConnectionConfiguration.isSendPresence() - if (connection != null && connection.isAuthenticated() && presenceSend != null) { - try { - connection.sendStanza(presenceSend.cloneWithNewId()); - } - catch (InterruptedException | NotConnectedException e) { - LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e); - } - } } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/EntityCapabilitiesChangedListener.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/EntityCapabilitiesChangedListener.java index e075117f2..bb790813f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/EntityCapabilitiesChangedListener.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/EntityCapabilitiesChangedListener.java @@ -1,6 +1,6 @@ /** * - * Copyright 2018 Florian Schmaus. + * Copyright 2018-2020 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,10 @@ */ package org.jivesoftware.smackx.disco; +import org.jivesoftware.smackx.disco.packet.DiscoverInfo; + public interface EntityCapabilitiesChangedListener { - void onEntityCapailitiesChanged(); + void onEntityCapabilitiesChanged(DiscoverInfo synthesizedDiscoveryInfo); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java index 30daeda7e..61103d2ee 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java @@ -31,8 +31,11 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.ConnectionCreationListener; +import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.ScheduledAction; import org.jivesoftware.smack.SmackException.NoResponseException; @@ -40,10 +43,12 @@ import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.filter.PresenceTypeFilter; import org.jivesoftware.smack.internal.AbstractStats; import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler; import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.StanzaError; import org.jivesoftware.smack.util.CollectionUtil; @@ -77,6 +82,8 @@ import org.jxmpp.util.cache.ExpirationCache; */ public final class ServiceDiscoveryManager extends Manager { + private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName()); + private static final String DEFAULT_IDENTITY_NAME = "Smack"; private static final String DEFAULT_IDENTITY_CATEGORY = "client"; private static final String DEFAULT_IDENTITY_TYPE = "pc"; @@ -97,6 +104,8 @@ public final class ServiceDiscoveryManager extends Manager { private List extendedInfos = new ArrayList<>(2); private final Map nodeInformationProviders = new ConcurrentHashMap<>(); + private volatile Presence presenceSend; + // Create a new ServiceDiscoveryManager on every established connection static { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { @@ -196,6 +205,18 @@ public final class ServiceDiscoveryManager extends Manager { return response; } }); + + connection.addConnectionListener(new ConnectionListener() { + @Override + public void authenticated(XMPPConnection connection, boolean resumed) { + // Reset presenceSend when the connection was not resumed + if (!resumed) { + presenceSend = null; + } + } + }); + connection.addStanzaSendingListener(p -> presenceSend = (Presence) p, + PresenceTypeFilter.OUTGOING_PRESENCE_BROADCAST); } /** @@ -920,11 +941,33 @@ public final class ServiceDiscoveryManager extends Manager { } } + final XMPPConnection connection = connection(); + renewEntityCapsScheduledAction = scheduleBlocking(() -> { renewEntityCapsPerformed.incrementAndGet(); + DiscoverInfoBuilder discoverInfoBuilder = DiscoverInfo.builder("synthetized-disco-info-response") + .ofType(IQ.Type.result); + addDiscoverInfoTo(discoverInfoBuilder); + DiscoverInfo synthesizedDiscoveryInfo = discoverInfoBuilder.build(); + for (EntityCapabilitiesChangedListener entityCapabilitiesChangedListener : entityCapabilitiesChangedListeners) { - entityCapabilitiesChangedListener.onEntityCapailitiesChanged(); + entityCapabilitiesChangedListener.onEntityCapabilitiesChanged(synthesizedDiscoveryInfo); + } + + // Re-send the last sent presence, and let the stanza interceptor + // add a node to it. + // See http://xmpp.org/extensions/xep-0115.html#advertise + // We only send a presence packet if there was already one send + // to respect ConnectionConfiguration.isSendPresence() + final Presence presenceSend = this.presenceSend; + if (connection.isAuthenticated() && presenceSend != null) { + try { + connection.sendStanza(presenceSend.cloneWithNewId()); + } + catch (InterruptedException | NotConnectedException e) { + LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e); + } } }, RENEW_ENTITY_CAPS_DELAY_MILLIS, TimeUnit.MILLISECONDS); }