From 5b3d639ea718ab792cc739afa688bef36ab1f611 Mon Sep 17 00:00:00 2001 From: Iscle Date: Mon, 7 Feb 2022 12:55:09 +0100 Subject: [PATCH 1/4] Annotate onPlaybackFailed Exception parameter as NotNull --- .../xyz/gianlu/librespot/api/handlers/EventsHandler.java | 2 +- .../src/main/java/xyz/gianlu/librespot/player/Player.java | 6 +++--- .../main/java/xyz/gianlu/librespot/player/ShellEvents.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java b/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java index 38b6078a..b470d6e2 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java @@ -91,7 +91,7 @@ public void onPlaybackResumed(@NotNull Player player, long trackTime) { } @Override - public void onPlaybackFailed(@NotNull Player player, Exception e) { + public void onPlaybackFailed(@NotNull Player player, @NotNull Exception e) { JsonObject obj = new JsonObject(); obj.addProperty("event", "playbackFailed"); obj.addProperty("exception", e.getMessage()); 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 824ba57f..83420e21 100644 --- a/player/src/main/java/xyz/gianlu/librespot/player/Player.java +++ b/player/src/main/java/xyz/gianlu/librespot/player/Player.java @@ -872,7 +872,7 @@ public interface EventsListener { void onPlaybackResumed(@NotNull Player player, long trackTime); - void onPlaybackFailed(@NotNull Player player, Exception e); + void onPlaybackFailed(@NotNull Player player, @NotNull Exception e); void onTrackSeeked(@NotNull Player player, long trackTime); @@ -1005,7 +1005,7 @@ public void onPlaybackResumed(@NotNull Player player, long trackTime) { } @Override - public void onPlaybackFailed(@NotNull Player player, Exception e) { + public void onPlaybackFailed(@NotNull Player player, @NotNull Exception e) { } @Override @@ -1082,7 +1082,7 @@ void playbackResumed() { executorService.execute(() -> l.onPlaybackResumed(Player.this, trackTime)); } - void playbackFailed(Exception ex) { + void playbackFailed(@NotNull Exception ex) { for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(() -> l.onPlaybackFailed(Player.this, ex)); } diff --git a/player/src/main/java/xyz/gianlu/librespot/player/ShellEvents.java b/player/src/main/java/xyz/gianlu/librespot/player/ShellEvents.java index 57529363..11ba1613 100644 --- a/player/src/main/java/xyz/gianlu/librespot/player/ShellEvents.java +++ b/player/src/main/java/xyz/gianlu/librespot/player/ShellEvents.java @@ -89,7 +89,7 @@ public void onPlaybackResumed(@NotNull Player player, long trackTime) { } @Override - public void onPlaybackFailed(@NotNull Player player, Exception e) { + public void onPlaybackFailed(@NotNull Player player, @NotNull Exception e) { exec(conf.onPlaybackFailed, "EXCEPTION=" + e.getClass().getCanonicalName(), "MESSAGE=" + e.getMessage()); } From 6ee3ba4c08d3b9472875f619b0490ecdf018b555 Mon Sep 17 00:00:00 2001 From: Iscle Date: Mon, 7 Feb 2022 13:22:52 +0100 Subject: [PATCH 2/4] Add Excepction class name and message to onPlaybackFailed json event --- .../java/xyz/gianlu/librespot/api/handlers/EventsHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java b/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java index b470d6e2..3f88b3ac 100644 --- a/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java +++ b/api/src/main/java/xyz/gianlu/librespot/api/handlers/EventsHandler.java @@ -94,7 +94,8 @@ public void onPlaybackResumed(@NotNull Player player, long trackTime) { public void onPlaybackFailed(@NotNull Player player, @NotNull Exception e) { JsonObject obj = new JsonObject(); obj.addProperty("event", "playbackFailed"); - obj.addProperty("exception", e.getMessage()); + obj.addProperty("exception", e.getClass().getCanonicalName()); + obj.addProperty("message", e.getMessage()); dispatch(obj); } From 078a1966018941b25d0a841ba64b17d4bfbe4730 Mon Sep 17 00:00:00 2001 From: Iscle Date: Mon, 7 Feb 2022 13:23:18 +0100 Subject: [PATCH 3/4] Remove redundant try-with-resources block --- .../xyz/gianlu/librespot/core/ApResolver.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/src/main/java/xyz/gianlu/librespot/core/ApResolver.java b/lib/src/main/java/xyz/gianlu/librespot/core/ApResolver.java index a2b7dd65..133d8922 100644 --- a/lib/src/main/java/xyz/gianlu/librespot/core/ApResolver.java +++ b/lib/src/main/java/xyz/gianlu/librespot/core/ApResolver.java @@ -85,20 +85,18 @@ private void request(@NotNull String... types) throws IOException { try (Response response = client.newCall(request).execute()) { ResponseBody body = response.body(); if (body == null) throw new IOException("No body"); - try (Reader reader = body.charStream()) { - JsonObject obj = JsonParser.parseReader(reader).getAsJsonObject(); - HashMap> map = new HashMap<>(); - for (String type : types) - map.put(type, getUrls(obj, type)); - - synchronized (pool) { - pool.putAll(map); - poolReady = true; - pool.notifyAll(); - } + JsonObject obj = JsonParser.parseReader(body.charStream()).getAsJsonObject(); + HashMap> map = new HashMap<>(); + for (String type : types) + map.put(type, getUrls(obj, type)); - LOGGER.info("Loaded aps into pool: " + pool); + synchronized (pool) { + pool.putAll(map); + poolReady = true; + pool.notifyAll(); } + + LOGGER.info("Loaded aps into pool: " + pool); } } From 09569b75df0323f1267d1dc7636a989a1e9ca0d9 Mon Sep 17 00:00:00 2001 From: Iscle Date: Tue, 8 Feb 2022 09:50:50 +0100 Subject: [PATCH 4/4] Refresh the AP pool after stopping the receiver Before, we tried to refresh the AP pool before stopping the receiver. If the AP pool refresh failed it would throw an exception and the receiver would not be stopped, making it trying to access a null pointer afterwards. This should fix #447. --- lib/src/main/java/xyz/gianlu/librespot/core/Session.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/xyz/gianlu/librespot/core/Session.java b/lib/src/main/java/xyz/gianlu/librespot/core/Session.java index 664167b7..534b4686 100644 --- a/lib/src/main/java/xyz/gianlu/librespot/core/Session.java +++ b/lib/src/main/java/xyz/gianlu/librespot/core/Session.java @@ -718,13 +718,13 @@ private void reconnect() { } try { - apResolver.refreshPool(); - if (conn != null) { - conn.socket.close(); receiver.stop(); + conn.socket.close(); } + apResolver.refreshPool(); + conn = ConnectionHolder.create(apResolver.getRandomAccesspoint(), inner.conf); connect(); authenticatePartial(Authentication.LoginCredentials.newBuilder()