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.AbstractVerticle;
|
|
|
|
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;
|
2020-10-22 22:01:05 +02:00
|
|
|
import reactor.core.scheduler.Scheduler;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
public class AsyncTdMiddleEventBusServer extends AbstractVerticle {
|
|
|
|
|
|
|
|
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;
|
2020-10-22 22:01:05 +02:00
|
|
|
private final Scheduler tdSrvPoll;
|
2021-01-13 17:22:14 +01:00
|
|
|
private final Scheduler vertxStatusScheduler = Schedulers.newSingle("VertxStatus", false);
|
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);
|
2020-10-22 22:01:05 +02:00
|
|
|
this.tdSrvPoll = Schedulers.newSingle("TdSrvPoll");
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void start(Promise<Void> startPromise) {
|
2021-01-13 17:22:14 +01:00
|
|
|
vertxStatusScheduler.schedule(() -> {
|
|
|
|
var botAddress = config().getString("botAddress");
|
|
|
|
if (botAddress == null || botAddress.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("botAddress is not set!");
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
2021-01-13 17:22:14 +01:00
|
|
|
this.botAddress = botAddress;
|
|
|
|
var botAlias = config().getString("botAlias");
|
|
|
|
if (botAlias == null || botAlias.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("botAlias is not set!");
|
|
|
|
}
|
|
|
|
this.botAlias = botAlias;
|
|
|
|
var local = config().getBoolean("local");
|
|
|
|
if (local == null) {
|
|
|
|
throw new IllegalArgumentException("local is not set!");
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
2021-01-13 17:22:14 +01:00
|
|
|
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));
|
|
|
|
})).subscribeOn(this.tdSrvPoll)
|
|
|
|
.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 -> {
|
|
|
|
startPromise.complete(h.result());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
startPromise.fail(h.cause());
|
|
|
|
}
|
|
|
|
});
|
2020-10-14 01:38:44 +02: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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void stop(Promise<Void> stopPromise) {
|
2021-01-13 17:22:14 +01:00
|
|
|
vertxStatusScheduler.schedule(() -> {
|
|
|
|
runAll(onBeforeStopListeners, onBeforeStopHandler -> {
|
|
|
|
if (onBeforeStopHandler.failed()) {
|
|
|
|
logger.error("A beforeStop listener failed: "+ onBeforeStopHandler.cause());
|
2021-01-13 04:00:43 +01:00
|
|
|
}
|
2020-10-28 12:04:42 +01:00
|
|
|
|
2021-01-13 17:22:14 +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());
|
2021-01-13 04:00:43 +01:00
|
|
|
}
|
2020-10-28 12:04:42 +01:00
|
|
|
|
2021-01-13 04:00:43 +01:00
|
|
|
this.executeConsumer.unregister(result4 -> {
|
|
|
|
if (result4.failed()) {
|
|
|
|
logger.error("Can't unregister consumer", result4.cause());
|
2020-10-28 12:04:42 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:00:43 +01:00
|
|
|
sink.success();
|
2020-10-28 12:04:42 +01:00
|
|
|
});
|
|
|
|
});
|
2021-01-13 17:22:14 +01:00
|
|
|
})).doFinally(signalType -> {
|
|
|
|
logger.info("TdMiddle verticle \"" + botAddress + "\" stopped");
|
2020-10-28 12:04:42 +01:00
|
|
|
|
2021-01-13 17:22:14 +01:00
|
|
|
runAll(onAfterStopListeners, onAfterStopHandler -> {
|
|
|
|
if (onAfterStopHandler.failed()) {
|
|
|
|
logger.error("An afterStop listener failed: " + onAfterStopHandler.cause());
|
|
|
|
}
|
|
|
|
stopPromise.complete();
|
|
|
|
});
|
|
|
|
}).subscribeOn(this.tdSrvPoll).subscribe(v -> {}, ex -> {
|
|
|
|
logger.error("Error when stopping", ex);
|
|
|
|
}, () -> {});
|
|
|
|
});
|
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 04:00:43 +01:00
|
|
|
}).subscribeOn(this.tdSrvPoll)
|
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));
|
2020-10-14 01:38:44 +02:00
|
|
|
|
2021-01-13 04:00:43 +01:00
|
|
|
});
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:04:42 +01:00
|
|
|
private void undeploy(Runnable whenUndeployed) {
|
|
|
|
vertx.undeploy(deploymentID(), undeployed -> {
|
|
|
|
if (undeployed.failed()) {
|
|
|
|
logger.error("Error when undeploying td verticle", undeployed.cause());
|
|
|
|
}
|
|
|
|
whenUndeployed.run();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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(" = ", "="));
|
|
|
|
}
|
|
|
|
}).bufferTimeout(1000, local ? Duration.ofMillis(1) : Duration.ofMillis(100))
|
|
|
|
.windowTimeout(1, Duration.ofSeconds(5))
|
|
|
|
.flatMap(w -> w.defaultIfEmpty(Collections.emptyList()))
|
|
|
|
.map(TdResultList::new).doOnTerminate(() -> {
|
|
|
|
System.out.println("<=: end (1)");
|
|
|
|
}).doOnComplete(() -> {
|
|
|
|
System.out.println("<=: end (2)");
|
|
|
|
}).doFinally(s -> {
|
|
|
|
System.out.println("<=: end (3)");
|
|
|
|
this.undeploy(() -> {});
|
2021-01-13 04:00:43 +01:00
|
|
|
});
|
2021-01-13 17:22:14 +01:00
|
|
|
var fluxCodec = new TdResultListMessageCodec();
|
|
|
|
EventBusFlux.registerFluxCodec(cluster.getEventBus(), fluxCodec);
|
|
|
|
return EventBusFlux.<TdResultList>serve(updatesFlux,
|
|
|
|
cluster.getEventBus(),
|
|
|
|
botAddress + ".updates",
|
|
|
|
cluster.newDeliveryOpts().setLocalOnly(local),
|
|
|
|
fluxCodec,
|
|
|
|
Duration.ofSeconds(30)
|
|
|
|
).subscribeOn(tdSrvPoll);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|