From 58193fbec89244ed4363d6ef40d70cfe362329c2 Mon Sep 17 00:00:00 2001 From: Kevin Herron Date: Thu, 26 Sep 2024 03:15:29 -0700 Subject: [PATCH] ~ access to underlying ChannelFsm --- .../client/NettyTcpClientTransport.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/modbus-tcp/src/main/java/com/digitalpetri/modbus/client/NettyTcpClientTransport.java b/modbus-tcp/src/main/java/com/digitalpetri/modbus/client/NettyTcpClientTransport.java index e6ad303..a7af0d9 100644 --- a/modbus-tcp/src/main/java/com/digitalpetri/modbus/client/NettyTcpClientTransport.java +++ b/modbus-tcp/src/main/java/com/digitalpetri/modbus/client/NettyTcpClientTransport.java @@ -6,6 +6,7 @@ import com.digitalpetri.modbus.internal.util.ExecutionQueue; import com.digitalpetri.netty.fsm.ChannelActions; import com.digitalpetri.netty.fsm.ChannelFsm; +import com.digitalpetri.netty.fsm.ChannelFsm.TransitionListener; import com.digitalpetri.netty.fsm.ChannelFsmConfig; import com.digitalpetri.netty.fsm.ChannelFsmFactory; import com.digitalpetri.netty.fsm.Event; @@ -64,16 +65,24 @@ public NettyTcpClientTransport(NettyClientTransportConfig config) { (from, to, via) -> { logger.debug("onStateTransition: {} -> {} via {}", from, to, via); - executionQueue.submit(() -> handleStateTransition(from, to, via)); + maybeNotifyConnectionListeners(from, to); } ); } - private void handleStateTransition(State from, State to, Event via) { + private void maybeNotifyConnectionListeners(State from, State to) { + if (connectionListeners.isEmpty()) { + return; + } + if (from != State.Connected && to == State.Connected) { - connectionListeners.forEach(ConnectionListener::onConnection); + executionQueue.submit(() -> + connectionListeners.forEach(ConnectionListener::onConnection) + ); } else if (from == State.Connected && to != State.Connected) { - connectionListeners.forEach(ConnectionListener::onConnectionLost); + executionQueue.submit(() -> + connectionListeners.forEach(ConnectionListener::onConnectionLost) + ); } } @@ -114,6 +123,18 @@ public boolean isConnected() { return channelFsm.getState() == State.Connected; } + /** + * Get the {@link ChannelFsm} used by this transport. + * + *

This should not generally be used by client code except perhaps to add a + * {@link TransitionListener} to receive more detailed callbacks about the connection status. + * + * @return the {@link ChannelFsm} used by this transport. + */ + public ChannelFsm getChannelFsm() { + return channelFsm; + } + /** * Add a {@link ConnectionListener} to this transport. *