synchronize around the disco object

This commit is contained in:
Daniel Gultsch 2015-10-19 23:03:19 +02:00
parent 0bd4105b1d
commit adca670196
1 changed files with 53 additions and 44 deletions

View File

@ -812,7 +812,7 @@ public class XmppConnection implements Runnable {
}
private void sendBindRequest() {
while(!mXmppConnectionService.areMessagesInitialized()) {
while(!mXmppConnectionService.areMessagesInitialized() && socket != null && !socket.isClosed()) {
try {
Thread.sleep(500);
} catch (final InterruptedException ignored) {
@ -907,7 +907,9 @@ public class XmppConnection implements Runnable {
}
features.carbonsEnabled = false;
features.blockListRequested = false;
disco.clear();
synchronized (this.disco) {
this.disco.clear();
}
sendServiceDiscoveryInfo(account.getServer());
sendServiceDiscoveryInfo(account.getJid().toBareJid());
sendServiceDiscoveryItems(account.getServer());
@ -920,11 +922,6 @@ public class XmppConnection implements Runnable {
}
private void sendServiceDiscoveryInfo(final Jid jid) {
if (disco.containsKey(jid)) {
if (account.getServer().equals(jid)) {
enableAdvancedStreamFeatures();
}
} else {
final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(jid);
iq.query("http://jabber.org/protocol/disco#info");
@ -933,6 +930,8 @@ public class XmppConnection implements Runnable {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) {
boolean advancedStreamFeaturesLoaded = false;
synchronized (XmppConnection.this.disco) {
final List<Element> elements = packet.query().getChildren();
final Info info = new Info();
for (final Element element : elements) {
@ -947,9 +946,10 @@ public class XmppConnection implements Runnable {
}
}
disco.put(jid, info);
if ((jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))
&& disco.containsKey(account.getServer())
&& disco.containsKey(account.getJid().toBareJid())) {
advancedStreamFeaturesLoaded = disco.containsKey(account.getServer())
&& disco.containsKey(account.getJid().toBareJid());
}
if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))) {
enableAdvancedStreamFeatures();
}
} else {
@ -958,7 +958,6 @@ public class XmppConnection implements Runnable {
}
});
}
}
private void enableAdvancedStreamFeatures() {
if (getFeatures().carbons() && !features.carbonsEnabled) {
@ -1184,14 +1183,16 @@ public class XmppConnection implements Runnable {
}
public List<Jid> findDiscoItemsByFeature(final String feature) {
synchronized (this.disco) {
final List<Jid> items = new ArrayList<>();
for (final Entry<Jid, Info> cursor : disco.entrySet()) {
for (final Entry<Jid, Info> cursor : this.disco.entrySet()) {
if (cursor.getValue().features.contains(feature)) {
items.add(cursor.getKey());
}
}
return items;
}
}
public Jid findDiscoItemByFeature(final String feature) {
final List<Jid> items = findDiscoItemsByFeature(feature);
@ -1211,6 +1212,7 @@ public class XmppConnection implements Runnable {
}
public String getMucServer() {
synchronized (this.disco) {
for (final Entry<Jid, Info> cursor : disco.entrySet()) {
final Info value = cursor.getValue();
if (value.features.contains("http://jabber.org/protocol/muc")
@ -1219,6 +1221,7 @@ public class XmppConnection implements Runnable {
return cursor.getKey().toString();
}
}
}
return null;
}
@ -1302,9 +1305,11 @@ public class XmppConnection implements Runnable {
}
private boolean hasDiscoFeature(final Jid server, final String feature) {
synchronized (XmppConnection.this.disco) {
return connection.disco.containsKey(server) &&
connection.disco.get(server).features.contains(feature);
}
}
public boolean carbons() {
return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
@ -1328,6 +1333,7 @@ public class XmppConnection implements Runnable {
}
public boolean pep() {
synchronized (XmppConnection.this.disco) {
final Pair<String, String> needle = new Pair<>("pubsub", "pep");
Info info = disco.get(account.getServer());
if (info != null && info.identities.contains(needle)) {
@ -1337,6 +1343,7 @@ public class XmppConnection implements Runnable {
return info != null && info.identities.contains(needle);
}
}
}
public boolean mam() {
if (hasDiscoFeature(account.getJid().toBareJid(), "urn:xmpp:mam:0")) {
@ -1347,8 +1354,10 @@ public class XmppConnection implements Runnable {
}
public boolean advancedStreamFeaturesLoaded() {
synchronized (XmppConnection.this.disco) {
return disco.containsKey(account.getServer());
}
}
public boolean rosterVersioning() {
return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");