avoid requesting blocklist after stream resumption

This commit is contained in:
Daniel Gultsch 2015-01-05 16:17:05 +01:00
parent 2679824770
commit 85f24c9106
2 changed files with 24 additions and 27 deletions

View file

@ -92,6 +92,7 @@ public class IqParser extends AbstractParser implements OnIqPacketReceived {
// Otherwise, just update the existing blocklist. // Otherwise, just update the existing blocklist.
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.clearBlocklist(); account.clearBlocklist();
account.getXmppConnection().getFeatures().setBlockListRequested(true);
} }
if (items != null) { if (items != null) {
final Collection<Jid> jids = new ArrayList<>(items.size()); final Collection<Jid> jids = new ArrayList<>(items.size());

View file

@ -95,12 +95,9 @@ public class XmppConnection implements Runnable {
private int smVersion = 3; private int smVersion = 3;
private final SparseArray<String> messageReceipts = new SparseArray<>(); private final SparseArray<String> messageReceipts = new SparseArray<>();
private boolean enabledEncryption = false;
private boolean enabledCarbons = false;
private int stanzasReceived = 0; private int stanzasReceived = 0;
private int stanzasSent = 0; private int stanzasSent = 0;
private long lastPaketReceived = 0; private long lastPacketReceived = 0;
private long lastPingSent = 0; private long lastPingSent = 0;
private long lastConnect = 0; private long lastConnect = 0;
private long lastSessionStarted = 0; private long lastSessionStarted = 0;
@ -147,13 +144,12 @@ public class XmppConnection implements Runnable {
protected void connect() { protected void connect() {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting"); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
enabledEncryption = false; features.encryptionEnabled = false;
lastConnect = SystemClock.elapsedRealtime(); lastConnect = SystemClock.elapsedRealtime();
lastPingSent = SystemClock.elapsedRealtime(); lastPingSent = SystemClock.elapsedRealtime();
this.attempt++; this.attempt++;
try { try {
shouldAuthenticate = shouldBind = !account shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
.isOptionSet(Account.OPTION_REGISTER);
tagReader = new XmlReader(wakeLock); tagReader = new XmlReader(wakeLock);
tagWriter = new TagWriter(); tagWriter = new TagWriter();
packetCallbacks.clear(); packetCallbacks.clear();
@ -304,7 +300,7 @@ public class XmppConnection implements Runnable {
final RequestPacket r = new RequestPacket(smVersion); final RequestPacket r = new RequestPacket(smVersion);
tagWriter.writeStanzaAsync(r); tagWriter.writeStanzaAsync(r);
} else if (nextTag.isStart("resumed")) { } else if (nextTag.isStart("resumed")) {
lastPaketReceived = SystemClock.elapsedRealtime(); lastPacketReceived = SystemClock.elapsedRealtime();
final Element resumed = tagReader.readElement(nextTag); final Element resumed = tagReader.readElement(nextTag);
final String h = resumed.getAttribute("h"); final String h = resumed.getAttribute("h");
try { try {
@ -337,7 +333,7 @@ public class XmppConnection implements Runnable {
tagWriter.writeStanzaAsync(ack); tagWriter.writeStanzaAsync(ack);
} else if (nextTag.isStart("a")) { } else if (nextTag.isStart("a")) {
final Element ack = tagReader.readElement(nextTag); final Element ack = tagReader.readElement(nextTag);
lastPaketReceived = SystemClock.elapsedRealtime(); lastPacketReceived = SystemClock.elapsedRealtime();
final int serverSequence = Integer.parseInt(ack.getAttribute("h")); final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
final String msgId = this.messageReceipts.get(serverSequence); final String msgId = this.messageReceipts.get(serverSequence);
if (msgId != null) { if (msgId != null) {
@ -426,7 +422,7 @@ public class XmppConnection implements Runnable {
} }
} }
++stanzasReceived; ++stanzasReceived;
lastPaketReceived = SystemClock.elapsedRealtime(); lastPacketReceived = SystemClock.elapsedRealtime();
return element; return element;
} }
@ -530,7 +526,7 @@ public class XmppConnection implements Runnable {
tagWriter.setOutputStream(sslSocket.getOutputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream());
sendStartStream(); sendStartStream();
Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established"); Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established");
enabledEncryption = true; features.encryptionEnabled = true;
processStream(tagReader.readTag()); processStream(tagReader.readTag());
sslSocket.close(); sslSocket.close();
} catch (final NoSuchAlgorithmException | KeyManagementException e1) { } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
@ -543,18 +539,18 @@ public class XmppConnection implements Runnable {
private void processStreamFeatures(final Tag currentTag) private void processStreamFeatures(final Tag currentTag)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
this.streamFeatures = tagReader.readElement(currentTag); this.streamFeatures = tagReader.readElement(currentTag);
if (this.streamFeatures.hasChild("starttls") && !enabledEncryption) { if (this.streamFeatures.hasChild("starttls") && !features.encryptionEnabled) {
sendStartTLS(); sendStartTLS();
} else if (this.streamFeatures.hasChild("register") } else if (this.streamFeatures.hasChild("register")
&& account.isOptionSet(Account.OPTION_REGISTER) && account.isOptionSet(Account.OPTION_REGISTER)
&& enabledEncryption) { && features.encryptionEnabled) {
sendRegistryRequest(); sendRegistryRequest();
} else if (!this.streamFeatures.hasChild("register") } else if (!this.streamFeatures.hasChild("register")
&& account.isOptionSet(Account.OPTION_REGISTER)) { && account.isOptionSet(Account.OPTION_REGISTER)) {
changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED); changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
disconnect(true); disconnect(true);
} else if (this.streamFeatures.hasChild("mechanisms") } else if (this.streamFeatures.hasChild("mechanisms")
&& shouldAuthenticate && enabledEncryption) { && shouldAuthenticate && features.encryptionEnabled) {
final List<String> mechanisms = extractMechanisms(streamFeatures final List<String> mechanisms = extractMechanisms(streamFeatures
.findChild("mechanisms")); .findChild("mechanisms"));
final Element auth = new Element("auth"); final Element auth = new Element("auth");
@ -679,15 +675,15 @@ public class XmppConnection implements Runnable {
tagWriter.writeStanzaAsync(enable); tagWriter.writeStanzaAsync(enable);
stanzasSent = 0; stanzasSent = 0;
messageReceipts.clear(); messageReceipts.clear();
} else if (streamFeatures.hasChild("sm", } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
"urn:xmpp:sm:2")) {
smVersion = 2; smVersion = 2;
final EnablePacket enable = new EnablePacket(smVersion); final EnablePacket enable = new EnablePacket(smVersion);
tagWriter.writeStanzaAsync(enable); tagWriter.writeStanzaAsync(enable);
stanzasSent = 0; stanzasSent = 0;
messageReceipts.clear(); messageReceipts.clear();
} }
enabledCarbons = false; features.carbonsEnabled = false;
features.blockListRequested = false;
disco.clear(); disco.clear();
sendServiceDiscoveryInfo(account.getServer()); sendServiceDiscoveryInfo(account.getServer());
sendServiceDiscoveryItems(account.getServer()); sendServiceDiscoveryItems(account.getServer());
@ -750,12 +746,10 @@ public class XmppConnection implements Runnable {
} }
private void enableAdvancedStreamFeatures() { private void enableAdvancedStreamFeatures() {
if (getFeatures().carbons()) { if (getFeatures().carbons() && !features.carbonsEnabled) {
if (!enabledCarbons) {
sendEnableCarbons(); sendEnableCarbons();
} }
} if (getFeatures().blocking() && !features.blockListRequested) {
if (getFeatures().blocking()) {
Log.d(Config.LOGTAG, "Requesting block list"); Log.d(Config.LOGTAG, "Requesting block list");
this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser()); this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
} }
@ -792,7 +786,7 @@ public class XmppConnection implements Runnable {
if (!packet.hasChild("error")) { if (!packet.hasChild("error")) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": successfully enabled carbons"); + ": successfully enabled carbons");
enabledCarbons = true; features.carbonsEnabled = true;
} else { } else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() Log.d(Config.LOGTAG, account.getJid().toBareJid()
+ ": error enableing carbons " + packet.toString()); + ": error enableing carbons " + packet.toString());
@ -1018,7 +1012,7 @@ public class XmppConnection implements Runnable {
} }
public long getLastPacketReceived() { public long getLastPacketReceived() {
return this.lastPaketReceived; return this.lastPacketReceived;
} }
public void sendActive() { public void sendActive() {
@ -1031,6 +1025,9 @@ public class XmppConnection implements Runnable {
public class Features { public class Features {
XmppConnection connection; XmppConnection connection;
private boolean carbonsEnabled = false;
private boolean encryptionEnabled = false;
private boolean blockListRequested = false;
public Features(final XmppConnection connection) { public Features(final XmppConnection connection) {
this.connection = connection; this.connection = connection;
@ -1078,9 +1075,8 @@ public class XmppConnection implements Runnable {
return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver"); return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
} }
public boolean streamhost() { public void setBlockListRequested(boolean value) {
return connection this.blockListRequested = value;
.findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null;
} }
} }