use of net.i2p.util.ObjectCounter in project i2p.i2p by i2p.
the class TunnelPoolManager method selectPeersInTooManyTunnels.
/**
* For reliability reasons, don't allow a peer in more than x% of
* client and exploratory tunnels.
*
* This also will prevent a single huge-capacity (or malicious) peer from
* taking all the tunnels in the network (although it would be nice to limit
* the % of total network tunnels to 10% or so, but that appears to be
* too low to set as a default here... much lower than 33% will push client
* tunnels out of the fast tier into high cap or beyond...)
*
* Possible improvement - restrict based on count per IP, or IP block,
* to slightly increase costs of collusion
*
* @return Set of peers that should not be allowed in another tunnel
*/
public Set<Hash> selectPeersInTooManyTunnels() {
ObjectCounter<Hash> lc = new ObjectCounter<Hash>();
int tunnelCount = countTunnelsPerPeer(lc);
Set<Hash> rv = new HashSet<Hash>();
if (tunnelCount >= 4 && _context.router().getUptime() > 10 * 60 * 1000) {
int max = _context.getProperty("router.maxTunnelPercentage", DEFAULT_MAX_PCT_TUNNELS);
for (Hash h : lc.objects()) {
if (lc.count(h) > 0 && (lc.count(h) + 1) * 100 / (tunnelCount + 1) > max)
rv.add(h);
}
}
return rv;
}
use of net.i2p.util.ObjectCounter 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();
}
Aggregations