Class NioTcpTransport

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

public class NioTcpTransport extends Object implements Transport
A Transport implementation using Java NIO for TCP-based communication.

This class provides non-blocking I/O for message exchange between components, services or devices using a simple JSON-based protocol. Messages are framed by newline characters and contain fields for address, payload, and type.

Features:

Message Format:


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

Example usage:


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

 transport.send("localhost", 8080, "sensor.data", "Hello");
 

Note: Always call shutdown() to release resources when done.

  • Constructor Summary

    Constructors
    Constructor
    Description
    NioTcpTransport(int port)
    Creates a new NIO 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 to all subscribers of the specified address.
    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 message to the first subscriber of the specified address.
    void
    Shuts down the transport, closing the selector, server channel, and worker pool.
    void
    Starts the transport event loop in a background thread.

    Methods inherited from class java.lang.Object

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

    • NioTcpTransport

      public NioTcpTransport(int port) throws IOException
      Creates a new NIO TCP transport bound to the specified port.
      Parameters:
      port - the TCP port to listen on
      Throws:
      IOException - if the server socket or selector cannot be initialized
  • Method Details

    • start

      public void start()
      Starts the transport event loop in a background thread.

      Accepts new connections and reads incoming messages asynchronously.

      Specified by:
      start in interface Transport
    • send

      public void send(String host, int port, String address, Object message)
      Sends a message to the first subscriber of the specified address.
      Specified by:
      send in interface Transport
      Parameters:
      host - the target host
      port - the target port
      address - the logical address for the message
      message - the message payload
    • publish

      public void publish(String host, int port, String address, Object message)
      Publishes a message to all subscribers of the specified address.
      Specified by:
      publish in interface Transport
      Parameters:
      host - the target host
      port - the target port
      address - the logical address for the message
      message - the message payload
    • shutdown

      public void shutdown()
      Shuts down the transport, closing the selector, server channel, and worker 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.
      Specified by:
      register in interface Transport
      Parameters:
      address - the address to listen on
      handler - the handler to process messages