java.lang.Object
net.i2p.router.web.HelperBase
net.i2p.router.web.helpers.PeerHelper

public class PeerHelperextends HelperBase
Helper for peer connections page rendering and form processing.
  • Field Details

    • _sortFlags

      private int _sortFlags
      Flags selecting the peer list sort order.
    • _urlBase

      private String _urlBase
      Base URL for the peer list page.
    • _transport

      private String _transport
      Transport name being displayed.
    • _fragmentKeys

      private boolean _fragmentKeys
      Emit a data-key (peer hash) per row for worker-side row diffing; fragment renders only, so full pages stay byte-for-byte identical.
    • KEY_LEN

      private static final int KEY_LEN
      Length of the truncated base64 hash used as a per-row data-key in fragment mode.
      See Also:
    • titles

      private static final String[] titles
      Column titles of the peer list.
    • THINSP

      private static final String THINSP
      Separator used in the peer list.
      See Also:
    • _rateFmt

      private static final NumberFormat _rateFmt
      Formatter for the rate display.
    • _fmt

      private static final DecimalFormat _fmt
      Formatter for the SSU stats display.
  • Constructor Details

    • PeerHelper

      public PeerHelper()
      Default constructor for the console peer summary helper.
  • Method Details

    • setSort

      public void setSort(String flags)
      Parses the decimal sort order, falling back to 0 when absent or invalid.
      Parameters:
      flags - the decimal sort order requested by the console page; null or unparseable text falls back to 0, the default ordering
    • setUrlBase

      public void setUrlBase(String base)
      Stores the console URL prefix that rendered peer links are built from.
      Parameters:
      base - the console URL prefix that the rendered links are built from, ending in a path separator
    • setTransport

      public void setTransport(String t)
      Restrict the peer listing to one transport, as chosen by the console's transport selector.
      Parameters:
      t - the transport name to list peers for, as passed in the query string, or null for all transports
    • getPeerSummary

      public String getPeerSummary()
      Render the peer summary table into the page being built. The peer page is written straight to the response, so there is no value to hand back.
      Returns:
      the empty string, as the page is written straight to the response
    • renderStatusHTML

      private void renderStatusHTML(Writer out, String urlBase, int sortFlags) throws IOException
      Render the whole peer page: nav bar, rings and transport summary, or the single transport table when a transport is selected. Blocking, and slow with many peers.
      Parameters:
      out - where the peer page markup is streamed to
      urlBase - console URL prefix the transport tab and sort links are built from
      sortFlags - sort column, one of the FLAG_ constants in UDPSorters; negative reverses the order
      Throws:
      IOException - if the response writer fails while the page is written
    • getTab

      private int getTab()
      Return the currently selected tab index.
      Returns:
      the tab index matching the current transport
    • getStatAvg

      private double getStatAvg(String name)
      Get the 1-minute average of a RateStat, or 0 if unavailable.
      Parameters:
      name - the rate stat's full dotted name, e.g. router.activePeers
      Returns:
      the average value recorded in the last minute, 0 if the stat or its 1-minute rate is not registered
    • getStatCount1m

      private double getStatCount1m(String name)
      Get the 1-minute event count of a RateStat, preferring the current partial window. For count-type stats whose event value carries no information (e.g. peer.testTimeout always records 1), this is the meaningful signal - the average would read a constant 1.0.
      Parameters:
      name - dotted rate stat name whose 1-minute window is read
      Returns:
      events in the last minute, 0 if the stat or its 1-minute rate is not registered
    • renderPeerRings

      private void renderPeerRings(Writer out) throws IOException
      Render peer-landscape ring dashboard above the transport summary table. Ten rings: Active, Fast, HighCap, Reliable, Floodfill, Known, Profiles, Unreachable, Banned, Test Fail.
      Parameters:
      out - where the ring dashboard markup is written
      Throws:
      IOException - if the rendered dashboard cannot be written to the page
    • renderSummary

      private void renderSummary(Writer out) throws IOException
      Render the peer connectivity summary section.
      Parameters:
      out - where the transport summary table is written
      Throws:
      IOException - if the summary table cannot be written to the page
    • getTransportBandwidth

      private void getTransportBandwidth(Transport transport, int[] peerCounts, int[] bandwidths)
      Sums peer counts and bandwidth for one transport.
      Parameters:
      transport - the transport to measure
      peerCounts - out: peers per bandwidth tier
      bandwidths - out: bandwidth per tier
    • renderFragment

      public void renderFragment(String id) throws IOException
      Render a single named element for the contentonly fragment mode. Renders nothing for ids the current transport view does not own.
      Parameters:
      id - the element id
      Throws:
      IOException - if an I/O error occurs
    • renderNTCPFragment

      private void renderNTCPFragment(NTCPTransport nt, Writer out, String id, int sortFlags) throws IOException
      Render a named NTCP element in fragment mode: the count heading, the tbody with data-key rows, or the totals tfoot.
      Parameters:
      nt - the NTCP transport whose peers are re-rendered
      out - where the fragment markup is written
      id - DOM id of the element to re-render: ntcpcon, peersNTCP or ntcpfoot
      sortFlags - sort column, one of the FLAG_ constants in UDPSorters; negative reverses the order
      Throws:
      IOException - if the fragment cannot be written to the page
    • renderSSUFragment

      private void renderSSUFragment(UDPTransport ut, Writer out, String id, int sortFlags, boolean debugmode) throws IOException
      Render a named SSU element in fragment mode: the count heading, the tbody with data-key rows, or the totals tfoot.
      Parameters:
      ut - the SSU transport whose peers are re-rendered
      out - where the fragment markup is written
      id - DOM id of the element to re-render: ssucon, peersSSU or ssufoot
      sortFlags - sort column, one of the FLAG_ constants in UDPSorters; negative reverses the order
      debugmode - true renders the advanced view, with the extra columns
      Throws:
      IOException - if the fragment cannot be written to the page
    • includeNTCP

      private static boolean includeNTCP(NTCPConnection con, int peerCount, long now)
      Whether an NTCP connection is recent enough to render.
      Parameters:
      con - the NTCP connection being considered for display
      peerCount - the number of established connections, which selects the idle cutoff
      now - the current time
      Returns:
      true when the connection is rendered
    • includeSSU

      private static boolean includeSSU(PeerState peer, int peerCount, long now)
      Whether an SSU peer is recent enough to render.
      Parameters:
      peer - the SSU peer state whose last receive time is tested
      peerCount - the total peer count, which selects the idle cutoff
      now - the current time
      Returns:
      true when the peer is rendered
    • renderNavBar

      private void renderNavBar(Writer out) throws IOException
      Render the peer page navigation bar.
      Parameters:
      out - where the tab bar markup is written
      Throws:
      IOException - if the tab bar cannot be written to the page
    • render

      private void render(NTCPTransport nt, Writer out, int sortFlags) throws IOException
      Render the NTCP transport peer listing.
      Parameters:
      nt - the NTCP transport whose established connections are listed
      out - where the peer table markup is written
      sortFlags - sort column, one of the FLAG_ constants in UDPSorters; negative reverses the order
      Throws:
      IOException - if the peer table cannot be written to the page
    • renderNTCPHeader

      private void renderNTCPHeader(StringBuilder buf, boolean ipv6Enabled, int activePeers, int maxConnections, boolean hasPeers)
      Append the NTCP peer table header, including the IPv6 column when enabled.
      Parameters:
      buf - the buffer the header markup is appended to; the caller writes it out and resets it
      ipv6Enabled - true to add the IPv6 column, set from the NTCP or UDP ipv6 property
      activePeers - established connection count shown in the heading
      maxConnections - configured NTCP connection limit shown beside it
      hasPeers - false writes only the heading and the table opener, and skips the column headers
    • renderNTCPCount

      private void renderNTCPCount(StringBuilder buf, int activePeers, int maxConnections)
      Append the NTCP connections heading fragment containing the count.
      Parameters:
      buf - the buffer to append to
      activePeers - the established connection count
      maxConnections - the configured connection limit
    • renderNTCPRow

      private void renderNTCPRow(StringBuilder buf, NTCPConnection con, long now, boolean ipv6Enabled, int peerCount, PeerHelper.NTCPTotals totals)
      Append one NTCP peer row and accumulate its stats into totals.
      Parameters:
      buf - the buffer the row markup is appended to
      con - an established NTCP connection to render as one row
      now - current time in ms since the epoch, the base for the idle times
      ipv6Enabled - true to emit the IPv6 cell for this peer
      peerCount - peers listed, which sets the idle window a rate must fall inside to be counted: 60s above 300 peers, 3min above 100
      totals - accumulators this row's rates, uptime and clock skew are added to
    • renderNTCPFooter

      private void renderNTCPFooter(StringBuilder buf, boolean ipv6Enabled, int activePeers, int peerCount, PeerHelper.NTCPTotals totals)
      Append the NTCP peer table footer with the accumulated totals.
      Parameters:
      buf - the buffer the footer markup is appended to
      ipv6Enabled - true widens the label cell to colspan 5 to cover the IPv6 column
      activePeers - established connection count shown in the "n peers" label
      peerCount - rows rendered, the divisor of the average uptime and clock skew, so it must be greater than 0
      totals - per-column sums accumulated from the rendered rows
    • formatRate

      private static String formatRate(float rate)
      Formats a rate for the peer list.
      Parameters:
      rate - the rate, in bytes per second
      Returns:
      the formatted text
    • getNTCPComparator

      private Comparator<NTCPConnection> getNTCPComparator(int sortFlags)
      Returns the comparator for the chosen sort.
      Parameters:
      sortFlags - the sort flags
      Returns:
      the comparator to use
    • render

      private void render(UDPTransport ut, Writer out, String urlBase, int sortFlags, boolean debugmode) throws IOException
      Render the SSU/UDP transport peer listing.
      Parameters:
      ut - the SSU transport whose peer states are listed
      out - where the peer table markup is written
      urlBase - console URL prefix the sort links are built from
      sortFlags - sort column, one of the FLAG_ constants in UDPSorters; negative reverses the order
      debugmode - true for the advanced view: the extra columns, the per-column sort links and the wider footer
      Throws:
      IOException - if the peer table cannot be written to the page
    • renderSSUHeader

      private void renderSSUHeader(StringBuilder buf, String urlBase, int sortFlags, boolean debugmode, int activePeers, int maxConnections, boolean hasPeers)
      Append the SSU peer table header, including the sort links in debug mode.
      Parameters:
      buf - the buffer the header markup is appended to
      urlBase - page path the sort links point at, without a query string
      sortFlags - column and direction currently in effect, as passed to UDPSorters.getComparator(int); negative means descending
      debugmode - true adds the sort links, the IPv6 column and the advanced-view table class; false adds the link to it
      activePeers - active peer count shown in the heading
      maxConnections - configured UDP connection limit shown beside it
      hasPeers - false writes only the heading and the table opener, and skips the column headers
    • renderSSUCount

      private void renderSSUCount(StringBuilder buf, int activePeers, int maxConnections, boolean debugmode)
      Append the UDP connections heading fragment containing the count.
      Parameters:
      buf - the buffer to append to
      activePeers - the established connection count
      maxConnections - the configured connection limit
      debugmode - render the advanced view link
    • renderSSURow

      private void renderSSURow(StringBuilder buf, PeerState peer, long now, boolean debugmode, PeerHelper.SSUTotals totals)
      Append one SSU peer row and accumulate its stats into totals.
      Parameters:
      buf - the buffer the row markup is appended to
      peer - the peer state to render as one row
      now - current time in ms since the epoch, the base for the idle times and the uptime
      debugmode - true adds the IPv6, ban and failed-send notes and the congestion-window, RTT, RTO and MTU cells
      totals - accumulators this row's rates, timings and packet counts are added to
    • renderSSUFooter

      private void renderSSUFooter(StringBuilder buf, boolean debugmode, UDPTransport ut, PeerHelper.SSUTotals totals)
      Append the SSU peer table footer with the accumulated totals.
      Parameters:
      buf - the buffer the footer markup is appended to
      debugmode - true widens the label cell to colspan 5 and adds the congestion-window, RTT, RTO and MTU cells
      ut - transport supplying the active peer count for the label and our own MTU for the footer
      totals - sums accumulated from the rendered rows; numPeers is the divisor of the averages, so it must be greater than 0
    • formatKBps

      private static final String formatKBps(int bps)
      Formats a byte rate for display.
      Parameters:
      bps - the rate, in bytes per second
      Returns:
      the formatted text