Package com.martiansoftware.nailgun
Class NGInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.martiansoftware.nailgun.NGInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
A FilterInputStream that is able to read the chunked stdin stream from a NailGun client.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final Set
<NGClientListener> private final DataInputStream
private boolean
private final Set
<NGHeartbeatListener> private static final Logger
private byte[]
private final ExecutorService
private final DataOutputStream
private final ExecutorService
private int
private boolean
private InputStream
private static final long
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsConstructorDescriptionNGInputStream
(DataInputStream in, DataOutputStream out, int heartbeatTimeoutMillis) Creates a new NGInputStream wrapping the specified InputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClientListener
(NGClientListener listener) Registers a new NGClientListener to be called on client disconnection or calls the listeners clientDisconnected method if the client has already disconnected to avoid races.void
addHeartbeatListener
(NGHeartbeatListener listener) int
void
close()
Cancel the thread reading from the NailGun client and close underlying input streamboolean
boolean
private void
Calls clientDisconnected method on all registered NGClientListeners.int
read()
int
read
(byte[] b) int
read
(byte[] b, int offset, int length) private void
Reads a NailGun chunk header from the underlying InputStream.private InputStream
readPayload
(InputStream in, int len) Reads a NailGun chunk payload fromFilterInputStream.in
and returns an InputStream that reads from that chunk.void
Do not notify anymore about client disconnectsvoid
removeClientListener
(NGClientListener listener) void
removeHeartbeatListener
(NGHeartbeatListener listener) private void
private void
Notify threads waiting in read() on either EOF chunk read or client disconnection.private void
setEof()
Notify threads waiting in read() on either EOF chunk read or client disconnection.private static void
terminateExecutor
(ExecutorService service, String which) Methods inherited from class java.io.FilterInputStream
mark, reset, skip
-
Field Details
-
LOG
-
orchestratorExecutor
-
readExecutor
-
din
-
stdin
-
eof
private boolean eof -
clientConnected
private boolean clientConnected -
remaining
private int remaining -
oneByteBuffer
private byte[] oneByteBuffer -
out
-
started
private boolean started -
clientListeners
-
heartbeatListeners
-
TERMINATION_TIMEOUT_MS
private static final long TERMINATION_TIMEOUT_MS- See Also:
-
-
Constructor Details
-
NGInputStream
Creates a new NGInputStream wrapping the specified InputStream. Also sets up a timer to periodically consume heartbeats sent from the client and call registered NGClientListeners if a client disconnection is detected.- Parameters:
in
- the InputStream to wrapout
- the OutputStream to which SENDINPUT chunks should be sentheartbeatTimeoutMillis
- the interval between heartbeats before considering the client disconnected
-
-
Method Details
-
notifyClientListeners
private void notifyClientListeners()Calls clientDisconnected method on all registered NGClientListeners. -
close
Cancel the thread reading from the NailGun client and close underlying input stream- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterInputStream
- Throws:
IOException
-
terminateExecutor
-
readPayload
Reads a NailGun chunk payload fromFilterInputStream.in
and returns an InputStream that reads from that chunk.- Parameters:
in
- the InputStream to read the chunk payload from.len
- the size of the payload chunk read from the chunkHeader.- Returns:
- an InputStream containing the read data.
- Throws:
IOException
- if thrown by the underlying InputStreamEOFException
- if EOF is reached by underlying stream before the payload has been read.
-
readChunk
Reads a NailGun chunk header from the underlying InputStream.- Throws:
EOFException
- if underlying stream / socket is closed which happens on client disconnectionIOException
- if thrown by the underlying InputStream, or if an unexpected NailGun chunk type is encountered.
-
setEof
private void setEof()Notify threads waiting in read() on either EOF chunk read or client disconnection. -
setClientDisconnected
private void setClientDisconnected()Notify threads waiting in read() on either EOF chunk read or client disconnection. -
available
- Overrides:
available
in classFilterInputStream
- Throws:
IOException
- See Also:
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classFilterInputStream
- See Also:
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
- See Also:
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
- See Also:
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
- See Also:
-
sendSendInput
- Throws:
IOException
-
isClientConnected
public boolean isClientConnected()- Returns:
- true if interval since last read is less than heartbeat timeout interval.
-
addClientListener
Registers a new NGClientListener to be called on client disconnection or calls the listeners clientDisconnected method if the client has already disconnected to avoid races.- Parameters:
listener
- theNGClientListener
to be notified of client events.
-
removeClientListener
- Parameters:
listener
- theNGClientListener
to no longer be notified of client events.
-
removeAllClientListeners
public void removeAllClientListeners()Do not notify anymore about client disconnects -
addHeartbeatListener
- Parameters:
listener
- theNGHeartbeatListener
to be notified of client events.
-
removeHeartbeatListener
- Parameters:
listener
- theNGClientListener
to no longer be notified of client events.
-