synchronize around the disco object

This commit is contained in:
Daniel Gultsch 2015-10-19 23:03:19 +02:00
parent 0bd4105b1d
commit adca670196

View file

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