Include version

This commit is contained in:
Andrea Cavalli 2022-05-05 20:25:00 +02:00
parent 272f7dfe75
commit cc3bdc76c5
7 changed files with 73 additions and 21 deletions

View File

@ -27,11 +27,11 @@ jobs:
export REVISION=${{ github.run_number }}
echo "REVISION=$REVISION" >> $GITHUB_ENV
- name: Set up JDK 15
- name: Set up JDK 17
if: github.ref == 'refs/heads/master'
uses: actions/setup-java@v1
with:
java-version: 15
java-version: 17
server-id: mchv-release-distribution
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD

View File

@ -8,7 +8,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0-SNAPSHOT</revision>
<atomix.version>3.1.10</atomix.version>
<atomix.version>3.1.12</atomix.version>
<record.builder.version>33</record.builder.version>
</properties>
<repositories>
@ -159,6 +159,11 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>net.minecrell</groupId>
<artifactId>terminalconsoleappender</artifactId>

View File

@ -6,7 +6,7 @@ import io.atomix.cluster.messaging.ClusterEventService;
import io.atomix.cluster.messaging.MessagingException;
import it.tdlight.jni.TdApi;
import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.Request;
import it.tdlight.reactiveapi.Event.OnRequest.Request;
import java.net.ConnectException;
import java.time.Duration;
import java.time.Instant;

View File

@ -1,30 +1,39 @@
package it.tdlight.reactiveapi;
import static it.tdlight.reactiveapi.Event.SERIAL_VERSION;
import io.atomix.cluster.messaging.ClusterEventService;
import io.atomix.cluster.messaging.MessagingException;
import io.atomix.core.Atomix;
import it.tdlight.common.utils.LibraryVersion;
import it.tdlight.jni.TdApi;
import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.Ignored;
import it.tdlight.reactiveapi.Event.OnBotLoginCodeRequested;
import it.tdlight.reactiveapi.Event.OnOtherDeviceLoginRequested;
import it.tdlight.reactiveapi.Event.OnPasswordRequested;
import it.tdlight.reactiveapi.Event.OnRequest.Request;
import it.tdlight.reactiveapi.Event.OnUpdateData;
import it.tdlight.reactiveapi.Event.OnUpdateError;
import it.tdlight.reactiveapi.Event.OnUserLoginCodeRequested;
import it.tdlight.reactiveapi.Event.Request;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeoutException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SerializationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.Disposable;
@ -119,7 +128,12 @@ abstract class BaseAtomixReactiveApiClient implements ReactiveApiClient, AutoClo
if (bytes == null || bytes.length == 0) {
return null;
}
return TdApi.Deserializer.deserialize(new DataInputStream(new ByteArrayInputStream(bytes)));
var dis = new DataInputStream(new ByteArrayInputStream(bytes));
var serialVersion = dis.readInt();
if (serialVersion != SERIAL_VERSION) {
return new TdApi.Error(400, "Conflicting protocol version");
}
return TdApi.Deserializer.deserialize(dis);
} catch (IOException ex) {
throw new SerializationException(ex);
}
@ -129,6 +143,7 @@ abstract class BaseAtomixReactiveApiClient implements ReactiveApiClient, AutoClo
try (var byteArrayOutputStream = new ByteArrayOutputStream()) {
try (var dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {
dataOutputStream.writeLong(request.liveId());
dataOutputStream.writeInt(SERIAL_VERSION);
dataOutputStream.writeLong(request.timeout().toEpochMilli());
request.request().serialize(dataOutputStream);
dataOutputStream.flush();
@ -164,9 +179,13 @@ abstract class BaseAtomixReactiveApiClient implements ReactiveApiClient, AutoClo
}
}
static ClientBoundEvent deserializeEvent(DataInputStream is) throws IOException {
static @NotNull ClientBoundEvent deserializeEvent(DataInputStream is) throws IOException {
var liveId = is.readLong();
var userId = is.readLong();
var dataVersion = is.readInt();
if (dataVersion != SERIAL_VERSION) {
return new Ignored(liveId, userId);
}
return switch (is.readByte()) {
case 0x01 -> new OnUpdateData(liveId, userId, (TdApi.Update) TdApi.Deserializer.deserialize(is));
case 0x02 -> new OnUpdateError(liveId, userId, (TdApi.Error) TdApi.Deserializer.deserialize(is));

View File

@ -1,17 +1,25 @@
package it.tdlight.reactiveapi;
import it.tdlight.common.utils.LibraryVersion;
import it.tdlight.jni.TdApi;
import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.OnRequest.InvalidRequest;
import it.tdlight.reactiveapi.Event.OnRequest.Request;
import it.tdlight.reactiveapi.Event.ServerBoundEvent;
import java.io.DataInput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SerializationException;
import org.jetbrains.annotations.Nullable;
/**
* Any event received from a session
*/
public sealed interface Event permits ClientBoundEvent, ServerBoundEvent {
public sealed interface Event {
int SERIAL_VERSION = ArrayUtils.hashCode(LibraryVersion.VERSION.getBytes(StandardCharsets.US_ASCII));
/**
*
@ -22,8 +30,7 @@ public sealed interface Event permits ClientBoundEvent, ServerBoundEvent {
/**
* Event received after choosing the user id of the session
*/
sealed interface ClientBoundEvent extends Event permits OnLoginCodeRequested, OnOtherDeviceLoginRequested,
OnPasswordRequested, OnUpdate {
sealed interface ClientBoundEvent extends Event {
/**
*
@ -32,13 +39,12 @@ public sealed interface Event permits ClientBoundEvent, ServerBoundEvent {
long userId();
}
sealed interface ServerBoundEvent extends Event permits Request {}
sealed interface ServerBoundEvent extends Event {}
/**
* TDLib is asking for an authorization code
*/
sealed interface OnLoginCodeRequested extends ClientBoundEvent
permits OnBotLoginCodeRequested, OnUserLoginCodeRequested {}
sealed interface OnLoginCodeRequested extends ClientBoundEvent {}
record OnUserLoginCodeRequested(long liveId, long userId, long phoneNumber) implements OnLoginCodeRequested {}
@ -49,21 +55,32 @@ public sealed interface Event permits ClientBoundEvent, ServerBoundEvent {
record OnPasswordRequested(long liveId, long userId, String passwordHint, boolean hasRecoveryEmail,
String recoveryEmailPattern) implements ClientBoundEvent {}
record Ignored(long liveId, long userId) implements ClientBoundEvent {}
/**
* Event received from TDLib
*/
sealed interface OnUpdate extends ClientBoundEvent permits OnUpdateData, OnUpdateError {}
sealed interface OnUpdate extends ClientBoundEvent {}
record OnUpdateData(long liveId, long userId, TdApi.Update update) implements OnUpdate {}
record OnUpdateError(long liveId, long userId, TdApi.Error error) implements OnUpdate {}
record Request<T extends TdApi.Object>(long liveId, TdApi.Function<T> request, Instant timeout) implements
ServerBoundEvent {
sealed interface OnRequest<T extends TdApi.Object> extends ServerBoundEvent {
public static <T extends TdApi.Object> Request<T> deserialize(DataInput dataInput) {
record Request<T extends TdApi.Object>(long liveId, TdApi.Function<T> request, Instant timeout)
implements OnRequest<T> {}
record InvalidRequest<T extends TdApi.Object>(long liveId) implements OnRequest<T> {}
static <T extends TdApi.Object> Event.OnRequest<T> deserialize(DataInput dataInput) {
try {
var liveId = dataInput.readLong();
var dataVersion = dataInput.readInt();
if (dataVersion != SERIAL_VERSION) {
// Deprecated request
return new InvalidRequest<>(liveId);
}
long millis = dataInput.readLong();
var timeout = Instant.ofEpochMilli(millis);
@SuppressWarnings("unchecked")

View File

@ -2,6 +2,7 @@ package it.tdlight.reactiveapi;
import static it.tdlight.reactiveapi.AuthPhase.LOGGED_IN;
import static it.tdlight.reactiveapi.AuthPhase.LOGGED_OUT;
import static it.tdlight.reactiveapi.Event.SERIAL_VERSION;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
@ -30,10 +31,12 @@ import it.tdlight.reactiveapi.Event.ClientBoundEvent;
import it.tdlight.reactiveapi.Event.OnBotLoginCodeRequested;
import it.tdlight.reactiveapi.Event.OnOtherDeviceLoginRequested;
import it.tdlight.reactiveapi.Event.OnPasswordRequested;
import it.tdlight.reactiveapi.Event.OnRequest;
import it.tdlight.reactiveapi.Event.OnRequest.InvalidRequest;
import it.tdlight.reactiveapi.Event.OnRequest.Request;
import it.tdlight.reactiveapi.Event.OnUpdateData;
import it.tdlight.reactiveapi.Event.OnUpdateError;
import it.tdlight.reactiveapi.Event.OnUserLoginCodeRequested;
import it.tdlight.reactiveapi.Event.Request;
import it.tdlight.reactiveapi.ResultingEvent.ClientBoundResultingEvent;
import it.tdlight.reactiveapi.ResultingEvent.ClusterBoundResultingEvent;
import it.tdlight.reactiveapi.ResultingEvent.ResultingEventPublisherClosed;
@ -439,6 +442,7 @@ public abstract class ReactiveApiPublisher {
throws IOException {
dataOutputStream.writeLong(clientBoundEvent.liveId());
dataOutputStream.writeLong(clientBoundEvent.userId());
dataOutputStream.writeInt(SERIAL_VERSION);
if (clientBoundEvent instanceof OnUpdateData onUpdateData) {
dataOutputStream.writeByte(0x1);
onUpdateData.update().serialize(dataOutputStream);
@ -485,6 +489,7 @@ public abstract class ReactiveApiPublisher {
var object = response.getObject();
try (var byteArrayOutputStream = new ByteArrayOutputStream()) {
try (var dataOutputStream = new DataOutputStream(byteArrayOutputStream)) {
dataOutputStream.writeInt(SERIAL_VERSION);
//dataOutputStream.writeLong(id);
object.serialize(dataOutputStream);
return byteArrayOutputStream.toByteArray();
@ -494,7 +499,11 @@ public abstract class ReactiveApiPublisher {
}
}
private CompletableFuture<Response> handleRequest(Request<Object> requestObj) {
private CompletableFuture<Response> handleRequest(OnRequest<Object> onRequestObj) {
if (onRequestObj instanceof OnRequest.InvalidRequest invalidRequest) {
return completedFuture(new Response(invalidRequest.liveId(), new TdApi.Error(400, "Conflicting protocol version")));
}
var requestObj = (Request<Object>) onRequestObj;
if (liveId != requestObj.liveId()) {
LOG.error("Received a request for another session!");
return completedFuture(new Response(liveId,
@ -522,8 +531,8 @@ public abstract class ReactiveApiPublisher {
}
}
private static <T extends TdApi.Object> Request<T> deserializeRequest(byte[] bytes) {
return Request.deserialize(new DataInputStream(new ByteArrayInputStream(bytes)));
private static <T extends TdApi.Object> OnRequest<T> deserializeRequest(byte[] bytes) {
return OnRequest.deserialize(new DataInputStream(new ByteArrayInputStream(bytes)));
}
@Override

View File

@ -27,4 +27,6 @@ module tdlib.reactive.api {
requires it.unimi.dsi.fastutil;
requires net.minecrell.terminalconsole;
requires org.jline.reader;
requires jdk.unsupported;
requires jakarta.xml.bind;
}