1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

Implemented keep-alives.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2219 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2004-01-28 21:30:37 +00:00 committed by mtucker
parent 37b56c9370
commit 14896e4d31

View file

@ -74,6 +74,7 @@ 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.
@ -100,6 +101,10 @@ 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
// out a space character each time it runs to keep the TCP/IP connection open.
keepAliveTimer.scheduleAtFixedRate(new KeepAliveTask(), 30000, 30000);
} }
/** /**
@ -207,10 +212,12 @@ class PacketWriter {
while (!done) { while (!done) {
Packet packet = nextPacket(); Packet packet = nextPacket();
if (packet != null) { if (packet != null) {
synchronized (writer) {
writer.write(packet.toXML()); writer.write(packet.toXML());
writer.flush(); writer.flush();
} }
} }
}
// Close the stream. // Close the stream.
try { try {
writer.write("</stream:stream>"); writer.write("</stream:stream>");
@ -220,6 +227,7 @@ class PacketWriter {
finally { finally {
try { try {
writer.close(); writer.close();
keepAliveTimer.cancel();
} }
catch (Exception e) { } catch (Exception e) { }
} }
@ -310,4 +318,21 @@ class PacketWriter {
} }
} }
} }
/**
* A TimerTask that keeps connections to the server alive by sending a space
* character. The
*/
private class KeepAliveTask extends TimerTask {
public void run() {
if (!done) {
synchronized (writer) {
try {
writer.write(" ");
}
catch (Exception e) { }
}
}
}
}
} }