Skip to content

Commit 1e8255b

Browse files
committed
Updated MercuryClient to throw exceptions for timed out requests
1 parent 9c2fb72 commit 1e8255b

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/core/TokenProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public String get(@NotNull String scope) throws IOException, MercuryClient.Mercu
3434
else return token.accessToken;
3535
}
3636

37-
LOGGER.debug(String.format("Token expired or not suitable, requesting again. {scope: %s, token: %s}", scope, token));
37+
LOGGER.debug(String.format("Token expired or not suitable, requesting again. {scope: %s, oldToken: %s}", scope, token));
3838
MercuryRequests.KeymasterToken resp = session.mercury().sendSync(MercuryRequests.requestToken(session.deviceId(), scope));
3939
token = new StoredToken(resp);
4040

41+
LOGGER.debug(String.format("Updated token successfully! {scope: %s, newToken: %s}", scope, token));
42+
4143
tokens.put(scope, token);
4244
return token.accessToken;
4345
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class DealerClient implements Closeable {
3636
private final Map<MessageListener, List<String>> msgListeners = new HashMap<>();
3737
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NameThreadFactory((r) -> "dealer-scheduler-" + r.hashCode()));
3838
private final AtomicReference<ConnectionHolder> conn = new AtomicReference<>();
39-
private ScheduledFuture lastScheduledReconnection;
39+
private ScheduledFuture<?> lastScheduledReconnection;
4040

4141
public DealerClient(@NotNull Session session) {
4242
this.session = session;
@@ -249,7 +249,7 @@ private class ConnectionHolder implements Closeable {
249249
private final WebSocket ws;
250250
private boolean closed = false;
251251
private boolean receivedPong = false;
252-
private ScheduledFuture lastScheduledPing;
252+
private ScheduledFuture<?> lastScheduledPing;
253253

254254
ConnectionHolder(@NotNull Session session, @NotNull Request request) {
255255
ws = session.client().newWebSocket(request, new WebSocketListenerImpl());

core/src/main/java/xyz/gianlu/librespot/mercury/MercuryClient.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.protobuf.Message;
66
import org.apache.log4j.Logger;
77
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
89
import xyz.gianlu.librespot.BytesArrayList;
910
import xyz.gianlu.librespot.common.ProtobufToJson;
1011
import xyz.gianlu.librespot.common.Utils;
@@ -64,8 +65,13 @@ public void unsubscribe(@NotNull String uri) throws IOException, PubSubException
6465
@NotNull
6566
public Response sendSync(@NotNull RawMercuryRequest request) throws IOException {
6667
SyncCallback callback = new SyncCallback();
67-
send(request, callback);
68-
return callback.waitResponse();
68+
int seq = send(request, callback);
69+
70+
Response resp = callback.waitResponse();
71+
if (resp == null)
72+
throw new IOException(String.format("Request timeout out, %d passed, yet no response. {seq: %d}", MERCURY_REQUEST_TIMEOUT, seq));
73+
74+
return resp;
6975
}
7076

7177
@NotNull
@@ -113,7 +119,7 @@ public <P extends Message> void send(@NotNull ProtobufMercuryRequest<P> request,
113119
}
114120
}
115121

116-
public void send(@NotNull RawMercuryRequest request, @NotNull Callback callback) throws IOException {
122+
public int send(@NotNull RawMercuryRequest request, @NotNull Callback callback) throws IOException {
117123
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
118124
DataOutputStream out = new DataOutputStream(bytesOut);
119125

@@ -143,6 +149,7 @@ public void send(@NotNull RawMercuryRequest request, @NotNull Callback callback)
143149
session.send(cmd, bytesOut.toByteArray());
144150

145151
callbacks.put((long) seq, callback);
152+
return seq;
146153
}
147154

148155
@Override
@@ -225,10 +232,7 @@ public void interestedIn(@NotNull String uri, @NotNull SubListener listener) {
225232

226233
public void notInterested(@NotNull SubListener listener) {
227234
synchronized (subscriptions) {
228-
Iterator<InternalSubListener> iter = subscriptions.iterator();
229-
while (iter.hasNext())
230-
if (iter.next().listener == listener)
231-
iter.remove();
235+
subscriptions.removeIf(internalSubListener -> internalSubListener.listener == listener);
232236
}
233237
}
234238

@@ -288,7 +292,7 @@ public void response(@NotNull Response response) {
288292
}
289293
}
290294

291-
@NotNull
295+
@Nullable
292296
Response waitResponse() {
293297
synchronized (reference) {
294298
try {

0 commit comments

Comments
 (0)