Work on keep-alive interval. Fixes SMACK-123 and SMACK-121. Also general work on SmackConfiguration class.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2253 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2004-03-15 18:59:41 +00:00 committed by mtucker
parent a7e7152b52
commit 167d40ac20
2 changed files with 82 additions and 39 deletions

View File

@ -74,7 +74,6 @@ class PacketWriter {
private List listeners = new ArrayList(); private List listeners = new ArrayList();
private Thread listenerThread; private Thread listenerThread;
private LinkedList sentPackets = new LinkedList(); private LinkedList sentPackets = new LinkedList();
private Timer keepAliveTimer = new Timer(true);
/** /**
* Creates a new packet writer with the specified connection. * Creates a new packet writer with the specified connection.
@ -102,9 +101,14 @@ class PacketWriter {
listenerThread.setName("Smack Writer Listener Processor"); listenerThread.setName("Smack Writer Listener Processor");
listenerThread.setDaemon(true); listenerThread.setDaemon(true);
// Schedule a keep-alive task for once every 30 seconds. It will write // Schedule a keep-alive task to run if the feature is enabled. will write
// out a space character each time it runs to keep the TCP/IP connection open. // out a space character each time it runs to keep the TCP/IP connection open.
keepAliveTimer.scheduleAtFixedRate(new KeepAliveTask(), 30000, 30000); int keepAliveInterval = SmackConfiguration.getKeepAliveInterval();
if (keepAliveInterval > 0) {
Thread keepAliveThread = new Thread(new KeepAliveTask(keepAliveInterval));
keepAliveThread.setDaemon(true);
keepAliveThread.start();
}
} }
/** /**
@ -227,7 +231,6 @@ class PacketWriter {
finally { finally {
try { try {
writer.close(); writer.close();
keepAliveTimer.cancel();
} }
catch (Exception e) { } catch (Exception e) { }
} }
@ -323,14 +326,26 @@ class PacketWriter {
* A TimerTask that keeps connections to the server alive by sending a space * A TimerTask that keeps connections to the server alive by sending a space
* character. The * character. The
*/ */
private class KeepAliveTask extends TimerTask { private class KeepAliveTask implements Runnable {
private int delay;
public KeepAliveTask(int delay) {
this.delay = delay;
}
public void run() { public void run() {
if (!done) { while (!done) {
synchronized (writer) { synchronized (writer) {
try { try {
writer.write(" "); writer.write(" ");
} }
catch (Exception e) { } catch (Exception e) { }
try {
// Sleep 30 seconds.
Thread.sleep(delay);
}
catch (InterruptedException ie) { }
} }
} }
} }

View File

@ -69,8 +69,10 @@ import org.xmlpull.v1.*;
*/ */
public final class SmackConfiguration { public final class SmackConfiguration {
private static String versionNumber = "1.3.0"; private static final String SMACK_VERSION = "1.3.1";
private static int packetReplyTimeout = -1;
private static int packetReplyTimeout = 5000;
private static int keepAliveInterval = 30000;
private SmackConfiguration() { private SmackConfiguration() {
} }
@ -109,7 +111,10 @@ public final class SmackConfiguration {
parseClassToLoad(parser); parseClassToLoad(parser);
} }
else if (parser.getName().equals("packetReplyTimeout")) { else if (parser.getName().equals("packetReplyTimeout")) {
parsePacketReplyTimeout(parser); packetReplyTimeout = parseIntProperty(parser, packetReplyTimeout);
}
else if (parser.getName().equals("keepAliveInterval")) {
keepAliveInterval = parseIntProperty(parser, keepAliveInterval);
} }
} }
eventType = parser.next(); eventType = parser.next();
@ -129,48 +134,23 @@ public final class SmackConfiguration {
catch (Exception e) { catch (Exception e) {
} }
} }
private static void parseClassToLoad(XmlPullParser parser) throws Exception {
String className = parser.nextText();
// Attempt to load the class so that the class can get initialized
try {
Class.forName(className);
}
catch (ClassNotFoundException cnfe) {
// TODO Replace the printStackTrace to the console with a logger system
//cnfe.printStackTrace();
}
}
private static void parsePacketReplyTimeout(XmlPullParser parser) throws Exception {
int timeout = Integer.parseInt(parser.nextText());
// Set the specified timeout value in the file if the user didn't specify
// a timeout value before. Don't overwrite user preferences.
if (packetReplyTimeout == -1) {
packetReplyTimeout = timeout;
}
}
/** /**
* Returns the current Smack release version. * Returns the Smack version information, e.g. "1.3.0".
* *
* @return the current Smack release version number * @return the Smack version information.
*/ */
public static String getVersionNumber() { public static String getVersion() {
return versionNumber; return SMACK_VERSION;
} }
/** /**
* Returns the number of milliseconds to wait for a response from * Returns the number of milliseconds to wait for a response from
* the server. * the server. The default value is 5000 ms.
* *
* @return the milliseconds to wait for a response from the server * @return the milliseconds to wait for a response from the server
*/ */
public static int getPacketReplyTimeout() { public static int getPacketReplyTimeout() {
// Return a default value if packetReplyTimeout has not been initialized yet
if (packetReplyTimeout == -1) {
return 5000;
}
return packetReplyTimeout; return packetReplyTimeout;
} }
@ -184,6 +164,54 @@ public final class SmackConfiguration {
packetReplyTimeout = timeout; packetReplyTimeout = timeout;
} }
/**
* Returns the number of milleseconds delay between sending keep-alive
* requests to the server. The default value is 30000 ms. A value of -1
* mean no keep-alive requests will be sent to the server.
*
* @return the milliseconds to wait between keep-alive requests, or -1 if
* no keep-alive should be sent.
*/
public static int getKeepAliveInterval() {
return keepAliveInterval;
}
/**
* Sets the number of milleseconds delay between sending keep-alive
* requests to the server. The default value is 30000 ms. A value of -1
* mean no keep-alive requests will be sent to the server.
*
* @param interval the milliseconds to wait between keep-alive requests,
* or -1 if no keep-alive should be sent.
*/
public static void setKeepAliveInterval(int interval) {
keepAliveInterval = interval;
}
private static void parseClassToLoad(XmlPullParser parser) throws Exception {
String className = parser.nextText();
// Attempt to load the class so that the class can get initialized
try {
Class.forName(className);
}
catch (ClassNotFoundException cnfe) {
System.err.println("Error! a startup class specified in smack-config.xml could " +
"not be loaded: " + className);
}
}
private static int parseIntProperty(XmlPullParser parser, int defaultValue)
throws Exception
{
try {
return Integer.parseInt(parser.nextText());
}
catch (NumberFormatException nfe) {
nfe.printStackTrace();
return defaultValue;
}
}
/** /**
* Returns an array of class loaders to load resources from. * Returns an array of class loaders to load resources from.
* *