Search in sources :

Example 1 with VersionComparator

use of net.i2p.util.VersionComparator in project i2p.i2p by i2p.

the class NetDbRenderer method renderStatusHTML.

/**
 *  @param mode 0: charts only; 1: full routerinfos; 2: abbreviated routerinfos
 */
public void renderStatusHTML(Writer out, int mode) throws IOException {
    if (!_context.netDb().isInitialized()) {
        out.write("<div id=\"notinitialized\">");
        out.write(_t("Not initialized"));
        out.write("</div>");
        out.flush();
        return;
    }
    Log log = _context.logManager().getLog(NetDbRenderer.class);
    long start = System.currentTimeMillis();
    boolean full = mode == 1;
    boolean shortStats = mode == 2;
    // this means show the router infos
    boolean showStats = full || shortStats;
    Hash us = _context.routerHash();
    StringBuilder buf = new StringBuilder(8192);
    if (showStats) {
        RouterInfo ourInfo = _context.router().getRouterInfo();
        renderRouterInfo(buf, ourInfo, true, true);
        out.write(buf.toString());
        buf.setLength(0);
    }
    ObjectCounter<String> versions = new ObjectCounter<String>();
    ObjectCounter<String> countries = new ObjectCounter<String>();
    int[] transportCount = new int[TNAMES.length];
    Set<RouterInfo> routers = new TreeSet<RouterInfo>(new RouterInfoComparator());
    routers.addAll(_context.netDb().getRouters());
    for (RouterInfo ri : routers) {
        Hash key = ri.getIdentity().getHash();
        boolean isUs = key.equals(us);
        if (!isUs) {
            if (showStats) {
                renderRouterInfo(buf, ri, false, full);
                out.write(buf.toString());
                buf.setLength(0);
            }
            String routerVersion = ri.getOption("router.version");
            if (routerVersion != null)
                versions.increment(routerVersion);
            String country = _context.commSystem().getCountry(key);
            if (country != null)
                countries.increment(country);
            transportCount[classifyTransports(ri)]++;
        }
    }
    long end = System.currentTimeMillis();
    if (log.shouldWarn())
        log.warn("part 1 took " + (end - start));
    start = end;
    // 
    if (!showStats) {
        // the summary table
        buf.append("<table id=\"netdboverview\" border=\"0\" cellspacing=\"30\"><tr><th colspan=\"3\">").append(_t("Network Database Router Statistics")).append("</th></tr><tr><td style=\"vertical-align: top;\">");
        // versions table
        List<String> versionList = new ArrayList<String>(versions.objects());
        if (!versionList.isEmpty()) {
            Collections.sort(versionList, Collections.reverseOrder(new VersionComparator()));
            buf.append("<table id=\"netdbversions\">\n");
            buf.append("<tr><th>" + _t("Version") + "</th><th>" + _t("Count") + "</th></tr>\n");
            for (String routerVersion : versionList) {
                int num = versions.count(routerVersion);
                String ver = DataHelper.stripHTML(routerVersion);
                buf.append("<tr><td align=\"center\"><a href=\"/netdb?v=").append(ver).append("\">").append(ver);
                buf.append("</a></td><td align=\"center\">").append(num).append("</td></tr>\n");
            }
            buf.append("</table>\n");
        }
        buf.append("</td><td style=\"vertical-align: top;\">");
        out.write(buf.toString());
        buf.setLength(0);
        end = System.currentTimeMillis();
        if (log.shouldWarn())
            log.warn("part 2 took " + (end - start));
        start = end;
        // transports table
        buf.append("<table id=\"netdbtransports\">\n");
        buf.append("<tr><th align=\"left\">" + _t("Transports") + "</th><th>" + _t("Count") + "</th></tr>\n");
        for (int i = 0; i < TNAMES.length; i++) {
            int num = transportCount[i];
            if (num > 0) {
                buf.append("<tr><td>").append(_t(TNAMES[i]));
                buf.append("</td><td align=\"center\">").append(num).append("</td></tr>\n");
            }
        }
        buf.append("</table>\n");
        buf.append("</td><td style=\"vertical-align: top;\">");
        out.write(buf.toString());
        buf.setLength(0);
        end = System.currentTimeMillis();
        if (log.shouldWarn())
            log.warn("part 3 took " + (end - start));
        start = end;
        // country table
        List<String> countryList = new ArrayList<String>(countries.objects());
        if (!countryList.isEmpty()) {
            Collections.sort(countryList, new CountryComparator());
            buf.append("<table id=\"netdbcountrylist\">\n");
            buf.append("<tr><th align=\"left\">" + _t("Country") + "</th><th>" + _t("Count") + "</th></tr>\n");
            for (String country : countryList) {
                int num = countries.count(country);
                buf.append("<tr><td><a href=\"/netdb?c=").append(country).append("\">");
                buf.append("<img height=\"11\" width=\"16\" alt=\"").append(country.toUpperCase(Locale.US)).append("\"");
                buf.append(" src=\"/flags.jsp?c=").append(country).append("\">");
                buf.append(getTranslatedCountry(country));
                buf.append("</a></td><td align=\"center\">").append(num).append("</td></tr>\n");
            }
            buf.append("</table>\n");
        }
        buf.append("</td></tr></table>");
        end = System.currentTimeMillis();
        if (log.shouldWarn())
            log.warn("part 4 took " + (end - start));
        start = end;
    // 
    // don't bother to reindent
    // 
    }
    // if !showStats
    out.write(buf.toString());
    out.flush();
}
Also used : Log(net.i2p.util.Log) RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList) Hash(net.i2p.data.Hash) ObjectCounter(net.i2p.util.ObjectCounter) TreeSet(java.util.TreeSet) VersionComparator(net.i2p.util.VersionComparator)

Aggregations

ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Hash (net.i2p.data.Hash)1 RouterInfo (net.i2p.data.router.RouterInfo)1 Log (net.i2p.util.Log)1 ObjectCounter (net.i2p.util.ObjectCounter)1 VersionComparator (net.i2p.util.VersionComparator)1