Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Gateways: Bug fix for WebsocketIOClient to prevent channel wriite before handshake complete (#2502)

Rich Turnerb9f367a34854
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
index ddad132ad..53d20fe39 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
@@ -157,6 +157,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
protected int connectTimeout = 5000;
protected Supplier<ChannelHandler[]> encoderDecoderProvider;
protected Supplier<CompletableFuture<Void>> initFutureSupplier;
+ protected CompletableFuture<Void> connectFuture;
protected AbstractNettyIOClient() {
this.executorService = Container.EXECUTOR;
@@ -256,7 +257,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
}
LOG.fine("Connection attempt '" + (execution.getAttemptCount()+1) + "' for: " + getClientUri());
- CompletableFuture<Void> connectFuture = doConnect();
+ connectFuture = doConnect();
waitForConnectFuture(connectFuture);
execution.recordResult(null);
});
@@ -307,6 +308,13 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
return startFuture;
}
+ /**
+ * Simply returns {@link Channel#isActive} but can be overridden as needed
+ */
+ protected boolean isChannelReady() {
+ return channel != null && channel.isActive();
+ }
+
@Override
public void disconnect() {
synchronized (this) {
@@ -363,7 +371,10 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
@Override
public void sendMessage(T message) {
- if (channel == null) {
+ LOG.finest("Sending message to server: " + getClientUri());
+
+ if (!isChannelReady()) {
+ LOG.finer("Channel not ready, message not sent: " + getClientUri());
return;
}
@@ -446,6 +457,14 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
channel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+ if (!isChannelReady()) {
+ if (connectFuture != null) {
+ connectFuture.completeExceptionally(cause);
+ }
+ ctx.close();
+ return;
+ }
+
if (cause instanceof DecoderException decoderException) {
onDecodeException(ctx, decoderException);
} else if (cause instanceof EncoderException encoderException) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
index 2a91f8a22..a0192f329 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
@@ -154,6 +154,11 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
);
}
+ @Override
+ protected boolean isChannelReady() {
+ return super.isChannelReady() && handshakeFuture != null && handshakeFuture.isDone();
+ }
+
@Override
protected void addEncodersDecoders(Channel channel) throws Exception {
HttpHeaders hdrs = new DefaultHttpHeaders();
@@ -235,8 +240,8 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
protected void onHandshakeDone() {
if (handshakeFuture != null) {
+ LOG.finer("Handshake complete: " + getClientUri());
handshakeFuture.complete(null);
- handshakeFuture = null;
}
}
@@ -289,7 +294,7 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
if (pingFuture != null) {
pingFuture.cancel(false);
}
-
+ handshakeFuture = null;
super.doDisconnect();
}
@@ -305,7 +310,7 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
executorService.submit(() -> {
if (oAuthGrant != null) {
- LOG.finest("Retrieving OAuth access token: " + getClientUri());
+ LOG.finer("Retrieving OAuth access token: " + getClientUri());
try {
OAuthFilter oAuthFilter = new OAuthFilter(getClient(), oAuthGrant);
@@ -313,7 +318,7 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
if (TextUtil.isNullOrEmpty(authHeaderValue)) {
throw new RuntimeException("Returned access token is null");
}
- LOG.fine("Retrieved access token via OAuth: " + getClientUri());
+ LOG.finer("Retrieved access token via OAuth: " + getClientUri());
future.complete(authHeaderValue);
} catch (Exception e) {
future.completeExceptionally(new Exception("Error retrieving OAuth access token for '" + getClientUri() + "': " + e.getMessage()));
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java
index fe66ad1a5..8ebf9ea24 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java
@@ -138,7 +138,6 @@ public class GatewayClientConnector implements AutoCloseable {
this.activeTunnelSessions.forEach(GatewayTunnelSession::disconnect);
this.activeTunnelSessions.clear();
}
-
} catch (Exception e) {
LOG.log(Level.WARNING, "An exception occurred whilst trying to disconnect the gateway IO client", e);
} finally {
@@ -238,7 +237,7 @@ public class GatewayClientConnector implements AutoCloseable {
SharedEvent event = messageFromString(message, SharedEvent.MESSAGE_PREFIX, SharedEvent.class);
if (event == null) {
- LOG.finer("Received empty message from central manager: realm=" + connection.getLocalRealm());
+ LOG.warning("Received empty message from central manager: realm=" + connection);
return;
}
@@ -275,7 +274,7 @@ public class GatewayClientConnector implements AutoCloseable {
// The central manager sends N number of these to synchronise gateway descendant assets on the central manager
case ReadAssetsEvent readAssets -> {
if (gatewayAPIVersion == null) {
- LOG.finer("Pre version 1.0.0 central manager read assets request so closing all active tunnels: " + connection);
+ LOG.info("Pre version 1.0.0 central manager read assets request so closing all active tunnels: " + connection);
stopAllGatewayTunnels();
}
@@ -295,7 +294,7 @@ public class GatewayClientConnector implements AutoCloseable {
if (gatewayAPIVersion == null) {
// This is a legacy version of the openremote on the central instance so we won't get a GatewayInitialisedEvent
// so we mark this client as connected now
- LOG.fine("Central manager running an older version so assuming connection is initialised");
+ LOG.info("Central manager running an older version so assuming connection is initialised");
initFuture.complete(null);
}
@@ -371,13 +370,7 @@ public class GatewayClientConnector implements AutoCloseable {
Container.EXECUTOR.submit(() -> {
- if (activeTunnels == null || activeTunnels.length == 0) {
- LOG.fine("No gateway tunnel sessions on central instance so stopping any active sessions: " + connection);
- stopAllGatewayTunnels();
- return;
- }
-
- LOG.fine("Synchronising gateway tunnel sessions with central instance: " + connection);
+ LOG.info("Synchronising gateway tunnel sessions with central instance: " + connection);
// Synchronise the SSH sessions with what the central instance thinks are open
// Filter out any tunnels expiring in the next 5 seconds
@@ -389,6 +382,12 @@ public class GatewayClientConnector implements AutoCloseable {
return true;
}).toList();
+ if (tunnels.isEmpty()) {
+ LOG.finer("No gateway tunnel sessions on central instance so stopping any active sessions: " + connection);
+ stopAllGatewayTunnels();
+ return;
+ }
+
// Create any new tunnels currently not established
tunnels.forEach(activeTunnel -> {
GatewayTunnelSession activeSession = this.activeTunnelSessions.stream()
@@ -399,7 +398,7 @@ public class GatewayClientConnector implements AutoCloseable {
LOG.finer("Active tunnel session found for tunnel: " + activeTunnel);
} else {
// No existing session for this active tunnel
- LOG.fine("Active tunnel session not found for tunnel so starting: " + activeTunnel);
+ LOG.finer("Active tunnel session not found for tunnel so starting: " + activeTunnel);
startGatewayTunnel(activeTunnel);
}
});
@@ -408,7 +407,7 @@ public class GatewayClientConnector implements AutoCloseable {
this.activeTunnelSessions.removeIf(activeTunnelSession -> {
boolean obsolete = tunnels.stream().noneMatch(tunnel -> tunnel.equals(activeTunnelSession.getTunnelInfo()));
if (obsolete) {
- LOG.fine("Removing obsolete tunnel session: " + activeTunnelSession);
+ LOG.finer("Removing obsolete tunnel session: " + activeTunnelSession);
activeTunnelSession.disconnect();
}
return obsolete;
@@ -442,10 +441,11 @@ public class GatewayClientConnector implements AutoCloseable {
protected void sendCentralManagerMessage(SharedEvent event) {
if (client != null) {
- if (LOG.isLoggable(Level.FINEST)) {
- LOG.finest("Sending message to central manager: realm=" + connection.getLocalRealm() + ", " + event);
- } else {
- LOG.finer(() -> "Sending message to central manager: realm=" + connection.getLocalRealm() + ", " + event.getClass().getSimpleName());
+ boolean isAttributeEvent = event instanceof AttributeEvent;
+ if (isAttributeEvent && LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Sending attribute event to central manager: realm=" + connection.getLocalRealm() + ", " + event);
+ } else if (!isAttributeEvent) {
+ LOG.fine(() -> "Sending message to central manager: realm=" + connection.getLocalRealm() + ", " + event);
}
client.sendMessage(messageToString(SharedEvent.MESSAGE_PREFIX, event));
}
@@ -465,7 +465,7 @@ public class GatewayClientConnector implements AutoCloseable {
.orElse(null);
if (activeSession != null) {
- LOG.fine("Existing active tunnel session found for tunnel so returning: " + connection + ", " + tunnelInfo);
+ LOG.fine("Existing active tunnel session found for tunnel so returning it: " + connection + ", " + tunnelInfo);
return activeSession.connectFuture;
}
@@ -478,6 +478,7 @@ public class GatewayClientConnector implements AutoCloseable {
if (throwable != null) {
LOG.log(Level.WARNING, "Gateway tunnel session failed to connect: " + connection + ", " + tunnelInfo, throwable);
activeTunnelSessions.remove(session);
+ session.disconnect();
}
}
);
@@ -644,7 +645,7 @@ public class GatewayClientConnector implements AutoCloseable {
if (allowEvent && connection.getAssetSyncRules() != null) {
GatewayAssetSyncRule syncRule = connection.getAssetSyncRules().getOrDefault(ev.getAssetType(), connection.getAssetSyncRules().get("*"));
if (syncRule != null && syncRule.excludeAttributes != null && syncRule.excludeAttributes.contains(ev.getName())) {
- LOG.finer(() -> "Attribute event excluded due to sync rule: " + ev);
+ LOG.finest(() -> "Attribute event excluded due to sync rule: " + ev);
allowEvent = false;
}
}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
index 5ca970b0c..5079a56d0 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
@@ -260,6 +260,7 @@ public class GatewayConnector {
}
}
// Let the gateway know init is complete
+ LOG.info("Gateway fully initialised: " + getGatewayIdString());
sendMessageToGateway(new GatewayInitDoneEvent());
tunnellingSupported = response != null && response.isTunnelingSupported();
gatewayVersion = response != null ? response.getVersion() : null;
@@ -636,7 +637,7 @@ public class GatewayConnector {
}
protected void onInitialSyncComplete() {
- LOG.info("Initial sync complete: " + gatewayId);
+ LOG.fine("Asset sync complete: " + gatewayId);
initialSyncInProgress = false;
cachedAssetEvents.clear();
cachedAttributeEvents.clear();
diff --git a/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java
index d8351a4ba..db458e5a0 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java
@@ -89,13 +89,13 @@ public class MINAGatewayTunnelFactory implements GatewayTunnelFactory {
void stop() {
if (isRunning.compareAndSet(true, false)) {
- LOG.fine("Stopping session: " + this);
+ LOG.info("Stopping session: " + this);
ClientSession session = currentSession.getAndSet(null);
if (session != null) {
try {
session.close();
} catch (IOException e) {
- LOG.fine("Error closing session during tunnel stop msg=" + e.getMessage() + ": " + this);
+ LOG.warning("Error closing session during tunnel stop msg=" + e.getMessage() + ": " + this);
}
}
}
@@ -187,7 +187,7 @@ public class MINAGatewayTunnelFactory implements GatewayTunnelFactory {
initialFuture.complete(null);
session.addCloseFutureListener(f -> {
- LOG.info("Remote port forwarding closed: " + this);
+ LOG.finer("Remote port forwarding session closed: " + this);
if (isRunning.get()) {
handleFailure(null);
}
@@ -201,7 +201,7 @@ public class MINAGatewayTunnelFactory implements GatewayTunnelFactory {
if (wasInitialFailure) {
// If initial connection attempt failed, we stop completely.
- LOG.fine("Initial connect has failed so aborting: " + this);
+ LOG.info("Initial connect has failed so aborting: " + this);
isRunning.set(false);
} else if (isRunning.get()) {
// Future was already done (meaning previous success), so this is a drop. Retry.