java.lang.Object
java.net.ServerSocket
net.i2p.util.InternalServerSocket
- All Implemented Interfaces:
Closeable, AutoCloseable
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final BlockingQueue<InternalSocket> Connections waiting to be accepted.private final StringName shown in the logs for this socket.private final IntegerThe port being listened on.private booleanFalse once the socket is closed.private static final ConcurrentHashMap<Integer, InternalServerSocket> The open sockets, by port.Constructor Summary
ConstructorsConstructorDescriptionInternalServerSocket(int port) Claim an internal port for this router.InternalServerSocket(int port, String label) Claim an internal port, naming the service that holds it so debug views can show what listens there instead of a bare port number.Method Summary
Modifier and TypeMethodDescriptionaccept()voidclose()Returns null as of 0.9.33, prior to that threw IllegalArgumentException.getLabel()The service name given at construction, if any.intAll currently claimed internal ports with their optional service labels.int(package private) static voidinternalConnect(int port, InternalSocket clientSock) This is how the client connects.booleanisBound()Returns true as of 0.9.33, prior to that threw IllegalArgumentException.booleanisClosed()Supported as of 0.9.33, prior to that threw IllegalArgumentException.private voidHands a new connection to the accept queue.static voidrenderStatusHTML(Writer out) Table of the ports currently claimed by internal server sockets.voidsetSoTimeout(int timeout) Unsupported, ignored.toString()Methods inherited from class ServerSocket
bind, bind, getInetAddress, getLocalSocketAddress, getOption, getReceiveBufferSize, getReuseAddress, implAccept, setOption, setReceiveBufferSize, setReuseAddress, supportedOptions
Field Details
_sockets
The open sockets, by port._acceptQueue
Connections waiting to be accepted._port
The port being listened on._label
Name shown in the logs for this socket._running
private volatile boolean _runningFalse once the socket is closed.
Constructor Details
InternalServerSocket
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
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- > 0label- 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classServerSocket
accept
- Overrides:
acceptin classServerSocket- Throws:
IOException
toString
- Overrides:
toStringin classServerSocket
internalConnect
This is how the client connects.- Parameters:
port- > 0clientSock- 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
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:
getLocalPortin classServerSocket
setSoTimeout
public void setSoTimeout(int timeout) Unsupported, ignored.- Overrides:
setSoTimeoutin classServerSocket
getSoTimeout
public int getSoTimeout()- Overrides:
getSoTimeoutin classServerSocket
getChannel
Returns null as of 0.9.33, prior to that threw IllegalArgumentException.- Overrides:
getChannelin classServerSocket- 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:
isBoundin classServerSocket- 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:
isClosedin classServerSocket- Returns:
- true once
close()has run, false while still listening
getLabel
The service name given at construction, if any.- Returns:
- the label, or null for an unlabelled socket
getPortLabels
renderStatusHTML
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