Class AprSocketConnector

    • Field Detail

      • APR_TIMEUP_ERROR

        private static final int APR_TIMEUP_ERROR
        This constant is deduced from the APR code. It is used when the timeout has expired while doing a poll() operation.
        See Also:
        Constant Field Values
      • wakeupLock

        private final java.lang.Object wakeupLock
      • wakeupSocket

        private volatile long wakeupSocket
      • toBeWakenUp

        private volatile boolean toBeWakenUp
      • pool

        private volatile long pool
      • pollset

        private volatile long pollset
      • polledSockets

        private final long[] polledSockets
      • polledHandles

        private final java.util.Queue<java.lang.Long> polledHandles
      • failedHandles

        private final java.util.Set<java.lang.Long> failedHandles
      • dummyBuffer

        private volatile java.nio.ByteBuffer dummyBuffer
    • Constructor Detail

      • AprSocketConnector

        public AprSocketConnector()
        Create an AprSocketConnector with default configuration (multiple thread model).
      • AprSocketConnector

        public AprSocketConnector​(int processorCount)
        Constructor for AprSocketConnector with default configuration, and given number of AprIoProcessor for multithreading I/O operations
        Parameters:
        processorCount - the number of processor to create and place in a SimpleIoProcessorPool
      • AprSocketConnector

        public AprSocketConnector​(IoProcessor<AprSession> processor)
        Constructor for AprSocketConnector with default configuration but a specific IoProcessor, useful for sharing the same processor over multiple IoService of the same type.
        Parameters:
        processor - the processor to use for managing I/O events
      • AprSocketConnector

        public AprSocketConnector​(java.util.concurrent.Executor executor,
                                  IoProcessor<AprSession> processor)
        Constructor for AprSocketConnector with a given Executor for handling connection events and a given IoProcessor for handling I/O events, useful for sharing the same processor and executor over multiple IoService of the same type.
        Parameters:
        executor - the executor for connection
        processor - the processor for I/O operations
    • Method Detail

      • init

        protected void init()
                     throws java.lang.Exception
        Initialize the polling system, will be called at construction time.
        Specified by:
        init in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Throws:
        java.lang.Exception - any exception thrown by the underlying system calls
      • destroy

        protected void destroy()
                        throws java.lang.Exception
        Destroy the polling system, will be called when this IoConnector implementation will be disposed.
        Specified by:
        destroy in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • allHandles

        protected java.util.Iterator<java.lang.Long> allHandles()
        Iterator for all the client sockets polled for connection.
        Specified by:
        allHandles in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Returns:
        the list of client sockets currently polled for connection
      • connect

        protected boolean connect​(java.lang.Long handle,
                                  java.net.SocketAddress remoteAddress)
                           throws java.lang.Exception
        Connect a newly created client socket handle to a remote SocketAddress. This operation is non-blocking, so at end of the call the socket can be still in connection process.
        Specified by:
        connect in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        handle - the client socket handle
        remoteAddress - the remote address where to connect
        Returns:
        true if a connection was established, false if this client socket is in non-blocking mode and the connection operation is in progress
        Throws:
        java.lang.Exception - If the connect failed
      • close

        protected void close​(java.lang.Long handle)
                      throws java.lang.Exception
        Close a client socket.
        Specified by:
        close in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        handle - the client socket
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • finishConnect

        protected boolean finishConnect​(java.lang.Long handle)
                                 throws java.lang.Exception
        Finish the connection process of a client socket after it was marked as ready to process by the AbstractPollingIoConnector.select(int) call. The socket will be connected or reported as connection failed.
        Specified by:
        finishConnect in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        handle - the client socket handle to finish to connect
        Returns:
        true if the socket is connected
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • newHandle

        protected java.lang.Long newHandle​(java.net.SocketAddress localAddress)
                                    throws java.lang.Exception
        Create a new client socket handle from a local SocketAddress
        Specified by:
        newHandle in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        localAddress - the socket address for binding the new client socket
        Returns:
        a new client socket handle
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • newSession

        protected AprSession newSession​(IoProcessor<AprSession> processor,
                                        java.lang.Long handle)
                                 throws java.lang.Exception
        Create a new IoSession from a connected socket client handle. Will assign the created IoSession to the given IoProcessor for managing future I/O events.
        Specified by:
        newSession in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        processor - the processor in charge of this session
        handle - the newly connected client socket handle
        Returns:
        a new IoSession
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • select

        protected int select​(int timeout)
                      throws java.lang.Exception
        Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect). All the client socket descriptors processed need to be returned by AbstractPollingIoConnector.selectedHandles()
        Specified by:
        select in class AbstractPollingIoConnector<AprSession,​java.lang.Long>
        Parameters:
        timeout - The timeout for the select() method
        Returns:
        The number of socket having received some data
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • throwException

        private void throwException​(int code)
                             throws java.io.IOException
        transform an APR error number in a more fancy exception
        Parameters:
        code - APR error code
        Throws:
        java.io.IOException - the produced exception for the given APR error number