100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Various gateway connection and tunnelling improvements (#1787)
Rich Turner3872165879c5
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.javaindex 19450b4a2..5e095071e 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java@@ -421,6 +421,10 @@ public class GatewayClientService extends RouteBuilder implements ContainerServiprotected void onGatewayClientConnectionStatusChanged(GatewayConnection connection, ConnectionStatus connectionStatus) {LOG.info("Connection status change for gateway IO client '" + connectionStatus + "': " + connection);clientEventService.publishEvent(new GatewayConnectionStatusEvent(timerService.getCurrentTimeMillis(), connection.getLocalRealm(), connectionStatus));+ if (gatewayTunnelFactory != null) {+ LOG.finer("Terminating all gateway tunnel sessions");+ gatewayTunnelFactory.stopAll();+ }}protected void onCentralManagerMessage(GatewayConnection connection, String message) {@@ -442,6 +446,9 @@ public class GatewayClientService extends RouteBuilder implements ContainerServimessageToString(SharedEvent.MESSAGE_PREFIX, responseEvent));} else if (event instanceof GatewayTunnelStartRequestEvent gatewayTunnelStartRequestEvent) {+ if (gatewayTunnelFactory == null) {+ return;+ }LOG.info("Start tunnel request received: " + gatewayTunnelStartRequestEvent);String error = null;@@ -458,6 +465,9 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi);} else if (event instanceof GatewayTunnelStopRequestEvent stopRequestEvent) {+ if (gatewayTunnelFactory == null) {+ return;+ }LOG.info("Stop tunnel request received: " + stopRequestEvent);String error = null;diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.javaindex 7991e8178..a3d298f4f 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java@@ -172,6 +172,7 @@ public class GatewayConnector {this.sessionId.set(null);}+ requestDisconnect.run();LOG.fine("Gateway connector disconnected: " + this);if (syncProcessorFuture != null) {LOG.finest("Aborting active sync process: " + this);@@ -189,10 +190,9 @@ public class GatewayConnector {protected void disconnect(GatewayDisconnectEvent.Reason reason) {synchronized (this.sessionId) {if (isConnected()) {- if (disabled) {+ if (!disabled) {sendMessageToGateway(new GatewayDisconnectEvent(reason));}- requestDisconnect.run();disconnected(getSessionId());}}@@ -302,6 +302,8 @@ public class GatewayConnector {});}+ sendMessageToGateway(new GatewayTunnelStopRequestEvent(tunnelInfo));+return future.orTimeout(RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).whenComplete((result, ex) -> {@@ -402,7 +404,7 @@ public class GatewayConnector {protected boolean syncAborted() {if (syncErrors == MAX_SYNC_RETRIES) {LOG.warning("Gateway sync max retries reached so disconnecting the gateway: " + this);- requestDisconnect.run();+ disconnect(GatewayDisconnectEvent.Reason.SYNC_ERROR);return true;}diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.javaindex fbda20837..5edc7c0b2 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java@@ -23,6 +23,7 @@ package org.openremote.manager.gateway;import org.openremote.agent.protocol.websocket.WebsocketIOClient;import org.openremote.model.auth.OAuthGrant;import org.openremote.model.gateway.GatewayCapabilitiesRequestEvent;+import org.openremote.model.gateway.GatewayDisconnectEvent;import org.openremote.model.syslog.SyslogCategory;import java.net.URI;@@ -69,7 +70,11 @@ public class GatewayIOClient extends WebsocketIOClient<String> {.orTimeout(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS).handle((result, ex) -> {syncFuture = null;- if (ex instanceof TimeoutException) {+ if (ex instanceof TimeoutException && !channel.isOpen()) {+ // Channel could have been closed whilst waiting for sync+ LOG.info("Channel has been closed unexpectedly during sync");+ throw new RuntimeException("Channel has been closed unexpectedly during sync");+ } else if (ex instanceof TimeoutException) {LOG.finest("Timeout reached whilst waiting for sync complete event");} else if (ex != null) {throw new RuntimeException(ex.getMessage());@@ -97,6 +102,10 @@ public class GatewayIOClient extends WebsocketIOClient<String> {syncFuture.complete(null);}+ if (syncFuture != null && message.contains(GatewayDisconnectEvent.TYPE)) {+ LOG.finest("Gateway disconnect event received during sync: " + message);+ syncFuture.completeExceptionally(new RuntimeException("Gateway disconnect event received during sync"));+ }super.onMessageReceived(message);}}diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.javaindex 4d82fbd64..8145584ac 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java@@ -663,8 +663,9 @@ public class GatewayService extends RouteBuilder implements ContainerService {}connector.connected(sessionId, createConnectorMessageConsumer(sessionId), () -> {- clientEventService.closeWebsocketSession(sessionId);- tunnelInfos.values().removeIf(tunnelInfo -> tunnelInfo.getGatewayId().equals(gatewayId));+ try {+ clientEventService.closeWebsocketSession(sessionId);+ } catch (Exception ignored) {}});}@@ -672,8 +673,13 @@ public class GatewayService extends RouteBuilder implements ContainerService {String gatewayId = getGatewayIdFromClientId(gatewayClientId);GatewayConnector connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));- if (connector != null) {- connector.disconnected(sessionId);+ try {+ if (connector != null) {+ connector.disconnected(sessionId);+ }+ } finally {+ // Assume all gateway tunnels have been terminated+ tunnelInfos.values().removeIf(tunnelInfo -> tunnelInfo.getGatewayId().equalsIgnoreCase(gatewayId));}}diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.javaindex 3fe3646d7..54a4b1ce6 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java@@ -38,4 +38,10 @@ public interface GatewayTunnelFactory {void startTunnel(GatewayTunnelStartRequestEvent startRequestEvent) throws Exception;void stopTunnel(GatewayTunnelInfo tunnelInfo) throws Exception;++ /**+ * Terminate all known tunnel sessions; should perform try/catch to prevent any exceptions bubbling and to ensure+ * termination of each session is attempted.+ */+ void stopAll();}diff --git a/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.javaindex 23d94b81b..d1c79d9a3 100644--- a/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java+++ b/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java@@ -77,4 +77,18 @@ public class JSchGatewayTunnelFactory implements GatewayTunnelFactory {session.disconnect();}}++ public void stopAll() {+ try {+ sessionMap.values()+ .forEach(session -> {+ try {+ session.disconnect();+ } catch (Exception ignored) {+ }+ });+ } finally {+ sessionMap.clear();+ }+ }}diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.javaindex c35a57f0f..c988c69b7 100644--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java@@ -49,7 +49,7 @@ import org.openremote.model.alarm.AlarmEvent;@JsonSubTypes.Type(value = RequestSimulatorState.class, name = "request-simulator-state"),@JsonSubTypes.Type(value = RulesEngineStatusEvent.class, name = "rules-engine-status"),@JsonSubTypes.Type(value = RulesetChangedEvent.class, name = "ruleset-changed"),- @JsonSubTypes.Type(value = GatewayDisconnectEvent.class, name = "gateway-disconnect"),+ @JsonSubTypes.Type(value = GatewayDisconnectEvent.class, name = GatewayDisconnectEvent.TYPE),@JsonSubTypes.Type(value = GatewayConnectionStatusEvent.class, name = "gateway-connection-status"),@JsonSubTypes.Type(value = GatewayCapabilitiesRequestEvent.class, name = GatewayCapabilitiesRequestEvent.TYPE),@JsonSubTypes.Type(value = GatewayCapabilitiesResponseEvent.class, name = GatewayCapabilitiesResponseEvent.TYPE),diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.javaindex c041ddbdb..e8ff9da94 100644--- a/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java+++ b/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java@@ -32,6 +32,8 @@ import java.util.Date;*/public class GatewayDisconnectEvent extends SharedEvent {+ public static final String TYPE = "gateway-disconnect";+public enum Reason {TERMINATING,DISABLED,diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.ymlindex f0dd9d243..0e9abfd87 100644--- a/profile/dev-proxy.yml+++ b/profile/dev-proxy.yml@@ -21,6 +21,9 @@ services:service: proxyenvironment:MANAGER_HOST: 'host.docker.internal'+ # Uncomment to use sish in development+ #SISH_HOST: sish+ #SISH_PORT: 8090keycloak:extends:@@ -50,3 +53,25 @@ services:# Access directly if needed on localhostports:- "5432:5432"++# Uncomment to use sish in development tunnels won't be usable without wildcard dns but the start/stop tunnel logic can be tested+# sish:+# image: antoniomika/sish:latest+# depends_on:+# - proxy+# healthcheck:+# test: ["CMD", "/app/app", "-v"]+# ports:+# - "2222:2222"+# - "9000-9002:9000-9002"+# command: |+# --ssh-address=:2222+# --http-address=:8090+# --https=false+# --verify-ssl=false+# --idle-connection=false+# --port-bind-range="9000-9002"+# --bind-random-ports=false+# --bind-random-subdomains=false+# --force-requested-subdomains=true+# restart: always