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
Gateways: Check if tunnel already exists and do some cleanup of tunnels on timeout or error (#2485)
Rich Turner6f876930c7e3
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.javaindex 285b02e53..0f2c41e3d 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientConnector.java@@ -452,9 +452,31 @@ public class GatewayClientConnector implements AutoCloseable {LOG.info("Start tunnel request received: " + connection + ", " + tunnelInfo);+ // Check if there is an active tunnel session for this tunnel info already+ GatewayTunnelSession activeSession = this.activeTunnelSessions.stream()+ .filter(activeTunnelSession -> activeTunnelSession.getTunnelInfo().equals(tunnelInfo))+ .findFirst()+ .orElse(null);++ if (activeSession != null) {+ LOG.fine("Existing active tunnel session found for tunnel so returning: " + connection + ", " + tunnelInfo);+ return activeSession.connectFuture;+ }+GatewayTunnelSession session = tunnelFactory.createSession(tunnelHostname, tunnelPort, tunnelInfo, this::onTunnelSessionClosed);activeTunnelSessions.add(session);- return session.connectFuture.orTimeout(60000, TimeUnit.MILLISECONDS);++ // Do cleanup on timeout or error+ session.connectFuture.orTimeout(GatewayConnector.RESPONSE_TIMEOUT_MILLIS-1000, TimeUnit.MILLISECONDS).whenComplete(+ (unused, throwable) -> {+ if (throwable != null) {+ LOG.log(Level.WARNING, "Gateway tunnel session failed to connect: " + connection + ", " + tunnelInfo, throwable);+ activeTunnelSessions.remove(session);+ }+ }+ );++ return session.connectFuture;}protected String stopGatewayTunnel(GatewayTunnelStopRequestEvent stopRequestEvent) {diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.javaindex a482f5c0f..5ca970b0c 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java@@ -290,6 +290,7 @@ public class GatewayConnector {if (response != null && response.getError() != null) {future.completeExceptionally(new RuntimeException("Failed to start tunnel: error=" + response.getError() + ", " + tunnelInfo));} else {+ LOG.info("Tunnel started: " + tunnelInfo);future.complete(null);}});diff --git a/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.javaindex bd6ca4d46..d8351a4ba 100644--- a/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java+++ b/manager/src/main/java/org/openremote/manager/gateway/MINAGatewayTunnelFactory.java@@ -182,12 +182,12 @@ public class MINAGatewayTunnelFactory implements GatewayTunnelFactory {if (!initialFuture.isDone()) {LOG.info("Remote port forwarding started: " + this);} else {- LOG.fine("Remote port forwarding reconnected: " + this);+ LOG.info("Remote port forwarding reconnected: " + this);}initialFuture.complete(null);session.addCloseFutureListener(f -> {- LOG.fine("Remote port forwarding closed: " + this);+ LOG.info("Remote port forwarding 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.info("Initial connect has failed so aborting: " + this);+ LOG.fine("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.