Update pom.xml, TdError.java, and 5 more files...
This commit is contained in:
parent
9ea736308a
commit
4fe963c3b7
4
pom.xml
4
pom.xml
|
@ -4,7 +4,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlib-session-container</artifactId>
|
<artifactId>tdlib-session-container</artifactId>
|
||||||
<version>3.169.52</version>
|
<version>3.169.54</version>
|
||||||
<name>TDLib Session Container</name>
|
<name>TDLib Session Container</name>
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -84,7 +84,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-java</artifactId>
|
<artifactId>tdlight-java</artifactId>
|
||||||
<version>3.169.52</version>
|
<version>3.169.54</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.cavallium</groupId>
|
<groupId>it.cavallium</groupId>
|
||||||
|
|
|
@ -1,12 +1,34 @@
|
||||||
package it.tdlight.tdlibsession.td;
|
package it.tdlight.tdlibsession.td;
|
||||||
|
|
||||||
|
import it.tdlight.jni.TdApi;
|
||||||
|
import it.tdlight.jni.TdApi.Error;
|
||||||
|
|
||||||
public class TdError extends RuntimeException {
|
public class TdError extends RuntimeException {
|
||||||
|
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
public TdError(int code, String message) {
|
public TdError(int code, String message) {
|
||||||
super(code + " " + message);
|
super(code + " " + message);
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TdError(int code, String message, Throwable cause) {
|
public TdError(int code, String message, Throwable cause) {
|
||||||
super(code + " " + message, cause);
|
super(code + " " + message, cause);
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTdCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTdMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TdApi.Error getTdError() {
|
||||||
|
return new Error(code, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package it.tdlight.tdlibsession.td;
|
||||||
|
|
||||||
import it.tdlight.jni.TdApi;
|
import it.tdlight.jni.TdApi;
|
||||||
import it.tdlight.jni.TdApi.Error;
|
import it.tdlight.jni.TdApi.Error;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
@ -219,10 +220,12 @@ public interface TdResult<T extends TdApi.Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends TdApi.Object> TdResult<T> succeeded(@NotNull T value) {
|
static <T extends TdApi.Object> TdResult<T> succeeded(@NotNull T value) {
|
||||||
|
Objects.requireNonNull(value);
|
||||||
return new TdResultImpl<T>(value, null);
|
return new TdResultImpl<T>(value, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends TdApi.Object> TdResult<T> failed(@NotNull TdApi.Error error) {
|
static <T extends TdApi.Object> TdResult<T> failed(@NotNull TdApi.Error error) {
|
||||||
|
Objects.requireNonNull(error);
|
||||||
return new TdResultImpl<T>(null, error);
|
return new TdResultImpl<T>(null, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -200,7 +200,7 @@ public class AsyncTdEasy {
|
||||||
* @return The value or nothing
|
* @return The value or nothing
|
||||||
*/
|
*/
|
||||||
public Mono<String> getOptionString(String name) {
|
public Mono<String> getOptionString(String name) {
|
||||||
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<OptionValue>handle(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<OptionValue>flatMap(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
||||||
switch (value.getConstructor()) {
|
switch (value.getConstructor()) {
|
||||||
case OptionValueString.CONSTRUCTOR:
|
case OptionValueString.CONSTRUCTOR:
|
||||||
return Mono.just(((OptionValueString) value).value);
|
return Mono.just(((OptionValueString) value).value);
|
||||||
|
@ -219,7 +219,7 @@ public class AsyncTdEasy {
|
||||||
* @return The value or nothing
|
* @return The value or nothing
|
||||||
*/
|
*/
|
||||||
public Mono<Long> getOptionInteger(String name) {
|
public Mono<Long> getOptionInteger(String name) {
|
||||||
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<TdApi.OptionValue>handle(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<TdApi.OptionValue>flatMap(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
||||||
switch (value.getConstructor()) {
|
switch (value.getConstructor()) {
|
||||||
case OptionValueInteger.CONSTRUCTOR:
|
case OptionValueInteger.CONSTRUCTOR:
|
||||||
return Mono.just(((OptionValueInteger) value).value);
|
return Mono.just(((OptionValueInteger) value).value);
|
||||||
|
@ -238,7 +238,7 @@ public class AsyncTdEasy {
|
||||||
* @return The value or nothing
|
* @return The value or nothing
|
||||||
*/
|
*/
|
||||||
public Mono<Boolean> getOptionBoolean(String name) {
|
public Mono<Boolean> getOptionBoolean(String name) {
|
||||||
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<TdApi.OptionValue>handle(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
return this.<TdApi.OptionValue>sendDirectly(new TdApi.GetOption(name)).<TdApi.OptionValue>flatMap(MonoUtils::orElseThrow).flatMap((TdApi.OptionValue value) -> {
|
||||||
switch (value.getConstructor()) {
|
switch (value.getConstructor()) {
|
||||||
case OptionValueBoolean.CONSTRUCTOR:
|
case OptionValueBoolean.CONSTRUCTOR:
|
||||||
return Mono.just(((OptionValueBoolean) value).value);
|
return Mono.just(((OptionValueBoolean) value).value);
|
||||||
|
|
|
@ -171,7 +171,7 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<Void> pipe() {
|
private Mono<Void> pipe() {
|
||||||
incomingUpdatesCo.onNext(this.requestUpdatesBatchFromNetwork()
|
var updates = this.requestUpdatesBatchFromNetwork()
|
||||||
.repeatWhen(nFlux -> {
|
.repeatWhen(nFlux -> {
|
||||||
return Flux.push(emitter -> {
|
return Flux.push(emitter -> {
|
||||||
var dispos = Flux.combineLatest(nFlux, tdClosed, Pair::of).subscribe(val -> {
|
var dispos = Flux.combineLatest(nFlux, tdClosed, Pair::of).subscribe(val -> {
|
||||||
|
@ -210,7 +210,13 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.log("TdMiddle", Level.FINEST).publish().autoConnect(1));
|
.log("TdMiddle", Level.FINEST).publish().autoConnect(1);
|
||||||
|
|
||||||
|
updates.subscribe(t -> incomingUpdatesCo.onNext(Flux.just(t)),
|
||||||
|
incomingUpdatesCo::onError,
|
||||||
|
incomingUpdatesCo::onComplete
|
||||||
|
);
|
||||||
|
|
||||||
return Mono.empty();
|
return Mono.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,32 +309,43 @@ public class AsyncTdMiddleEventBusClient extends AbstractVerticle implements Asy
|
||||||
.replace(" = ", "="));
|
.replace(" = ", "="));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Mono.from(tdClosed).single()
|
return Mono.from(tdClosed).single().filter(tdClosed -> !tdClosed).<TdResult<T>>flatMap((_x) -> Mono.create(sink -> {
|
||||||
.filter(tdClosed -> !tdClosed)
|
try {
|
||||||
.<TdResult<T>>flatMap((_x) -> Mono.create(sink -> {
|
cluster
|
||||||
cluster.getEventBus().request(botAddress + ".execute", req, cluster.newDeliveryOpts().setLocalOnly(local), (AsyncResult<Message<TdResultMessage>> event) -> {
|
.getEventBus()
|
||||||
if (event.succeeded()) {
|
.request(botAddress + ".execute",
|
||||||
if (event.result().body() == null) {
|
req,
|
||||||
sink.error(new NullPointerException("Response is empty"));
|
cluster.newDeliveryOpts().setLocalOnly(local),
|
||||||
} else {
|
(AsyncResult<Message<TdResultMessage>> event) -> {
|
||||||
sink.success(Objects.requireNonNull(event.result().body()).toTdResult());
|
try {
|
||||||
}
|
if (event.succeeded()) {
|
||||||
} else {
|
if (event.result().body() == null) {
|
||||||
sink.error(ResponseError.newResponseError(request, botAlias, event.cause()));
|
sink.error(new NullPointerException("Response is empty"));
|
||||||
}
|
} else {
|
||||||
});
|
sink.success(Objects.requireNonNull(event.result().body()).toTdResult());
|
||||||
})).<TdResult<T>>handle((response, sink) -> {
|
}
|
||||||
|
} else {
|
||||||
|
sink.error(ResponseError.newResponseError(request, botAlias, event.cause()));
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
sink.error(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
sink.error(t);
|
||||||
|
}
|
||||||
|
})).<TdResult<T>>switchIfEmpty(Mono.defer(() -> Mono.just(TdResult.<T>failed(new TdApi.Error(500,
|
||||||
|
"Client is closed or response is empty"
|
||||||
|
))))).<TdResult<T>>handle((response, sink) -> {
|
||||||
try {
|
try {
|
||||||
Objects.requireNonNull(response);
|
Objects.requireNonNull(response);
|
||||||
if (OUTPUT_REQUESTS) {
|
if (OUTPUT_REQUESTS) {
|
||||||
System.out.println(" <- " + response.toString()
|
System.out.println(
|
||||||
.replace("\n", " ")
|
" <- " + response.toString().replace("\n", " ").replace("\t", "").replace(" ", "").replace(" = ", "="));
|
||||||
.replace("\t", "")
|
|
||||||
.replace(" ", "")
|
|
||||||
.replace(" = ", "="));
|
|
||||||
}
|
}
|
||||||
sink.next(response);
|
sink.next(response);
|
||||||
} catch (ClassCastException | NullPointerException e) {
|
} catch (Exception e) {
|
||||||
sink.error(e);
|
sink.error(e);
|
||||||
}
|
}
|
||||||
}).switchIfEmpty(Mono.fromSupplier(() -> {
|
}).switchIfEmpty(Mono.fromSupplier(() -> {
|
||||||
|
|
|
@ -216,11 +216,17 @@ public class AsyncTdMiddleEventBusServer extends AbstractVerticle {
|
||||||
.switchIfEmpty(Mono.fromSupplier(() -> {
|
.switchIfEmpty(Mono.fromSupplier(() -> {
|
||||||
return TdResult.failed(new TdApi.Error(500, "Received null response"));
|
return TdResult.failed(new TdApi.Error(500, "Received null response"));
|
||||||
}))
|
}))
|
||||||
.subscribe(response -> {
|
.handle((response, sink) -> {
|
||||||
msg.reply(new TdResultMessage(response.result(), response.cause()),
|
try {
|
||||||
cluster.newDeliveryOpts().setLocalOnly(local)
|
msg.reply(new TdResultMessage(response.result(), response.cause()),
|
||||||
);
|
cluster.newDeliveryOpts().setLocalOnly(local)
|
||||||
}, ex -> {
|
);
|
||||||
|
sink.next(response);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
sink.error(ex);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.subscribe(response -> {}, ex -> {
|
||||||
logger.error("Error when processing a request", ex);
|
logger.error("Error when processing a request", ex);
|
||||||
msg.fail(500, ex.getLocalizedMessage());
|
msg.fail(500, ex.getLocalizedMessage());
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,12 +5,14 @@ import io.vertx.core.Handler;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import it.tdlight.jni.TdApi;
|
import it.tdlight.jni.TdApi;
|
||||||
|
import it.tdlight.jni.TdApi.Object;
|
||||||
import it.tdlight.tdlibsession.td.TdError;
|
import it.tdlight.tdlibsession.td.TdError;
|
||||||
import it.tdlight.tdlibsession.td.TdResult;
|
import it.tdlight.tdlibsession.td.TdResult;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
import org.warp.commonutils.concurrency.future.CompletableFutureUtils;
|
import org.warp.commonutils.concurrency.future.CompletableFutureUtils;
|
||||||
import reactor.core.CoreSubscriber;
|
import reactor.core.CoreSubscriber;
|
||||||
|
@ -117,12 +119,11 @@ public class MonoUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <R extends TdApi.Object> void orElseThrow(TdResult<R> value, SynchronousSink<R> sink) {
|
public static <R extends TdApi.Object> Mono<R> orElseThrow(TdResult<R> value) {
|
||||||
if (value.succeeded()) {
|
if (value.succeeded()) {
|
||||||
sink.next(value.result());
|
return Mono.just(value.result());
|
||||||
} else {
|
} else {
|
||||||
sink.complete();
|
return Mono.error(new TdError(value.cause().code, value.cause().message));
|
||||||
//sink.error(new TdError(value.cause().code, value.cause().message));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,4 +136,42 @@ public class MonoUtils {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> Mono<T> fromFuture(CompletableFuture<T> future) {
|
||||||
|
return Mono.create(sink -> {
|
||||||
|
future.whenComplete((result, error) -> {
|
||||||
|
if (error != null) {
|
||||||
|
sink.error(error);
|
||||||
|
} else if (result != null) {
|
||||||
|
sink.success(result);
|
||||||
|
} else {
|
||||||
|
sink.success();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Mono<T> fromFuture(Supplier<CompletableFuture<T>> future) {
|
||||||
|
return Mono.create(sink -> {
|
||||||
|
CompletableFutureUtils.getCompletableFuture(future).whenComplete((result, error) -> {
|
||||||
|
if (error != null) {
|
||||||
|
sink.error(error.getCause());
|
||||||
|
} else if (result != null) {
|
||||||
|
sink.success(result);
|
||||||
|
} else {
|
||||||
|
sink.success();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends Object> CompletableFuture<T> toFuture(Mono<T> mono) {
|
||||||
|
var cf = new CompletableFuture<T>();
|
||||||
|
mono.subscribe(value -> {
|
||||||
|
cf.complete(value);
|
||||||
|
}, ex -> {
|
||||||
|
cf.completeExceptionally(ex);
|
||||||
|
}, () -> cf.complete(null));
|
||||||
|
return cf;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user