|
5 | 5 | import com.google.protobuf.Message; |
6 | 6 | import org.apache.log4j.Logger; |
7 | 7 | import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
8 | 9 | import xyz.gianlu.librespot.BytesArrayList; |
9 | 10 | import xyz.gianlu.librespot.common.ProtobufToJson; |
10 | 11 | import xyz.gianlu.librespot.common.Utils; |
@@ -64,8 +65,13 @@ public void unsubscribe(@NotNull String uri) throws IOException, PubSubException |
64 | 65 | @NotNull |
65 | 66 | public Response sendSync(@NotNull RawMercuryRequest request) throws IOException { |
66 | 67 | 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; |
69 | 75 | } |
70 | 76 |
|
71 | 77 | @NotNull |
@@ -113,7 +119,7 @@ public <P extends Message> void send(@NotNull ProtobufMercuryRequest<P> request, |
113 | 119 | } |
114 | 120 | } |
115 | 121 |
|
116 | | - public void send(@NotNull RawMercuryRequest request, @NotNull Callback callback) throws IOException { |
| 122 | + public int send(@NotNull RawMercuryRequest request, @NotNull Callback callback) throws IOException { |
117 | 123 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
118 | 124 | DataOutputStream out = new DataOutputStream(bytesOut); |
119 | 125 |
|
@@ -143,6 +149,7 @@ public void send(@NotNull RawMercuryRequest request, @NotNull Callback callback) |
143 | 149 | session.send(cmd, bytesOut.toByteArray()); |
144 | 150 |
|
145 | 151 | callbacks.put((long) seq, callback); |
| 152 | + return seq; |
146 | 153 | } |
147 | 154 |
|
148 | 155 | @Override |
@@ -225,10 +232,7 @@ public void interestedIn(@NotNull String uri, @NotNull SubListener listener) { |
225 | 232 |
|
226 | 233 | public void notInterested(@NotNull SubListener listener) { |
227 | 234 | 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); |
232 | 236 | } |
233 | 237 | } |
234 | 238 |
|
@@ -288,7 +292,7 @@ public void response(@NotNull Response response) { |
288 | 292 | } |
289 | 293 | } |
290 | 294 |
|
291 | | - @NotNull |
| 295 | + @Nullable |
292 | 296 | Response waitResponse() { |
293 | 297 | synchronized (reference) { |
294 | 298 | try { |
|
0 commit comments