Class Connector<T extends Client<Connection<T>>>


  • public class Connector<T extends Client<Connection<T>>>
    extends java.lang.Object
    • Constructor Detail

      • Connector

        public Connector()
    • Method Detail

      • create

        public static <T extends Client<Connection<T>>> Connector<T> create​(ClientFactory<T> clientFactory,
                                                                            PacketHandler<T> packetHandler,
                                                                            PacketExecutor<T> executor)
        Creates a Connector holding the minimum requirements to create a Client.
        Type Parameters:
        T - - The Type of client that ConnectionBuilder will handle.
        Parameters:
        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.
      • addBufferPool

        public Connector<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 Connector<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 Connector<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 Connector<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 can be disposed.

        Parameters:
        threshold - the minimum value to drop packets. The default value is 250
        Returns:
        this
      • useCachedThreadPool

        public Connector<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 Connector<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 Connector<T> maxCachedThreads​(int size)
        Set the size of max threads allowed in the cached thread pool. This config is ignored when a fixed thread pool is used.
        Parameters:
        size - the max cached threads in the cached thread pool.
        Returns:
        this
      • connect

        public T connect​(java.lang.String host,
                         int port)
                  throws java.io.IOException,
                         java.util.concurrent.ExecutionException,
                         java.lang.InterruptedException
        Connects to a host using the address and port.
        Parameters:
        host - the address to be connected to
        port - the port of the host
        Returns:
        A client connected to the host and port
        Throws:
        java.io.IOException - if a IO error happens during the connection.
        java.util.concurrent.ExecutionException - if the computation threw an exception
        java.lang.InterruptedException - if the current thread was interrupted while waiting
      • connect

        public T connect​(java.net.InetSocketAddress socketAddress)
                  throws java.io.IOException,
                         java.util.concurrent.ExecutionException,
                         java.lang.InterruptedException
        Connects to a host with socketAddress
        Parameters:
        socketAddress - the address which will be connected
        Returns:
        a client that represents the connection with the socketAddress
        Throws:
        java.io.IOException - if a IO error happens during the connection.
        java.util.concurrent.ExecutionException - if the computation threw an exception
        java.lang.InterruptedException - if the current thread was interrupted while waiting