mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-27 00:32:07 +01:00
SMACK-388
Use ScheduledExecutorService. Set ping received when pinging another entity. Refactored PacketListener and ConnectionListeners as anonymous inner-classes git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13531 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
c6248ec000
commit
5c6f257027
4 changed files with 153 additions and 176 deletions
|
@ -31,7 +31,7 @@
|
||||||
<!-- Port of the local Socks5 proxy -->
|
<!-- Port of the local Socks5 proxy -->
|
||||||
<packetCollectorSize>10000</packetCollectorSize>
|
<packetCollectorSize>10000</packetCollectorSize>
|
||||||
|
|
||||||
<!-- Default interval in which the Ping Manager sends ping request to the server (30 minutes) -->
|
<!-- Default interval (seconds) in which the Ping Manager sends ping request to the server (30 minutes) -->
|
||||||
<defaultPingInterval>1800000</defaultPingInterval>
|
<defaultPingInterval>1800</defaultPingInterval>
|
||||||
|
|
||||||
</smack>
|
</smack>
|
||||||
|
|
|
@ -58,7 +58,10 @@ public final class SmackConfiguration {
|
||||||
private static int localSocks5ProxyPort = 7777;
|
private static int localSocks5ProxyPort = 7777;
|
||||||
private static int packetCollectorSize = 5000;
|
private static int packetCollectorSize = 5000;
|
||||||
|
|
||||||
private static int defaultPingInterval = 1800000; // 30 min (30*60*1000)
|
/**
|
||||||
|
* defaultPingInterval (in seconds)
|
||||||
|
*/
|
||||||
|
private static int defaultPingInterval = 1800; // 30 min (30*60)
|
||||||
|
|
||||||
private SmackConfiguration() {
|
private SmackConfiguration() {
|
||||||
}
|
}
|
||||||
|
@ -109,7 +112,7 @@ public final class SmackConfiguration {
|
||||||
else if (parser.getName().equals("packetCollectorSize")) {
|
else if (parser.getName().equals("packetCollectorSize")) {
|
||||||
packetCollectorSize = parseIntProperty(parser, packetCollectorSize);
|
packetCollectorSize = parseIntProperty(parser, packetCollectorSize);
|
||||||
}
|
}
|
||||||
else if (parser.getName().equals("defaultPingInterval")) {
|
else if (parser.getName().equals("defaultPingInterval")) {
|
||||||
defaultPingInterval = parseIntProperty(parser, defaultPingInterval);
|
defaultPingInterval = parseIntProperty(parser, defaultPingInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,10 +311,20 @@ public final class SmackConfiguration {
|
||||||
SmackConfiguration.localSocks5ProxyPort = localSocks5ProxyPort;
|
SmackConfiguration.localSocks5ProxyPort = localSocks5ProxyPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the default ping interval (seconds)
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static int getDefaultPingInterval() {
|
public static int getDefaultPingInterval() {
|
||||||
return defaultPingInterval;
|
return defaultPingInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the default ping interval (seconds). Set it to '-1' to disable the periodic ping
|
||||||
|
*
|
||||||
|
* @param defaultPingInterval
|
||||||
|
*/
|
||||||
public static void setDefaultPingInterval(int defaultPingInterval) {
|
public static void setDefaultPingInterval(int defaultPingInterval) {
|
||||||
SmackConfiguration.defaultPingInterval = defaultPingInterval;
|
SmackConfiguration.defaultPingInterval = defaultPingInterval;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,10 +21,14 @@ import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.jivesoftware.smack.AbstractConnectionListener;
|
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
import org.jivesoftware.smack.ConnectionListener;
|
||||||
import org.jivesoftware.smack.PacketCollector;
|
import org.jivesoftware.smack.PacketCollector;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.SmackConfiguration;
|
import org.jivesoftware.smack.SmackConfiguration;
|
||||||
|
@ -51,13 +55,14 @@ import org.jivesoftware.smackx.ping.packet.Pong;
|
||||||
* Ping</a>
|
* Ping</a>
|
||||||
*/
|
*/
|
||||||
public class PingManager {
|
public class PingManager {
|
||||||
|
|
||||||
public static final String NAMESPACE = "urn:xmpp:ping";
|
public static final String NAMESPACE = "urn:xmpp:ping";
|
||||||
public static final String ELEMENT = "ping";
|
public static final String ELEMENT = "ping";
|
||||||
|
|
||||||
|
|
||||||
private static Map<Connection, PingManager> instances =
|
private static Map<Connection, PingManager> instances =
|
||||||
Collections.synchronizedMap(new WeakHashMap<Connection, PingManager>());
|
Collections.synchronizedMap(new WeakHashMap<Connection, PingManager>());
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||||
public void connectionCreated(Connection connection) {
|
public void connectionCreated(Connection connection) {
|
||||||
|
@ -65,49 +70,96 @@ public class PingManager {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ScheduledExecutorService periodicPingExecutorService;
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private Thread serverPingThread;
|
|
||||||
private ServerPingTask serverPingTask;
|
|
||||||
private int pingInterval = SmackConfiguration.getDefaultPingInterval();
|
private int pingInterval = SmackConfiguration.getDefaultPingInterval();
|
||||||
private Set<PingFailedListener> pingFailedListeners = Collections
|
private Set<PingFailedListener> pingFailedListeners = Collections
|
||||||
.synchronizedSet(new HashSet<PingFailedListener>());
|
.synchronizedSet(new HashSet<PingFailedListener>());
|
||||||
|
private ScheduledFuture<?> periodicPingTask;
|
||||||
|
protected volatile long lastSuccessfulPingByTask = -1;
|
||||||
|
|
||||||
|
|
||||||
// Ping Flood protection
|
// Ping Flood protection
|
||||||
private long pingMinDelta = 100;
|
private long pingMinDelta = 100;
|
||||||
private long lastPingStamp = 0; // timestamp of the last received ping
|
private long lastPingStamp = 0; // timestamp of the last received ping
|
||||||
|
|
||||||
// Last server pong timestamp if a ping request manually
|
// Timestamp of the last pong received, either from the server or another entity
|
||||||
private long lastServerPingStamp = -1;
|
// Note, no need to synchronize this value, it will only increase over time
|
||||||
|
private long lastSuccessfulManualPing = -1;
|
||||||
|
|
||||||
private PingManager(Connection connection) {
|
private PingManager(Connection connection) {
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(NAMESPACE);
|
sdm.addFeature(NAMESPACE);
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class);
|
init();
|
||||||
connection.addPacketListener(new PingPacketListener(), pingPacketFilter);
|
|
||||||
connection.addConnectionListener(new PingConnectionListener());
|
|
||||||
instances.put(connection, this);
|
|
||||||
maybeStartPingServerTask();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
periodicPingExecutorService = new ScheduledThreadPoolExecutor(1);
|
||||||
|
PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class);
|
||||||
|
connection.addPacketListener(new PacketListener() {
|
||||||
|
/**
|
||||||
|
* Sends a Pong for every Ping
|
||||||
|
*/
|
||||||
|
public void processPacket(Packet packet) {
|
||||||
|
if (pingMinDelta > 0) {
|
||||||
|
// Ping flood protection enabled
|
||||||
|
long currentMillies = System.currentTimeMillis();
|
||||||
|
long delta = currentMillies - lastPingStamp;
|
||||||
|
lastPingStamp = currentMillies;
|
||||||
|
if (delta < pingMinDelta) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Pong pong = new Pong((Ping)packet);
|
||||||
|
connection.sendPacket(pong);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, pingPacketFilter);
|
||||||
|
connection.addConnectionListener(new ConnectionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connectionClosed() {
|
||||||
|
maybeStopPingServerTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connectionClosedOnError(Exception arg0) {
|
||||||
|
maybeStopPingServerTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reconnectionSuccessful() {
|
||||||
|
maybeSchedulePingServerTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reconnectingIn(int seconds) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reconnectionFailed(Exception e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
instances.put(connection, this);
|
||||||
|
maybeSchedulePingServerTask();
|
||||||
|
}
|
||||||
|
|
||||||
public static PingManager getInstanceFor(Connection connection) {
|
public static PingManager getInstanceFor(Connection connection) {
|
||||||
PingManager pingManager = instances.get(connection);
|
PingManager pingManager = instances.get(connection);
|
||||||
|
|
||||||
if (pingManager == null) {
|
if (pingManager == null) {
|
||||||
pingManager = new PingManager(connection);
|
pingManager = new PingManager(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
return pingManager;
|
return pingManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPingIntervall(int pingIntervall) {
|
public void setPingIntervall(int pingIntervall) {
|
||||||
this.pingInterval = pingIntervall;
|
this.pingInterval = pingIntervall;
|
||||||
if (serverPingTask != null) {
|
|
||||||
serverPingTask.setPingInterval(pingIntervall);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPingIntervall() {
|
public int getPingIntervall() {
|
||||||
return pingInterval;
|
return pingInterval;
|
||||||
}
|
}
|
||||||
|
@ -189,11 +241,11 @@ public class PingManager {
|
||||||
*/
|
*/
|
||||||
public boolean pingEntity(String jid, long pingTimeout) {
|
public boolean pingEntity(String jid, long pingTimeout) {
|
||||||
IQ result = ping(jid, pingTimeout);
|
IQ result = ping(jid, pingTimeout);
|
||||||
|
|
||||||
if (result == null
|
if (result == null || result.getType() == IQ.Type.ERROR) {
|
||||||
|| result.getType() == IQ.Type.ERROR) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
pongReceived();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,7 +270,8 @@ public class PingManager {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
lastServerPingStamp = System.currentTimeMillis();
|
// Maybe not really a pong, but an answer is an answer
|
||||||
|
pongReceived();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,80 +310,34 @@ public class PingManager {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public long getLastSuccessfulPing() {
|
public long getLastSuccessfulPing() {
|
||||||
long taskLastSuccessfulPing = -1;
|
return Math.max(lastSuccessfulPingByTask, lastSuccessfulManualPing);
|
||||||
if (serverPingTask != null) {
|
|
||||||
taskLastSuccessfulPing = serverPingTask.getLastSucessfulPing();
|
|
||||||
}
|
|
||||||
return Math.max(taskLastSuccessfulPing, lastServerPingStamp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Set<PingFailedListener> getPingFailedListeners() {
|
protected Set<PingFailedListener> getPingFailedListeners() {
|
||||||
return pingFailedListeners;
|
return pingFailedListeners;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class PingConnectionListener extends AbstractConnectionListener {
|
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void connectionClosed() {
|
* Cancels any existing periodic ping task if there is one and schedules a new ping task if pingInterval is greater
|
||||||
maybeStopPingServerTask();
|
* then zero.
|
||||||
}
|
*
|
||||||
|
*/
|
||||||
@Override
|
protected synchronized void maybeSchedulePingServerTask() {
|
||||||
public void connectionClosedOnError(Exception arg0) {
|
maybeStopPingServerTask();
|
||||||
maybeStopPingServerTask();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reconnectionSuccessful() {
|
|
||||||
maybeStartPingServerTask();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void maybeStartPingServerTask() {
|
|
||||||
if (serverPingTask != null) {
|
|
||||||
serverPingTask.setDone();
|
|
||||||
serverPingThread.interrupt();
|
|
||||||
serverPingTask = null;
|
|
||||||
serverPingThread = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pingInterval > 0) {
|
if (pingInterval > 0) {
|
||||||
serverPingTask = new ServerPingTask(connection, pingInterval);
|
periodicPingTask = periodicPingExecutorService.schedule(new ServerPingTask(connection), pingInterval,
|
||||||
serverPingThread = new Thread(serverPingTask);
|
TimeUnit.SECONDS);
|
||||||
serverPingThread.setDaemon(true);
|
|
||||||
serverPingThread.setName("Smack Ping Server Task (" + connection.getServiceName() + ")");
|
|
||||||
serverPingThread.start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeStopPingServerTask() {
|
private void maybeStopPingServerTask() {
|
||||||
if (serverPingThread != null) {
|
if (periodicPingTask != null) {
|
||||||
serverPingTask.setDone();
|
periodicPingTask.cancel(true);
|
||||||
serverPingThread.interrupt();
|
periodicPingTask = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class PingPacketListener implements PacketListener {
|
private void pongReceived() {
|
||||||
|
lastSuccessfulManualPing = System.currentTimeMillis();
|
||||||
public PingPacketListener() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a Pong for every Ping
|
|
||||||
*/
|
|
||||||
public void processPacket(Packet packet) {
|
|
||||||
if (pingMinDelta > 0) {
|
|
||||||
// Ping flood protection enabled
|
|
||||||
long currentMillies = System.currentTimeMillis();
|
|
||||||
long delta = currentMillies - lastPingStamp;
|
|
||||||
lastPingStamp = currentMillies;
|
|
||||||
if (delta < pingMinDelta) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Pong pong = new Pong((Ping)packet);
|
|
||||||
connection.sendPacket(pong);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* Copyright 2012 Florian Schmaus
|
* Copyright 2012-2013 Florian Schmaus
|
||||||
*
|
*
|
||||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -22,99 +22,56 @@ import java.util.Set;
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
|
|
||||||
class ServerPingTask implements Runnable {
|
class ServerPingTask implements Runnable {
|
||||||
|
|
||||||
// This has to be a weak reference because IIRC all threads are roots
|
// This has to be a weak reference because IIRC all threads are roots
|
||||||
// for objects and we have a new thread here that should hold a strong
|
// for objects and we have a new thread here that should hold a strong
|
||||||
// reference to connection so that it can be GCed.
|
// reference to connection so that it can be GCed.
|
||||||
private WeakReference<Connection> weakConnection;
|
private WeakReference<Connection> weakConnection;
|
||||||
private int pingInterval;
|
|
||||||
private volatile long lastSuccessfulPing = -1;
|
|
||||||
|
|
||||||
private int delta = 1000; // 1 seconds
|
private int delta = 1000; // 1 seconds
|
||||||
private int tries = 3; // 3 tries
|
private int tries = 3; // 3 tries
|
||||||
|
|
||||||
protected ServerPingTask(Connection connection, int pingIntervall) {
|
protected ServerPingTask(Connection connection) {
|
||||||
this.weakConnection = new WeakReference<Connection>(connection);
|
this.weakConnection = new WeakReference<Connection>(connection);
|
||||||
this.pingInterval = pingIntervall;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setDone() {
|
|
||||||
this.pingInterval = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setPingInterval(int pingIntervall) {
|
|
||||||
this.pingInterval = pingIntervall;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int getIntInterval() {
|
|
||||||
return pingInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long getLastSucessfulPing() {
|
|
||||||
return lastSuccessfulPing;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
sleep(60000);
|
Connection connection = weakConnection.get();
|
||||||
|
if (connection == null) {
|
||||||
outerLoop:
|
// connection has been collected by GC
|
||||||
while(pingInterval > 0) {
|
// which means we can stop the thread by breaking the loop
|
||||||
Connection connection = weakConnection.get();
|
return;
|
||||||
if (connection == null) {
|
}
|
||||||
// connection has been collected by GC
|
if (connection.isAuthenticated()) {
|
||||||
// which means we can stop the thread by breaking the loop
|
PingManager pingManager = PingManager.getInstanceFor(connection);
|
||||||
break;
|
boolean res = false;
|
||||||
}
|
|
||||||
if (connection.isAuthenticated()) {
|
for (int i = 0; i < tries; i++) {
|
||||||
PingManager pingManager = PingManager.getInstanceFor(connection);
|
if (i != 0) {
|
||||||
boolean res = false;
|
try {
|
||||||
|
Thread.sleep(delta);
|
||||||
for(int i = 0; i < tries; i++) {
|
} catch (InterruptedException e) {
|
||||||
if (i != 0) {
|
// We received an interrupt
|
||||||
try {
|
// This only happens if we should stop pinging
|
||||||
Thread.sleep(delta);
|
return;
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// We received an interrupt
|
|
||||||
// This only happens if we should stop pinging
|
|
||||||
break outerLoop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res = pingManager.pingMyServer();
|
|
||||||
// stop when we receive a pong back
|
|
||||||
if (res) {
|
|
||||||
lastSuccessfulPing = System.currentTimeMillis();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!res) {
|
res = pingManager.pingMyServer();
|
||||||
Set<PingFailedListener> pingFailedListeners = pingManager.getPingFailedListeners();
|
// stop when we receive a pong back
|
||||||
for (PingFailedListener l : pingFailedListeners) {
|
if (res) {
|
||||||
l.pingFailed();
|
pingManager.lastSuccessfulPingByTask = System.currentTimeMillis();
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sleep();
|
if (!res) {
|
||||||
}
|
Set<PingFailedListener> pingFailedListeners = pingManager.getPingFailedListeners();
|
||||||
}
|
for (PingFailedListener l : pingFailedListeners) {
|
||||||
|
l.pingFailed();
|
||||||
/*
|
}
|
||||||
* If pingInterval > 0 sleeps a minimum of pingInterval
|
} else {
|
||||||
*/
|
// Ping was successful, wind-up the periodic task again
|
||||||
private void sleep(int extraSleepTime) {
|
pingManager.maybeSchedulePingServerTask();
|
||||||
int totalSleep = pingInterval + extraSleepTime;
|
|
||||||
if (totalSleep > 0) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(totalSleep);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
/* Ignore */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sleeps the amount of pingInterval
|
|
||||||
*/
|
|
||||||
private void sleep() {
|
|
||||||
sleep(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue