Skip to content

Commit 4be25f7

Browse files
committed
Simplified reconnection logic + additional logging
1 parent c0d8fbb commit 4be25f7

File tree

3 files changed

+5
-23
lines changed

3 files changed

+5
-23
lines changed

core/src/main/java/xyz/gianlu/librespot/connectstate/DeviceStateHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ public void onMessage(@NotNull String uri, @NotNull Map<String, String> headers,
150150
}
151151
}
152152

153-
@Override
154-
public void onConnectionRestarted() {
155-
// Do nothing (already handled by StateWrapper)
156-
}
157-
158153
@NotNull
159154
@Override
160155
public RequestResult onRequest(@NotNull String mid, int pid, @NotNull String sender, @NotNull JsonObject command) {
@@ -205,7 +200,7 @@ private synchronized void putState(@NotNull Connect.PutStateReason reason, @NotN
205200
.getDeviceBuilder().setDeviceInfo(deviceInfo).setPlayerState(state);
206201

207202
session.api().putConnectState(connectionId, putState.build());
208-
LOGGER.info(String.format("Put state. {ts: %d, reason: %s, request: %s}", TimeProvider.currentTimeMillis(), reason, TextFormat.shortDebugString(putState)));
203+
LOGGER.info(String.format("Put state. {ts: %d, connId: %s[truncated], reason: %s, request: %s}", TimeProvider.currentTimeMillis(), connectionId.substring(0, 6), reason, TextFormat.shortDebugString(putState)));
209204
}
210205

211206
public synchronized int getVolume() {

core/src/main/java/xyz/gianlu/librespot/dealer/DealerClient.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,13 @@ private void connectionInvalided() {
196196

197197
try {
198198
connect();
199-
notifyConnectionRestarted();
200199
} catch (IOException | MercuryClient.MercuryException ex) {
201200
LOGGER.error("Failed reconnecting, retrying...", ex);
202201
connectionInvalided();
203202
}
204203
}, 10, TimeUnit.SECONDS);
205204
}
206205

207-
private void notifyConnectionRestarted() {
208-
synchronized (msgListeners) {
209-
for (MessageListener listener : msgListeners.keySet())
210-
listener.onConnectionRestarted();
211-
}
212-
}
213-
214206
public enum RequestResult {
215207
UNKNOWN_SEND_COMMAND_RESULT, SUCCESS,
216208
DEVICE_NOT_FOUND, CONTEXT_PLAYER_ERROR,
@@ -225,8 +217,6 @@ public interface RequestListener {
225217

226218
public interface MessageListener {
227219
void onMessage(@NotNull String uri, @NotNull Map<String, String> headers, @NotNull String[] payloads) throws IOException;
228-
229-
void onConnectionRestarted();
230220
}
231221

232222
private static class Looper implements Runnable, Closeable {
@@ -289,6 +279,8 @@ public void close() {
289279

290280
if (conn == ConnectionHolder.this)
291281
connectionInvalided();
282+
else
283+
LOGGER.debug(String.format("Did not dispatch connection invalidated: %s != %s", conn, ConnectionHolder.this));
292284
}
293285

294286
private class WebSocketListenerImpl extends WebSocketListener {
@@ -310,7 +302,7 @@ public void onOpen(@NotNull WebSocket ws, @NotNull Response response) {
310302

311303
if (!receivedPong) {
312304
LOGGER.warn("Did not receive ping in 3 seconds. Reconnecting...");
313-
close();
305+
ConnectionHolder.this.close();
314306
return;
315307
}
316308

@@ -346,7 +338,7 @@ public void onMessage(@NotNull WebSocket ws, @NotNull String text) {
346338
@Override
347339
public void onFailure(@NotNull WebSocket ws, @NotNull Throwable t, @Nullable Response response) {
348340
LOGGER.warn("An exception occurred. Reconnecting...", t);
349-
close();
341+
ConnectionHolder.this.close();
350342
}
351343
}
352344
}

core/src/main/java/xyz/gianlu/librespot/player/StateWrapper.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,6 @@ else if (op.getKind() == Playlist4ApiProto.Op.Kind.REM)
658658
}
659659
}
660660

661-
@Override
662-
public void onConnectionRestarted() {
663-
updated();
664-
}
665-
666661
private synchronized void performCollectionUpdate(@NotNull List<String> uris, boolean inCollection) {
667662
for (String uri : uris)
668663
tracksKeeper.updateMetadataFor(uri, "collection.in_collection", String.valueOf(inCollection));

0 commit comments

Comments
 (0)