Repository navigation
Expand file tree
/
Copy pathRealtimeConnection.java
More file actions
585 lines (529 loc) · 19.9 KB
/
Copy pathRealtimeConnection.java
File metadata and controls
585 lines (529 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package com.qoder.cloudagents.forward.realtime;
import com.qoder.cloudagents.core.*;
import com.qoder.cloudagents.core.internal.*;
import com.qoder.cloudagents.errors.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import okhttp3.*;
import okio.ByteString;
/**
* One connection, with a single receive consumer and serialized sends. Close explicitly after use.
* Async continuations run on SDK/transport threads: use thenAcceptAsync for blocking application
* work.
*/
public final class RealtimeConnection implements AutoCloseable {
private final ClientEngine engine;
private final RealtimeConnectParams params;
private final CompletableFuture<RealtimeConnection> connected = new CompletableFuture<>();
private final Deque<Frame> inbound = new ArrayDeque<>();
private final Deque<Send> outbound = new ArrayDeque<>();
private long inboundBytes, outboundBytes;
private WebSocket socket;
private CompletableFuture<WebSocket> opening;
private CompletableFuture<RealtimeEvent> receiving;
private CompletableFuture<RealtimeCloseResult> closingResult;
private ScheduledFuture<?> stageTimer, receiveTimer, closeTimer, sendPoll;
private Runnable unwatch = () -> {};
private RealtimeReady ready;
private ApiResponse<Void> handshake;
private RuntimeException failure;
private boolean terminated, closing;
private Integer closeCode;
private String closeReason, closeId;
private RealtimeConnection(ClientEngine engine, RealtimeConnectParams params) {
this.engine = engine;
this.params = params;
}
static CompletableFuture<RealtimeConnection> open(
ClientEngine engine, RealtimeConnectParams params, RequestOptions options) {
if (params == null || options == null)
throw new ConfigurationException("Parameters and options must not be null");
if (!options.query().isEmpty())
throw new ConfigurationException("Realtime connection only accepts conversation_id");
RealtimeConnection connection = new RealtimeConnection(engine, params);
connection.start(options);
return connection.connected;
}
private synchronized void start(RequestOptions options) {
connected.whenComplete(
(v, e) -> {
if (connected.isCancelled()) close();
});
unwatch = engine.watchStream(this::fail, options.totalTimeout());
if (terminated) {
unwatch.run();
return;
}
Duration timeout = options.timeout() == null ? params.handshakeTimeout() : options.timeout();
stageTimer =
schedule(
() -> {
synchronized (this) {
if (handshake == null)
fail(new ApiTimeoutException("Realtime handshake timed out"));
}
},
timeout);
if (terminated) return;
opening =
engine.webSocket(
params.conversationId(), options, timeout, params.sendTimeout(), listener());
opening.whenComplete(
(ws, error) -> {
synchronized (this) {
if (error != null) {
if (!terminated) fail(asRuntime(error));
} else if (terminated) ws.cancel();
else socket = ws;
}
});
}
public String conversationId() {
return params.conversationId();
}
public synchronized String requestId() {
return handshake == null ? null : handshake.requestId();
}
public synchronized ApiResponse<Void> handshakeResponse() {
return handshake;
}
public synchronized RealtimeReady ready() {
return ready;
}
public synchronized boolean isClosed() {
return terminated;
}
public synchronized Integer closeCode() {
return closeCode;
}
public synchronized String closeReason() {
return closeReason;
}
/** Returns null after a normal close; buffered events are delivered before terminal failures. */
public RealtimeEvent receive() {
return Futures.await(receiveAsync());
}
public RealtimeEvent receive(Duration timeout) {
return Futures.await(receiveAsync(timeout));
}
public CompletableFuture<RealtimeEvent> receiveAsync() {
return receiveAsync(null);
}
/** Timeout/cancellation affects this receive only. The connection remains usable. */
public synchronized CompletableFuture<RealtimeEvent> receiveAsync(Duration timeout) {
if (timeout != null) RequestOptions.positiveMillis(timeout);
if (receiving != null)
throw new ConfigurationException("Only one Realtime receive may be pending");
if (!inbound.isEmpty()) {
Frame frame = inbound.removeFirst();
inboundBytes -= frame.bytes;
return CompletableFuture.completedFuture(frame.event);
}
if (terminated)
return failure == null ? CompletableFuture.completedFuture(null) : Futures.failed(failure);
CompletableFuture<RealtimeEvent> result = new CompletableFuture<>();
receiving = result;
if (timeout != null)
receiveTimer =
schedule(
() -> {
synchronized (this) {
if (receiving == result) {
receiving = null;
receiveTimer = null;
result.completeExceptionally(
new ApiTimeoutException("Realtime receive timed out"));
}
}
},
timeout);
result.whenComplete(
(v, e) -> {
synchronized (this) {
if (receiving == result) {
receiving = null;
cancel(receiveTimer);
receiveTimer = null;
}
}
});
return result;
}
public void sendText(String text) {
Futures.await(sendTextAsync(text));
}
public CompletableFuture<Void> sendTextAsync(String text) {
text = RealtimeValidation.trim(text);
if (text == null
|| text.isEmpty()
|| !RealtimeValidation.validUnicode(text)
|| text.getBytes(StandardCharsets.UTF_8).length > 16384)
throw new ConfigurationException("Realtime text must be nonempty and at most 16 KiB");
return sendEventAsync("text.message", JsonValue.of(Collections.singletonMap("text", text)));
}
/** Raw PCM16 little-endian, 16 kHz, mono. No WAV/container headers. */
public void appendAudio(byte[] pcm) {
Futures.await(appendAudioAsync(pcm));
}
public CompletableFuture<Void> appendAudioAsync(byte[] pcm) {
if (pcm == null || pcm.length == 0 || pcm.length % 2 != 0)
throw new ConfigurationException("Audio must contain complete PCM16 samples");
if (pcm.length > 196560)
throw new ConfigurationException("Audio chunk exceeds the Realtime event limit");
return sendEventAsync(
"audio.append",
JsonValue.of(Collections.singletonMap("audio", Base64.getEncoder().encodeToString(pcm))));
}
public void interrupt() {
Futures.await(interruptAsync());
}
public CompletableFuture<Void> interruptAsync() {
return sendEventAsync("interrupt", JsonValue.of(Collections.emptyMap()));
}
public void ping() {
Futures.await(pingAsync());
}
public CompletableFuture<Void> pingAsync() {
return sendEventAsync("ping", JsonValue.of(Collections.emptyMap()));
}
public void playbackStarted(String workId, String announcementId) {
Futures.await(playbackStartedAsync(workId, announcementId));
}
public void playbackEnded(String workId, String announcementId) {
Futures.await(playbackEndedAsync(workId, announcementId));
}
public void playbackCancelled(String workId, String announcementId) {
Futures.await(playbackCancelledAsync(workId, announcementId));
}
public CompletableFuture<Void> playbackStartedAsync(String workId, String announcementId) {
return playback("playback.started", workId, announcementId);
}
public CompletableFuture<Void> playbackEndedAsync(String workId, String announcementId) {
return playback("playback.ended", workId, announcementId);
}
public CompletableFuture<Void> playbackCancelledAsync(String workId, String announcementId) {
return playback("playback.cancelled", workId, announcementId);
}
private CompletableFuture<Void> playback(String type, String workId, String announcementId) {
Map<String, String> payload = new LinkedHashMap<>();
if (workId != null) payload.put("work_id", workId);
if (announcementId != null) payload.put("announcement_id", announcementId);
return sendEventAsync(type, JsonValue.of(payload));
}
public void sendEvent(String type, JsonValue payload) {
Futures.await(sendEventAsync(type, payload));
}
/**
* Sends JSON text frames. Completion means transport write completion, not server
* acknowledgement.
*/
public synchronized CompletableFuture<Void> sendEventAsync(String type, JsonValue payload) {
return enqueue(type, payload, false);
}
private CompletableFuture<Void> enqueue(String type, JsonValue payload, boolean closeEvent) {
if (terminated)
return Futures.failed(
failure == null ? new ApiUserAbortException("Realtime connection closed") : failure);
if (ready == null || (closing && !closeEvent))
return Futures.failed(
new ConfigurationException("Realtime connection is not accepting input"));
RealtimeValidation.text(type, "event type", 128);
if (payload == null || !payload.isObject())
throw new ConfigurationException("Event payload must be an object");
Map<String, Object> frame = new LinkedHashMap<>();
frame.put("version", RealtimeEvent.VERSION);
frame.put("type", type);
frame.put("payload", payload);
String json = JsonValue.of(frame).toJson();
int bytes = json.getBytes(StandardCharsets.UTF_8).length;
if (bytes > 262144) throw new ConfigurationException("Realtime event exceeds 256 KiB");
if (outbound.size() >= params.queueCapacity() || outboundBytes + bytes > params.queueBytes())
return Futures.failed(problem("Realtime send queue is full", null));
Send send = new Send(json, bytes);
outbound.addLast(send);
outboundBytes += bytes;
send.deadline =
schedule(
() -> {
synchronized (this) {
if (!outbound.contains(send)) return;
if (send.started)
fail(new ApiTimeoutException("Realtime send timed out; delivery is uncertain"));
else {
remove(send);
send.result.completeExceptionally(
new ApiTimeoutException("Realtime send queue timed out"));
}
}
},
params.sendTimeout());
send.result.whenComplete(
(v, e) -> {
synchronized (this) {
if (send.result.isCancelled() && outbound.contains(send)) {
if (send.started)
fail(new ApiUserAbortException("Realtime send cancelled; delivery is uncertain"));
else remove(send);
}
}
});
pump();
return send.result;
}
private void pump() {
if (terminated || outbound.isEmpty()) return;
Send send = outbound.peekFirst();
if (!send.started) {
send.started = true;
if (!socket.send(send.json)) {
fail(problem("Realtime transport rejected a send", null));
return;
}
}
if (sendPoll == null)
sendPoll =
scheduleMillis(
() -> {
synchronized (this) {
sendPoll = null;
if (terminated) return;
Send current = outbound.peekFirst();
if (current != null && current.started && socket.queueSize() == 0) {
remove(current);
current.result.complete(null);
}
pump();
}
},
5);
}
private void remove(Send send) {
if (outbound.remove(send)) outboundBytes -= send.bytes;
cancel(send.deadline);
}
public RealtimeCloseResult closeGracefully() {
return Futures.await(closeGracefullyAsync());
}
/** Waits for connection.closed with this request ID before releasing the transport. */
public synchronized CompletableFuture<RealtimeCloseResult> closeGracefullyAsync() {
if (closingResult != null) return closingResult;
if (terminated)
return Futures.failed(
failure == null
? new ApiUserAbortException("Realtime connection already closed")
: failure);
if (ready == null || !Boolean.TRUE.equals(ready.capabilities().gracefulClose()))
return Futures.failed(new ConfigurationException("Server does not support graceful close"));
closing = true;
closeId = UUID.randomUUID().toString();
closingResult = new CompletableFuture<>();
closeTimer =
schedule(
() -> fail(new ApiTimeoutException("Realtime close acknowledgement timed out")),
params.closeTimeout());
closingResult.whenComplete(
(v, e) -> {
if (closingResult.isCancelled()) close();
});
enqueue("connection.close", JsonValue.of(Collections.singletonMap("request_id", closeId)), true)
.whenComplete(
(v, e) -> {
if (e != null) fail(asRuntime(e));
});
return closingResult;
}
/** Releases the connection immediately, without claiming that pending output was saved. */
@Override
public synchronized void close() {
terminate(null);
}
private synchronized void fail(RuntimeException error) {
terminate(error);
}
private void terminate(RuntimeException error) {
if (terminated) return;
terminated = true;
failure = error;
cancel(stageTimer);
cancel(receiveTimer);
cancel(closeTimer);
cancel(sendPoll);
unwatch.run();
if (opening != null && !opening.isDone()) opening.cancel(true);
if (socket != null) socket.cancel();
RuntimeException pendingError =
error == null ? new ApiUserAbortException("Realtime connection closed") : error;
connected.completeExceptionally(pendingError);
if (closingResult != null && !closingResult.isDone())
closingResult.completeExceptionally(pendingError);
List<Send> sends = new ArrayList<>(outbound);
outbound.clear();
outboundBytes = 0;
for (Send send : sends) {
cancel(send.deadline);
send.result.completeExceptionally(pendingError);
}
if (receiving != null) {
CompletableFuture<RealtimeEvent> target = receiving;
receiving = null;
if (error == null) target.complete(null);
else target.completeExceptionally(error);
}
}
private ScheduledFuture<?> schedule(Runnable action, Duration duration) {
return scheduleMillis(action, RequestOptions.positiveMillis(duration));
}
private ScheduledFuture<?> scheduleMillis(Runnable action, long millis) {
try {
return engine.schedule(action, millis);
} catch (RejectedExecutionException e) {
fail(new ApiUserAbortException("Client closed"));
return null;
}
}
private static void cancel(ScheduledFuture<?> task) {
if (task != null) task.cancel(false);
}
private RuntimeException asRuntime(Throwable e) {
Throwable cause = Futures.unwrap(e);
return cause instanceof RuntimeException
? (RuntimeException) cause
: new RealtimeException(
"Realtime connection failed",
conversationId(),
requestId(),
closeCode,
closeReason,
null,
cause);
}
private RealtimeException problem(String message, JsonValue event) {
return new RealtimeException(
message, conversationId(), requestId(), closeCode, closeReason, event);
}
private WebSocketListener listener() {
return new WebSocketListener() {
@Override
public void onOpen(WebSocket ws, Response response) {
synchronized (RealtimeConnection.this) {
if (terminated) {
ws.cancel();
return;
}
socket = ws;
handshake = new ApiResponse<>(null, response.code(), response.headers().toMultimap());
cancel(stageTimer);
stageTimer =
schedule(
() -> {
synchronized (RealtimeConnection.this) {
if (ready == null)
fail(new ApiTimeoutException("Realtime voice.ready timed out"));
}
},
params.readyTimeout());
}
}
@Override
public void onMessage(WebSocket ws, String text) {
synchronized (RealtimeConnection.this) {
if (terminated) return;
try {
int bytes = text.getBytes(StandardCharsets.UTF_8).length;
if (bytes > params.readLimit())
throw problem("Realtime message exceeds read limit", null);
RealtimeEvent event = RealtimeEvent.fromJson(JsonValue.parse(text));
if (!event.conversationId().equals(conversationId()))
throw problem("Realtime conversation ID mismatch", null);
if (event.fatal() || (ready == null && event.error() != null)) {
fail(problem("Realtime server rejected the connection", event.toJsonValue()));
return;
}
boolean firstReady = ready == null && event.ready() != null;
if (firstReady) {
ready = event.ready();
cancel(stageTimer);
}
boolean delivered = false;
if (receiving != null) {
CompletableFuture<RealtimeEvent> target = receiving;
receiving = null;
cancel(receiveTimer);
receiveTimer = null;
delivered = target.complete(event);
}
if (!delivered) {
if (inbound.size() >= params.queueCapacity()
|| inboundBytes + bytes > params.queueBytes())
throw problem("Realtime receive queue is full", null);
inbound.addLast(new Frame(event, bytes));
inboundBytes += bytes;
}
if (firstReady) connected.complete(RealtimeConnection.this);
if (closingResult != null
&& event.closed() != null
&& closeId.equals(event.closed().requestId())) {
closingResult.complete(event.closed());
terminate(null);
}
} catch (RuntimeException e) {
fail(e);
}
}
}
@Override
public void onMessage(WebSocket ws, ByteString bytes) {
fail(problem("Expected a Realtime JSON text frame", null));
}
@Override
public void onClosing(WebSocket ws, int code, String reason) {
synchronized (RealtimeConnection.this) {
closeCode = code;
closeReason = reason;
ws.close(1000, "");
if (code == 1000 && ready != null && !closing) terminate(null);
else fail(problem("Realtime closed before completion", null));
}
}
@Override
public void onClosed(WebSocket ws, int code, String reason) {
synchronized (RealtimeConnection.this) {
closeCode = code;
closeReason = reason;
if (!terminated) terminate(null);
}
}
@Override
public void onFailure(WebSocket ws, Throwable error, Response response) {
synchronized (RealtimeConnection.this) {
if (terminated) return;
if (response != null && handshake == null)
handshake = new ApiResponse<>(null, response.code(), response.headers().toMultimap());
if (closeCode == null) closeCode = 1006;
fail(asRuntime(error));
}
}
};
}
private static final class Frame {
final RealtimeEvent event;
final int bytes;
Frame(RealtimeEvent event, int bytes) {
this.event = event;
this.bytes = bytes;
}
}
private static final class Send {
final String json;
final int bytes;
final CompletableFuture<Void> result = new CompletableFuture<>();
boolean started;
ScheduledFuture<?> deadline;
Send(String json, int bytes) {
this.json = json;
this.bytes = bytes;
}
}
}