java.lang.Object
java.net.ServerSocket
net.i2p.util.InternalServerSocket
All Implemented Interfaces:
Closeable, AutoCloseable

public class InternalServerSocketextends ServerSocket
A simple in-JVM ServerSocket using Piped Streams. We use port numbers just like regular sockets. Can only be connected by InternalSocket.

Warning - this uses Piped Streams, which don't like multiple writers from threads that may vanish. If you do use multiple writers, you may get intermittent 'write end dead' or 'pipe broken' IOExceptions on the reader side. See http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

  • Field Details

    • _sockets

      private static final ConcurrentHashMap<Integer, InternalServerSocket> _sockets
      The open sockets, by port.
    • _acceptQueue

      private final BlockingQueue<InternalSocket> _acceptQueue
      Connections waiting to be accepted.
    • _port

      private final Integer _port
      The port being listened on.
    • _label

      private final String _label
      Name shown in the logs for this socket.
    • _running

      private volatile boolean _running
      False once the socket is closed.
  • Constructor Details

    • InternalServerSocket

      public InternalServerSocket(int port) throws IOException
      Claim an internal port for this router. The claim is tracked in a static map, so the same port cannot be taken twice even across routers in one JVM.
      Parameters:
      port - > 0
      Throws:
      IOException - if the port is not positive, or another server socket already holds it
    • InternalServerSocket

      public InternalServerSocket(int port, String label) throws IOException
      Claim an internal port, naming the service that holds it so debug views can show what listens there instead of a bare port number.
      Parameters:
      port - > 0
      label - human-readable service name, or null for none
      Throws:
      IOException - if the port is not positive, or another server socket already holds it
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ServerSocket
    • accept

      public Socket accept() throws IOException
      Overrides:
      accept in class ServerSocket
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class ServerSocket
    • internalConnect

      static void internalConnect(int port, InternalSocket clientSock) throws IOException
      This is how the client connects.
      Parameters:
      port - > 0
      clientSock - the client socket to wire the paired piped streams onto
      Throws:
      IOException - if no server socket is listening on that port, or it has been closed
    • queueConnection

      private void queueConnection(InternalSocket sock) throws IOException
      Hands a new connection to the accept queue.
      Parameters:
      sock - the connection to queue
      Throws:
      IOException - if the socket is closed
    • getLocalPort

      public int getLocalPort()
      Overrides:
      getLocalPort in class ServerSocket
    • setSoTimeout

      public void setSoTimeout(int timeout)
      Unsupported, ignored.
      Overrides:
      setSoTimeout in class ServerSocket
    • getSoTimeout

      public int getSoTimeout()
      Overrides:
      getSoTimeout in class ServerSocket
    • getChannel

      public ServerSocketChannel getChannel()
      Returns null as of 0.9.33, prior to that threw IllegalArgumentException.
      Overrides:
      getChannel in class ServerSocket
      Returns:
      null, as this socket does not wrap a channel
    • isBound

      public boolean isBound()
      Returns true as of 0.9.33, prior to that threw IllegalArgumentException.
      Overrides:
      isBound in class ServerSocket
      Returns:
      always true, as the socket is listening from construction
    • isClosed

      public boolean isClosed()
      Supported as of 0.9.33, prior to that threw IllegalArgumentException.
      Overrides:
      isClosed in class ServerSocket
      Returns:
      true once close() has run, false while still listening
    • getLabel

      public String getLabel()
      The service name given at construction, if any.
      Returns:
      the label, or null for an unlabelled socket
    • getPortLabels

      public static Map<Integer,String> getPortLabels()
      All currently claimed internal ports with their optional service labels.
      Returns:
      a copy of the map of port to label (null value for unlabelled sockets), never null
    • renderStatusHTML

      public static void renderStatusHTML(Writer out) throws IOException
      Table of the ports currently claimed by internal server sockets.
      Parameters:
      out - writer to write HTML output to
      Throws:
      IOException - if writing to output fails