Class UDPConnection<T>
java.lang.Object
uk.ac.manchester.spinnaker.connections.UDPConnection<T>
- Type Parameters:
T
- The type of message to be received. It's possible for the received information to even be metadata about the message, and not the content of the message.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Connection
- Direct Known Subclasses:
BMPConnection
,BootConnection
,EIEIOConnection
,IPAddressConnection
,NotificationConnection
,ProxyUDPConnection
,SDPConnection
public abstract class UDPConnection<T> extends Object implements Connection
A connection to SpiNNaker over UDP/IPv4.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UDPConnection.TrafficClass
The type of traffic being sent on a socket. -
Constructor Summary
Constructors Constructor Description UDPConnection(InetAddress localHost, Integer localPort, InetAddress remoteHost, Integer remotePort, UDPConnection.TrafficClass trafficClass)
Main constructor, any argument of which couldnull
. -
Method Summary
Modifier and Type Method Description void
close()
protected ByteBuffer
doReceive(int timeout)
Receive data from the connection.protected UDPPacket
doReceiveWithAddress(int timeout)
Receive data from the connection along with the address where the data was received from.protected void
doSend(ByteBuffer data)
Send data down this connection.protected void
doSendTo(ByteBuffer data, InetAddress address, int port)
Send data down this connection.protected InetSocketAddress
getLocalAddress()
Get the local socket address.InetAddress
getLocalIPAddress()
int
getLocalPort()
protected InetSocketAddress
getRemoteAddress()
Get the remote socket address.InetAddress
getRemoteIPAddress()
int
getRemotePort()
boolean
isClosed()
Determines if the medium is closed at this point in time.boolean
isConnected()
Determines if the medium is connected at this point in time.ByteBuffer
receive(int timeout)
Receive data from the connection.ByteBuffer
receive(Integer timeout)
Receive data from the connection.T
receiveMessage()
Receives a SpiNNaker message from this connection.abstract T
receiveMessage(int timeout)
Receives a SpiNNaker message from this connection.UDPPacket
receiveWithAddress(int timeout)
Receive data from the connection along with the address where the data was received from.void
send(DatagramPacket data)
Send data down this connection.void
send(ByteBuffer data)
Send data down this connection.void
sendPortTriggerMessage(InetAddress host)
Sends a port trigger message using a connection to (hopefully) open a port in a NAT and/or firewall to allow incoming packets to be received.void
sendTo(byte[] data, InetAddress address, int port)
Send data down this connection.void
sendTo(DatagramPacket data, InetAddress address, int port)
Send data down this connection.void
sendTo(ByteBuffer data, InetAddress address, int port)
Send data down this connection.protected void
setReceivePacketSize(int receivePacketSize)
Set the maximum size of packet that can be received.String
toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface uk.ac.manchester.spinnaker.connections.model.Connection
convertTimeout
-
Constructor Details
-
UDPConnection
public UDPConnection(InetAddress localHost, Integer localPort, InetAddress remoteHost, Integer remotePort, UDPConnection.TrafficClass trafficClass) throws IOExceptionMain constructor, any argument of which couldnull
.No default constructors are provided as it would not be possible to disambiguate between ones with only a local host/port like
IPAddressConnection
and ones with only remote host/port likeBMPConnection
.- Parameters:
localHost
- The local host to bind to. Ifnull
, it defaults to binding to all interfaces, unlessremoteHost
is specified, in which case binding is done to the IP address that will be used to send packets.localPort
- The local port to bind to, 0 (ornull
) or between 1025 and 65535.remoteHost
- The remote host name or IP address to send packets to. Ifnull
, the socket will be available for listening only, and will throw an exception if used for sending.remotePort
- The remote port to send packets to. IfremoteHost
isnull
, this is ignored. IfremoteHost
is specified, this must also be specified as non-zero for the connection to allow sending.trafficClass
- What sort of traffic is this socket going to send. Ifnull
, no traffic class will be used. Receive-only sockets should leave this asnull
.- Throws:
IOException
- If there is an error setting up the communication channel
-
-
Method Details
-
setReceivePacketSize
protected void setReceivePacketSize(int receivePacketSize)Set the maximum size of packet that can be received. Packets larger than this will be truncated. The default is large enough for any packet that is sent by SCAMP.- Parameters:
receivePacketSize
- The new maximum packet size.
-
getLocalAddress
Get the local socket address. (Sockets have two ends, one local, one remote.)This operation is delegatable; see
DelegatingSCPConnection
.- Returns:
- The socket's local address
- Throws:
IOException
- If the socket is closed.
-
getRemoteAddress
Get the remote socket address. (Sockets have two ends, one local, one remote.)This operation is delegatable; see
DelegatingSCPConnection
.- Returns:
- The socket's remote address
-
getLocalIPAddress
- Specified by:
getLocalIPAddress
in interfaceConnection
- Returns:
- The local IP address to which the connection is bound.
-
getLocalPort
public final int getLocalPort()- Specified by:
getLocalPort
in interfaceConnection
- Returns:
- The local port to which the connection is bound.
-
getRemoteIPAddress
- Specified by:
getRemoteIPAddress
in interfaceConnection
- Returns:
- The remote IP address to which the connection is connected, or
null
if it is not connected.
-
getRemotePort
public final int getRemotePort()- Specified by:
getRemotePort
in interfaceConnection
- Returns:
- The remote port to which the connection is connected, or zero if it is not connected.
-
receive
public final ByteBuffer receive(Integer timeout) throws SocketTimeoutException, IOException, InterruptedExceptionDescription copied from interface:Connection
Receive data from the connection.- Specified by:
receive
in interfaceConnection
- Parameters:
timeout
- The timeout in milliseconds, ornull
to wait forever- Returns:
- The data received, in a little-endian buffer
- Throws:
SocketTimeoutException
- If a timeout occurs before any data is receivedEOFException
- If the connection is closedIOException
- If an error occurs receiving the dataInterruptedException
- If communications are interrupted.
-
receive
public final ByteBuffer receive(int timeout) throws SocketTimeoutException, IOException, InterruptedExceptionReceive data from the connection.- Parameters:
timeout
- The timeout in milliseconds- Returns:
- The data received, in a little-endian buffer
- Throws:
SocketTimeoutException
- If a timeout occurs before any data is receivedEOFException
- If the connection is closedIOException
- If an error occurs receiving the dataInterruptedException
- If communications are interrupted.
-
doReceive
@ForOverride protected ByteBuffer doReceive(int timeout) throws SocketTimeoutException, IOException, InterruptedExceptionReceive data from the connection.This operation is delegatable; see
DelegatingSCPConnection
.- Parameters:
timeout
- The timeout in milliseconds- Returns:
- The data received, in a little-endian buffer
- Throws:
SocketTimeoutException
- If a timeout occurs before any data is receivedIOException
- If an error occurs receiving the dataInterruptedException
- If communications are interrupted.
-
receiveWithAddress
Description copied from interface:Connection
Receive data from the connection along with the address where the data was received from.- Specified by:
receiveWithAddress
in interfaceConnection
- Parameters:
timeout
- The timeout in milliseconds- Returns:
- The datagram packet received; caller is responsible for only accessing the valid part of the buffer.
- Throws:
SocketTimeoutException
- If a timeout occurs before any data is receivedEOFException
- If the connection is closedIOException
- If an error occurs receiving the data
-
doReceiveWithAddress
@ForOverride protected UDPPacket doReceiveWithAddress(int timeout) throws SocketTimeoutException, IOExceptionReceive data from the connection along with the address where the data was received from.This operation is delegatable; see
DelegatingSCPConnection
.- Parameters:
timeout
- The timeout in milliseconds- Returns:
- The datagram packet received
- Throws:
SocketTimeoutException
- If a timeout occurs before any data is receivedIOException
- If an error occurs receiving the data
-
receiveMessage
Receives a SpiNNaker message from this connection. Blocks until a message has been received.- Returns:
- the received message
- Throws:
IOException
- If there is an error receiving the messageInterruptedException
- If communications are interrupted.IllegalArgumentException
- If one of the fields of the SpiNNaker message is invalid
-
receiveMessage
Receives a SpiNNaker message from this connection. Blocks until a message has been received, or a timeout occurs.- Parameters:
timeout
- The time in seconds to wait for the message to arrive, or until the connection is closed.- Returns:
- the received message
- Throws:
IOException
- If there is an error receiving the messageInterruptedException
- If communications are interrupted.SocketTimeoutException
- If there is a timeout during receivingIllegalArgumentException
- If one of the fields of the SpiNNaker message is invalid
-
send
Send data down this connection.- Parameters:
data
- The data to be sent- Throws:
EOFException
- If the connection is closedIOException
- If there is an error sending the dataIllegalStateException
- If the data packet doesn't hold a real message; zero-length messages are not supported!
-
doSend
Send data down this connection.This operation is delegatable; see
DelegatingSCPConnection
.- Parameters:
data
- The data to be sent; the position in this buffer will not be updated by this method- Throws:
IOException
- If there is an error sending the data
-
send
Description copied from interface:Connection
Send data down this connection.- Specified by:
send
in interfaceConnection
- Parameters:
data
- The data to be sent- Throws:
EOFException
- If the connection is closedIOException
- If there is an error sending the data
-
sendTo
Send data down this connection.- Parameters:
data
- The data to be sentaddress
- Where to send (must be non-null
)port
- What port to send to (must be non-zero)- Throws:
EOFException
- If the connection is closedIOException
- If there is an error sending the dataIllegalStateException
- If the data packet doesn't hold a real message; zero-length messages are not supported!
-
sendTo
Send data down this connection.- Parameters:
data
- The data to be sentaddress
- Where to send (must be non-null
)port
- What port to send to (must be non-zero)- Throws:
EOFException
- If the connection is closedIOException
- If there is an error sending the dataIllegalStateException
- If the data array doesn't hold a message; zero-length messages are not supported!
-
sendTo
Description copied from interface:Connection
Send data down this connection.- Specified by:
sendTo
in interfaceConnection
- Parameters:
data
- The data to be sentaddress
- Where to send (must be non-null
)port
- What port to send to (must be non-zero)- Throws:
EOFException
- If the connection is closedIOException
- If there is an error sending the data
-
doSendTo
@ForOverride protected void doSendTo(ByteBuffer data, InetAddress address, int port) throws IOExceptionSend data down this connection.This operation is delegatable; see
DelegatingSCPConnection
.- Parameters:
data
- The data to be sentaddress
- Where to send (must be non-null
)port
- What port to send to (must be non-zero)- Throws:
IOException
- If there is an error sending the data
-
isConnected
public boolean isConnected()Description copied from interface:Connection
Determines if the medium is connected at this point in time. Connected media are not closed. Disconnected media might not be open.- Specified by:
isConnected
in interfaceConnection
- Returns:
- true if the medium is connected, false otherwise
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
isClosed
public boolean isClosed()Description copied from interface:Connection
Determines if the medium is closed at this point in time. Closed media are not connected. Open media might not be connected.- Specified by:
isClosed
in interfaceConnection
- Returns:
- true if the medium is closed, false otherwise
-
sendPortTriggerMessage
Sends a port trigger message using a connection to (hopefully) open a port in a NAT and/or firewall to allow incoming packets to be received.- Parameters:
host
- The address of the SpiNNaker board to which the message should be sent- Throws:
IOException
- If anything goes wrong
-
toString
-