code clean up. use Optional to parse SM’s h attribute
This commit is contained in:
parent
f5b7fbc441
commit
01fba162f0
|
@ -1,5 +1,8 @@
|
||||||
package eu.siacs.conversations.xml;
|
package eu.siacs.conversations.xml;
|
||||||
|
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
import com.google.common.primitives.Ints;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -150,6 +153,14 @@ public class Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<Integer> getOptionalIntAttribute(final String name) {
|
||||||
|
final String value = getAttribute(name);
|
||||||
|
if (value == null) {
|
||||||
|
return Optional.absent();
|
||||||
|
}
|
||||||
|
return Optional.fromNullable(Ints.tryParse(value));
|
||||||
|
}
|
||||||
|
|
||||||
public Jid getAttributeAsJid(String name) {
|
public Jid getAttributeAsJid(String name) {
|
||||||
final String jid = this.getAttribute(name);
|
final String jid = this.getAttribute(name);
|
||||||
if (jid != null && !jid.isEmpty()) {
|
if (jid != null && !jid.isEmpty()) {
|
||||||
|
|
|
@ -16,6 +16,7 @@ import android.util.SparseArray;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
@ -631,21 +632,22 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
final Element ack = tagReader.readElement(nextTag);
|
final Element ack = tagReader.readElement(nextTag);
|
||||||
lastPacketReceived = SystemClock.elapsedRealtime();
|
lastPacketReceived = SystemClock.elapsedRealtime();
|
||||||
try {
|
|
||||||
final boolean acknowledgedMessages;
|
final boolean acknowledgedMessages;
|
||||||
synchronized (this.mStanzaQueue) {
|
synchronized (this.mStanzaQueue) {
|
||||||
final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
|
final Optional<Integer> serverSequence = ack.getOptionalIntAttribute("h");
|
||||||
acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence);
|
if (serverSequence.isPresent()) {
|
||||||
}
|
acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence.get());
|
||||||
if (acknowledgedMessages) {
|
} else {
|
||||||
mXmppConnectionService.updateConversationUi();
|
acknowledgedMessages = false;
|
||||||
}
|
|
||||||
} catch (NumberFormatException | NullPointerException e) {
|
|
||||||
Log.d(
|
Log.d(
|
||||||
Config.LOGTAG,
|
Config.LOGTAG,
|
||||||
account.getJid().asBareJid()
|
account.getJid().asBareJid()
|
||||||
+ ": server send ack without sequence number");
|
+ ": server send ack without sequence number");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (acknowledgedMessages) {
|
||||||
|
mXmppConnectionService.updateConversationUi();
|
||||||
|
}
|
||||||
} else if (nextTag.isStart("failed")) {
|
} else if (nextTag.isStart("failed")) {
|
||||||
final Element failed = tagReader.readElement(nextTag);
|
final Element failed = tagReader.readElement(nextTag);
|
||||||
processFailed(failed, true);
|
processFailed(failed, true);
|
||||||
|
@ -942,15 +944,11 @@ public class XmppConnection implements Runnable {
|
||||||
this.isBound = true;
|
this.isBound = true;
|
||||||
this.tagWriter.writeStanzaAsync(new RequestPacket());
|
this.tagWriter.writeStanzaAsync(new RequestPacket());
|
||||||
lastPacketReceived = SystemClock.elapsedRealtime();
|
lastPacketReceived = SystemClock.elapsedRealtime();
|
||||||
final String h = resumed.getAttribute("h");
|
final Optional<Integer> h = resumed.getOptionalIntAttribute("h");
|
||||||
if (h == null) {
|
|
||||||
resetStreamId();
|
|
||||||
throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
|
|
||||||
}
|
|
||||||
final int serverCount;
|
final int serverCount;
|
||||||
try {
|
if (h.isPresent()) {
|
||||||
serverCount = Integer.parseInt(h);
|
serverCount = h.get();
|
||||||
} catch (final NumberFormatException e) {
|
} else {
|
||||||
resetStreamId();
|
resetStreamId();
|
||||||
throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
|
throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
|
||||||
}
|
}
|
||||||
|
@ -999,36 +997,30 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processFailed(final Element failed, final boolean sendBindRequest) {
|
private void processFailed(final Element failed, final boolean sendBindRequest) {
|
||||||
final int serverCount;
|
final Optional<Integer> serverCount = failed.getOptionalIntAttribute("h");
|
||||||
try {
|
if (serverCount.isPresent()) {
|
||||||
serverCount = Integer.parseInt(failed.getAttribute("h"));
|
|
||||||
} catch (final NumberFormatException | NullPointerException e) {
|
|
||||||
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
|
|
||||||
resetStreamId();
|
|
||||||
if (sendBindRequest) {
|
|
||||||
sendBindRequest();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Log.d(
|
Log.d(
|
||||||
Config.LOGTAG,
|
Config.LOGTAG,
|
||||||
account.getJid().asBareJid()
|
account.getJid().asBareJid()
|
||||||
+ ": resumption failed but server acknowledged stanza #"
|
+ ": resumption failed but server acknowledged stanza #"
|
||||||
+ serverCount);
|
+ serverCount.get());
|
||||||
final boolean acknowledgedMessages;
|
final boolean acknowledgedMessages;
|
||||||
synchronized (this.mStanzaQueue) {
|
synchronized (this.mStanzaQueue) {
|
||||||
acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
|
acknowledgedMessages = acknowledgeStanzaUpTo(serverCount.get());
|
||||||
}
|
}
|
||||||
if (acknowledgedMessages) {
|
if (acknowledgedMessages) {
|
||||||
mXmppConnectionService.updateConversationUi();
|
mXmppConnectionService.updateConversationUi();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
|
||||||
|
}
|
||||||
resetStreamId();
|
resetStreamId();
|
||||||
if (sendBindRequest) {
|
if (sendBindRequest) {
|
||||||
sendBindRequest();
|
sendBindRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean acknowledgeStanzaUpTo(int serverCount) {
|
private boolean acknowledgeStanzaUpTo(final int serverCount) {
|
||||||
if (serverCount > stanzasSent) {
|
if (serverCount > stanzasSent) {
|
||||||
Log.e(
|
Log.e(
|
||||||
Config.LOGTAG,
|
Config.LOGTAG,
|
||||||
|
@ -2277,6 +2269,9 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
++stanzasSent;
|
++stanzasSent;
|
||||||
|
if (Config.EXTENDED_SM_LOGGING) {
|
||||||
|
Log.d(Config.LOGTAG, account.getJid().asBareJid()+": counting outbound "+packet.getName()+" as #" + stanzasSent);
|
||||||
|
}
|
||||||
this.mStanzaQueue.append(stanzasSent, stanza);
|
this.mStanzaQueue.append(stanzasSent, stanza);
|
||||||
if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
|
if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
|
||||||
if (Config.EXTENDED_SM_LOGGING) {
|
if (Config.EXTENDED_SM_LOGGING) {
|
||||||
|
|
Loading…
Reference in a new issue