1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02: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.ExtensionElement;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
@ -200,9 +201,10 @@ public class ServiceDiscoveryManager extends Manager {
*
* @param identity
*/
public void setIdentity(Identity identity) {
if (identity == null) throw new IllegalArgumentException("Identity can not be null");
this.identity = identity;
public synchronized void setIdentity(Identity identity) {
this.identity = Objects.requireNonNull(identity, "Identity can not be null");
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
}
@ -232,8 +234,10 @@ public class ServiceDiscoveryManager extends Manager {
*
* @param identity
*/
public void addIdentity(DiscoverInfo.Identity identity) {
public synchronized void addIdentity(DiscoverInfo.Identity identity) {
identities.add(identity);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
}
@ -244,9 +248,11 @@ public class ServiceDiscoveryManager extends Manager {
* @param identity
* @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;
identities.remove(identity);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
return true;
}
@ -286,18 +292,16 @@ public class ServiceDiscoveryManager extends Manager {
*
* @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
response.addIdentities(getIdentities());
// Add the registered features to the response
synchronized (features) {
for (String feature : getFeatures()) {
response.addFeature(feature);
}
response.addExtension(extendedInfo);
}
}
/**
* 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.
*/
public List<String> getFeatures() {
synchronized (features) {
public synchronized List<String> getFeatures() {
return new ArrayList<String>(features);
}
}
/**
* 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.
*/
public void addFeature(String feature) {
synchronized (features) {
if (!features.contains(feature)) {
public synchronized void addFeature(String feature) {
features.add(feature);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
}
}
}
/**
* 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.
*/
public void removeFeature(String feature) {
synchronized (features) {
public synchronized void removeFeature(String feature) {
features.remove(feature);
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
}
}
/**
* 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.
* @return a boolean indicating if the specified featured is registered or not.
*/
public boolean includesFeature(String feature) {
synchronized (features) {
public synchronized boolean includesFeature(String feature) {
return features.contains(feature);
}
}
/**
* 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
* information.
*/
public void setExtendedInfo(DataForm info) {
public synchronized void setExtendedInfo(DataForm info) {
extendedInfo = info;
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
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
* operation before logging to the server.
*/
public void removeExtendedInfo() {
public synchronized void removeExtendedInfo() {
extendedInfo = null;
// Notify others of a state change of SDM. In order to keep the state consistent, this
// method is synchronized
renewEntityCapsVersion();
}