Class NGInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.martiansoftware.nailgun.NGInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class NGInputStream extends FilterInputStream implements Closeable
A FilterInputStream that is able to read the chunked stdin stream from a NailGun client.
  • Field Details

    • LOG

      private static final Logger LOG
    • orchestratorExecutor

      private final ExecutorService orchestratorExecutor
    • readExecutor

      private final ExecutorService readExecutor
    • din

      private final DataInputStream din
    • stdin

      private InputStream stdin
    • eof

      private boolean eof
    • clientConnected

      private boolean clientConnected
    • remaining

      private int remaining
    • oneByteBuffer

      private byte[] oneByteBuffer
    • out

      private final DataOutputStream out
    • started

      private boolean started
    • clientListeners

      private final Set<NGClientListener> clientListeners
    • heartbeatListeners

      private final Set<NGHeartbeatListener> heartbeatListeners
    • TERMINATION_TIMEOUT_MS

      private static final long TERMINATION_TIMEOUT_MS
      See Also:
  • Constructor Details

    • NGInputStream

      public NGInputStream(DataInputStream in, DataOutputStream out, int heartbeatTimeoutMillis)
      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 wrap
      out - the OutputStream to which SENDINPUT chunks should be sent
      heartbeatTimeoutMillis - the interval between heartbeats before considering the client disconnected
  • Method Details

    • notifyClientListeners

      private void notifyClientListeners()
      Calls clientDisconnected method on all registered NGClientListeners.
    • close

      public void close() throws IOException
      Cancel the thread reading from the NailGun client and close underlying input stream
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException
    • terminateExecutor

      private static void terminateExecutor(ExecutorService service, String which)
    • readPayload

      private InputStream readPayload(InputStream in, int len) throws IOException
      Reads a NailGun chunk payload from FilterInputStream.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 InputStream
      EOFException - if EOF is reached by underlying stream before the payload has been read.
    • readChunk

      private void readChunk() throws IOException
      Reads a NailGun chunk header from the underlying InputStream.
      Throws:
      EOFException - if underlying stream / socket is closed which happens on client disconnection
      IOException - 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

      public int available() throws IOException
      Overrides:
      available in class FilterInputStream
      Throws:
      IOException
      See Also:
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class FilterInputStream
      See Also:
    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
      See Also:
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
      See Also:
    • read

      public int read(byte[] b, int offset, int length) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
      See Also:
    • sendSendInput

      private void sendSendInput() throws IOException
      Throws:
      IOException
    • isClientConnected

      public boolean isClientConnected()
      Returns:
      true if interval since last read is less than heartbeat timeout interval.
    • addClientListener

      public void 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.
      Parameters:
      listener - the NGClientListener to be notified of client events.
    • removeClientListener

      public void removeClientListener(NGClientListener listener)
      Parameters:
      listener - the NGClientListener to no longer be notified of client events.
    • removeAllClientListeners

      public void removeAllClientListeners()
      Do not notify anymore about client disconnects
    • addHeartbeatListener

      public void addHeartbeatListener(NGHeartbeatListener listener)
      Parameters:
      listener - the NGHeartbeatListener to be notified of client events.
    • removeHeartbeatListener

      public void removeHeartbeatListener(NGHeartbeatListener listener)
      Parameters:
      listener - the NGClientListener to no longer be notified of client events.