Class UDPTransport

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

public class UDPTransport extends Object implements Transport
A Transport implementation using UDP datagrams for lightweight, connectionless messaging.

This class provides a simple UDP-based per-handler blocking transport mechanism for sending and receiving messages encoded as JSON objects. It supports asynchronous message handling and concurrent consumers.

Features:

Message Format:


 {
   "address": "topic.name",
   "payload": ...
 }
 

Example usage:


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

 transport.send("localhost", 9090, "sensor.data", "Hello over UDP");

 // Later:
 transport.shutdown();
 

Note: UDP is unreliable by design; messages may be lost or arrive out of order.

  • Constructor Summary

    Constructors
    Constructor
    Description
    UDPTransport(int port)
    Creates a new UDP transport bound to the specified 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 via UDP.
    void
    Shuts down the transport and releases resources.
    void
    Starts the UDP listener in a background thread.

    Methods inherited from class java.lang.Object

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

    • UDPTransport

      public UDPTransport(int port)
      Creates a new UDP transport bound to the specified port.
      Parameters:
      port - the UDP port to listen on
  • Method Details

    • start

      public void start()
      Starts the UDP listener in a background thread.

      Receives datagrams, parses them as JSON, and dispatches to registered handlers.

      Specified by:
      start in interface Transport
    • send

      public void send(String host, int port, String address, Object message)
      Sends a point-to-point message 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
    • 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
    • shutdown

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

      Stops receiving datagrams, closes the socket, and terminates the thread pool.

      Specified by:
      shutdown in interface Transport