From 03ec47271bd434f46272f472d56dd7956c9ed6a5 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Mon, 29 Jun 2020 07:50:13 +0200 Subject: [PATCH 1/4] [lgwebos] Improved WebSocket reconnect/disconnect Related to #8027 Signed-off-by: Laurent Garnier --- .../lgwebos/internal/LGWebOSHandlerFactory.java | 10 ++++++---- .../lgwebos/internal/handler/LGWebOSHandler.java | 6 ++++-- .../lgwebos/internal/handler/LGWebOSTVSocket.java | 9 ++++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java index 945d896b84bfc..af49355adaf28 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java @@ -95,10 +95,12 @@ protected void activate(ComponentContext componentContext) { @Override protected void deactivate(ComponentContext componentContext) { super.deactivate(componentContext); - try { - this.webSocketClient.stop(); - } catch (Exception e) { - logger.warn("Unable to to stop websocket client.", e); + if (this.webSocketClient.isStarted()) { + try { + this.webSocketClient.stop(); + } catch (Exception e) { + logger.warn("Unable to to stop websocket client.", e); + } } } } diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java index 71437142569a6..22e613279b4c7 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java @@ -168,8 +168,10 @@ public void dispose() { private void startReconnectJob() { ScheduledFuture job = reconnectJob; if (job == null || job.isCancelled()) { - reconnectJob = scheduler.scheduleWithFixedDelay(() -> getSocket().connect(), - RECONNECT_START_UP_DELAY_SECONDS, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS); + reconnectJob = scheduler.scheduleWithFixedDelay(() -> { + getSocket().disconnect(); + getSocket().connect(); + }, RECONNECT_START_UP_DELAY_SECONDS, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS); } } diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java index 70b0f8f953dfb..1658f7551d2fc 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java @@ -48,6 +48,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -127,6 +128,7 @@ public enum State { private State state = State.DISCONNECTED; private @Nullable Session session; + private @Nullable Future sessionFuture; private @Nullable WebOSTVSocketListener listener; /** @@ -176,7 +178,7 @@ public void clearRequests() { public void connect() { try { - this.client.connect(this, this.destUri); + sessionFuture = this.client.connect(this, this.destUri); logger.debug("Connecting to: {}", this.destUri); } catch (IOException e) { logger.debug("Unable to connect.", e); @@ -185,6 +187,11 @@ public void connect() { public void disconnect() { Optional.ofNullable(this.session).ifPresent(s -> s.close()); + this.session = null; + Future future = sessionFuture; + if (future != null && !future.isDone()) { + future.cancel(true); + } stopDisconnectingJob(); setState(State.DISCONNECTED); } From f31b4f7bee1d0a07d35f26e661509cfe8f758ba0 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sat, 4 Jul 2020 16:55:35 +0200 Subject: [PATCH 2/4] Review comments considered Signed-off-by: Laurent Garnier --- .../lgwebos/internal/LGWebOSHandlerFactory.java | 10 ++++------ .../lgwebos/internal/handler/LGWebOSTVSocket.java | 1 - 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java index af49355adaf28..945d896b84bfc 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java @@ -95,12 +95,10 @@ protected void activate(ComponentContext componentContext) { @Override protected void deactivate(ComponentContext componentContext) { super.deactivate(componentContext); - if (this.webSocketClient.isStarted()) { - try { - this.webSocketClient.stop(); - } catch (Exception e) { - logger.warn("Unable to to stop websocket client.", e); - } + try { + this.webSocketClient.stop(); + } catch (Exception e) { + logger.warn("Unable to to stop websocket client.", e); } } } diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java index 1658f7551d2fc..a619750f6505a 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java @@ -187,7 +187,6 @@ public void connect() { public void disconnect() { Optional.ofNullable(this.session).ifPresent(s -> s.close()); - this.session = null; Future future = sessionFuture; if (future != null && !future.isDone()) { future.cancel(true); From 62a1f151dcd15543208583f73e7a80f16b1cf78f Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sun, 5 Jul 2020 12:35:41 +0200 Subject: [PATCH 3/4] Destroy WebSocket client after stopping it Signed-off-by: Laurent Garnier --- .../openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java | 1 + 1 file changed, 1 insertion(+) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java index 945d896b84bfc..5e9aee4042b76 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java @@ -97,6 +97,7 @@ protected void deactivate(ComponentContext componentContext) { super.deactivate(componentContext); try { this.webSocketClient.stop(); + this.webSocketClient.destroy(); } catch (Exception e) { logger.warn("Unable to to stop websocket client.", e); } From 0bea83345548211751a99ba9fd46105a9dabcd4f Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Fri, 10 Jul 2020 08:42:51 +0200 Subject: [PATCH 4/4] Do not destroy the WebSocket client Signed-off-by: Laurent Garnier --- .../openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java | 1 - 1 file changed, 1 deletion(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java index 5e9aee4042b76..945d896b84bfc 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/LGWebOSHandlerFactory.java @@ -97,7 +97,6 @@ protected void deactivate(ComponentContext componentContext) { super.deactivate(componentContext); try { this.webSocketClient.stop(); - this.webSocketClient.destroy(); } catch (Exception e) { logger.warn("Unable to to stop websocket client.", e); }