Class ConnectionListener<MessageType>

java.lang.Object
java.lang.Thread
uk.ac.manchester.spinnaker.connections.ConnectionListener<MessageType>
Type Parameters:
MessageType - The type of message being listened for by the connection.
All Implemented Interfaces:
Closeable, AutoCloseable, Runnable

public final class ConnectionListener<MessageType>
extends Thread
implements Closeable
Thread that listens to a connection and calls callbacks with new messages when they arrive.
  • Field Details

  • Constructor Details

    • ConnectionListener

      @MustBeClosed public ConnectionListener​(UDPConnection<MessageType> connection)
      Create a connection listener with the default number of listening threads and the default OS-level timeout.
      Parameters:
      connection - The connection to listen to.
    • ConnectionListener

      @MustBeClosed public ConnectionListener​(UDPConnection<MessageType> connection, int numProcesses, int timeout)
      Create a connection listener.
      Parameters:
      connection - The connection to listen to.
      numProcesses - The maximum number of threads to use to do the listening.
      timeout - How long to wait in the OS for a message to arrive; if 0, wait indefinitely.
  • Method Details

    • run

      public void run()
      Receive messages and dispatch them to the registered handlers. Stops running when closed.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • addCallback

      public void addCallback​(MessageHandler<MessageType> callback)
      Add a callback to be called when a message is received.
      Parameters:
      callback - The callback to add.
    • close

      public void close()
      Closes the listener. Note that this does not close the provider of the messages; this instead marks the listener as closed. The listener will not truly stop until the get message call returns, and this call will block until the callback thread pool has terminated.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable