Class NioUdpTransport

java.lang.Object
com.framed.core.remote.NioUdpTransport
All Implemented Interfaces:
Transport

public class NioUdpTransport extends Object implements Transport
A Transport implementation using Java NIO over UDP for lightweight, connectionless message delivery between services or devices.

This class uses a Selector with a non-blocking DatagramChannel to receive datagrams and a simple JSON-based envelope for outgoing messages. Each datagram is expected to contain a single JSON object with address, payload, and type fields.

Characteristics of UDP

  • Unreliable: Datagrams may be lost, duplicated, or arrive out of order.
  • Message-oriented: Each packet is an independent message; there is no stream.
  • No back-pressure: Sender and receiver are decoupled; overspeed can cause drops.

Message Format


 {
   "address": "topic.name",
   "payload": ...,
   "type": "send" | "publish"
 }
 

Example usage


 NioUdpTransport transport = new NioUdpTransport(9000);
 transport.register("sensor.data", msg -> System.out.println("Received: " + msg));
 transport.start();

 // Send a message to a remote UDP listener
 transport.send("localhost", 9000, "sensor.data", "Hello over UDP");

 // Later:
 transport.shutdown();
 

Note: Always call shutdown() to release resources. Consider adding your own reliability or acknowledgement layer if message loss is unacceptable.

  • Constructor Summary

    Constructors
    Constructor
    Description
    NioUdpTransport(int port)
    Creates a new UDP transport bound to the specified local port.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    publish(String host, int port, String address, Object message)
    Publishes a message intended for broadcast semantics on the receiver side.
    void
    register(String address, Consumer<Object> handler)
    Registers a handler for messages received on the specified address.
    void
    send(String host, int port, String address, Object message)
    Sends a point-to-point message to the specified host and port via UDP.
    void
    Shuts down the transport and releases resources.
    void
    Starts the selector loop on a background thread.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NioUdpTransport

      public NioUdpTransport(int port) throws IOException
      Creates a new UDP transport bound to the specified local port.
      Parameters:
      port - the UDP port to listen on
      Throws:
      IOException - if the selector or channel cannot be initialized or bound
  • Method Details

    • start

      public void start()
      Starts the selector loop on a background thread.

      When the channel is readable, a datagram is decoded as UTF-8, parsed as JSON, and dispatched to handlers based on its address field and message type.

      Implementation note: The received buffer is reused per-iteration. Ensure DatagramChannel.receive(java.nio.ByteBuffer) populates the buffer before decoding.

      Specified by:
      start in interface Transport
    • send

      public void send(String host, int port, String address, Object message)
      Sends a point-to-point message to the specified host and port via UDP.
      Specified by:
      send in interface Transport
      Parameters:
      host - target hostname or IP
      port - target UDP port
      address - logical address/topic for routing by the receiver
      message - payload object to include in the JSON envelope
    • publish

      public void publish(String host, int port, String address, Object message)
      Publishes a message intended for broadcast semantics on the receiver side.
      Specified by:
      publish in interface Transport
      Parameters:
      host - target hostname or IP
      port - target UDP port
      address - logical address/topic for routing by the receiver
      message - payload object to include in the JSON envelope
    • shutdown

      public void shutdown()
      Shuts down the transport and releases resources.

      Closes the selector and datagram channel and stops the event loop.

      Specified by:
      shutdown in interface Transport
    • register

      public void register(String address, Consumer<Object> handler)
      Registers a handler for messages received on the specified address.
      Specified by:
      register in interface Transport
      Parameters:
      address - the logical address/topic to listen on
      handler - the handler to process incoming payloads