1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 08:12:05 +01:00

Synchronize SDM methods that change the state

Other Managers, e.g. EntityCapsManager, may be notified if e.g. a
feature is added or removed. While they are notified, the state of SDM
must be consistent, therefore synchronize SDM methods that modify the
state.
This commit is contained in:
Florian Schmaus 2015-03-24 11:30:32 +01:00
parent 130a8216d1
commit 49c2c35609

View file

@ -29,6 +29,7 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.caps.EntityCapsManager; import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems; import org.jivesoftware.smackx.disco.packet.DiscoverItems;
@ -200,9 +201,10 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @param identity * @param identity
*/ */
public void setIdentity(Identity identity) { public synchronized void setIdentity(Identity identity) {
if (identity == null) throw new IllegalArgumentException("Identity can not be null"); this.identity = Objects.requireNonNull(identity, "Identity can not be null");
this.identity = identity; // Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }
@ -232,8 +234,10 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @param identity * @param identity
*/ */
public void addIdentity(DiscoverInfo.Identity identity) { public synchronized void addIdentity(DiscoverInfo.Identity identity) {
identities.add(identity); identities.add(identity);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }
@ -244,9 +248,11 @@ public class ServiceDiscoveryManager extends Manager {
* @param identity * @param identity
* @return true, if successful. Otherwise the default identity was given. * @return true, if successful. Otherwise the default identity was given.
*/ */
public boolean removeIdentity(DiscoverInfo.Identity identity) { public synchronized boolean removeIdentity(DiscoverInfo.Identity identity) {
if (identity.equals(this.identity)) return false; if (identity.equals(this.identity)) return false;
identities.remove(identity); identities.remove(identity);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
return true; return true;
} }
@ -286,18 +292,16 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @param response the discover info response packet * @param response the discover info response packet
*/ */
public void addDiscoverInfoTo(DiscoverInfo response) { public synchronized void addDiscoverInfoTo(DiscoverInfo response) {
// First add the identities of the connection // First add the identities of the connection
response.addIdentities(getIdentities()); response.addIdentities(getIdentities());
// Add the registered features to the response // Add the registered features to the response
synchronized (features) {
for (String feature : getFeatures()) { for (String feature : getFeatures()) {
response.addFeature(feature); response.addFeature(feature);
} }
response.addExtension(extendedInfo); response.addExtension(extendedInfo);
} }
}
/** /**
* Returns the NodeInformationProvider responsible for providing information * Returns the NodeInformationProvider responsible for providing information
@ -356,11 +360,9 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @return a List of the supported features by this XMPP entity. * @return a List of the supported features by this XMPP entity.
*/ */
public List<String> getFeatures() { public synchronized List<String> getFeatures() {
synchronized (features) {
return new ArrayList<String>(features); return new ArrayList<String>(features);
} }
}
/** /**
* Registers that a new feature is supported by this XMPP entity. When this client is * Registers that a new feature is supported by this XMPP entity. When this client is
@ -373,14 +375,12 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @param feature the feature to register as supported. * @param feature the feature to register as supported.
*/ */
public void addFeature(String feature) { public synchronized void addFeature(String feature) {
synchronized (features) {
if (!features.contains(feature)) {
features.add(feature); features.add(feature);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }
}
}
/** /**
* Removes the specified feature from the supported features by this XMPP entity.<p> * Removes the specified feature from the supported features by this XMPP entity.<p>
@ -390,12 +390,12 @@ public class ServiceDiscoveryManager extends Manager {
* *
* @param feature the feature to remove from the supported features. * @param feature the feature to remove from the supported features.
*/ */
public void removeFeature(String feature) { public synchronized void removeFeature(String feature) {
synchronized (features) {
features.remove(feature); features.remove(feature);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }
}
/** /**
* Returns true if the specified feature is registered in the ServiceDiscoveryManager. * Returns true if the specified feature is registered in the ServiceDiscoveryManager.
@ -403,11 +403,9 @@ public class ServiceDiscoveryManager extends Manager {
* @param feature the feature to look for. * @param feature the feature to look for.
* @return a boolean indicating if the specified featured is registered or not. * @return a boolean indicating if the specified featured is registered or not.
*/ */
public boolean includesFeature(String feature) { public synchronized boolean includesFeature(String feature) {
synchronized (features) {
return features.contains(feature); return features.contains(feature);
} }
}
/** /**
* Registers extended discovery information of this XMPP entity. When this * Registers extended discovery information of this XMPP entity. When this
@ -424,8 +422,10 @@ public class ServiceDiscoveryManager extends Manager {
* the data form that contains the extend service discovery * the data form that contains the extend service discovery
* information. * information.
*/ */
public void setExtendedInfo(DataForm info) { public synchronized void setExtendedInfo(DataForm info) {
extendedInfo = info; extendedInfo = info;
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }
@ -461,8 +461,10 @@ public class ServiceDiscoveryManager extends Manager {
* Since no stanza(/packet) is actually sent to the server it is safe to perform this * Since no stanza(/packet) is actually sent to the server it is safe to perform this
* operation before logging to the server. * operation before logging to the server.
*/ */
public void removeExtendedInfo() { public synchronized void removeExtendedInfo() {
extendedInfo = null; extendedInfo = null;
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion(); renewEntityCapsVersion();
} }