Skip to content

Commit

Permalink
Dispatch client again whenever Spotify-Connection-Id changes
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Apr 7, 2020
1 parent 7118092 commit a3edb34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import xyz.gianlu.librespot.dealer.DealerClient;
import xyz.gianlu.librespot.dealer.DealerClient.RequestResult;
import xyz.gianlu.librespot.mercury.MercuryClient;
import xyz.gianlu.librespot.mercury.SubListener;
import xyz.gianlu.librespot.player.PlayerRunner;

import java.io.IOException;
Expand All @@ -26,7 +27,7 @@
/**
* @author Gianlu
*/
public final class DeviceStateHandler implements DealerClient.MessageListener, DealerClient.RequestListener {
public final class DeviceStateHandler implements DealerClient.MessageListener, DealerClient.RequestListener, SubListener {
private static final Logger LOGGER = Logger.getLogger(DeviceStateHandler.class);

static {
Expand Down Expand Up @@ -54,6 +55,7 @@ public DeviceStateHandler(@NotNull Session session) {

session.dealer().addMessageListener(this, "hm://pusher/v1/connections/", "hm://connect-state/v1/connect/volume", "hm://connect-state/v1/cluster");
session.dealer().addRequestListener(this, "hm://connect-state/v1/");
session.mercury().interestedIn("hm://pusher/v1/connections/", this);
}

@NotNull
Expand Down Expand Up @@ -116,14 +118,25 @@ private void notifyNotActive() {
}

@Override
public void onMessage(@NotNull String uri, @NotNull Map<String, String> headers, @NotNull String[] payloads) throws IOException {
if (uri.startsWith("hm://pusher/v1/connections/")) {
synchronized (this) {
connectionId = headers.get("Spotify-Connection-Id");
}
public void event(@NotNull MercuryClient.Response resp) {
if (resp.uri.startsWith("hm://pusher/v1/connections/")) {
int index = resp.uri.lastIndexOf('/');
updateConnectionId(resp.uri.substring(index + 1));
}
}

private synchronized void updateConnectionId(@NotNull String newer) {
if (connectionId == null || !connectionId.equals(newer)) {
connectionId = newer;
LOGGER.debug("Updated Spotify-Connection-Id: " + connectionId);
notifyReady();
}
}

@Override
public void onMessage(@NotNull String uri, @NotNull Map<String, String> headers, @NotNull String[] payloads) throws IOException {
if (uri.startsWith("hm://pusher/v1/connections/")) {
updateConnectionId(headers.get("Spotify-Connection-Id"));
} else if (Objects.equals(uri, "hm://connect-state/v1/connect/volume")) {
Connect.SetVolumeCommand cmd = Connect.SetVolumeCommand.parseFrom(BytesArrayList.streamBase64(payloads));
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ public void close() {
lastScheduledPing = null;
}

if (conn.get() == ConnectionHolder.this)
ConnectionHolder holder = conn.get();
if (holder == null) return;

if (holder == ConnectionHolder.this)
connectionInvalided();
else
LOGGER.debug(String.format("Did not dispatch connection invalidated: %s != %s", conn.get(), ConnectionHolder.this));
LOGGER.debug(String.format("Did not dispatch connection invalidated: %s != %s", holder, ConnectionHolder.this));
}

private class WebSocketListenerImpl extends WebSocketListener {
Expand Down

0 comments on commit a3edb34

Please sign in to comment.