2020-10-14 01:38:44 +02:00
|
|
|
package it.tdlight.tdlibsession.td.middle.direct;
|
|
|
|
|
|
|
|
import static it.tdlight.tdlibsession.td.middle.server.AsyncTdMiddleEventBusServer.WAIT_DURATION;
|
|
|
|
|
|
|
|
import io.vertx.core.AbstractVerticle;
|
|
|
|
import io.vertx.core.Promise;
|
|
|
|
import io.vertx.core.json.JsonObject;
|
2020-10-28 12:04:42 +01:00
|
|
|
import it.tdlight.jni.TdApi;
|
2020-10-14 01:38:44 +02:00
|
|
|
import it.tdlight.jni.TdApi.Function;
|
|
|
|
import it.tdlight.jni.TdApi.Object;
|
|
|
|
import it.tdlight.tdlibsession.td.ResponseError;
|
|
|
|
import it.tdlight.tdlibsession.td.TdResult;
|
|
|
|
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.AsyncTdMiddle;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.TdClusterManager;
|
|
|
|
import it.tdlight.utils.MonoUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
2021-01-13 04:00:43 +01:00
|
|
|
import reactor.core.publisher.Sinks;
|
|
|
|
import reactor.core.publisher.Sinks.Empty;
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
public class AsyncTdMiddleDirect extends AbstractVerticle implements AsyncTdMiddle {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AsyncTdMiddleDirect.class);
|
|
|
|
|
|
|
|
protected AsyncTdDirectImpl td;
|
|
|
|
private String botAddress;
|
|
|
|
private String botAlias;
|
2021-01-22 17:31:09 +01:00
|
|
|
private final Empty<Object> closeRequest = Sinks.empty();
|
2020-10-14 01:38:44 +02:00
|
|
|
|
|
|
|
public AsyncTdMiddleDirect() {
|
|
|
|
}
|
|
|
|
|
2021-01-22 17:31:09 +01:00
|
|
|
public static Mono<AsyncTdMiddle> getAndDeployInstance(TdClusterManager clusterManager,
|
2020-10-14 01:38:44 +02:00
|
|
|
String botAlias,
|
2021-01-22 17:31:09 +01:00
|
|
|
String botAddress) {
|
2020-10-14 01:38:44 +02:00
|
|
|
var instance = new AsyncTdMiddleDirect();
|
2020-10-19 00:50:27 +02:00
|
|
|
var options = clusterManager.newDeploymentOpts().setConfig(new JsonObject()
|
2020-10-14 01:38:44 +02:00
|
|
|
.put("botAlias", botAlias)
|
|
|
|
.put("botAddress", botAddress));
|
2021-01-22 17:31:09 +01:00
|
|
|
return clusterManager.getVertx()
|
|
|
|
.rxDeployVerticle(instance, options)
|
|
|
|
.as(MonoUtils::toMono)
|
|
|
|
.doOnNext(_v -> logger.trace("Deployed verticle for bot " + botAlias + ", address: " + botAddress))
|
|
|
|
.thenReturn(instance);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void start(Promise<Void> startPromise) {
|
|
|
|
var botAddress = config().getString("botAddress");
|
|
|
|
if (botAddress == null || botAddress.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("botAddress is not set!");
|
|
|
|
}
|
|
|
|
this.botAddress = botAddress;
|
|
|
|
var botAlias = config().getString("botAlias");
|
|
|
|
if (botAlias == null || botAlias.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("botAlias is not set!");
|
|
|
|
}
|
|
|
|
this.botAlias = botAlias;
|
|
|
|
|
|
|
|
this.td = new AsyncTdDirectImpl(botAlias);
|
|
|
|
|
2021-01-13 04:00:43 +01:00
|
|
|
startPromise.complete();
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void stop(Promise<Void> stopPromise) {
|
2021-01-13 04:00:43 +01:00
|
|
|
closeRequest.tryEmitEmpty();
|
|
|
|
stopPromise.complete();
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-13 04:00:43 +01:00
|
|
|
public Flux<TdApi.Object> receive() {
|
|
|
|
return td
|
|
|
|
.receive(new AsyncTdDirectOptions(WAIT_DURATION, 1000))
|
|
|
|
.takeUntilOther(closeRequest.asMono())
|
2021-01-22 17:31:09 +01:00
|
|
|
.doOnError(ex -> logger.info("TdMiddle verticle error", ex))
|
|
|
|
.doOnTerminate(() -> logger.debug("TdMiddle verticle stopped"))
|
|
|
|
.doOnNext(result -> {
|
|
|
|
if (result.failed()) {
|
|
|
|
logger.error("Received an errored update: {}", result.cause());
|
2021-01-13 04:00:43 +01:00
|
|
|
}
|
2021-01-22 17:31:09 +01:00
|
|
|
})
|
|
|
|
.filter(TdResult::succeeded)
|
|
|
|
.map(TdResult::result);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Object> Mono<TdResult<T>> execute(Function requestFunction, boolean executeDirectly) {
|
2021-01-22 17:31:09 +01:00
|
|
|
return td
|
|
|
|
.<T>execute(requestFunction, executeDirectly)
|
|
|
|
.onErrorMap(error -> ResponseError.newResponseError(requestFunction, botAlias, error));
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|