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

import org.springframework.jdbc.core.JdbcTemplate;

import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ugnich
 */
public class WSKeepAlive implements Runnable {
    private static final Logger logger = Logger.getLogger(WSKeepAlive.class.getName());

    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;
                    logger.severe("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) {
                            logger.log(Level.SEVERE, "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) {
                    logger.log(Level.SEVERE, "WSKeepAlive statsFile close", e);
                }
            }

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
        }
    }
}