Class Transport

  • All Implemented Interfaces:
    AutoCloseable, Runnable

    public abstract class Transport
    extends Object
    implements Runnable, AutoCloseable
    This class simplifies communication for protocols that support multiplexing requests. It encapsulates a stream and some protocol knowledge (provided by a concrete subclass) so that connecting, disconnecting, sending, and receiving can be syncronized properly. Apparatus is provided to send and receive requests concurrently.
    • Field Detail

      • state

        protected volatile int state
      • inLock

        protected final Object inLock
      • outLock

        protected final Object outLock
    • Constructor Detail

      • Transport

        public Transport()
    • Method Detail

      • readn

        public static int readn​(InputStream in,
                                byte[] b,
                                int off,
                                int len)
                         throws IOException
        Read bytes from the input stream into a buffer
        Parameters:
        in -
        b -
        off -
        len -
        Returns:
        number of bytes read
        Throws:
        IOException
      • acquire

        public Transport acquire()
        Returns:
        session increased usage count
      • release

        public void release()
      • getUsageCount

        protected long getUsageCount()
        Returns:
        the number of known usages
      • isDisconnected

        public boolean isDisconnected()
        Returns:
        whether the transport is disconnected
      • isFailed

        public boolean isFailed()
        Returns:
        whether the transport is marked failed
      • sendrecv

        public <T extends Response> T sendrecv​(Request request,
                                               T response,
                                               Set<RequestParam> params)
                                        throws IOException
        Send a request message and recieve response
        Parameters:
        request -
        response -
        params -
        Returns:
        the response
        Throws:
        IOException
      • handleIntermediate

        protected <T extends Response> boolean handleIntermediate​(Request request,
                                                                  T response)
        Parameters:
        request -
        response -
        Returns:
      • getResponseTimeout

        protected abstract int getResponseTimeout​(Request request)
        Parameters:
        request -
        Returns:
      • doDisconnect

        protected abstract boolean doDisconnect​(boolean hard,
                                                boolean inUse)
                                         throws IOException
        Throws:
        IOException
      • connect

        public boolean connect​(long timeout)
                        throws TransportException
        Connect the transport
        Parameters:
        timeout -
        Returns:
        whether the transport was connected
        Throws:
        TransportException
      • disconnect

        public boolean disconnect​(boolean hard)
                           throws IOException
        Disconnect the transport
        Parameters:
        hard -
        Returns:
        whether conenction was in use
        Throws:
        IOException
      • disconnect

        public boolean disconnect​(boolean hard,
                                  boolean inUse)
                           throws IOException
        Disconnect the transport
        Parameters:
        hard -
        inUse - whether the caller is holding a usage reference on the transport
        Returns:
        whether conenction was in use
        Throws:
        IOException
      • run

        public void run()
        Specified by:
        run in interface Runnable