aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/xmpp/s2s/CleaningUp.java
blob: 14d97ed84eddb874ebeb1d7125df38b14fd797ff (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
package com.juick.xmpp.s2s;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

/**
 *
 * @author ugnich
 */
public class CleaningUp implements Runnable {
    XMPPComponent xmpp;
    
    public CleaningUp(XMPPComponent xmpp) {
        this.xmpp = xmpp;
    }

    @Override
    public void run() {
        while (true) {
            try {
                PrintWriter statsFile = new PrintWriter(xmpp.STATSFILE, "UTF-8");
                statsFile.write("<html><body><h2>Threads: " + Thread.activeCount() + "</h2>");
                statsFile.write("<h2>Out (" + xmpp.outConnections.size() + ")</h2><table border=1><tr><th>to</th><th>sid</th><th>inactive</th><th>out packets</th><th>out bytes</th></tr>");

                long now = System.currentTimeMillis();

                synchronized (xmpp.outConnections) {
                    for (Iterator<ConnectionOut> i = xmpp.outConnections.iterator(); i.hasNext();) {
                        ConnectionOut c = i.next();
                        int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0);
                        if (inactive > 900) {
                            c.closeConnection();
                            i.remove();
                        } else {
                            statsFile.write("  <tr>");
                            statsFile.write("    <td>" + c.to + "</td>\n");
                            statsFile.write("    <td>" + c.streamID + "</td>\n");
                            statsFile.write("    <td>" + inactive + "</td>\n");
                            statsFile.write("    <td>" + c.packetsLocal + "</td>\n");
                            statsFile.write("    <td>" + c.bytesLocal + "</td>\n");
                            statsFile.write("  <tr>");
                        }
                    }
                }

                statsFile.write("</table><h2>In (" + xmpp.inConnections.size() + ")</h2><table border=1><tr><th>from</th><th>sid</th><th>inactive</th><th>in packets</th></tr>");

                synchronized (xmpp.inConnections) {
                    for (Iterator<ConnectionIn> i = xmpp.inConnections.iterator(); i.hasNext();) {
                        ConnectionIn c = i.next();
                        int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0);
                        if (inactive > 900) {
                            c.closeConnection();
                            i.remove();
                        } else {
                            statsFile.write("  <tr>");
                            if (c.from.isEmpty()) {
                                statsFile.write("    <td>&nbsp;</td>\n");
                            } else if (c.from.size() == 1) {
                                statsFile.write("    <td>" + c.from.get(0) + "</td>\n");
                            } else {
                                String out = "    <td>";
                                for (int n = 0; n < c.from.size(); n++) {
                                    if (n > 0) {
                                        out += "<br/>";
                                    }
                                    out += c.from.get(n);
                                }
                                statsFile.write(out + "</td>\n");
                            }
                            statsFile.write("    <td>" + c.streamID + "</td>\n");
                            statsFile.write("    <td>" + inactive + "</td>\n");
                            statsFile.write("    <td>" + c.packetsRemote + "</td>\n");
                            statsFile.write("  <tr>");
                        }
                    }
                }

                statsFile.write("</table><h2>Cache (" + xmpp.outCache.size() + ")</h2><table border=1><tr><th>host</th><th>live</th><th>size</th></tr>");

                synchronized (xmpp.outCache) {
                    for (Iterator<CacheEntry> i = xmpp.outCache.iterator(); i.hasNext();) {
                        CacheEntry c = i.next();
                        int inactive = (int) ((double) (now - c.tsCreated) / 1000.0);
                        if (inactive > 600) {
                            i.remove();
                        } else {
                            statsFile.write("<tr><td>" + c.hostname + "</td><td>" + inactive + "</td><td>" + c.xml.length() + "</td></tr>");
                        }
                    }
                }

                statsFile.write("</table></body></html>");
                statsFile.close();

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