Class TCPTransport

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

public class TCPTransport extends Object implements 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:

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

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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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).

      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 TCP.
      Specified by:
      send in interface Transport
      Parameters:
      host - target hostname or IP
      port - target TCP 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 TCP 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.

      Stops accepting new connections, closes the server socket, and terminates the thread pool.

      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. Creates a single threaded executor for that handler.
      Specified by:
      register in interface Transport
      Parameters:
      address - the logical address/topic to listen on
      handler - the handler to process incoming payloads