Class ConnectionBuilder<T extends Client<Connection<T>>>


  • public class ConnectionBuilder<T extends Client<Connection<T>>>
    extends java.lang.Object
    Builds the ConnectionHandler responsible to manage all incoming connections.
    • Constructor Detail

      • ConnectionBuilder

        public ConnectionBuilder()
    • Method Detail

      • create

        public static <T extends Client<Connection<T>>> ConnectionBuilder<T> create​(java.net.InetSocketAddress address,
                                                                                    ClientFactory<T> clientFactory,
                                                                                    PacketHandler<T> packetHandler,
                                                                                    PacketExecutor<T> executor)
        Creates a ConnectionBuilder holding the minimum requirements to create a ConnectionHandler.
        Type Parameters:
        T - - The Type of client that ConnectionBuilder will handle.
        Parameters:
        address - - The socket address to listen the incoming connections.
        clientFactory - - The factory responsible to create a new Client when a new connection is accepted.
        packetHandler - - The handle responsible to convert the data received into a ReadablePacket
        executor - - The responsible to execute the incoming packets.
        Returns:
        A ConnectionBuilder with default configuration.
      • filter

        public ConnectionBuilder<T> filter​(ConnectionFilter filter)
        Sets a filter to be used on incoming connections. The filter must decide if a connection is acceptable or not.
        Parameters:
        filter - - the ConnectionFilter to be set.
        Returns:
        this.
      • useCachedThreadPool

        public ConnectionBuilder<T> useCachedThreadPool​(boolean cached)
        Configures the connection to use CachedThreadPool as defined in AsynchronousChannelGroup.withCachedThreadPool(java.util.concurrent.ExecutorService, int). The default behaviour is to use a fixed thread poll as defined in AsynchronousChannelGroup.withFixedThreadPool(int, java.util.concurrent.ThreadFactory).
        Parameters:
        cached - use a cached thread pool if true, otherwise use fixed thread pool
        Returns:
        this
      • threadPoolSize

        public ConnectionBuilder<T> threadPoolSize​(int size)
        Set the size of the threadPool used to manage the connections and data sending. If the thread pool is cached this method defines the corePoolSize of (ThreadPoolExecutor) If the thread pool is fixed this method defines the amount of threads The min accepted value is 1. The default value is the quantity of available processors minus 2.
        Parameters:
        size - - the size of thread pool to be Set
        Returns:
        this
      • maxCachedThreads

        public ConnectionBuilder<T> maxCachedThreads​(int size)
        Set the size of max threads allowed in the cached thread pool. The execution we be rejected when all the threads in the cached thread pool is busy after reaching the max thread allowed. This config is ignored when a fixed thread pool is used.
        Parameters:
        size - the max cached threads in the cached thread pool.
        Returns:
        this
      • useNagle

        public ConnectionBuilder<T> useNagle​(boolean useNagle)
        Defines if small outgoing packets must be combined to be sent all at once. This improves the network performance, but can cause lags on clients waiting for the packet. The default value is false.
        Parameters:
        useNagle - - true if the Nagle's algorithm must be used.
        Returns:
        this.
      • shutdownWaitTime

        public ConnectionBuilder<T> shutdownWaitTime​(long waitTime)
        Sets the shutdown wait time in milliseconds. The default value is 5 seconds.
        Parameters:
        waitTime - - the wait time to close all connections resources after a ConnectionHandler.shutdown() is called.
        Returns:
        this
      • addBufferPool

        public ConnectionBuilder<T> addBufferPool​(int size,
                                                  int bufferSize)
        Add a new ByteBuffer grouping pool
        Parameters:
        size - the max amount of ByteBuffer supported
        bufferSize - the ByteBuffer's size inside the pool.
        Returns:
        this
      • initBufferPoolFactor

        public ConnectionBuilder<T> initBufferPoolFactor​(float factor)
        Define the factor of pre-initialized ByteBuffer inside a pool.
        Parameters:
        factor - the factor of initialized buffers
        Returns:
        this
      • bufferSegmentSize

        public ConnectionBuilder<T> bufferSegmentSize​(int size)
        Define the size of dynamic buffer's segment. A segment is used to increase the Buffer when needed.
        Parameters:
        size - of dynamic buffer segment
        Returns:
        this
      • dropPacketThreshold

        public ConnectionBuilder<T> dropPacketThreshold​(int threshold)
        Define the threshold to allow the client to drop disposable packets. When the client has queued more than threshold disposable packets will be disposed.
        Parameters:
        threshold - the minimum value to drop packets. The default value is 250
        Returns:
        this
      • build

        public ConnectionHandler<T> build()
                                   throws java.io.IOException
        Builds a new ConnectionHandler based on the options configured.
        Returns:
        a ConnectionHandler
        Throws:
        java.io.IOException - - If the Socket Address configured can't be used.