Class IODispatcher

  • All Implemented Interfaces:
    Runnable, DatagramDispatcher<Endpoint>

    public class IODispatcher
    extends Object
    implements DatagramDispatcher<Endpoint>, Runnable
    I/O dispatcher for incoming UDP packets. This class is responsible for receiving UDP packets and dispatch the processing of these packets to worker threads. It uses a Selector to receive packets from multiple endpoints and a WorkerPool to dispatch the processing tasks.

    The dispatcher uses the following thread model: Incoming packets for all the registered endpoints are received in the thread that executes the run() method. For every packet received, a ProcessPacketTask instance is created and dispatched to a worker thread from the configured pool.

    The methods addEndpoint(Endpoint), removeEndpoint(Endpoint) and stop() are thread safe and may be called from any thread. However, to avoid concurrency issues, the operation on the underlying Selector will always be executed by the thread executing the run() method. The three methods mentioned above will block until the operation has completed.

    • Method Detail

      • addEndpoint

        public void addEndpoint​(Endpoint endpoint)
                         throws IOException
        Add a new endpoint. This method creates a new socket listening on the UDP port specified in the endpoint description and makes sure that incoming packets are routed to the specified service.
        Specified by:
        addEndpoint in interface DatagramDispatcher<Endpoint>
        Parameters:
        endpoint - the endpoint description
        Throws:
        IOException - if the socket could not be created or registered with the selector
      • removeEndpoint

        public void removeEndpoint​(Endpoint endpoint)
                            throws IOException
        Remove an endpoint. This causes the corresponding UDP socket to be closed.
        Specified by:
        removeEndpoint in interface DatagramDispatcher<Endpoint>
        Parameters:
        endpoint - the endpoint description
        Throws:
        IOException - if an error occurred when closing the socket
      • run

        public void run()
        Run the I/O dispatcher. This method contains the event loop that polls the selector, reads the incoming packets and dispatches the work. It only returns when stop() is called.
        Specified by:
        run in interface Runnable