Package com.framed.core.remote
Class TCPTransport
java.lang.Object
com.framed.core.remote.TCPTransport
- All Implemented Interfaces:
Transport
A
Transport implementation using traditional blocking I/O over TCP sockets.
This class provides a simple server that accepts incoming TCP connections and processes
messages line-by-line using a JSON-based protocol. Each message is expected to be a single
JSON object containing address, payload, and type fields.
Features:
- Blocking I/O using
ServerSocketandSocket. - Handles multiple clients concurrently using an
ExecutorService. - Supports point-to-point (
send(java.lang.String, int, java.lang.String, java.lang.Object)) and broadcast (publish(java.lang.String, int, java.lang.String, java.lang.Object)) messaging. - Graceful shutdown via
shutdown().
Message Format:
{
"address": "topic.name",
"payload": ...,
"type": "send" | "publish"
}
Example usage:
TCPTransport transport = new TCPTransport(8080);
transport.register("sensor.data", msg -> System.out.println("Received: " + msg));
transport.start();
transport.send("localhost", 8080, "sensor.data", "Hello over TCP");
// Later:
transport.shutdown();
Note: Always call shutdown() to release resources and stop the server.
-
Constructor Summary
ConstructorsConstructorDescriptionTCPTransport(int port) Creates a new TCP 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 to the specified host and port via TCP.voidshutdown()Shuts down the transport and releases resources.voidstart()Starts the TCP server in a background thread.
-
Constructor Details
-
TCPTransport
public TCPTransport(int port) Creates a new TCP transport bound to the specified port.- Parameters:
port- the TCP port to listen on
-
-
Method Details
-
start
public void start()Starts the TCP server in a background thread.Accepts incoming connections and delegates each client to
handleClient(Socket). -
send
Sends a point-to-point message to the specified host and port via TCP. -
publish
Publishes a message intended for broadcast semantics on the receiver side. -
shutdown
public void shutdown()Shuts down the transport and releases resources.Stops accepting new connections, closes the server socket, and terminates the thread pool.
-
register
Registers a handler for messages received on the specified address. Creates a single threaded executor for that handler.
-