From b288bd1785579af05ee4042e76cef0f801298998 Mon Sep 17 00:00:00 2001 From: Gianlu Date: Fri, 15 Jan 2021 11:51:36 +0100 Subject: [PATCH] Couple of fixes in PlayerWrapper and SessionWrapper + run closing async --- .../main/java/xyz/gianlu/librespot/api/ApiServer.java | 8 ++++++-- .../java/xyz/gianlu/librespot/api/PlayerApiServer.java | 3 +++ .../java/xyz/gianlu/librespot/api/PlayerWrapper.java | 4 ++-- .../java/xyz/gianlu/librespot/api/SessionWrapper.java | 10 +++++++++- .../gianlu/librespot/api/handlers/InstanceHandler.java | 4 ++-- .../main/java/xyz/gianlu/librespot/player/Player.java | 2 ++ 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java b/api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java index a76cfedb..382aa1dc 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java @@ -13,6 +13,7 @@ public class ApiServer { private static final Logger LOGGER = LogManager.getLogger(ApiServer.class); protected final RoutingHandler handler; protected final EventsHandler events = new EventsHandler(); + private final SessionWrapper wrapper; private final int port; private final String host; private Undertow undertow = null; @@ -20,6 +21,7 @@ public class ApiServer { public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper) { this.port = port; this.host = host; + this.wrapper = wrapper; this.handler = new RoutingHandler() .post("/metadata/{type}/{uri}", new MetadataHandler(wrapper, true)) .post("/metadata/{uri}", new MetadataHandler(wrapper, false)) @@ -44,11 +46,13 @@ public void start() { } public void stop() { + wrapper.clear(); + if (undertow != null) { undertow.stop(); undertow = null; - } - LOGGER.info("Server stopped!"); + LOGGER.info("Server stopped!"); + } } } diff --git a/api/src/main/java/xyz/gianlu/librespot/api/PlayerApiServer.java b/api/src/main/java/xyz/gianlu/librespot/api/PlayerApiServer.java index 36dbccb2..282683c7 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/PlayerApiServer.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/PlayerApiServer.java @@ -8,8 +8,11 @@ * @author devgianlu */ public class PlayerApiServer extends ApiServer { + private final PlayerWrapper wrapper; + public PlayerApiServer(int port, @NotNull String host, @NotNull PlayerWrapper wrapper) { super(port, host, wrapper); + this.wrapper = wrapper; handler.post("/player/{cmd}", new PlayerHandler(wrapper)); handler.post("/instance/{action}", InstanceHandler.forPlayer(this, wrapper)); // Overrides session only handler diff --git a/api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java b/api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java index 50710bd3..c807026e 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java @@ -81,13 +81,13 @@ protected void set(@NotNull Session session) { @Override protected void clear() { - super.clear(); - Player old = playerRef.get(); if (old != null) old.close(); playerRef.set(null); if (listener != null && old != null) listener.onPlayerCleared(old); + + super.clear(); } @Nullable diff --git a/api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java b/api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java index c43765a6..f61a56eb 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java @@ -5,6 +5,7 @@ import xyz.gianlu.librespot.ZeroconfServer; import xyz.gianlu.librespot.core.Session; +import java.io.IOException; import java.util.concurrent.atomic.AtomicReference; /** @@ -70,7 +71,14 @@ protected void set(@NotNull Session session) { protected void clear() { Session old = sessionRef.get(); sessionRef.set(null); - if (listener != null && old != null) listener.onSessionCleared(old); + if (old != null) { + try { + old.close(); + } catch (IOException ignored) { + } + + if (listener != null) listener.onSessionCleared(old); + } } @Nullable diff --git a/api/src/main/java/xyz/gianlu/librespot/api/handlers/InstanceHandler.java b/api/src/main/java/xyz/gianlu/librespot/api/handlers/InstanceHandler.java index b94fe3ff..04d17c69 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/handlers/InstanceHandler.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/handlers/InstanceHandler.java @@ -60,7 +60,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess switch (action) { case "terminate": - server.stop(); + new Thread(server::stop).start(); break; case "close": session.close(); @@ -93,7 +93,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess switch (action) { case "terminate": - server.stop(); + new Thread(server::stop).start(); break; case "close": player.close(); diff --git a/player/src/main/java/xyz/gianlu/librespot/player/Player.java b/player/src/main/java/xyz/gianlu/librespot/player/Player.java index 526175fd..d037691c 100644 --- a/player/src/main/java/xyz/gianlu/librespot/player/Player.java +++ b/player/src/main/java/xyz/gianlu/librespot/player/Player.java @@ -829,6 +829,8 @@ public void close() { scheduler.shutdown(); events.close(); + + LOGGER.info("Closed player."); } public interface EventsListener {