blob: 2e36b1002f91dc16709d57851d7b8894e4c5071b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package com.juick.jabber.ws;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.sql.Connection;
import java.util.Iterator;
/**
*
* @author ugnich
*/
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) {
}
}
}
}
|