-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractWebSocketChannel.java
476 lines (441 loc) · 21.2 KB
/
AbstractWebSocketChannel.java
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
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.exchange.api.websocket.channel;
import static io.gravitee.exchange.api.command.CommandStatus.ERROR;
import io.gravitee.exchange.api.channel.Channel;
import io.gravitee.exchange.api.channel.exception.ChannelClosedException;
import io.gravitee.exchange.api.channel.exception.ChannelInactiveException;
import io.gravitee.exchange.api.channel.exception.ChannelInitializationException;
import io.gravitee.exchange.api.channel.exception.ChannelNoReplyException;
import io.gravitee.exchange.api.channel.exception.ChannelReplyException;
import io.gravitee.exchange.api.channel.exception.ChannelTimeoutException;
import io.gravitee.exchange.api.channel.exception.ChannelUnknownCommandException;
import io.gravitee.exchange.api.command.Command;
import io.gravitee.exchange.api.command.CommandAdapter;
import io.gravitee.exchange.api.command.CommandHandler;
import io.gravitee.exchange.api.command.CommandStatus;
import io.gravitee.exchange.api.command.Payload;
import io.gravitee.exchange.api.command.Reply;
import io.gravitee.exchange.api.command.ReplyAdapter;
import io.gravitee.exchange.api.command.goodbye.GoodByeCommand;
import io.gravitee.exchange.api.command.goodbye.GoodByeCommandPayload;
import io.gravitee.exchange.api.command.hello.HelloCommand;
import io.gravitee.exchange.api.command.hello.HelloReply;
import io.gravitee.exchange.api.command.hello.HelloReplyPayload;
import io.gravitee.exchange.api.command.noreply.NoReply;
import io.gravitee.exchange.api.command.unknown.UnknownCommandHandler;
import io.gravitee.exchange.api.command.unknown.UnknownReply;
import io.gravitee.exchange.api.websocket.protocol.ProtocolAdapter;
import io.gravitee.exchange.api.websocket.protocol.ProtocolExchange;
import io.gravitee.exchange.api.websocket.protocol.legacy.ignored.IgnoredReply;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.CompletableEmitter;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleEmitter;
import io.vertx.rxjava3.core.Vertx;
import io.vertx.rxjava3.core.buffer.Buffer;
import io.vertx.rxjava3.core.http.WebSocketBase;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
/**
* @author Guillaume LAMIRAND (guillaume.lamirand at graviteesource.com)
* @author GraviteeSource Team
*/
@Slf4j
public abstract class AbstractWebSocketChannel implements Channel {
private static final int PING_DELAY = 5_000;
protected final String id = UUID.randomUUID().toString();
protected final Map<String, CommandHandler<? extends Command<?>, ? extends Reply<?>>> commandHandlers = new ConcurrentHashMap<>();
protected final Map<String, CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> commandAdapters =
new ConcurrentHashMap<>();
protected final Map<String, ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> replyAdapters = new ConcurrentHashMap<>();
protected final Vertx vertx;
protected final WebSocketBase webSocket;
protected final ProtocolAdapter protocolAdapter;
protected String targetId;
protected final Map<String, SingleEmitter<? extends Reply<?>>> resultEmitters = new ConcurrentHashMap<>();
protected boolean active;
private long pingTaskId = -1;
protected AbstractWebSocketChannel(
final List<CommandHandler<? extends Command<?>, ? extends Reply<?>>> commandHandlers,
final List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> commandAdapters,
final List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> replyAdapters,
final Vertx vertx,
final WebSocketBase webSocket,
final ProtocolAdapter protocolAdapter
) {
this.addCommandHandlers(commandHandlers);
this.addCommandHandlers(List.of(new UnknownCommandHandler()));
this.addCommandHandlers(protocolAdapter.commandHandlers());
this.addCommandAdapters(commandAdapters);
this.addCommandAdapters(protocolAdapter.commandAdapters());
this.addReplyAdapters(replyAdapters);
this.addReplyAdapters(protocolAdapter.replyAdapters());
this.vertx = vertx;
this.webSocket = webSocket;
this.protocolAdapter = protocolAdapter;
}
@Override
public String id() {
return id;
}
@Override
public String targetId() {
return targetId;
}
@Override
public boolean isActive() {
return this.active;
}
@Override
public Completable initialize() {
return Completable
.create(emitter -> {
webSocket.closeHandler(v -> {
log.warn("Channel '{}' for target '{}' is closing", id, targetId);
active = false;
cleanChannel();
});
webSocket.pongHandler(buffer -> log.debug("Receiving pong frame from channel '{}' for target '{}'", id, targetId));
webSocket.textMessageHandler(buffer -> webSocket.close((short) 1003, "Unsupported text frame").subscribe());
webSocket.binaryMessageHandler(buffer -> {
if (buffer.length() > 0) {
ProtocolExchange websocketExchange = protocolAdapter.read(buffer);
try {
if (ProtocolExchange.Type.COMMAND == websocketExchange.type()) {
receiveCommand(emitter, websocketExchange.asCommand());
} else if (ProtocolExchange.Type.REPLY == websocketExchange.type()) {
receiveReply(websocketExchange.asReply());
} else {
webSocket.close((short) 1002, "Exchange message unknown").subscribe();
}
} catch (Exception e) {
log.warn(
String.format(
"An error occurred when trying to decode incoming websocket exchange [%s]. Closing Socket.",
websocketExchange
),
e
);
webSocket.close((short) 1011, "Unexpected error while handling incoming websocket exchange").subscribe();
}
}
});
if (!expectHelloCommand()) {
this.active = true;
emitter.onComplete();
}
})
.doOnComplete(() -> log.debug("Channel '{}' for target '{}' has been successfully initialized", id, targetId))
.doOnError(throwable -> log.error("Unable to initialize channel '{}' for target '{}'", id, targetId));
}
private <C extends Command<?>> void receiveCommand(final CompletableEmitter emitter, final C command) {
if (command == null) {
webSocket.close((short) 1002, "Unrecognized incoming exchange").subscribe();
emitter.onError(new ChannelUnknownCommandException("Unrecognized incoming exchange"));
return;
}
Single<? extends Command<?>> commandObs;
CommandAdapter<Command<?>, Command<?>, Reply<?>> commandAdapter =
(CommandAdapter<Command<?>, Command<?>, Reply<?>>) commandAdapters.get(command.getType());
if (commandAdapter != null) {
commandObs = commandAdapter.adapt(command);
} else {
commandObs = Single.just(command);
}
commandObs
.flatMapCompletable(adaptedCommand -> {
CommandHandler<Command<?>, Reply<?>> commandHandler = (CommandHandler<Command<?>, Reply<?>>) commandHandlers.get(
adaptedCommand.getType()
);
if (expectHelloCommand() && !active && !Objects.equals(adaptedCommand.getType(), HelloCommand.COMMAND_TYPE)) {
webSocket.close((short) 1002, "Hello Command is first expected to initialize the exchange channel").subscribe();
emitter.onError(new ChannelInitializationException("Hello Command is first expected to initialize the channel"));
} else if (Objects.equals(adaptedCommand.getType(), HelloCommand.COMMAND_TYPE)) {
return handleHelloCommand(emitter, adaptedCommand, commandHandler);
} else if (Objects.equals(adaptedCommand.getType(), GoodByeCommand.COMMAND_TYPE)) {
return handleGoodByeCommand(adaptedCommand, commandHandler);
} else if (commandHandler != null) {
return handleCommandAsync(adaptedCommand, commandHandler);
} else {
log.info("No handler found for command type {}. Ignoring", adaptedCommand.getType());
return writeReply(
new NoReply(
adaptedCommand.getId(),
"No handler found for command type %s. Ignoring".formatted(adaptedCommand.getType())
)
);
}
return Completable.complete();
})
.subscribe();
}
protected abstract boolean expectHelloCommand();
private void receiveReply(final Reply<?> reply) {
SingleEmitter<? extends Reply<?>> replyEmitter = resultEmitters.remove(reply.getCommandId());
if (replyEmitter != null) {
Single<? extends Reply<?>> replyObs;
ReplyAdapter<Reply<?>, Reply<?>> replyAdapter = (ReplyAdapter<Reply<?>, Reply<?>>) replyAdapters.get(reply.getType());
if (replyAdapter != null) {
replyObs = replyAdapter.adapt(reply);
} else {
replyObs = Single.just(reply);
}
replyObs
.doOnSuccess(adaptedReply -> {
if (adaptedReply instanceof UnknownReply) {
replyEmitter.onError(new ChannelUnknownCommandException(adaptedReply.getErrorDetails()));
} else if (adaptedReply instanceof NoReply || adaptedReply instanceof IgnoredReply) {
replyEmitter.onError(new ChannelNoReplyException(adaptedReply.getErrorDetails()));
} else {
((SingleEmitter<Reply<?>>) replyEmitter).onSuccess(adaptedReply);
}
if (adaptedReply.stopOnErrorStatus() && adaptedReply.getCommandStatus() == ERROR) {
webSocket.close().subscribe();
}
})
.doOnError(throwable -> {
log.warn("Unable to handle reply [{}, {}]", reply.getType(), reply.getCommandId());
replyEmitter.onError(new ChannelReplyException(throwable));
})
.subscribe();
}
}
@Override
public Completable close() {
return Completable.fromRunnable(() -> {
webSocket.close((short) 1000).subscribe();
this.cleanChannel();
});
}
protected void cleanChannel() {
this.active = false;
this.resultEmitters.forEach((type, emitter) -> {
if (!emitter.isDisposed()) {
emitter.onError(new ChannelClosedException());
}
});
this.resultEmitters.clear();
if (pingTaskId != -1) {
this.vertx.cancelTimer(this.pingTaskId);
this.pingTaskId = -1;
}
if (webSocket != null && !webSocket.isClosed()) {
this.webSocket.close((short) 1011).subscribe();
}
}
/**
* Method call to handle initialize command type
*/
protected Completable handleHelloCommand(
final CompletableEmitter emitter,
final Command<?> command,
final CommandHandler<Command<?>, Reply<?>> commandHandler
) {
if (commandHandler != null) {
return handleCommand(command, commandHandler, false)
.doOnSuccess(reply -> {
if (reply.getCommandStatus() == CommandStatus.SUCCEEDED) {
Payload payload = reply.getPayload();
if (payload instanceof HelloReplyPayload helloReplyPayload) {
this.targetId = helloReplyPayload.getTargetId();
this.active = true;
startPingTask();
emitter.onComplete();
} else {
emitter.onError(new ChannelInitializationException("Unable to parse hello reply payload"));
}
}
})
.ignoreElement();
} else {
return Completable.fromRunnable(() -> {
startPingTask();
emitter.onComplete();
});
}
}
private void startPingTask() {
this.pingTaskId =
this.vertx.setPeriodic(
PING_DELAY,
timerId -> {
if (!this.webSocket.isClosed()) {
this.webSocket.writePing(Buffer.buffer()).subscribe();
}
}
);
}
/**
* Method call to handle custom command type
*/
protected Completable handleGoodByeCommand(final Command<?> command, final CommandHandler<Command<?>, Reply<?>> commandHandler) {
if (commandHandler != null) {
return handleCommand(command, commandHandler, true)
.doOnSuccess(reply -> {
if (reply.getCommandStatus() == CommandStatus.SUCCEEDED) {
Payload payload = command.getPayload();
if (payload instanceof GoodByeCommandPayload goodByeCommandPayload && goodByeCommandPayload.isReconnect()) {
webSocket.close((short) 1013, "GoodBye Command with reconnection requested.").subscribe();
} else {
webSocket.close((short) 1000, "GoodBye Command without reconnection.").subscribe();
}
}
})
.doFinally(this::cleanChannel)
.ignoreElement();
} else {
return Completable.fromRunnable(() -> {
webSocket.close((short) 1013).subscribe();
this.cleanChannel();
});
}
}
protected Completable handleCommandAsync(final Command<?> command, final CommandHandler<Command<?>, Reply<?>> commandHandler) {
return handleCommand(command, commandHandler, false).ignoreElement();
}
protected Single<Reply<?>> handleCommand(
final Command<?> command,
final CommandHandler<Command<?>, Reply<?>> commandHandler,
boolean dontReply
) {
return commandHandler
.handle(command)
.flatMap(reply -> {
if (!dontReply) {
return writeReply(reply).andThen(Single.just(reply));
}
return Single.just(reply);
})
.doOnError(throwable -> {
log.warn("Unable to handle command [{}, {}]", command.getType(), command.getId());
webSocket.close((short) 1011, "Unexpected error").subscribe();
});
}
@Override
public <C extends Command<?>, R extends Reply<?>> Single<R> send(final C command) {
return send(command, false);
}
protected Single<HelloReply> sendHelloCommand(final HelloCommand helloCommand) {
return send(helloCommand, true);
}
protected <C extends Command<?>, R extends Reply<?>> Single<R> send(final C command, final boolean ignoreActiveStatus) {
return Single
.defer(() -> {
if (!ignoreActiveStatus && !active) {
return Single.error(new ChannelInactiveException());
}
CommandAdapter<C, Command<?>, R> commandAdapter = (CommandAdapter<C, Command<?>, R>) commandAdapters.get(command.getType());
if (commandAdapter != null) {
return commandAdapter.adapt(command);
} else {
return Single.just(command);
}
})
.flatMap(adaptedCommand ->
Single
.<R>create(emitter -> {
resultEmitters.put(adaptedCommand.getId(), emitter);
writeCommand(adaptedCommand).doOnError(emitter::onError).onErrorComplete().subscribe();
})
.timeout(
adaptedCommand.getReplyTimeoutMs(),
TimeUnit.MILLISECONDS,
Single.error(() -> {
log.warn("No reply received in time for command [{}, {}]", adaptedCommand.getType(), adaptedCommand.getId());
throw new ChannelTimeoutException();
})
)
)
.onErrorResumeNext(throwable -> {
CommandAdapter<C, Command<?>, R> commandAdapter = (CommandAdapter<C, Command<?>, R>) commandAdapters.get(command.getType());
if (commandAdapter != null) {
return commandAdapter.onError(command, throwable);
} else {
return Single.error(throwable);
}
})
// Cleanup result emitters list if cancelled by the upstream.
.doOnDispose(() -> resultEmitters.remove(command.getId()));
}
protected <C extends Command<?>> Completable writeCommand(C command) {
ProtocolExchange protocolExchange = ProtocolExchange
.builder()
.type(ProtocolExchange.Type.COMMAND)
.exchangeType(command.getType())
.exchange(command)
.build();
return writeToSocket(command.getId(), protocolExchange);
}
protected <R extends Reply<?>> Completable writeReply(R reply) {
return Single
.defer(() -> {
ReplyAdapter<R, Reply<?>> replyAdapter = (ReplyAdapter<R, Reply<?>>) replyAdapters.get(reply.getType());
if (replyAdapter != null) {
return replyAdapter.adapt(reply);
} else {
return Single.just(reply);
}
})
.flatMapCompletable(adaptedReply -> {
ProtocolExchange protocolExchange = ProtocolExchange
.builder()
.type(ProtocolExchange.Type.REPLY)
.exchangeType(adaptedReply.getType())
.exchange(adaptedReply)
.build();
return writeToSocket(adaptedReply.getCommandId(), protocolExchange);
});
}
private Completable writeToSocket(final String commandId, final ProtocolExchange websocketExchange) {
if (!webSocket.isClosed()) {
return webSocket
.writeBinaryMessage(protocolAdapter.write(websocketExchange))
.doOnComplete(() ->
log.debug("Write command/reply [{}, {}] to websocket successfully", websocketExchange.exchangeType(), commandId)
)
.onErrorResumeNext(throwable -> {
log.error("An error occurred when trying to send command/reply [{}, {}]", websocketExchange.exchangeType(), commandId);
return Completable.error(new Exception("Write to socket failed"));
});
} else {
return Completable.error(new ChannelClosedException());
}
}
@Override
public void addCommandHandlers(final List<CommandHandler<? extends Command<?>, ? extends Reply<?>>> commandHandlers) {
if (commandHandlers != null) {
commandHandlers.forEach(commandHandler -> this.commandHandlers.putIfAbsent(commandHandler.supportType(), commandHandler));
}
}
public void addCommandAdapters(
final List<CommandAdapter<? extends Command<?>, ? extends Command<?>, ? extends Reply<?>>> commandAdapters
) {
if (commandAdapters != null) {
commandAdapters.forEach(commandAdapter -> this.commandAdapters.putIfAbsent(commandAdapter.supportType(), commandAdapter));
}
}
public void addReplyAdapters(final List<ReplyAdapter<? extends Reply<?>, ? extends Reply<?>>> replyAdapters) {
if (replyAdapters != null) {
replyAdapters.forEach(replyAdapter -> this.replyAdapters.putIfAbsent(replyAdapter.supportType(), replyAdapter));
}
}
}