aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick/jabber/ws/WSKeepAlive.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/juick/jabber/ws/WSKeepAlive.java')
-rw-r--r--src/com/juick/jabber/ws/WSKeepAlive.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/com/juick/jabber/ws/WSKeepAlive.java b/src/com/juick/jabber/ws/WSKeepAlive.java
index ba6376a4..2e36b100 100644
--- a/src/com/juick/jabber/ws/WSKeepAlive.java
+++ b/src/com/juick/jabber/ws/WSKeepAlive.java
@@ -1,6 +1,9 @@
package com.juick.jabber.ws;
+import java.io.PrintWriter;
+import java.nio.ByteBuffer;
import java.sql.Connection;
+import java.util.Iterator;
/**
*
@@ -9,12 +12,115 @@ import java.sql.Connection;
public class WSKeepAlive implements Runnable {
Connection sql;
+ ByteBuffer pingBytes;
+ ByteBuffer closeBytes;
public WSKeepAlive(Connection sql) {
this.sql = sql;
+
+ //pingBytes = ByteBuffer.allocate(2);
+ //pingBytes.put((byte) 0x8A); // PONG FRAME
+ //pingBytes.put((byte) 0x00); // 0 byte long
+ pingBytes = ByteBuffer.allocate(3);
+ pingBytes.put((byte) 0x81); // TEXT FRAME
+ pingBytes.put((byte) 0x01); // 1 byte long
+ pingBytes.put((byte) 0x20); // ' '
+ pingBytes.flip();
+
+ closeBytes = ByteBuffer.allocate(2);
+ closeBytes.put((byte) 0x88); // CLOSE FRAME
+ closeBytes.put((byte) 0x00); // 0 byte long
+ closeBytes.flip();
}
@Override
public void run() {
+ while (true) {
+ PrintWriter statsFile = null;
+
+ if (Main.STATSFILE != null) {
+ try {
+ statsFile = new PrintWriter(Main.STATSFILE, "UTF-8");
+ } catch (Exception e) {
+ statsFile = null;
+ System.err.println("WSKeepAlive statsFile: " + e);
+ }
+ }
+
+ long now = System.currentTimeMillis();
+
+ synchronized (Main.clients) {
+ if (statsFile != null) {
+ statsFile.write("<html><body><h1>Connections (" + Main.clients.size() + ")</h2><table border=1><tr><th>IP</th><th>inactive</th><th>VUID</th><th>UID</th><th>MID</th><th>allM</th><th>allR</th></tr>");
+ }
+
+ for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
+ SocketSubscribed s = i.next();
+ int inactive = (int) ((double) (now - s.tsLastData) / 1000.0);
+
+ if (statsFile != null) {
+ try {
+ statsFile.print("<tr><td>" + (s.clientName != null ? s.clientName : "?") + "</td>");
+ statsFile.print("<td>" + inactive + "</td>");
+ statsFile.print("<td>" + (s.VUID > 0 ? s.VUID : "") + "</td>");
+ statsFile.print("<td>" + (s.UID > 0 ? s.UID : "") + "</td>");
+ statsFile.print("<td>" + (s.MID > 0 ? s.MID : "") + "</td>");
+ statsFile.print("<td>" + (s.allMessages ? "+" : "") + "</td>");
+ statsFile.print("<td>" + (s.allReplies ? "+" : "") + "</td></tr>");
+ } catch (Exception e) {
+ System.err.println("WSKeepAlive statsFile print: " + e);
+ }
+ }
+
+ if (inactive > 180) {
+ closeBytes.rewind();
+ try {
+ s.sock.write(closeBytes);
+ } catch (Exception e) {
+ } finally {
+ try {
+ s.sock.socket().close();
+ } catch (Exception ex) {
+ }
+ try {
+ s.sock.close();
+ } catch (Exception ex) {
+ }
+ i.remove();
+ }
+ } else if (inactive > 60) {
+ pingBytes.rewind();
+ try {
+ s.sock.write(pingBytes);
+ } catch (Exception e) {
+ System.err.println("WSKeepAlive ping: " + e);
+ try {
+ s.sock.socket().close();
+ } catch (Exception ex) {
+ }
+ try {
+ s.sock.close();
+ } catch (Exception ex) {
+ }
+ i.remove();
+ }
+ }
+ }
+ }
+
+ if (Main.STATSFILE != null) {
+ try {
+ statsFile.write("</table></body></html>");
+ statsFile.close();
+ } catch (Exception e) {
+ System.err.println("WSKeepAlive statsFile close: " + e);
+ }
+ }
+
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ }
+ }
}
}