1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-21 19:42:05 +01:00

Add getLastStanzaReceived() to XMPPConnection

also remove faulty PongFilter from PingManager. It never matched any
stanzas, since a Pong is just a plain result IQ that is not qualified by
any XMPP Ping namespace.

Fixes SMACK-597
This commit is contained in:
Florian Schmaus 2014-08-20 10:33:52 +02:00
parent f65c0d5528
commit 64e49cb934
5 changed files with 22 additions and 25 deletions

View file

@ -79,6 +79,7 @@ public class BOSHPacketReader implements BOSHClientResponseListener {
Packet packet = PacketParserUtils.parseStanza(parser, connection);
if (packet != null) {
connection.processPacket(packet);
// TODO call connection.reportStanzaReceived here
} else if (parser.getName().equals("challenge")) {
// The server is challenging the SASL authentication
// made by the client

View file

@ -1136,4 +1136,14 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
PacketFilter replyFilter = new IQReplyFilter(iqRequest, this);
sendStanzaWithResponseCallback(iqRequest, replyFilter, callback, exceptionCallback, timeout);
}
private long lastStanzaReceived;
public long getLastStanzaReceived() {
return lastStanzaReceived;
}
protected void reportStanzaReceived() {
this.lastStanzaReceived = System.currentTimeMillis();
}
}

View file

@ -457,4 +457,11 @@ public interface XMPPConnection {
public void sendIqWithResponseCallback(IQ iqRequest, final PacketListener callback,
final ExceptionCallback exceptionCallback, long timeout)
throws NotConnectedException;
/**
* Returns the timestamp in milliseconds when the last stanza was received.
*
* @return the timestamp in milliseconds
*/
public long getLastStanzaReceived();
}

View file

@ -68,8 +68,6 @@ public class PingManager extends Manager {
private static final PacketFilter PING_PACKET_FILTER = new AndFilter(
new PacketTypeFilter(Ping.class), IQTypeFilter.GET);
private static final PacketFilter PONG_PACKET_FILTER = new AndFilter(new PacketTypeFilter(
Pong.class), IQTypeFilter.RESULT);
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@ -134,11 +132,6 @@ public class PingManager extends Manager {
private ScheduledFuture<?> nextAutomaticPing;
/**
* The time in milliseconds the last pong was received.
*/
private long lastPongReceived = -1;
private PingManager(XMPPConnection connection) {
super(connection);
executorService = new ScheduledThreadPoolExecutor(1,
@ -155,12 +148,6 @@ public class PingManager extends Manager {
connection().sendPacket(pong);
}
}, PING_PACKET_FILTER);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {
lastPongReceived = System.currentTimeMillis();
}
}, PONG_PACKET_FILTER);
connection.addConnectionListener(new AbstractConnectionListener() {
@Override
public void authenticated(XMPPConnection connection) {
@ -306,15 +293,6 @@ public class PingManager extends Manager {
pingFailedListeners.remove(listener);
}
/**
* Returns the timestamp when the last XMPP Pong was received.
*
* @return the timestamp of the last XMPP Pong
*/
public long getLastReceivedPong() {
return lastPongReceived;
}
private void maybeSchedulePingServerTask() {
maybeSchedulePingServerTask(0);
}
@ -358,12 +336,12 @@ public class PingManager extends Manager {
// Ping has been disabled
return;
}
long lastReceivedPong = getLastReceivedPong();
if (lastReceivedPong > 0) {
long lastStanzaReceived = connection.getLastStanzaReceived();
if (lastStanzaReceived > 0) {
long now = System.currentTimeMillis();
// Calculate the delta from now to the next ping time. If delta is positive, the
// last successful ping was not to long ago, so we can defer the current ping.
int delta = (int) (((pingInterval * 1000) - (now - lastReceivedPong)) / 1000);
int delta = (int) (((pingInterval * 1000) - (now - lastStanzaReceived)) / 1000);
if (delta > 0) {
maybeSchedulePingServerTask(delta);
return;

View file

@ -1002,6 +1002,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
}
if (packet != null) {
processPacket(packet);
reportStanzaReceived();
}
// We found an opening stream. Record information about it, then notify
// the connectionID lock so that the packet reader startup can finish.