Class TrdpPdSession

java.lang.Object
com.trdp.pd.TrdpPdSession
All Implemented Interfaces:
AutoCloseable

public class TrdpPdSession extends Object implements AutoCloseable
High-performance PD session manager that shares a single UDP socket and a minimal number of threads across multiple publishers and subscribers.

Manages multiple PD publishers and subscribers on a single UDP socket with a single receive thread and shared cyclic send scheduler (2 threads and 1 socket total).

Usage:

try (TrdpPdSession session = new TrdpPdSession(17224)) {
    PdPublisherHandle pub = session.addPublisher(1000, "239.255.0.1", 17224, 100_000);
    PdSubscriberHandle sub = session.addSubscriber(2000, "239.255.0.1", 100_000, listener);
    session.start();
    pub.putData(data);
    // Dynamic add/remove after start:
    PdPublisherHandle pub2 = session.addPublisher(1001, "239.255.0.1", 17224, 50_000);
    session.removePublisher(1001);
}

Registration methods (addPublisher(int, String, int, long), addSubscriber(int, String, long, PdEventListener)) can be called before or after start(). Publishers added after start are not traffic-shaped. removePublisher(int) and removeSubscribers(int) allow removal at any time. Callbacks run on the receive thread.

  • Constructor Details

    • TrdpPdSession

      public TrdpPdSession(int port) throws IOException
      Creates a PD session on the specified port.
      Parameters:
      port - The UDP port to bind to. Use 0 for an ephemeral port.
      Throws:
      IOException - If socket creation fails.
    • TrdpPdSession

      public TrdpPdSession(int port, InetAddress bindAddress, int ttl, int qos) throws IOException
      Creates a PD session with custom socket options.
      Parameters:
      port - The UDP port to bind to. Use 0 for an ephemeral port.
      bindAddress - The local address to bind to, or null for wildcard.
      ttl - The IP time-to-live for outgoing packets.
      qos - The QoS value (IP Precedence 0..7).
      Throws:
      IOException - If socket creation fails.
  • Method Details

    • addPublisher

      public PdPublisherHandle addPublisher(int comId, String destinationAddress, int destinationPort, long intervalUs) throws IOException
      Registers a publisher for the given ComId. Can be called before or after start(). Publishers added after start use their interval as initial delay (no traffic shaping stagger).
      Parameters:
      comId - The ComID to publish.
      destinationAddress - The destination address for push/cyclic sends.
      destinationPort - The destination port for push/cyclic sends.
      intervalUs - The cyclic send interval in microseconds. 0 means no cyclic send.
      Returns:
      A handle for staging and sending data.
      Throws:
      IllegalArgumentException - If a publisher for this ComId already exists, or intervalUs is negative.
      IOException
    • addSubscriber

      public PdSubscriberHandle addSubscriber(int comId, String multicastGroup, long timeoutUs, PdEventListener listener) throws IOException
      Registers a subscriber for the given ComId. Can be called before or after start().
      Parameters:
      comId - The ComID to subscribe to.
      multicastGroup - The multicast group to join, or null for unicast-only.
      timeoutUs - Timeout in microseconds (0 to disable timeout detection).
      listener - The listener to receive callbacks.
      Returns:
      A handle for querying status and statistics.
      Throws:
      IOException
    • removePublisher

      public PdPublisherHandle removePublisher(int comId)
      Removes the publisher for the given ComId.
      Parameters:
      comId - The ComID of the publisher to remove.
      Returns:
      The removed publisher handle, or null if no publisher existed for this ComId.
    • removeSubscribers

      public List<PdSubscriberHandle> removeSubscribers(int comId)
      Removes all subscribers for the given ComId.
      Parameters:
      comId - The ComID of the subscribers to remove.
      Returns:
      The list of removed subscriber handles, or an empty list if none existed.
    • setTopologyCounters

      public void setTopologyCounters(int etbTopoCnt, int opTrnTopoCnt)
      Sets topology counters for the entire session (all publishers and subscribers).
    • setTrafficShapingEnabled

      public void setTrafficShapingEnabled(boolean enabled)
      Enables or disables traffic shaping for this session. When enabled, cyclic publishers sharing the same interval have their initial delays staggered evenly across the interval window, preventing network bursts. Must be called before start().
      Parameters:
      enabled - true to enable traffic shaping (default), false to disable.
      Throws:
      IllegalStateException - If the session has already been started.
    • isTrafficShapingEnabled

      public boolean isTrafficShapingEnabled()
      Returns whether traffic shaping is enabled for this session.
    • start

      public void start()
      Starts the session: begins cyclic sends and the receive loop. Publishers and subscribers may still be added or removed after this call.
      Throws:
      IllegalStateException - If the session has already been started.
    • getPublisherCount

      public int getPublisherCount()
      Returns the number of registered publishers.
    • getSubscriberCount

      public int getSubscriberCount()
      Returns the total number of registered subscribers across all ComIds.
    • getPort

      public int getPort()
      Returns the local port this session is bound to.
    • getFcsErrorCount

      public long getFcsErrorCount()
      Returns the number of packets rejected due to FCS (CRC) validation failure.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable