Use TDLib 1.7.10, add BOM, fix null updates, rewrite pom.xml

Fix #64: null updates sent to the updates handler
Fix gradle import issue (#61) by rewriting the maven dependency structure, and adding a BOM to being able to keep the natives version in sync with tdlight-java version
Fix #63 by adding the required dependencies for windows, linux and macOS in the README.md
This commit is contained in:
Andrea Cavalli 2021-12-08 02:00:32 +01:00
parent afa9d362a4
commit b27c736866
23 changed files with 690 additions and 187 deletions

View File

@ -11,8 +11,7 @@ jobs:
strategy:
matrix:
include:
- { os: ubuntu-20.04, arch: "linux/amd64", implementation: "tdlight" }
#- { os: ubuntu-20.04, arch: "linux/amd64", implementation: "tdlib" }
- { os: ubuntu-20.04, arch: "linux/amd64" }
runs-on: ${{ matrix.os }}
steps:
- name: Branch name
@ -30,10 +29,8 @@ jobs:
run: |
# ====== Variables
export REVISION=${{ steps.branch_name.outputs.SOURCE_TAG_VERSION }}
export IMPLEMENTATION_NAME=${{ matrix.implementation }}
echo "REVISION=$REVISION" >> $GITHUB_ENV
echo "IMPLEMENTATION_NAME=$IMPLEMENTATION_NAME" >> $GITHUB_ENV
- name: Set up JDK 17 (Snapshot)
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
uses: actions/setup-java@v1
@ -46,8 +43,6 @@ jobs:
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
shell: bash
run: |
echo "IMPLEMENTATION_NAME: $IMPLEMENTATION_NAME"
source ./scripts/continuous-integration/github-workflows/deploy-snapshot.sh
env:
MAVEN_USERNAME: ${{ secrets.MCHV_USERNAME }}
@ -65,7 +60,6 @@ jobs:
shell: bash
run: |
echo "REVISION: $REVISION"
echo "IMPLEMENTATION_NAME: $IMPLEMENTATION_NAME"
source ./scripts/continuous-integration/github-workflows/deploy-release.sh
env:

View File

@ -28,7 +28,10 @@
- s390x (Linux)
- ppc64el/ppc64le (Linux)
**Required libraries for Linux: OpenSSL and zlib**
## 📚 Required libraries
- **Linux: OpenSSL, zlib**
- **MacOS: OpenSSL**
- **Windows: [Microsoft Visual C++ Redistributable](https://aka.ms/vs/17/release/vc_redist.x64.exe)**
### Install OpenSSL on macOS
@ -62,18 +65,32 @@ If you are using Maven, edit your `pom.xml` file as below:
</repositories>
<dependencyManagement>
<dependencies>
<!-- Add the following dependency -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId>
<version>VERSION</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Add the following dependencies -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>VERSION</version>
<!-- don't specify the version here -->
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-amd64</artifactId>
<version>NATIVES_VERSION</version>
<!-- don't specify the version here -->
</dependency>
<!-- Include other native versions that you want, for example for windows, osx, ... -->
@ -84,9 +101,6 @@ If you are using Maven, edit your `pom.xml` file as below:
Replace `VERSION` with the latest release version, you can find
it [here](https://github.com/tdlight-team/tdlight-java/releases).
Replace `NATIVES_VERSION` with the latest native version. Make sure that you are using the correct natives version for
the release that you are using.
## Setting up the library using Gradle
If you are using Gradle, add the following lines into your `build.gradle` file
@ -95,9 +109,18 @@ If you are using Gradle, add the following lines into your `build.gradle` file
repositories {
maven { url "https://mvn.mchv.eu/repository/mchv/" }
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
dependencies {
implementation 'it.tdlight:tdlight-java:LATEST_VERSION'
implementation 'it.tdlight:tdlight-natives-linux-amd64:NATIVES_VERSION'
// import the BOM
implementation platform('it.tdlight:tdlight-java-bom:VERSION')
// do not specify the versions on the dependencies below!
implementation 'it.tdlight:tdlight-java'
implementation 'it.tdlight:tdlight-natives-linux-amd64'
// Include other native versions that you want, for example for windows, osx, ...
}
```
@ -105,9 +128,6 @@ dependencies {
Replace `VERSION` with the latest release version, you can find
it [here](https://github.com/tdlight-team/tdlight-java/releases).
Replace `NATIVES_VERSION` with the latest native version. Make sure that you are using the correct natives version for
the release that you are using.
## ⚒ Native dependencies
To use TDLight Java you need to include one or more native dependencies:

198
bom/pom.xml Normal file
View File

@ -0,0 +1,198 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<name>TDLight Java BOM</name>
<properties>
<revision>1.0.0.0-SNAPSHOT</revision>
<nativesRevisionNumber>194</nativesRevisionNumber>
<nativesRevisionSuffix/>
<tdlight.natives.version>4.0.${nativesRevisionNumber}${nativesRevisionSuffix}</tdlight.natives.version>
<apiRevisionNumber>193</apiRevisionNumber>
<apiRevisionSuffix/>
<tdlight.api.version>4.0.${apiRevisionNumber}${apiRevisionSuffix}</tdlight.api.version>
</properties>
<repositories>
<repository>
<id>mchv-release</id>
<name>MCHV Release Apache Maven Packages</name>
<url>https://mvn.mchv.eu/repository/mchv</url>
</repository>
<repository>
<id>mchv-snapshot</id>
<name>MCHV Snapshot Apache Maven Packages</name>
<url>https://mvn.mchv.eu/repository/mchv-snapshot</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>mchv-release-distribution</id>
<name>MCHV Release Apache Maven Packages Distribution</name>
<url>https://mvn.mchv.eu/repository/mchv</url>
</repository>
<snapshotRepository>
<id>mchv-snapshot-distribution</id>
<name>MCHV Snapshot Apache Maven Packages Distribution</name>
<url>https://mvn.mchv.eu/repository/mchv-snapshot</url>
</snapshotRepository>
</distributionManagement>
<scm>
<connection>scm:git:https://git.ignuranza.net/tdlight-team/tdlight-java.git</connection>
<developerConnection>scm:git:https://git.ignuranza.net/tdlight-team/tdlight-java.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.6</version>
</dependency>
<dependency>
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-api-legacy</artifactId>
<version>${tdlight.api.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-api-sealed</artifactId>
<version>${tdlight.api.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-api-legacy</artifactId>
<version>${tdlight.api.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-api-sealed</artifactId>
<version>${tdlight.api.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-java</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-parent</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-java-parent</artifactId>
<version>${revision}</version>
</dependency>
<!-- TDLight natives -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-aarch64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-x86</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-armv6</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-armv7</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-ppc64le</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-windows-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-osx-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<!-- TDLib natives -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-aarch64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-x86</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-armv6</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-armv7</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-ppc64le</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-windows-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-osx-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>../parent</module>
</modules>
</project>

View File

@ -8,8 +8,6 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tdlight.java.version>2.7.9.5</tdlight.java.version>
<tdlight.natives.version>4.0.186</tdlight.natives.version>
</properties>
<repositories>
<repository>
@ -23,29 +21,36 @@
<url>https://mvn.mchv.eu/repository/mchv-snapshot</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId>
<version>2.7.10.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- TDLight -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId>
<version>${tdlight.java.version}</version>
</dependency>
<!-- TDLight natives -->
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-windows-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-osx-amd64</artifactId>
<version>${tdlight.natives.version}</version>
</dependency>
<!-- log4j logging -->

21
parent/pom.xml Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tdlight-java-parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<parent>
<groupId>it.tdlight</groupId>
<version>${revision}</version>
<artifactId>tdlight-java-bom</artifactId>
<relativePath>../bom/pom.xml</relativePath>
</parent>
<properties>
<revision>1.0.0.0-SNAPSHOT</revision>
</properties>
<modules>
<!--disabled: <module>../tdlib</module>-->
<module>../tdlight</module>
</modules>
</project>

68
pom.xml
View File

@ -1,68 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<name>TDLight Java Parent</name>
<properties>
<revision>1.0.0.0-SNAPSHOT</revision>
<nativesRevisionNumber>187</nativesRevisionNumber>
<nativesRevisionSuffix/>
<apiRevisionNumber>185</apiRevisionNumber>
<apiRevisionSuffix/>
</properties>
<repositories>
<repository>
<id>mchv-release</id>
<name>MCHV Release Apache Maven Packages</name>
<url>https://mvn.mchv.eu/repository/mchv</url>
</repository>
<repository>
<id>mchv-snapshot</id>
<name>MCHV Snapshot Apache Maven Packages</name>
<url>https://mvn.mchv.eu/repository/mchv-snapshot</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>mchv-release-distribution</id>
<name>MCHV Release Apache Maven Packages Distribution</name>
<url>https://mvn.mchv.eu/repository/mchv</url>
</repository>
<snapshotRepository>
<id>mchv-snapshot-distribution</id>
<name>MCHV Snapshot Apache Maven Packages Distribution</name>
<url>https://mvn.mchv.eu/repository/mchv-snapshot</url>
</snapshotRepository>
</distributionManagement>
<scm>
<connection>scm:git:https://git.ignuranza.net/tdlight-team/tdlight-java.git</connection>
<developerConnection>scm:git:https://git.ignuranza.net/tdlight-team/tdlight-java.git</developerConnection>
<tag>HEAD</tag>
</scm>
<modules/>
<profiles>
<profile>
<id>tdlib</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>tdlib</module>
</modules>
</profile>
<profile>
<id>tdlight</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<modules>
<module>tdlight</module>
</modules>
</profile>
</profiles>
</project>

View File

@ -2,7 +2,6 @@
set -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# REVISION = <revision>
# IMPLEMENTATION_NAME = <tdlib | tdlight>
cd ./scripts/core/
./deploy_release.sh

View File

@ -1,7 +1,6 @@
#!/bin/bash -e
set -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# IMPLEMENTATION_NAME = <tdlib | tdlight>
cd ./scripts/core/
./deploy_snapshot.sh

View File

@ -1,7 +1,6 @@
#!/bin/bash -e
set -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# IMPLEMENTATION_NAME = <tdlib | tdlight>
cd ./scripts/core/
./only_compile_release.sh

View File

@ -1,21 +1,18 @@
#!/bin/bash -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# REVISION = <revision>
# IMPLEMENTATION_NAME = <tdlib | tdlight>
# Check variables correctness
if [ -z "${REVISION}" ]; then
echo "Missing parameter: REVISION"
exit 1
fi
if [ -z "${IMPLEMENTATION_NAME}" ]; then
echo "Missing parameter: IMPLEMENTATION_NAME"
exit 1
fi
cd ../../
cd "../../"
mvn -B -P "${IMPLEMENTATION_NAME}" -Drevision="${REVISION}" clean deploy
cd "bom"
mvn -B -Drevision="${REVISION}" clean deploy
cd "../"
echo "Done."
exit 0

View File

@ -1,16 +1,11 @@
#!/bin/bash -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# IMPLEMENTATION_NAME = <tdlib | tdlight>
# Check variables correctness
if [ -z "${IMPLEMENTATION_NAME}" ]; then
echo "Missing parameter: IMPLEMENTATION_NAME"
exit 1
fi
cd "../../"
cd ../../
mvn -B -P "${IMPLEMENTATION_NAME}" clean deploy
cd "bom"
mvn -B clean deploy
cd "../"
echo "Done."
exit 0

View File

@ -1,16 +1,11 @@
#!/bin/bash -e
# OTHER REQUIRED ENVIRONMENT VARIABLES:
# IMPLEMENTATION_NAME = <tdlib | tdlight>
# Check variables correctness
if [ -z "${IMPLEMENTATION_NAME}" ]; then
echo "Missing parameter: IMPLEMENTATION_NAME"
exit 1
fi
cd "../../"
mvn -B -P "${IMPLEMENTATION_NAME}" clean package
cd "bom"
mvn -B clean package
cd "../"
echo "Done."
exit 0

View File

@ -45,7 +45,7 @@ final class AuthorizationStateWaitAuthenticationDataHandler implements GenericUp
}
}, exceptionHandler);
} else {
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false);
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false, false, null);
String phoneNumber = String.valueOf(authenticationData.getUserPhoneNumber());
SetAuthenticationPhoneNumber response = new SetAuthenticationPhoneNumber(phoneNumber, phoneSettings);

View File

@ -73,7 +73,7 @@ final class CommandsHandler implements GenericUpdateHandler<UpdateNewMessage> {
if (response.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) response);
}
handler.onCommand((Chat) response, message.sender, arguments);
handler.onCommand((Chat) response, message.senderId, arguments);
}, error -> logger.warn("Error when handling the command {}", commandName, error));
}
}

View File

@ -47,24 +47,22 @@ public final class InternalClient implements ClientEventsHandler, TelegramClient
@Override
public void handleEvents(boolean isClosed, long[] eventIds, TdApi.Object[] events, int arrayOffset, int arrayLength) {
if (updatesHandler != null) {
LongArrayList idsToFilter = new LongArrayList(eventIds);
ObjectArrayList<TdApi.Object> eventsToFilter = new ObjectArrayList<>(events);
ObjectArrayList<TdApi.Object> updatesList = new ObjectArrayList<>(arrayLength);
for (int i = (arrayOffset + arrayLength) - 1; i >= arrayOffset; i--) {
if (eventIds[i] != 0) {
idsToFilter.removeLong(i);
eventsToFilter.remove(i);
long eventId = eventIds[i];
TdApi.Object event = events[i];
Handler<?> handler = handlers.remove(eventId);
handleResponse(eventId, event, handler);
} else {
updatesList.add(events[i]);
}
}
try {
updatesHandler.getUpdatesHandler().onUpdates(eventsToFilter);
updatesHandler.getUpdatesHandler().onUpdates(updatesList);
} catch (Throwable cause) {
handleException(updatesHandler.getExceptionHandler(), cause);
}

View File

@ -35,7 +35,7 @@ public final class InternalClientManager implements AutoCloseable {
System.exit(1);
}
this.implementationName = implementationName;
responseReceiver = new ResponseReceiver(this::handleClientEvents);
responseReceiver = new NativeResponseReceiver(this::handleClientEvents);
}
/**

View File

@ -0,0 +1,16 @@
package it.tdlight.common.internal;
import it.tdlight.common.EventsHandler;
import it.tdlight.jni.TdApi.Object;
public class NativeResponseReceiver extends ResponseReceiver {
public NativeResponseReceiver(EventsHandler eventsHandler) {
super(eventsHandler);
}
@Override
public int receive(int[] clientIds, long[] eventIds, Object[] events, double timeout) {
return NativeClientAccess.receive(clientIds, eventIds, events, timeout);
}
}

View File

@ -7,18 +7,19 @@ import it.tdlight.common.EventsHandler;
import it.tdlight.common.utils.IntSwapper;
import it.tdlight.common.utils.SpinWaitSupport;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
public final class ResponseReceiver extends Thread implements AutoCloseable {
abstract class ResponseReceiver extends Thread implements AutoCloseable {
private static final String FLAG_PAUSE_SHUTDOWN_UNTIL_ALL_CLOSED = "it.tdlight.pauseShutdownUntilAllClosed";
private static final String FLAG_USE_OPTIMIZED_DISPATCHER = "tdlight.dispatcher.use_optimized_dispatcher";
@ -58,30 +59,34 @@ public final class ResponseReceiver extends Thread implements AutoCloseable {
this.setDaemon(true);
}
/**
* @return results count
*/
public abstract int receive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout);
@Override
public synchronized void start() {
public void run() {
if (closeCalled.get()) {
throw new IllegalStateException("Closed");
}
if (startCalled.compareAndSet(false, true)) {
super.start();
this.runInternal();
} else {
throw new IllegalStateException("Start already called");
}
}
@Override
public void run() {
private void runInternal() {
int[] sortIndex;
try {
boolean interrupted;
while (
!(interrupted = Thread.interrupted())
&& !jvmShutdown.get()
&& (!closeCalled.get() || !registeredClients.isEmpty())
&& !jvmShutdown.get()
&& (!closeCalled.get() || !registeredClients.isEmpty())
) {
// Timeout is expressed in seconds
int resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0);
int resultsCount = receive(clientIds, eventIds, events, 2.0);
if (resultsCount <= 0) {
SpinWaitSupport.onSpinWait();
@ -145,13 +150,39 @@ public final class ResponseReceiver extends Thread implements AutoCloseable {
public final int clientId;
public final long eventId;
public final Object event;
public final TdApi.Object event;
public Event(int clientId, long eventId, Object event) {
public Event(int clientId, long eventId, TdApi.Object event) {
this.clientId = clientId;
this.eventId = eventId;
this.event = event;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Event event1 = (Event) o;
return clientId == event1.clientId && eventId == event1.eventId && Objects.equals(event, event1.event);
}
@Override
public int hashCode() {
return Objects.hash(clientId, eventId, event);
}
@Override
public String toString() {
return new StringJoiner(", ", Event.class.getSimpleName() + "[", "]")
.add("clientId=" + clientId)
.add("eventId=" + eventId)
.add("event=" + event)
.toString();
}
}
List<Event> eventsList = new ArrayList<>(resultsCount);

View File

@ -0,0 +1,51 @@
package it.tdlight.common.internal;
import it.tdlight.jni.TdApi;
import java.util.Objects;
public final class Event {
private final int clientId;
private final long eventId;
private final TdApi.Object event;
Event(int clientId, long eventId, TdApi.Object event) {
this.clientId = clientId;
this.eventId = eventId;
this.event = event;
}
public int clientId() {
return clientId;
}
public long eventId() {
return eventId;
}
public TdApi.Object event() {
return event;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Event that = (Event) obj;
return this.clientId == that.clientId && this.eventId == that.eventId && Objects.equals(this.event, that.event);
}
@Override
public int hashCode() {
return Objects.hash(clientId, eventId, event);
}
@Override
public String toString() {
return "Event[" + "clientId=" + clientId + ", " + "eventId=" + eventId + ", " + "event=" + event + ']';
}
}

View File

@ -0,0 +1,124 @@
package it.tdlight.common.internal;
import static org.junit.jupiter.api.Assertions.*;
import it.tdlight.common.EventsHandler;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
import it.unimi.dsi.fastutil.longs.LongArraySet;
import it.unimi.dsi.fastutil.longs.LongSets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class HandleEventsTest {
@Test
public void test() throws InterruptedException {
System.setProperty("tdlight.dispatcher.use_optimized_dispatcher", "false");
List<Event> initialEvents = new ArrayList<>();
initialEvents.add(new Event(2, 0, new TdApi.LogVerbosityLevel(0)));
initialEvents.add(new Event(1, 1, new TdApi.LogVerbosityLevel(1)));
initialEvents.add(new Event(7, 3, new TdApi.LogVerbosityLevel(2)));
initialEvents.add(new Event(7, 0, new TdApi.LogVerbosityLevel(3)));
initialEvents.add(new Event(1, 7, new TdApi.LogVerbosityLevel(4)));
initialEvents.add(new Event(2, 398, new TdApi.LogVerbosityLevel(5)));
initialEvents.add(new Event(7, 98832, new TdApi.LogVerbosityLevel(6)));
initialEvents.add(new Event(2, 32832, new TdApi.LogVerbosityLevel(7)));
initialEvents.add(new Event(1, 39484, new TdApi.LogVerbosityLevel(8)));
initialEvents.add(new Event(1, 39485, new TdApi.LogVerbosityLevel(9)));
initialEvents.add(new Event(1, 39486, new TdApi.LogVerbosityLevel(10)));
initialEvents.add(new Event(1, 39487, new TdApi.LogVerbosityLevel(11)));
initialEvents.add(new Event(1, 39488, new TdApi.LogVerbosityLevel(12)));
CountDownLatch eventsQueueEmptied = new CountDownLatch(1);
ArrayBlockingQueue<Event> eventsQueue = new ArrayBlockingQueue<>(1024);
eventsQueue.addAll(initialEvents);
ArrayBlockingQueue<Result> results = new ArrayBlockingQueue<>(1024);
ResponseReceiver responseReceiver = new ResponseReceiver((clientId, isClosed, clientEventIds, clientEvents, arrayOffset, arrayLength) -> {
results.add(new Result(clientId,
isClosed,
Arrays.copyOf(clientEventIds, clientEventIds.length),
Arrays.copyOf(clientEvents, clientEvents.length),
arrayOffset,
arrayLength
));
}) {
@Override
public int receive(int[] clientIds, long[] eventIds, Object[] events, double timeout) {
int i = 0;
while (!eventsQueue.isEmpty() && i < clientIds.length) {
Event event = eventsQueue.poll();
clientIds[i] = event.clientId();
eventIds[i] = event.eventId();
events[i] = event.event();
i++;
}
if (eventsQueue.isEmpty()) {
eventsQueueEmptied.countDown();
}
return i;
}
};
responseReceiver.registerClient(2);
responseReceiver.registerClient(1);
responseReceiver.registerClient(7);
responseReceiver.start();
eventsQueueEmptied.await();
responseReceiver.interrupt();
responseReceiver.close();
HashMap<Integer, Result> resultsMap = new HashMap<>();
while (!results.isEmpty()) {
Result part = results.poll();
if (part.arrayLength() > 0) {
Result prev = resultsMap.get(part.clientId());
if (prev == null) {
prev = new Result(part.clientId(), false, new long[0], new TdApi.Object[0], 0, 0);
}
int newSize = part.arrayLength() + prev.arrayLength();
long[] newIds = new long[newSize];
TdApi.Object[] newEvents = new TdApi.Object[newSize];
int i = 0;
for (int i1 = 0; i1 < prev.arrayLength(); i1++, i++) {
newIds[i] = prev.clientEventIds()[i1 + prev.arrayOffset()];
newEvents[i] = prev.clientEvents()[i1 + prev.arrayOffset()];
}
for (int i1 = 0; i1 < part.arrayLength(); i1++, i++) {
newIds[i] = part.clientEventIds()[i1 + part.arrayOffset()];
newEvents[i] = part.clientEvents()[i1 + part.arrayOffset()];
}
resultsMap.put(part.clientId(), new Result(part.clientId(), part.isClosed() || prev.isClosed(), newIds, newEvents, 0, newSize));
}
}
Result client2Results = resultsMap.remove(2);
Result client1Results = resultsMap.remove(1);
Result client7Results = resultsMap.remove(7);
assertTrue(resultsMap.isEmpty());
assertEquals(0, results.size());
assertEquals(2, client2Results.clientId());
assertEquals(1, client1Results.clientId());
assertEquals(7, client7Results.clientId());
assertEquals(getClientEventIds(initialEvents, 2), LongArraySet.of(Arrays.copyOfRange(client2Results.clientEventIds(), client2Results.arrayOffset(), client2Results.arrayOffset() + client2Results.arrayLength())));
assertEquals(getClientEventIds(initialEvents, 1), LongArraySet.of(Arrays.copyOfRange(client1Results.clientEventIds(), client1Results.arrayOffset(), client1Results.arrayOffset() + client1Results.arrayLength())));
assertEquals(getClientEventIds(initialEvents, 7), LongArraySet.of(Arrays.copyOfRange(client7Results.clientEventIds(), client7Results.arrayOffset(), client7Results.arrayOffset() + client7Results.arrayLength())));
assertEquals(initialEvents.size(), client2Results.arrayLength() + client1Results.arrayLength() + client7Results.arrayLength());
assertEquals(3, client2Results.arrayLength());
assertEquals(7, client1Results.arrayLength());
assertEquals(3, client7Results.arrayLength());
}
private Set<Long> getClientEventIds(List<Event> initialEvents, long clientId) {
return initialEvents.stream().filter(e -> e.clientId() == clientId).map(Event::eventId).collect(Collectors.toSet());
}
}

View File

@ -0,0 +1,80 @@
package it.tdlight.common.internal;
import it.tdlight.jni.TdApi;
import java.util.List;
import java.util.Objects;
public final class Result {
private final int clientId;
private final boolean isClosed;
private final long[] clientEventIds;
private final TdApi.Object[] clientEvents;
private final int arrayOffset;
private final int arrayLength;
public Result(int clientId,
boolean isClosed,
long[] clientEventIds,
TdApi.Object[] clientEvents,
int arrayOffset,
int arrayLength) {
this.clientId = clientId;
this.isClosed = isClosed;
this.clientEventIds = clientEventIds;
this.clientEvents = clientEvents;
this.arrayOffset = arrayOffset;
this.arrayLength = arrayLength;
}
public int clientId() {
return clientId;
}
public boolean isClosed() {
return isClosed;
}
public long[] clientEventIds() {
return clientEventIds;
}
public TdApi.Object[] clientEvents() {
return clientEvents;
}
public int arrayOffset() {
return arrayOffset;
}
public int arrayLength() {
return arrayLength;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Result that = (Result) obj;
return this.clientId == that.clientId && this.isClosed == that.isClosed && Objects.equals(this.clientEventIds,
that.clientEventIds
) && Objects.equals(this.clientEvents, that.clientEvents) && this.arrayOffset == that.arrayOffset
&& this.arrayLength == that.arrayLength;
}
@Override
public int hashCode() {
return Objects.hash(clientId, isClosed, clientEventIds, clientEvents, arrayOffset, arrayLength);
}
@Override
public String toString() {
return "Result[" + "clientId=" + clientId + ", " + "isClosed=" + isClosed + ", " + "clientEventIds="
+ clientEventIds + ", " + "clientEvents=" + clientEvents + ", " + "arrayOffset=" + arrayOffset + ", "
+ "arrayLength=" + arrayLength + ']';
}
}

View File

@ -9,11 +9,12 @@
<groupId>it.tdlight</groupId>
<version>${revision}</version>
<artifactId>tdlight-java-parent</artifactId>
<relativePath>../pom.xml</relativePath>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>1.0.0.0-SNAPSHOT</revision>
<tdlib.api.artifact.id>tdlib-api-legacy</tdlib.api.artifact.id>
</properties>
<repositories>
<repository>
@ -47,13 +48,7 @@
<dependencies>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-api</artifactId>
<version>4.0.${apiRevisionNumber}${apiRevisionSuffix}</version>
</dependency>
<dependency>
<groupId>it.cavallium</groupId>
<artifactId>concurrent-locks</artifactId>
<version>1.0.8</version>
<artifactId>${tdlib.api.artifact.id}</artifactId>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
@ -77,23 +72,21 @@
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.6</version>
</dependency>
<dependency>
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId>
<version>${revision}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
@ -110,9 +103,28 @@
<directory>target-snapshot</directory>
</build>
</profile>
<profile>
<id>jdk-with-sealed-interfaces</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties>
<tdlib.api.artifact.id>tdlib-api-sealed</tdlib.api.artifact.id>
</properties>
</profile>
<profile>
<id>jdk-with-legacy-interfaces</id>
<activation>
<jdk>(,17)</jdk>
</activation>
<properties>
<tdlib.api.artifact.id>tdlib-api-legacy</tdlib.api.artifact.id>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>${project.basedir}/../src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/../src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
@ -243,12 +255,11 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.2.2</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
@ -256,6 +267,7 @@
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
@ -284,6 +296,18 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -9,11 +9,12 @@
<groupId>it.tdlight</groupId>
<version>${revision}</version>
<artifactId>tdlight-java-parent</artifactId>
<relativePath>../pom.xml</relativePath>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>1.0.0.0-SNAPSHOT</revision>
<tdlight.api.artifact.id>tdlight-api-legacy</tdlight.api.artifact.id>
</properties>
<repositories>
<repository>
@ -47,13 +48,7 @@
<dependencies>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-api</artifactId>
<version>4.0.${apiRevisionNumber}${apiRevisionSuffix}</version>
</dependency>
<dependency>
<groupId>it.cavallium</groupId>
<artifactId>concurrent-locks</artifactId>
<version>1.0.8</version>
<artifactId>${tdlight.api.artifact.id}</artifactId>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
@ -77,23 +72,21 @@
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.6</version>
</dependency>
<dependency>
<groupId>net.harawata</groupId>
<artifactId>appdirs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId>
<version>${revision}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
@ -110,9 +103,28 @@
<directory>target-snapshot</directory>
</build>
</profile>
<profile>
<id>jdk-with-sealed-interfaces</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties>
<tdlight.api.artifact.id>tdlight-api-sealed</tdlight.api.artifact.id>
</properties>
</profile>
<profile>
<id>jdk-with-legacy-interfaces</id>
<activation>
<jdk>(,17)</jdk>
</activation>
<properties>
<tdlight.api.artifact.id>tdlight-api-legacy</tdlight.api.artifact.id>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>${project.basedir}/../src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/../src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
@ -187,6 +199,7 @@
<excludes>
<exclude>it/tdlight/tdlib/ClientManager.java</exclude>
</excludes>
<release>8</release>
</configuration>
<executions>
<!-- disable default phase due to fixed id and position in lifecycle -->
@ -243,12 +256,11 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.2.2</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
@ -256,6 +268,7 @@
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
@ -300,6 +313,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>