Skip to content

Commit

Permalink
~ access to underlying ChannelFsm
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Sep 26, 2024
1 parent 080ca2c commit 58193fb
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
}

Expand Down Expand Up @@ -114,6 +123,18 @@ public boolean isConnected() {
return channelFsm.getState() == State.Connected;
}

/**
* Get the {@link ChannelFsm} used by this transport.
*
* <p>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.
*
Expand Down

0 comments on commit 58193fb

Please sign in to comment.