This is an automated email from the ASF dual-hosted git repository. cdutz pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit f87bd6b686a15fc0e27abc573cc252aeaa97d6e0 Author: Christofer Dutz <[email protected]> AuthorDate: Sun Jan 11 17:25:48 2026 +0100 refactor: Refactored the ConnectionStateListener to allow reporting more types of events. --- RELEASE_NOTES | 6 +++++ .../java/api/listener/ConnectionStateListener.java | 6 ++--- .../PlcConnectionStateChangedEvent.java} | 28 +++++++++++++++------- .../ConnectionStateChangeType.java} | 21 +++++++++------- .../spi/connection/DefaultNettyPlcConnection.java | 10 ++++++-- .../SingleProtocolStackConfigurerTest.java | 16 +++++++++---- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/RELEASE_NOTES b/RELEASE_NOTES index f47f4b739f..73c3ffadbc 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -23,6 +23,12 @@ Incompatible changes - Updated the signature of the PlcBrowseRequestInterceptor to also accept a queryName, query and item instead of just an item. +- The ConnectionStateListener interface was updated to no longer + have a connected() and disconnected() method, but use a + onConnectionStateChanged method instead that accepts + PlcConnectionStateChangedEvent events which have many more + state change options beyond a simple connected and disconnected + event. Bug Fixes --------- diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java index 43352e904f..93982f1dbd 100644 --- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java +++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java @@ -18,13 +18,13 @@ */ package org.apache.plc4x.java.api.listener; +import org.apache.plc4x.java.api.model.PlcConnectionStateChangedEvent; + /** * Additional helper for tracking connection state. */ public interface ConnectionStateListener extends EventListener { - void connected(); - - void disconnected(); + void onConnectionStateChanged(PlcConnectionStateChangedEvent event); } diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcConnectionStateChangedEvent.java similarity index 55% copy from plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java copy to plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcConnectionStateChangedEvent.java index 43352e904f..19ea46d45a 100644 --- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java +++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/model/PlcConnectionStateChangedEvent.java @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -16,15 +16,27 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.plc4x.java.api.listener; -/** - * Additional helper for tracking connection state. - */ -public interface ConnectionStateListener extends EventListener { +package org.apache.plc4x.java.api.model; + +import org.apache.plc4x.java.api.types.ConnectionStateChangeType; + +public class PlcConnectionStateChangedEvent { + + private final ConnectionStateChangeType changeType; + private final String details; + + public PlcConnectionStateChangedEvent(ConnectionStateChangeType changeType, String details) { + this.changeType = changeType; + this.details = details; + } - void connected(); + public ConnectionStateChangeType getChangeType() { + return changeType; + } - void disconnected(); + public String getDetails() { + return details; + } } diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/ConnectionStateChangeType.java similarity index 51% copy from plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java copy to plc4j/api/src/main/java/org/apache/plc4x/java/api/types/ConnectionStateChangeType.java index 43352e904f..8319930b38 100644 --- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/listener/ConnectionStateListener.java +++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/ConnectionStateChangeType.java @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -16,15 +16,20 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.plc4x.java.api.listener; -/** - * Additional helper for tracking connection state. - */ -public interface ConnectionStateListener extends EventListener { +package org.apache.plc4x.java.api.types; - void connected(); +public enum ConnectionStateChangeType { + // Connection lifecycle + CONNECTED, // Connection established successfully + DISCONNECTED, // Graceful disconnection (close() called) + CONNECTION_LOST, // Unexpected disconnection (network error, timeout, etc.) - void disconnected(); + // Tag/metadata changes + TAGS_CHANGED, // Available tags changed, re-browse needed + // PLC mode changes + MODE_RUN, // PLC in RUN mode (executing program) + MODE_STOP, // PLC in STOP mode (program halted) + MODE_CONFIG // PLC in CONFIG/PROGRAM mode (being programmed) } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java index de73b83385..8cc242d688 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java @@ -22,6 +22,8 @@ import io.netty.channel.*; import java.util.concurrent.RejectedExecutionException; import org.apache.plc4x.java.api.EventPlcConnection; import org.apache.plc4x.java.api.authentication.PlcAuthentication; +import org.apache.plc4x.java.api.model.PlcConnectionStateChangedEvent; +import org.apache.plc4x.java.api.types.ConnectionStateChangeType; import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; import org.apache.plc4x.java.api.exceptions.PlcConnectionException; import org.apache.plc4x.java.api.exceptions.PlcIoException; @@ -238,10 +240,14 @@ public class DefaultNettyPlcConnection extends AbstractPlcConnection implements .map(ConnectionStateListener.class::cast); if (evt instanceof ConnectedEvent) { sessionSetupCompleteFuture.complete(null); - eventListeners.forEach(ConnectionStateListener::connected); + eventListeners.forEach(connectionStateListener -> + connectionStateListener.onConnectionStateChanged( + new PlcConnectionStateChangedEvent(ConnectionStateChangeType.CONNECTED, "Connected"))); } else if (evt instanceof DisconnectedEvent) { sessionDisconnectCompleteFuture.complete(null); - eventListeners.forEach(ConnectionStateListener::disconnected); + eventListeners.forEach(connectionStateListener -> + connectionStateListener.onConnectionStateChanged( + new PlcConnectionStateChangedEvent(ConnectionStateChangeType.DISCONNECTED, "Disconnected"))); // Fix for https://github.com/apache/plc4x/issues/801 super.userEventTriggered(ctx, evt); } else if (evt instanceof DiscoveredEvent) { diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurerTest.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurerTest.java index 0a75a0cf67..15dfe18c28 100644 --- a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurerTest.java +++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/SingleProtocolStackConfigurerTest.java @@ -20,6 +20,7 @@ package org.apache.plc4x.java.spi.connection; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -56,10 +57,12 @@ class SingleProtocolStackConfigurerTest { connection.addEventListener(connectionListener); connection.connect(); - verify(connectionListener).connected(); + // TODO: Update this to validate the type of event ... + verify(connectionListener).onConnectionStateChanged(any()); connection.close(); - verify(connectionListener).disconnected(); + // TODO: Update this to validate the type of event ... + verify(connectionListener).onConnectionStateChanged(any()); } @Test @@ -74,15 +77,18 @@ class SingleProtocolStackConfigurerTest { connection.addEventListener(connectionListener); connection.connect(); - verify(connectionListener).connected(); + // TODO: Update this to validate the type of event ... + verify(connectionListener).onConnectionStateChanged(any()); // append listener after connection been made ConnectionStateListener dynamicListener = mock(ConnectionStateListener.class); connection.addEventListener(dynamicListener); connection.close(); - verify(connectionListener).disconnected(); - verify(dynamicListener).disconnected(); + // TODO: Update this to validate the type of event ... + verify(connectionListener).onConnectionStateChanged(any()); + // TODO: Update this to validate the type of event ... + verify(dynamicListener).onConnectionStateChanged(any()); } @Test
