Class TrdpPdSession
java.lang.Object
com.trdp.pd.TrdpPdSession
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionTrdpPdSession(int port) Creates a PD session on the specified port.TrdpPdSession(int port, InetAddress bindAddress, int ttl, int qos) Creates a PD session with custom socket options. -
Method Summary
Modifier and TypeMethodDescriptionaddPublisher(int comId, String destinationAddress, int destinationPort, long intervalUs) Registers a publisher for the given ComId.addSubscriber(int comId, String multicastGroup, long timeoutUs, PdEventListener listener) Registers a subscriber for the given ComId.voidclose()longReturns the number of packets rejected due to FCS (CRC) validation failure.intgetPort()Returns the local port this session is bound to.intReturns the number of registered publishers.intReturns the total number of registered subscribers across all ComIds.booleanReturns whether traffic shaping is enabled for this session.removePublisher(int comId) Removes the publisher for the given ComId.removeSubscribers(int comId) Removes all subscribers for the given ComId.voidsetTopologyCounters(int etbTopoCnt, int opTrnTopoCnt) Sets topology counters for the entire session (all publishers and subscribers).voidsetTrafficShapingEnabled(boolean enabled) Enables or disables traffic shaping for this session.voidstart()Starts the session: begins cyclic sends and the receive loop.
-
Constructor Details
-
TrdpPdSession
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
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, ornullfor 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 afterstart(). 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 afterstart().- 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
Removes the publisher for the given ComId.- Parameters:
comId- The ComID of the publisher to remove.- Returns:
- The removed publisher handle, or
nullif no publisher existed for this ComId.
-
removeSubscribers
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 beforestart().- Parameters:
enabled-trueto enable traffic shaping (default),falseto 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:
closein interfaceAutoCloseable
-