Package com.framed.core.remote
Class UDPTransport
java.lang.Object
com.framed.core.remote.UDPTransport
- All Implemented Interfaces:
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:
- Connectionless communication using
DatagramSocket. - Blocking message dispatch via an
ExecutorService. - Supports point-to-point (
send(java.lang.String, int, java.lang.String, java.lang.Object)) and broadcast-like (publish(java.lang.String, int, java.lang.String, java.lang.Object)) semantics. - Graceful shutdown via
shutdown().
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
ConstructorsConstructorDescriptionUDPTransport(int port) Creates a new UDP transport bound to the specified port. -
Method Summary
Modifier and TypeMethodDescriptionvoidPublishes a message intended for broadcast semantics on the receiver side.voidRegisters a handler for messages received on the specified address.voidSends a point-to-point message via UDP.voidshutdown()Shuts down the transport and releases resources.voidstart()Starts the UDP listener in a background thread.
-
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.
-
send
Sends a point-to-point message via UDP. -
publish
Publishes a message intended for broadcast semantics on the receiver side. -
register
Registers a handler for messages received on the specified address. -
shutdown
public void shutdown()Shuts down the transport and releases resources.Stops receiving datagrams, closes the socket, and terminates the thread pool.
-