package net.i2p.router.web;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext;
/**
* Refactored from summarynoframe.jsp to save ~100KB
*
*/
public class SummaryBarRenderer {
private final RouterContext _context;
private final SummaryHelper _helper;
private final NewsHelper _newshelper;
public SummaryBarRenderer(RouterContext context, SummaryHelper helper) {
this(context, helper, null);
}
public SummaryBarRenderer(RouterContext context, NewsHelper newshelper) {
this(context, null, newshelper);
}
public SummaryBarRenderer(RouterContext context, SummaryHelper helper, NewsHelper newshelper) {
_context = context;
_helper = helper;
_newshelper = newshelper;
}
/**
* Note - ensure all links in here are absolute, as the summary bar may be displayed
* on lower-level directory errors.
*/
public void renderSummaryHTML(Writer out) throws IOException {
StringBuilder buf = new StringBuilder(8*1024);
String theme = _context.getProperty(CSSHelper.PROP_THEME_NAME, CSSHelper.DEFAULT_THEME);
// TODO - the bar would render more cleanly if we specified the img height and width here,
// but unfortunately the images in the different themes are different sizes.
// They range in height from 37 to 43 px. But there's a -2 bottom margin...
// So put it in a div.
buf.append("
)
\n")
.append("
\n")
.append(renderHelpAndFAQHTML())
.append("
\n");
File lpath = new File(_context.getBaseDir(), "docs/toolbar.html");
// you better have target="_top" for the links in there...
if (lpath.exists()) {
ContentHelper linkhelper = new ContentHelper();
linkhelper.setPage(lpath.getAbsolutePath());
linkhelper.setMaxLines("100");
buf.append(linkhelper.getContent());
} else {
buf.append(renderI2PServicesHTML())
.append("
\n")
.append(renderI2PInternalsHTML());
out.write(buf.toString());
buf.setLength(0);
}
buf.append("
\n")
.append(renderGeneralHTML())
.append("
\n")
.append(renderNetworkReachabilityHTML())
.append("
\n")
.append(renderUpdateStatusHTML())
.append(renderRestartStatusHTML())
.append("
\n")
.append(renderPeersHTML())
.append("
\n");
out.write(buf.toString());
buf.setLength(0);
buf.append(renderFirewallAndReseedStatusHTML())
.append(renderBandwidthHTML())
.append("
\n")
.append(renderTunnelsHTML())
.append("
\n")
.append(renderCongestionHTML())
.append("
\n")
.append(renderTunnelStatusHTML())
.append("
\n")
.append(renderDestinationsHTML())
.append("
\n");
out.write(buf.toString());
}
public String renderHelpAndFAQHTML() {
StringBuilder buf = new StringBuilder(512);
buf.append("");
return buf.toString();
}
public String renderI2PServicesHTML() {
StringBuilder buf = new StringBuilder(512);
buf.append("\n" +
"
\n");
return buf.toString();
}
public String renderI2PInternalsHTML() {
StringBuilder buf = new StringBuilder(512);
buf.append("
\n" +
"\n");
return buf.toString();
}
public String renderGeneralHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("
\n" +
"" +
"")
.append(_("Local Identity"))
.append(": | " +
"" +
"")
.append(_("show"))
.append(" |
\n" +
"")
.append(_("Version"))
.append(": | " +
"")
.append(_helper.getVersion())
.append(" |
\n" +
"" +
"")
.append(_("Uptime"))
.append(": | " +
"")
.append(_helper.getUptime())
.append(" |
\n");
return buf.toString();
}
public String renderNetworkReachabilityHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("\n");
return buf.toString();
}
public String renderUpdateStatusHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append(_helper.getUpdateStatus());
return buf.toString();
}
public String renderRestartStatusHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append(_helper.getRestartStatus());
return buf.toString();
}
public String renderPeersHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("
\n" +
"\n" +
"")
.append(_("Active"))
.append(": | ");
int active = _helper.getActivePeers();
buf.append(active)
.append(SummaryHelper.THINSP)
.append(Math.max(active, _helper.getActiveProfiles()))
.append(" |
\n" +
"")
.append(_("Fast"))
.append(": | ")
.append(_helper.getFastPeers())
.append(" |
\n" +
"")
.append(_("High capacity"))
.append(": | ")
.append(_helper.getHighCapacityPeers())
.append(" |
\n" +
"")
.append(_("Integrated"))
.append(": | ")
.append(_helper.getWellIntegratedPeers())
.append(" |
\n" +
"")
.append(_("Known"))
.append(": | ")
.append(_helper.getAllPeers())
.append(" |
\n" +
"
\n");
return buf.toString();
}
public String renderFirewallAndReseedStatusHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append(_helper.getFirewallAndReseedStatus());
return buf.toString();
}
public String renderBandwidthHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("
" +
"\n" +
"")
.append(DataHelper.formatDuration2(3 * 1000)) // lie and say 3 sec since 1 sec would appear as 1000 ms
.append(": | ")
.append(_helper.getSecondKBps())
.append("Bps |
\n");
if (_context.router().getUptime() > 6*60*1000) {
buf.append("")
.append(DataHelper.formatDuration2(5 * 60 * 1000)) // 5 min
.append(": | ")
.append(_helper.getFiveMinuteKBps())
.append("Bps |
\n");
}
if (_context.router().getUptime() > 2*60*1000) {
buf.append("")
.append(_("Total"))
.append(": | ")
.append(_helper.getLifetimeKBps())
.append("Bps |
\n");
}
buf.append("")
.append(_("Used"))
.append(": | ")
.append(_helper.getInboundTransferred())
.append(SummaryHelper.THINSP)
.append(_helper.getOutboundTransferred())
.append(" |
\n" +
"
\n");
return buf.toString();
}
public String renderTunnelsHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("
" +
"\n" +
"")
.append(_("Exploratory"))
.append(": | ")
.append(_helper.getInboundTunnels() + _helper.getOutboundTunnels())
.append(" |
\n" +
"")
.append(_("Client"))
.append(": | ")
.append(_helper.getInboundClientTunnels() + _helper.getOutboundClientTunnels())
.append(" |
\n" +
"")
.append(_("Participating"))
.append(": | ")
.append(_helper.getParticipatingTunnels())
.append(" |
\n" +
"")
.append(_("Share ratio"))
.append(": | ")
.append(_helper.getShareRatio())
.append(" |
\n" +
"
\n");
return buf.toString();
}
public String renderCongestionHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append("
" +
"\n" +
"")
.append(_("Job lag"))
.append(": | ")
.append(_helper.getJobLag())
.append(" |
\n" +
"")
.append(_("Message delay"))
.append(": | ")
.append(_helper.getMessageDelay())
.append(" |
\n");
if (!_context.getBooleanPropertyDefaultTrue("router.disableTunnelTesting")) {
buf.append("")
.append(_("Tunnel lag"))
.append(": | ")
.append(_helper.getTunnelLag())
.append(" |
\n");
}
buf.append("")
.append(_("Backlog"))
.append(": | ")
.append(_helper.getInboundBacklog())
.append(" |
\n" +
"
\n");
return buf.toString();
}
public String renderTunnelStatusHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(50);
buf.append("")
.append(_(_helper.getTunnelStatus()))
.append("
\n");
return buf.toString();
}
public String renderDestinationsHTML() {
if (_helper == null) return "";
StringBuilder buf = new StringBuilder(512);
buf.append(_helper.getDestinations());
return buf.toString();
}
/** @since 0.9.1 */
public String renderNewsHeadingsHTML() {
if (_newshelper == null) return "";
StringBuilder buf = new StringBuilder(512);
String consoleNonce = System.getProperty("router.consoleNonce");
if (consoleNonce != null) {
// Set up title and pre-headings stuff.
buf.append("
\n");
// Get news content.
String newsContent = _newshelper.getContent();
if (newsContent != "") {
buf.append("
\n");
// Parse news content for headings.
int start = newsContent.indexOf("");
while (start >= 0) {
// Add offset to start:
// 4 - gets rid of
// 16 - gets rid of the date as well (assuming form "yyyy-mm-dd: Foobarbaz...")
newsContent = newsContent.substring(start+16, newsContent.length());
int end = newsContent.indexOf("
");
if (end >= 0) {
String heading = newsContent.substring(0, end);
buf.append("
- ")
.append(heading)
.append("
\n");
}
start = newsContent.indexOf("");
}
buf.append("
\n");
// Set up string containing
to show news.
buf.append("")
.append(_("Show news"))
.append("\n");
} else {
buf.append("
")
.append(_("none"))
.append("");
}
// Add post-headings stuff.
buf.append("
\n");
}
return buf.toString();
}
/** translate a string */
private String _(String s) {
return Messages.getString(s, _context);
}
}