code clean up. use Optional to parse SM’s h attribute (iNPUTmice)

This commit is contained in:
Daniel Gultsch 2022-12-30 10:53:49 +01:00 committed by Arne
parent c72d392bf3
commit 942da95647
2 changed files with 48 additions and 45 deletions

View file

@ -6,6 +6,9 @@ import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import com.google.common.base.Optional;
import com.google.common.primitives.Ints;
import eu.siacs.conversations.utils.XmlHelper;
import eu.siacs.conversations.xmpp.InvalidJid;
import eu.siacs.conversations.xmpp.Jid;
@ -149,6 +152,13 @@ public class Element {
return null;
}
}
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) {
final String jid = this.getAttribute(name);

View file

@ -12,6 +12,7 @@ import android.util.Base64;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import com.google.common.base.Optional;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -682,22 +683,21 @@ public class XmppConnection implements Runnable {
}
final Element ack = tagReader.readElement(nextTag);
lastPacketReceived = SystemClock.elapsedRealtime();
try {
final boolean acknowledgedMessages;
synchronized (this.mStanzaQueue) {
final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence);
final boolean acknowledgedMessages;
synchronized (this.mStanzaQueue) {
final Optional<Integer> serverSequence = ack.getOptionalIntAttribute("h");
if (serverSequence.isPresent()) {
acknowledgedMessages = acknowledgeStanzaUpTo(serverSequence.get());
} else {
acknowledgedMessages = false;
Log.d(
Config.LOGTAG,
account.getJid().asBareJid()
+ ": server send ack without sequence number");
}
if (acknowledgedMessages) {
mXmppConnectionService.updateConversationUi();
}
} catch (NumberFormatException e) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server send ack without sequence number");
} catch (NullPointerException e) {
Log.d(
Config.LOGTAG,
account.getJid().asBareJid()
+ ": server send ack without sequence number");
}
if (acknowledgedMessages) {
mXmppConnectionService.updateConversationUi();
}
} else if (nextTag.isStart("failed")) {
final Element failed = tagReader.readElement(nextTag);
@ -995,15 +995,11 @@ public class XmppConnection implements Runnable {
this.isBound = true;
this.tagWriter.writeStanzaAsync(new RequestPacket());
lastPacketReceived = SystemClock.elapsedRealtime();
final String h = resumed.getAttribute("h");
if (h == null) {
resetStreamId();
throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
}
final Optional<Integer> h = resumed.getOptionalIntAttribute("h");
final int serverCount;
try {
serverCount = Integer.parseInt(h);
} catch (final NumberFormatException e) {
if (h.isPresent()) {
serverCount = h.get();
} else {
resetStreamId();
throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
}
@ -1052,28 +1048,22 @@ public class XmppConnection implements Runnable {
}
private void processFailed(final Element failed, final boolean sendBindRequest) {
final int serverCount;
try {
serverCount = Integer.parseInt(failed.getAttribute("h"));
} catch (final NumberFormatException | NullPointerException e) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
resetStreamId();
if (sendBindRequest) {
sendBindRequest();
final Optional<Integer> serverCount = failed.getOptionalIntAttribute("h");
if (serverCount.isPresent()) {
Log.d(
Config.LOGTAG,
account.getJid().asBareJid()
+ ": resumption failed but server acknowledged stanza #"
+ serverCount.get());
final boolean acknowledgedMessages;
synchronized (this.mStanzaQueue) {
acknowledgedMessages = acknowledgeStanzaUpTo(serverCount.get());
}
return;
}
Log.d(
Config.LOGTAG,
account.getJid().asBareJid()
+ ": resumption failed but server acknowledged stanza #"
+ serverCount);
final boolean acknowledgedMessages;
synchronized (this.mStanzaQueue) {
acknowledgedMessages = acknowledgeStanzaUpTo(serverCount);
}
if (acknowledgedMessages) {
mXmppConnectionService.updateConversationUi();
if (acknowledgedMessages) {
mXmppConnectionService.updateConversationUi();
}
} else {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resumption failed");
}
resetStreamId();
if (sendBindRequest) {
@ -1081,7 +1071,7 @@ public class XmppConnection implements Runnable {
}
}
private boolean acknowledgeStanzaUpTo(int serverCount) {
private boolean acknowledgeStanzaUpTo(final int serverCount) {
if (serverCount > stanzasSent) {
Log.e(
Config.LOGTAG,
@ -2422,6 +2412,9 @@ public class XmppConnection implements Runnable {
}
}
++stanzasSent;
if (Config.EXTENDED_SM_LOGGING) {
Log.d(Config.LOGTAG, account.getJid().asBareJid()+": counting outbound "+packet.getName()+" as #" + stanzasSent);
}
this.mStanzaQueue.append(stanzasSent, stanza);
if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
if (Config.EXTENDED_SM_LOGGING) {