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.
  • Constructor Details

    • UDPConnection

      public UDPConnection(InetAddress localHost, Integer localPort, InetAddress remoteHost, Integer remotePort, UDPConnection.TrafficClass trafficClass) throws IOException
      Main constructor, any argument of which could null.

      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 like BMPConnection.

      Parameters:
      localHost - The local host to bind to. If null, it defaults to binding to all interfaces, unless remoteHost 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 (or null) or between 1025 and 65535.
      remoteHost - The remote host name or IP address to send packets to. If null, 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. If remoteHost is null, this is ignored. If remoteHost 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. If null, no traffic class will be used. Receive-only sockets should leave this as null.
      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

      @ForOverride protected InetSocketAddress getLocalAddress() throws IOException
      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

      protected InetSocketAddress 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

      public final InetAddress getLocalIPAddress()
      Specified by:
      getLocalIPAddress in interface Connection
      Returns:
      The local IP address to which the connection is bound.
    • getLocalPort

      public final int getLocalPort()
      Specified by:
      getLocalPort in interface Connection
      Returns:
      The local port to which the connection is bound.
    • getRemoteIPAddress

      public final InetAddress getRemoteIPAddress()
      Specified by:
      getRemoteIPAddress in interface Connection
      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 interface Connection
      Returns:
      The remote port to which the connection is connected, or zero if it is not connected.
    • receive

      Description copied from interface: Connection
      Receive data from the connection.
      Specified by:
      receive in interface Connection
      Parameters:
      timeout - The timeout in milliseconds, or null to wait forever
      Returns:
      The data received, in a little-endian buffer
      Throws:
      SocketTimeoutException - If a timeout occurs before any data is received
      EOFException - If the connection is closed
      IOException - If an error occurs receiving the data
      InterruptedException - If communications are interrupted.
    • receive

      public final ByteBuffer receive(int timeout) throws SocketTimeoutException, IOException, InterruptedException
      Receive 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 received
      EOFException - If the connection is closed
      IOException - If an error occurs receiving the data
      InterruptedException - If communications are interrupted.
    • doReceive

      Receive 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 received
      IOException - If an error occurs receiving the data
      InterruptedException - If communications are interrupted.
    • receiveWithAddress

      public final UDPPacket receiveWithAddress(int timeout) throws SocketTimeoutException, IOException
      Description copied from interface: Connection
      Receive data from the connection along with the address where the data was received from.
      Specified by:
      receiveWithAddress in interface Connection
      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 received
      EOFException - If the connection is closed
      IOException - If an error occurs receiving the data
    • doReceiveWithAddress

      @ForOverride protected UDPPacket doReceiveWithAddress(int timeout) throws SocketTimeoutException, IOException
      Receive 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 received
      IOException - If an error occurs receiving the data
    • receiveMessage

      public T receiveMessage() throws IOException, InterruptedException
      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 message
      InterruptedException - If communications are interrupted.
      IllegalArgumentException - If one of the fields of the SpiNNaker message is invalid
    • receiveMessage

      public abstract T receiveMessage(int timeout) throws IOException, InterruptedException
      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 message
      InterruptedException - If communications are interrupted.
      SocketTimeoutException - If there is a timeout during receiving
      IllegalArgumentException - If one of the fields of the SpiNNaker message is invalid
    • send

      public final void send(DatagramPacket data) throws IOException
      Send data down this connection.
      Parameters:
      data - The data to be sent
      Throws:
      EOFException - If the connection is closed
      IOException - If there is an error sending the data
      IllegalStateException - If the data packet doesn't hold a real message; zero-length messages are not supported!
    • doSend

      @ForOverride protected void doSend(ByteBuffer data) throws IOException
      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

      public final void send(ByteBuffer data) throws IOException
      Description copied from interface: Connection
      Send data down this connection.
      Specified by:
      send in interface Connection
      Parameters:
      data - The data to be sent
      Throws:
      EOFException - If the connection is closed
      IOException - If there is an error sending the data
    • sendTo

      public final void sendTo(DatagramPacket data, InetAddress address, int port) throws IOException
      Send data down this connection.
      Parameters:
      data - The data to be sent
      address - Where to send (must be non-null)
      port - What port to send to (must be non-zero)
      Throws:
      EOFException - If the connection is closed
      IOException - If there is an error sending the data
      IllegalStateException - If the data packet doesn't hold a real message; zero-length messages are not supported!
    • sendTo

      public final void sendTo(byte[] data, InetAddress address, int port) throws IOException
      Send data down this connection.
      Parameters:
      data - The data to be sent
      address - Where to send (must be non-null)
      port - What port to send to (must be non-zero)
      Throws:
      EOFException - If the connection is closed
      IOException - If there is an error sending the data
      IllegalStateException - If the data array doesn't hold a message; zero-length messages are not supported!
    • sendTo

      public final void sendTo(ByteBuffer data, InetAddress address, int port) throws IOException
      Description copied from interface: Connection
      Send data down this connection.
      Specified by:
      sendTo in interface Connection
      Parameters:
      data - The data to be sent
      address - Where to send (must be non-null)
      port - What port to send to (must be non-zero)
      Throws:
      EOFException - If the connection is closed
      IOException - If there is an error sending the data
    • doSendTo

      @ForOverride protected void doSendTo(ByteBuffer data, InetAddress address, int port) throws IOException
      Send data down this connection.

      This operation is delegatable; see DelegatingSCPConnection.

      Parameters:
      data - The data to be sent
      address - 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 interface Connection
      Returns:
      true if the medium is connected, false otherwise
    • close

      @OverridingMethodsMustInvokeSuper public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      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 interface Connection
      Returns:
      true if the medium is closed, false otherwise
    • sendPortTriggerMessage

      public final void sendPortTriggerMessage(InetAddress host) throws IOException
      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

      public String toString()
      Overrides:
      toString in class Object
    • setInUse

      public void setInUse()
    • setNotInUse

      public void setNotInUse()