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 booleanprivate final Set<NGClientListener> private final DataInputStreamprivate booleanprivate final Set<NGHeartbeatListener> private static final Loggerprivate byte[]private final ExecutorServiceprivate final DataOutputStreamprivate final ExecutorServiceprivate intprivate booleanprivate InputStreamprivate static final longFields inherited from class FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionNGInputStream(DataInputStream in, DataOutputStream out, int heartbeatTimeoutMillis) Creates a new NGInputStream wrapping the specified InputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClientListener(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.voidaddHeartbeatListener(NGHeartbeatListener listener) intvoidclose()Cancel the thread reading from the NailGun client and close underlying input streambooleanbooleanprivate voidCalls clientDisconnected method on all registered NGClientListeners.intread()intread(byte[] b) intread(byte[] b, int offset, int length) private voidReads a NailGun chunk header from the underlying InputStream.private InputStreamreadPayload(InputStream in, int len) Reads a NailGun chunk payload fromFilterInputStream.inand returns an InputStream that reads from that chunk.voidDo not notify anymore about client disconnectsvoidremoveClientListener(NGClientListener listener) voidremoveHeartbeatListener(NGHeartbeatListener listener) private voidprivate voidNotify threads waiting in read() on either EOF chunk read or client disconnection.private voidsetEof()Notify threads waiting in read() on either EOF chunk read or client disconnection.private static voidterminateExecutor(ExecutorService service, String which) Methods inherited from class 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException
-
terminateExecutor
-
readPayload
Reads a NailGun chunk payload fromFilterInputStream.inand 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:
availablein classFilterInputStream- Throws:
IOException- See Also:
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classFilterInputStream- See Also:
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException- See Also:
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException- See Also:
-
read
- Overrides:
readin 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- theNGClientListenerto be notified of client events.
-
removeClientListener
- Parameters:
listener- theNGClientListenerto no longer be notified of client events.
-
removeAllClientListeners
public void removeAllClientListeners()Do not notify anymore about client disconnects -
addHeartbeatListener
- Parameters:
listener- theNGHeartbeatListenerto be notified of client events.
-
removeHeartbeatListener
- Parameters:
listener- theNGClientListenerto no longer be notified of client events.
-