2020-10-14 01:38:44 +02:00
|
|
|
package it.tdlight.tdlibsession.td.middle.server;
|
|
|
|
|
|
|
|
import static it.tdlight.tdlibsession.td.middle.client.AsyncTdMiddleEventBusClient.OUTPUT_REQUESTS;
|
|
|
|
|
|
|
|
import io.vertx.core.AsyncResult;
|
2020-10-28 12:04:42 +01:00
|
|
|
import io.vertx.core.Future;
|
|
|
|
import io.vertx.core.Handler;
|
2020-10-14 01:38:44 +02:00
|
|
|
import io.vertx.core.Promise;
|
|
|
|
import io.vertx.core.eventbus.Message;
|
2020-10-28 12:04:42 +01:00
|
|
|
import io.vertx.core.eventbus.MessageConsumer;
|
2020-10-14 01:38:44 +02:00
|
|
|
import it.tdlight.common.ConstructorDetector;
|
2020-10-14 15:14:54 +02:00
|
|
|
import it.tdlight.jni.TdApi;
|
2021-01-13 17:22:14 +01:00
|
|
|
import it.tdlight.tdlibsession.EventBusFlux;
|
2020-10-14 01:38:44 +02:00
|
|
|
import it.tdlight.tdlibsession.td.TdResult;
|
|
|
|
import it.tdlight.tdlibsession.td.TdResultMessage;
|
|
|
|
import it.tdlight.tdlibsession.td.direct.AsyncTdDirectImpl;
|
2021-01-13 04:00:43 +01:00
|
|
|
import it.tdlight.tdlibsession.td.direct.AsyncTdDirectOptions;
|
2020-10-14 01:38:44 +02:00
|
|
|
import it.tdlight.tdlibsession.td.middle.ExecuteObject;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.TdClusterManager;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.TdExecuteObjectMessageCodec;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.TdMessageCodec;
|
2021-01-13 17:22:14 +01:00
|
|
|
import it.tdlight.tdlibsession.td.middle.TdResultList;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.TdResultListMessageCodec;
|
2020-10-14 01:38:44 +02:00
|
|
|
import it.tdlight.tdlibsession.td.middle.TdResultMessageCodec;
|
|
|
|
import it.tdlight.utils.MonoUtils;
|
|
|
|
import java.time.Duration;
|
2020-10-22 04:45:28 +02:00
|
|
|
import java.util.ArrayList;
|
2020-10-14 01:38:44 +02:00
|
|
|
import java.util.Collections;
|
2020-10-22 04:55:58 +02:00
|
|
|
import java.util.List;
|
2020-10-28 12:04:42 +01:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2020-10-14 01:38:44 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2020-10-28 12:04:42 +01:00
|
|
|
import java.util.function.Consumer;
|
2020-10-14 01:38:44 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-01-13 19:46:46 +01:00
|
|
|
public class AsyncTdMiddleEventBusServer {
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AsyncTdMiddleEventBusServer.class);
|
2020-10-28 12:04:42 +01:00
|
|
|
|
2020-10-14 01:38:44 +02:00
|
|
|
private static final byte[] EMPTY = new byte[0];
|
|
|
|
// todo: restore duration to 2 seconds instead of 10 millis, when the bug of tdlight double queue wait is fixed
|
|
|
|
public static final Duration WAIT_DURATION = Duration.ofSeconds(1);// Duration.ofMillis(10);
|
2020-10-22 22:01:05 +02:00
|
|
|
// If you enable this the poll will wait up to 100 additional milliseconds between each poll, if the server is remote
|
|
|
|
private static final boolean ENABLE_MINIMUM_POLL_WAIT_INTERVAL = false;
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
private final TdClusterManager cluster;
|
2021-01-13 04:00:43 +01:00
|
|
|
private final AsyncTdDirectOptions tdOptions;
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
private String botAlias;
|
|
|
|
private String botAddress;
|
|
|
|
private boolean local;
|
|
|
|
|
|
|
|
protected AsyncTdDirectImpl td;
|
2021-01-13 04:00:43 +01:00
|
|
|
/**
|
|
|
|
* Value is not important, emits when a request is received
|
|
|
|
*/
|
|
|
|
private final List<Consumer<Promise<Void>>> onBeforeStopListeners = new CopyOnWriteArrayList<>();
|
|
|
|
private final List<Consumer<Promise<Void>>> onAfterStopListeners = new CopyOnWriteArrayList<>();
|
2020-10-28 12:04:42 +01:00
|
|
|
private MessageConsumer<?> startConsumer;
|
|
|
|
private MessageConsumer<byte[]> isWorkingConsumer;
|
|
|
|
private MessageConsumer<ExecuteObject> executeConsumer;
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
public AsyncTdMiddleEventBusServer(TdClusterManager clusterManager) {
|
|
|
|
this.cluster = clusterManager;
|
2021-01-13 04:00:43 +01:00
|
|
|
this.tdOptions = new AsyncTdDirectOptions(WAIT_DURATION, 1000);
|
2021-01-13 17:22:14 +01:00
|
|
|
if (cluster.registerDefaultCodec(TdResultList.class, new TdResultListMessageCodec())) {
|
2020-10-14 01:38:44 +02:00
|
|
|
cluster.registerDefaultCodec(ExecuteObject.class, new TdExecuteObjectMessageCodec());
|
|
|
|
cluster.registerDefaultCodec(TdResultMessage.class, new TdResultMessageCodec());
|
|
|
|
for (Class<?> value : ConstructorDetector.getTDConstructorsUnsafe().values()) {
|
|
|
|
cluster.registerDefaultCodec(value, new TdMessageCodec(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 19:46:46 +01:00
|
|
|
public Mono<Void> start(String botAddress, String botAlias, boolean local) {
|
|
|
|
return Mono.<Void>create(sink -> {
|
|
|
|
if (botAddress == null || botAddress.isEmpty()) {
|
|
|
|
sink.error(new IllegalArgumentException("botAddress is not set!"));
|
2021-01-13 18:21:34 +01:00
|
|
|
}
|
2021-01-13 19:46:46 +01:00
|
|
|
this.botAddress = botAddress;
|
|
|
|
if (botAlias == null || botAlias.isEmpty()) {
|
|
|
|
sink.error(new IllegalArgumentException("botAlias is not set!"));
|
2021-01-13 18:21:34 +01:00
|
|
|
}
|
2021-01-13 19:46:46 +01:00
|
|
|
this.botAlias = botAlias;
|
|
|
|
this.local = local;
|
|
|
|
this.td = new AsyncTdDirectImpl(botAlias);
|
|
|
|
|
|
|
|
AtomicBoolean alreadyDeployed = new AtomicBoolean(false);
|
|
|
|
this.startConsumer = cluster.getEventBus().consumer(botAddress + ".start", (Message<byte[]> msg) -> {
|
|
|
|
if (alreadyDeployed.compareAndSet(false, true)) {
|
|
|
|
this.listen().then(this.pipe()).then(Mono.<Void>create(registrationSink -> {
|
|
|
|
this.isWorkingConsumer = cluster.getEventBus().consumer(botAddress + ".isWorking", (Message<byte[]> workingMsg) -> {
|
|
|
|
workingMsg.reply(EMPTY, cluster.newDeliveryOpts().setLocalOnly(local));
|
|
|
|
});
|
|
|
|
this.isWorkingConsumer.completionHandler(MonoUtils.toHandler(registrationSink));
|
2021-01-13 22:05:34 +01:00
|
|
|
}))
|
2021-01-13 19:46:46 +01:00
|
|
|
.subscribe(v -> {}, ex -> {
|
|
|
|
logger.info(botAddress + " server deployed and started. succeeded: false");
|
|
|
|
logger.error(ex.getLocalizedMessage(), ex);
|
|
|
|
msg.fail(500, ex.getLocalizedMessage());
|
|
|
|
}, () -> {
|
|
|
|
logger.info(botAddress + " server deployed and started. succeeded: true");
|
|
|
|
msg.reply(EMPTY);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
msg.reply(EMPTY);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
startConsumer.completionHandler(h -> {
|
|
|
|
logger.info(botAddress + " server deployed. succeeded: " + h.succeeded());
|
|
|
|
if (h.succeeded()) {
|
|
|
|
logger.debug("Sending " + botAddress + ".readyToStart");
|
|
|
|
cluster.getEventBus().request(botAddress + ".readyToStart", EMPTY, cluster.newDeliveryOpts().setSendTimeout(30000), msg -> {
|
|
|
|
sink.success();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sink.error(h.cause());
|
|
|
|
}
|
|
|
|
});
|
2021-01-13 22:05:34 +01:00
|
|
|
});
|
2020-10-28 12:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onBeforeStop(Consumer<Promise<Void>> r) {
|
|
|
|
this.onBeforeStopListeners.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onAfterStop(Consumer<Promise<Void>> r) {
|
|
|
|
this.onAfterStopListeners.add(r);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:04:42 +01:00
|
|
|
private void runAll(List<Consumer<Promise<Void>>> actions, Handler<AsyncResult<Void>> resultHandler) {
|
|
|
|
if (actions.isEmpty()) {
|
|
|
|
resultHandler.handle(Future.succeededFuture());
|
|
|
|
} else {
|
|
|
|
var firstAction = actions.remove(0);
|
|
|
|
Promise<Void> promise = Promise.promise();
|
|
|
|
firstAction.accept(promise);
|
|
|
|
promise.future().onComplete(handler -> {
|
|
|
|
if (handler.succeeded()) {
|
|
|
|
runAll(new ArrayList<>(actions), resultHandler);
|
|
|
|
} else {
|
|
|
|
resultHandler.handle(Future.failedFuture(handler.cause()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Mono<Void> listen() {
|
|
|
|
return Mono.<Void>create(registrationSink -> {
|
|
|
|
|
2020-10-28 12:04:42 +01:00
|
|
|
this.executeConsumer = cluster.getEventBus().<ExecuteObject>consumer(botAddress + ".execute", (Message<ExecuteObject> msg) -> {
|
2020-10-14 01:38:44 +02:00
|
|
|
try {
|
|
|
|
if (OUTPUT_REQUESTS) {
|
|
|
|
System.out.println(":=> " + msg
|
|
|
|
.body()
|
|
|
|
.getRequest()
|
|
|
|
.toString()
|
|
|
|
.replace("\n", " ")
|
|
|
|
.replace("\t", "")
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace(" = ", "="));
|
|
|
|
}
|
2020-10-14 15:14:54 +02:00
|
|
|
td
|
|
|
|
.execute(msg.body().getRequest(), msg.body().isExecuteDirectly())
|
|
|
|
.switchIfEmpty(Mono.fromSupplier(() -> {
|
|
|
|
return TdResult.failed(new TdApi.Error(500, "Received null response"));
|
|
|
|
}))
|
2020-10-14 20:04:23 +02:00
|
|
|
.handle((response, sink) -> {
|
|
|
|
try {
|
|
|
|
msg.reply(new TdResultMessage(response.result(), response.cause()),
|
|
|
|
cluster.newDeliveryOpts().setLocalOnly(local)
|
|
|
|
);
|
|
|
|
sink.next(response);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
2021-01-13 22:05:34 +01:00
|
|
|
})
|
2020-10-14 20:04:23 +02:00
|
|
|
.subscribe(response -> {}, ex -> {
|
2020-10-14 15:14:54 +02:00
|
|
|
logger.error("Error when processing a request", ex);
|
|
|
|
msg.fail(500, ex.getLocalizedMessage());
|
|
|
|
});
|
2020-10-14 01:38:44 +02:00
|
|
|
} catch (ClassCastException ex) {
|
|
|
|
logger.error("Error when deserializing a request", ex);
|
2020-10-14 15:14:54 +02:00
|
|
|
msg.fail(500, ex.getMessage());
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
2020-10-28 12:04:42 +01:00
|
|
|
});
|
|
|
|
executeConsumer.completionHandler(MonoUtils.toHandler(registrationSink));
|
2021-01-13 22:05:34 +01:00
|
|
|
});
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:04:42 +01:00
|
|
|
private void undeploy(Runnable whenUndeployed) {
|
2021-01-13 18:21:34 +01:00
|
|
|
runAll(onBeforeStopListeners, onBeforeStopHandler -> {
|
|
|
|
if (onBeforeStopHandler.failed()) {
|
|
|
|
logger.error("A beforeStop listener failed: "+ onBeforeStopHandler.cause());
|
2020-10-28 12:04:42 +01:00
|
|
|
}
|
2021-01-13 18:21:34 +01:00
|
|
|
|
|
|
|
Mono.create(sink -> this.isWorkingConsumer.unregister(result -> {
|
|
|
|
if (result.failed()) {
|
|
|
|
logger.error("Can't unregister consumer", result.cause());
|
|
|
|
}
|
|
|
|
this.startConsumer.unregister(result2 -> {
|
|
|
|
if (result2.failed()) {
|
|
|
|
logger.error("Can't unregister consumer", result2.cause());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.executeConsumer.unregister(result4 -> {
|
|
|
|
if (result4.failed()) {
|
|
|
|
logger.error("Can't unregister consumer", result4.cause());
|
|
|
|
}
|
|
|
|
|
|
|
|
sink.success();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).doFinally(signalType -> {
|
|
|
|
logger.info("TdMiddle verticle \"" + botAddress + "\" stopped");
|
|
|
|
|
|
|
|
runAll(onAfterStopListeners, onAfterStopHandler -> {
|
|
|
|
if (onAfterStopHandler.failed()) {
|
|
|
|
logger.error("An afterStop listener failed: " + onAfterStopHandler.cause());
|
|
|
|
}
|
|
|
|
|
2021-01-13 19:46:46 +01:00
|
|
|
whenUndeployed.run();
|
2021-01-13 18:21:34 +01:00
|
|
|
});
|
2021-01-13 22:05:34 +01:00
|
|
|
}).subscribe(v -> {}, ex -> {
|
2021-01-13 18:21:34 +01:00
|
|
|
logger.error("Error when stopping", ex);
|
|
|
|
}, () -> {});
|
2020-10-28 12:04:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-14 01:38:44 +02:00
|
|
|
private Mono<Void> pipe() {
|
2021-01-13 17:22:14 +01:00
|
|
|
var updatesFlux = td.receive(tdOptions).doOnNext(update -> {
|
|
|
|
if (OUTPUT_REQUESTS) {
|
|
|
|
System.out.println("<=: " + update
|
|
|
|
.toString()
|
|
|
|
.replace("\n", " ")
|
|
|
|
.replace("\t", "")
|
|
|
|
.replace(" ", "")
|
|
|
|
.replace(" = ", "="));
|
|
|
|
}
|
2021-01-13 19:46:46 +01:00
|
|
|
}).bufferTimeout(tdOptions.getEventsSize(), local ? Duration.ofMillis(1) : Duration.ofMillis(100))
|
2021-01-14 21:09:44 +01:00
|
|
|
.filter(l -> !l.isEmpty())
|
2021-01-13 17:22:14 +01:00
|
|
|
.windowTimeout(1, Duration.ofSeconds(5))
|
|
|
|
.flatMap(w -> w.defaultIfEmpty(Collections.emptyList()))
|
2021-01-13 17:44:24 +01:00
|
|
|
.map(TdResultList::new).doFinally(s -> {
|
|
|
|
if (OUTPUT_REQUESTS) {
|
|
|
|
System.out.println("<=: end (3)");
|
|
|
|
}
|
2021-01-13 17:22:14 +01:00
|
|
|
this.undeploy(() -> {});
|
2021-01-13 22:05:34 +01:00
|
|
|
});
|
2021-01-13 17:22:14 +01:00
|
|
|
var fluxCodec = new TdResultListMessageCodec();
|
|
|
|
return EventBusFlux.<TdResultList>serve(updatesFlux,
|
|
|
|
cluster.getEventBus(),
|
|
|
|
botAddress + ".updates",
|
|
|
|
cluster.newDeliveryOpts().setLocalOnly(local),
|
|
|
|
fluxCodec,
|
|
|
|
Duration.ofSeconds(30)
|
2021-01-13 22:05:34 +01:00
|
|
|
);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|