aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/jabber/ws/WSKeepAlive.java
blob: f253439d4e189ed393612b4353c1adea3600a1fe (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
package com.juick.jabber.ws;

import org.springframework.jdbc.core.JdbcTemplate;

import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.util.Iterator;

/**
 *
 * @author ugnich
 */
public class WSKeepAlive implements Runnable {

    JdbcTemplate sql;
    ByteBuffer pingBytes;
    ByteBuffer closeBytes;

    public WSKeepAlive(JdbcTemplate 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) {
                        s.sendByteBuffer(closeBytes);
                        s.close();
                        i.remove();
                    } else if (inactive > 60) {
                        if (!s.sendByteBuffer(pingBytes)) {
                            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) {
            }
        }
    }
}