From: Christian Wicke Date: Sat, 20 Jan 2024 13:28:52 +0000 (+0100) Subject: reconnect without loosing commands on broken idle connection (#16299) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=73402f691cfa6b77dbfcc9ba3b5aa555ece51a48;p=openhab-addons.git reconnect without loosing commands on broken idle connection (#16299) Signed-off-by: Christian Wicke --- diff --git a/bundles/org.openhab.binding.mpd/src/main/java/org/openhab/binding/mpd/internal/protocol/MPDConnectionThread.java b/bundles/org.openhab.binding.mpd/src/main/java/org/openhab/binding/mpd/internal/protocol/MPDConnectionThread.java index c94dd7cd26..ec57db4994 100644 --- a/bundles/org.openhab.binding.mpd/src/main/java/org/openhab/binding/mpd/internal/protocol/MPDConnectionThread.java +++ b/bundles/org.openhab.binding.mpd/src/main/java/org/openhab/binding/mpd/internal/protocol/MPDConnectionThread.java @@ -54,6 +54,7 @@ public class MPDConnectionThread extends Thread { private final List pendingCommands = new ArrayList<>(); private AtomicBoolean isInIdle = new AtomicBoolean(false); + private AtomicBoolean wakingUpFromIdle = new AtomicBoolean(false); private AtomicBoolean disposed = new AtomicBoolean(false); public MPDConnectionThread(MPDResponseListener listener, String address, Integer port, String password) { @@ -70,7 +71,6 @@ public class MPDConnectionThread extends Thread { while (!disposed.get()) { try { synchronized (pendingCommands) { - pendingCommands.clear(); pendingCommands.add(new MPDCommand("status")); pendingCommands.add(new MPDCommand("currentsong")); } @@ -92,7 +92,16 @@ public class MPDConnectionThread extends Thread { closeSocket(); if (!disposed.get()) { - sleep(RECONNECTION_TIMEOUT_SEC * 1000); + if (wakingUpFromIdle.compareAndSet(true, false)) { + logger.debug("reconnecting immediately and keeping pending commands"); + } else { + logger.debug("reconnecting in {} seconds and clearing pending commands...", + RECONNECTION_TIMEOUT_SEC); + sleep(RECONNECTION_TIMEOUT_SEC * 1000); + synchronized (pendingCommands) { + pendingCommands.clear(); + } + } } } } catch (InterruptedException ignore) { @@ -246,6 +255,7 @@ public class MPDConnectionThread extends Thread { private void sendCommand(MPDCommand command) throws IOException { logger.trace("send command '{}'", command); + wakingUpFromIdle.set("noidle".equals(command.getCommand())); final Socket socket = this.socket; if (socket != null) { String line = command.asLine();