Skip to content

Commit 25655f8

Browse files
authored
Merge pull request igniterealtime#698 from Flowdalic/get-last-data-received
Add and use XMPPConnection.getLastDataReceived()
2 parents 0631706 + 6885336 commit 25655f8

8 files changed

Lines changed: 96 additions & 6 deletions

File tree

smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,20 @@ public long getLastStanzaReceived() {
21522152
return lastStanzaReceived;
21532153
}
21542154

2155+
private long lastDataReceived;
2156+
2157+
protected void notifyDataReceived() {
2158+
lastDataReceived = System.currentTimeMillis();
2159+
}
2160+
2161+
@Override
2162+
public long getLastDataReceived() {
2163+
if (lastDataReceived == 0) {
2164+
return getLastStanzaReceived();
2165+
}
2166+
return lastDataReceived;
2167+
}
2168+
21552169
@Override
21562170
public JxmppContext getJxmppContext() {
21572171
return jxmppContext;

smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,13 @@ default boolean hasFeature(String element, String namespace) {
715715
*/
716716
long getLastStanzaReceived();
717717

718+
/**
719+
* Returns the timestamp in milliseconds when the last data was received.
720+
*
721+
* @return the timestamp in milliseconds
722+
*/
723+
long getLastDataReceived();
724+
718725
default JxmppContext getJxmppContext() {
719726
return JxmppContext.getDefaultContext();
720727
};

smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ public void setTransport(XmppClientToServerTransport xmppTransport) {
280280
ModularXmppClientToServerConnection.this.connected = true;
281281
}
282282

283+
@Override
284+
public void notifyDataReceived() {
285+
ModularXmppClientToServerConnection.this.notifyDataReceived();
286+
}
283287
};
284288

285289
// Construct the modules from the module descriptor. We do this before constructing the state graph, as the

smack-core/src/main/java/org/jivesoftware/smack/c2s/internal/ModularXmppClientToServerConnectionInternal.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2020-2025 Florian Schmaus
3+
* Copyright 2020-2026 Florian Schmaus
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -142,4 +142,7 @@ public abstract void waitForConditionOrThrowConnectionException(Supplier<Boolean
142142
* @param xmppTransport the active transport.
143143
*/
144144
public abstract void setTransport(XmppClientToServerTransport xmppTransport);
145+
146+
public abstract void notifyDataReceived();
147+
145148
}

smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2012-2018 Florian Schmaus
3+
* Copyright 2012-2026 Florian Schmaus
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -416,11 +416,11 @@ public void pingServerIfNecessary() {
416416
// Ping has been disabled
417417
return;
418418
}
419-
long lastStanzaReceived = connection.getLastStanzaReceived();
420-
if (lastStanzaReceived > 0) {
419+
long lastDataReceived = connection.getLastDataReceived();
420+
if (lastDataReceived > 0) {
421421
long now = System.currentTimeMillis();
422422
// Delta since the last stanza was received
423-
int deltaInSeconds = (int) ((now - lastStanzaReceived) / 1000);
423+
int deltaInSeconds = (int) ((now - lastDataReceived) / 1000);
424424
// If the delta is small then the ping interval, then we can defer the ping
425425
if (deltaInSeconds < pingInterval) {
426426
maybeSchedulePingServerTask(deltaInSeconds);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
* Copyright 2026 Florian Schmaus
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jivesoftware.smack.tcp;
18+
19+
import java.io.FilterInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.function.IntConsumer;
23+
24+
class CallbackInputStream extends FilterInputStream {
25+
private final IntConsumer onReadCallback;
26+
27+
CallbackInputStream(InputStream in, IntConsumer onReadCallback) {
28+
super(in);
29+
this.onReadCallback = onReadCallback != null ? onReadCallback : (bytes) -> { };
30+
}
31+
32+
@Override
33+
public int read() throws IOException {
34+
int b = super.read();
35+
if (b != -1) {
36+
onReadCallback.accept(1);
37+
}
38+
return b;
39+
}
40+
41+
@Override
42+
public int read(byte[] b) throws IOException {
43+
int bytesRead = super.read(b);
44+
if (bytesRead > 0) {
45+
onReadCallback.accept(bytesRead);
46+
}
47+
return bytesRead;
48+
}
49+
50+
@Override
51+
public int read(byte[] b, int off, int len) throws IOException {
52+
int bytesRead = super.read(b, off, len);
53+
if (bytesRead > 0) {
54+
onReadCallback.accept(bytesRead);
55+
}
56+
return bytesRead;
57+
}
58+
}

smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ private void initConnection() throws IOException, InterruptedException {
702702

703703
private void initReaderAndWriter() throws IOException {
704704
InputStream is = socket.getInputStream();
705+
is = new CallbackInputStream(is, (bytesRead) -> notifyDataReceived());
705706
OutputStream os = socket.getOutputStream();
706707
if (compressionHandler != null) {
707708
is = compressionHandler.getInputStream(is);

smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XmppTcpTransportModule.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,10 @@ private void onChannelSelected(SelectableChannel selectedChannel, SelectionKey s
500500
newInterestedOps |= SelectionKey.OP_WRITE;
501501
}
502502

503-
callbackBytesRead += bytesRead;
503+
if (bytesRead > 0) {
504+
callbackBytesRead += bytesRead;
505+
this.connectionInternal.notifyDataReceived();
506+
}
504507

505508
ByteBuffer filteredIncomingBuffer = incomingBuffer;
506509
for (ListIterator<XmppInputOutputFilter> it = connectionInternal.getXmppInputOutputFilterEndIterator(); it.hasPrevious();) {

0 commit comments

Comments
 (0)