1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 09:39:39 +02: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:
Florian Schmaus 2013-02-27 22:49:04 +00:00 committed by flow
parent c6248ec000
commit 5c6f257027
4 changed files with 153 additions and 176 deletions

View file

@ -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");
* 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;
class ServerPingTask implements Runnable {
// 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
// reference to connection so that it can be GCed.
private WeakReference<Connection> weakConnection;
private int pingInterval;
private volatile long lastSuccessfulPing = -1;
private int delta = 1000; // 1 seconds
private int tries = 3; // 3 tries
protected ServerPingTask(Connection connection, int pingIntervall) {
protected ServerPingTask(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() {
sleep(60000);
outerLoop:
while(pingInterval > 0) {
Connection connection = weakConnection.get();
if (connection == null) {
// connection has been collected by GC
// which means we can stop the thread by breaking the loop
break;
}
if (connection.isAuthenticated()) {
PingManager pingManager = PingManager.getInstanceFor(connection);
boolean res = false;
for(int i = 0; i < tries; i++) {
if (i != 0) {
try {
Thread.sleep(delta);
} 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;
Connection connection = weakConnection.get();
if (connection == null) {
// connection has been collected by GC
// which means we can stop the thread by breaking the loop
return;
}
if (connection.isAuthenticated()) {
PingManager pingManager = PingManager.getInstanceFor(connection);
boolean res = false;
for (int i = 0; i < tries; i++) {
if (i != 0) {
try {
Thread.sleep(delta);
} catch (InterruptedException e) {
// We received an interrupt
// This only happens if we should stop pinging
return;
}
}
if (!res) {
Set<PingFailedListener> pingFailedListeners = pingManager.getPingFailedListeners();
for (PingFailedListener l : pingFailedListeners) {
l.pingFailed();
}
res = pingManager.pingMyServer();
// stop when we receive a pong back
if (res) {
pingManager.lastSuccessfulPingByTask = System.currentTimeMillis();
break;
}
}
sleep();
}
}
/*
* If pingInterval > 0 sleeps a minimum of pingInterval
*/
private void sleep(int extraSleepTime) {
int totalSleep = pingInterval + extraSleepTime;
if (totalSleep > 0) {
try {
Thread.sleep(totalSleep);
} catch (InterruptedException e) {
/* Ignore */
if (!res) {
Set<PingFailedListener> pingFailedListeners = pingManager.getPingFailedListeners();
for (PingFailedListener l : pingFailedListeners) {
l.pingFailed();
}
} else {
// Ping was successful, wind-up the periodic task again
pingManager.maybeSchedulePingServerTask();
}
}
}
/**
* Sleeps the amount of pingInterval
*/
private void sleep() {
sleep(0);
}
}