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.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ConnectionListener(UDPConnection<MessageType> connection)
Create a connection listener with the default number of listening threads and the default OS-level timeout.ConnectionListener(UDPConnection<MessageType> connection, int numProcesses, int timeout)
Create a connection listener. -
Method Summary
Modifier and Type Method Description void
addCallback(MessageHandler<MessageType> callback)
Add a callback to be called when a message is received.void
close()
Closes the listener.void
run()
Receive messages and dispatch them to the registered handlers.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
POOL_SIZE
public static final int POOL_SIZEWhat size of thread pool to use by default.- See Also:
- Constant Field Values
-
TIMEOUT
public static final int TIMEOUTThe default time (in ms) to wait in a system call for a message to arrive.- See Also:
- Constant Field Values
-
-
Constructor Details
-
ConnectionListener
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() -
addCallback
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-