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

public class StreamHelperextends HelperBase
Helper for the /streams page - shows active I2P streaming connections.
  • Field Details

  • Constructor Details

    • StreamHelper

      public StreamHelper()
      Creates a helper that renders active streaming connections to the /streams page.
  • Method Details

    • getDirection

      public String getDirection()
      Gets the direction filter applied to the streams table.
      Returns:
      the configured direction, either "inbound" or "outbound", or empty when no filter is set and the combined table is rendered
    • setDirection

      public void setDirection(String d)
      Sets the direction filter applied to the streams table.
      Parameters:
      d - the direction filter to apply: "inbound" or "outbound" restricts the table to one side, and null or empty renders the combined table
    • getStreamSummary

      public String getStreamSummary()
      Render the streaming connections table(s).
      Returns:
      the stream summary
    • getStatAvg

      private double getStatAvg(String name, long period)
      Read a rate stat value from the stat manager, returning 0 if unavailable.
      Parameters:
      name - the rate stat name, e.g. "stream.rtt"
      period - the rate period in ms, one of the RateConstants values
      Returns:
      the rate's average value, or 0 when the stat or period has no data
    • getStatCount

      private long getStatCount(String name, long period)
      Read the last-event-count from a rate stat, returning 0 if unavailable.
      Parameters:
      name - the rate stat name, e.g. "stream.connectionCreated"
      period - the rate period in ms, one of the RateConstants values
      Returns:
      the event count of the most recent period, or 0 when the stat has no data
    • countStreams

      static int[] countStreams(TunnelControllerGroup tcg)
      Count open streaming sockets by direction, walking the tunnel controllers the same way the stream tables do. Used by the debug overview tile.
      Parameters:
      tcg - the controller group, or null before router init
      Returns:
      {inbound, outbound} socket counts, or null if tcg is null
    • renderStreamsHTML

      private void renderStreamsHTML(Writer out) throws IOException
      Writes the table of open streams.
      Parameters:
      out - where the table is written
      Throws:
      IOException - if the page cannot be written
    • getStatAvgDuration

      private double getStatAvgDuration(String name, long period)
      Read the average event weight (duration in ms) from a rate stat, returning 0 if unavailable.
      Parameters:
      name - the rate stat name, e.g. "stream.con.lifetimeBytesSent"
      period - the rate period in ms, one of the RateConstants values
      Returns:
      the average event duration in ms, or 0 when the stat has no data
    • closedStreamRateBps

      static double closedStreamRateBps(double avgBytesPerConn, double avgLifetimeMs)
      Convert a byte-count stat's average value and average duration into a bytes/sec rate. The stream lifetime byte stats record each closed connection's total bytes weighted by its lifetime, so avgValue/avgDuration is the mean throughput of the streams that closed in the window. Streams that never close report nothing.
      Parameters:
      avgBytesPerConn - average lifetime bytes per closed connection
      avgLifetimeMs - average closed-connection lifetime in ms
      Returns:
      the average rate in bytes/s, 0 when either input is missing
    • renderStreamStats

      private void renderStreamStats(Writer out, int totalSockets) throws IOException
      Render the streaming health ring dashboard.
      Parameters:
      out - the writer the dashboard HTML is written to
      totalSockets - the number of open streaming sockets, shown in the Active ring; 0 renders that ring with no data
      Throws:
      IOException - if writing to the writer fails
    • renderCombinedTable

      private void renderCombinedTable(Writer out, List<TunnelController> controllers, int inCount, long inSent, long inRecv, int outCount, long outSent, long outRecv) throws IOException
      Render the combined table for the main page with inbound and outbound sections.
      Parameters:
      out - the writer the HTML is written to
      controllers - the tunnel controllers scanned for open sockets
      inCount - the number of inbound sockets, shown in the section header
      inSent - total bytes sent on inbound sockets, shown in the header
      inRecv - total bytes received on inbound sockets, shown in the header
      outCount - the number of outbound sockets, shown in the section header
      outSent - total bytes sent on outbound sockets, shown in the header
      outRecv - total bytes received on outbound sockets, shown in the header
      Throws:
      IOException - if writing to the writer fails
    • renderTableSection

      private void renderTableSection(Writer out, List<TunnelController> controllers, boolean inbound, String peerHeader, int count, long totalSent, long totalReceived) throws IOException
      Render one thead (section header + column headers) + tbody for a direction.
      Parameters:
      out - the writer the HTML is written to
      controllers - the tunnel controllers scanned for open sockets
      inbound - true renders the inbound (server) section, false the outbound one
      peerHeader - the header text for the peer column, "Peer" or "Target"
      count - the number of sockets in this direction, shown in the header
      totalSent - total bytes sent in this direction, shown in the header
      totalReceived - total bytes received in this direction, shown in the header
      Throws:
      IOException - if writing to the writer fails
    • renderTable

      private void renderTable(Writer out, List<TunnelController> controllers, boolean inbound, String peerHeader, int count, long totalSent, long totalReceived) throws IOException
      Render a simple filtered table (single section, no section header).
      Parameters:
      out - the writer the HTML is written to
      controllers - the tunnel controllers scanned for open sockets
      inbound - true renders the inbound (server) rows, false the outbound ones
      peerHeader - the header text for the peer column, "Peer" or "Target"
      count - the number of sockets in this direction, shown in the header
      totalSent - total bytes sent in this direction, shown in the header
      totalReceived - total bytes received in this direction, shown in the header
      Throws:
      IOException - if writing to the writer fails
    • formatKBps

      private static String formatKBps(double bytesPerSec)
      Format bytes/sec as KB/s or MB/s (decimal units, no HTML entities).
      Parameters:
      bytesPerSec - the rate to format, in bytes per second
      Returns:
      the formatted rate with one decimal for KB/s and MB/s, whole B/s below 1000
    • formatSize

      private static String formatSize(long bytes)
      Format bytes without space before unit: "0B", "1.23KiB".
      Parameters:
      bytes - the byte count to format
      Returns:
      the size with a binary unit suffix and no space, e.g. "1.23KiB"
    • esc

      private static String esc(String s)
      HTML-escape a string.
      Parameters:
      s - the text to escape; null is rendered as an empty string
      Returns:
      the text with ampersands, angle brackets and quotes replaced by HTML entities