Merge tdlib and tdlight branches into a single source directory

This commit is contained in:
Andrea Cavalli 2020-10-11 16:27:57 +02:00
parent da9942fd7a
commit bee3e185f7
25 changed files with 54894 additions and 34 deletions

View File

@ -89,11 +89,11 @@ dependencies {
## Usage
Simple initialization of a native TDLib client
```java
import java.io.File;
import it.tdlight.tdlight.Client;
import it.tdlight.tdlight.Init;
import it.tdlight.tdlight.Log;
import it.tdlight.common.Init;
import it.tdlight.common.Log;
public class Example {
public static void main(String[] args) {

View File

@ -1,4 +1,5 @@
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
public final class LibraryVersion {
public static final String VERSION = "${project.version}";
public static String IMPLEMENTATION_NAME = "tdlib";
}

View File

@ -0,0 +1,5 @@
package it.tdlight.common.utils;
public final class LibraryVersion {
public static final String VERSION = "${project.version}";
public static String IMPLEMENTATION_NAME = "tdlight";
}

View File

@ -1,4 +1,4 @@
package it.tdlight.tdlight;
package it.tdlight.common;
import java.util.StringJoiner;

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.common;
import it.tdlight.jni.TdApi;
import java.lang.reflect.Field;

View File

@ -15,11 +15,11 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.common;
import it.tdlight.tdlight.utils.CantLoadLibrary;
import it.tdlight.tdlight.utils.LoadLibrary;
import it.tdlight.tdlight.utils.Os;
import it.tdlight.common.utils.CantLoadLibrary;
import it.tdlight.common.utils.LoadLibrary;
import it.tdlight.common.utils.Os;
/**
* Init class to successfully initialize Tdlib
@ -38,12 +38,7 @@ public class Init {
Os os = LoadLibrary.getOs();
if (os == Os.win) {
LoadLibrary.load("libstdc++-6");
LoadLibrary.load("libwinpthread-1");
LoadLibrary.load("libgcc_s_seh-1");
LoadLibrary.load("libcrypto-3-x64");
LoadLibrary.load("libssl-3-x64");
LoadLibrary.load("zlib1");
// Since 3.0.0, libraries for windows are statically compiled into tdjni.dll
}
LoadLibrary.load("tdjni");

View File

@ -1,5 +1,6 @@
package it.tdlight.tdlight;
package it.tdlight.common;
import it.tdlight.common.Init;
import it.tdlight.jni.FatalErrorCallbackPtr;
import it.tdlight.jni.NativeLog;
import it.tdlight.jni.TdApi;

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.common;
import it.tdlight.jni.TdApi.Function;

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.common;
import it.tdlight.jni.TdApi.Object;

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.common;
/**
* An array of incoming updates from TDLib.

View File

@ -1,4 +1,4 @@
package it.tdlight.tdlight;
package it.tdlight.common;
import java.io.IOException;
import java.util.List;

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
/**
* Enumeration with all architectures recognized by this library.

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
/**
* An exception that is thrown when the LoadLibrary class fails to load the library.

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
/**
* Interface of callback for receive notification of closing Tdlib.

View File

@ -15,9 +15,9 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
import it.tdlight.tdlight.Response;
import it.tdlight.common.Response;
/**
* Interface of callback for receive incoming error response.

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
import java.io.IOException;
import java.io.InputStream;
@ -30,7 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class LoadLibrary {
private static ConcurrentHashMap<String, Boolean> libraryLoaded = new ConcurrentHashMap<>();
private static Path librariesPath = Paths.get(".tdlight-libs-cache");
private static Path librariesPath = Paths.get("."+ LibraryVersion.IMPLEMENTATION_NAME + "-libs-cache");
private static final String libsVersion = LibraryVersion.VERSION;
static {

View File

@ -15,9 +15,9 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
import it.tdlight.tdlight.Response;
import it.tdlight.common.Response;
/**
* Interface of callback for receive incoming error response.

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
/**
* Enumeration with all operating systems recognized by this library.

View File

@ -15,9 +15,9 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
import it.tdlight.tdlight.Response;
import it.tdlight.common.Response;
/**
* Interface of callback for receive incoming update or request response.

View File

@ -1,4 +1,4 @@
package it.tdlight.tdlight.utils;
package it.tdlight.common.utils;
import java.util.Scanner;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,248 @@
package it.tdlight.tdlib;
import it.cavallium.concurrentlocks.ReentrantReadWriteUpdateLock;
import it.tdlight.common.Init;
import it.tdlight.common.Request;
import it.tdlight.common.Response;
import it.tdlight.jni.NativeClient;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationStateClosed;
import it.tdlight.jni.TdApi.AuthorizationStateClosing;
import it.tdlight.jni.TdApi.AuthorizationStateWaitTdlibParameters;
import it.tdlight.jni.TdApi.GetOption;
import it.tdlight.jni.TdApi.Object;
import it.tdlight.jni.TdApi.SetOption;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
import it.tdlight.common.TelegramClient;
import it.tdlight.common.ClientState;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Interface for interaction with TDLib.
*/
public class Client extends NativeClient implements TelegramClient {
private ClientState state = ClientState.of(false, 0, false, false, false);
private final ReentrantReadWriteUpdateLock stateLock = new ReentrantReadWriteUpdateLock();
/**
* Creates a new TDLib client.
*/
public Client() {
try {
Init.start();
} catch (Throwable throwable) {
throwable.printStackTrace();
System.exit(1);
}
this.initializeClient();
}
@Override
public void send(Request request) {
long clientId;
stateLock.readLock().lock();
try {
requireInitialized();
requireReadyToSend(request.getFunction().getConstructor());
clientId = state.getClientId();
} finally {
stateLock.readLock().unlock();
}
nativeClientSend(clientId, request.getId(), request.getFunction());
}
@Override
public List<Response> receive(double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
long clientId;
stateLock.updateLock().lock();
try {
if (!state.isInitialized()) {
sleep(timeout);
return Collections.emptyList();
}
requireInitialized();
if (!state.isReadyToReceive()) {
sleep(timeout);
return Collections.emptyList();
}
requireReadyToReceive();
clientId = state.getClientId();
return Arrays.asList(this.internalReceive(clientId, timeout, eventsSize, receiveResponses, receiveUpdates));
} finally {
stateLock.updateLock().unlock();
}
}
private void sleep(double timeout) {
long nanos = (long) (timeout * 1000000000d);
int nanosPart = (int) (nanos % 1000000L);
long millis = Duration.ofNanos(nanos - nanosPart).toMillis();
try {
Thread.sleep(millis, nanosPart);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates) {
long clientId;
stateLock.updateLock().lock();
try {
if (!state.isInitialized()) {
sleep(timeout);
return null;
}
requireInitialized();
if (!state.isReadyToReceive()) {
sleep(timeout);
return null;
}
requireReadyToReceive();
clientId = state.getClientId();
Response[] responses = this.internalReceive(clientId, timeout, 1, receiveResponses, receiveUpdates);
if (responses.length > 0) {
return responses[0];
}
return null;
} finally {
stateLock.updateLock().unlock();
}
}
private Response[] internalReceive(long clientId, double timeout, int eventsSize, boolean receiveResponses, boolean receiveUpdates) {
long[] eventIds = new long[eventsSize];
TdApi.Object[] events = new TdApi.Object[eventsSize];
if (!(receiveResponses && receiveUpdates)) {
throw new IllegalArgumentException("The variables receiveResponses and receiveUpdates must be both true, because you are using the original TDLib!");
}
int resultSize = nativeClientReceive(clientId, eventIds, events, timeout);
Response[] responses = new Response[resultSize];
for (int i = 0; i < resultSize; i++) {
responses[i] = new Response(eventIds[i], events[i]);
if (eventIds[i] == 0) {
handleStateEvent(events[i]);
}
}
return responses;
}
private void handleStateEvent(Object event) {
if (event == null) {
return;
}
if (event.getConstructor() != UpdateAuthorizationState.CONSTRUCTOR) {
return;
}
UpdateAuthorizationState updateAuthorizationState = (UpdateAuthorizationState) event;
switch (updateAuthorizationState.authorizationState.getConstructor()) {
case AuthorizationStateWaitTdlibParameters.CONSTRUCTOR:
stateLock.writeLock().lock();
try {
state.setReadyToSend(true);
} finally {
stateLock.writeLock().unlock();
}
break;
case AuthorizationStateClosing.CONSTRUCTOR:
stateLock.writeLock().lock();
try {
state.setReadyToSend(false);
} finally {
stateLock.writeLock().unlock();
}
break;
case AuthorizationStateClosed.CONSTRUCTOR:
stateLock.writeLock().lock();
try {
state.setReadyToSend(false).setReadyToReceive(false);
} finally {
stateLock.writeLock().unlock();
}
break;
}
}
@Override
public Response execute(Request request) {
stateLock.readLock().lock();
try {
requireInitialized();
requireReadyToSend(request.getFunction().getConstructor());
} finally {
stateLock.readLock().unlock();
}
Object object = nativeClientExecute(request.getFunction());
return new Response(0, object);
}
@Override
public void destroyClient() {
stateLock.writeLock().lock();
try {
if (state.isInitialized() && state.hasClientId()) {
if (state.isReadyToSend() || state.isReadyToReceive()) {
throw new IllegalStateException("You need to close the Client before destroying it!");
}
destroyNativeClient(this.state.getClientId());
state = ClientState.of(false, 0, false, false, false);
}
} finally {
stateLock.writeLock().unlock();
}
}
@Override
public void initializeClient() {
stateLock.writeLock().lock();
try {
if (!state.isInitialized() && !state.hasClientId()) {
long clientId = createNativeClient();
state = ClientState.of(true, clientId, true, true, false);
}
} finally {
stateLock.writeLock().unlock();
}
}
private void requireInitialized() {
if (!state.isInitialized() || !state.hasClientId()) {
throw new IllegalStateException("Client not initialized");
}
}
private void requireReadyToSend(int constructor) {
if (!state.isReadyToSend()) {
switch (constructor) {
case SetOption.CONSTRUCTOR:
case GetOption.CONSTRUCTOR:
case TdApi.SetTdlibParameters.CONSTRUCTOR:
return;
}
throw new IllegalStateException("Client not ready to send");
}
}
private void requireReadyToReceive() {
if (!state.isReadyToReceive()) {
throw new IllegalStateException("Client not ready to receive");
}
}
}

View File

@ -1,5 +1,10 @@
package it.tdlight.tdlight;
import it.tdlight.common.ClientState;
import it.tdlight.common.Init;
import it.tdlight.common.Request;
import it.tdlight.common.Response;
import it.tdlight.common.TelegramClient;
import it.tdlight.jni.NativeClient;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationStateClosed;

137
tdlib/pom.xml Normal file
View File

@ -0,0 +1,137 @@
<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>tdlib-java</artifactId>
<version>3.169.0-SNAPSHOT</version>
<name>TDLib Java Wrapper</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
<dependencies>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-amd64</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-linux-aarch64</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-windows-amd64</artifactId>
<version>2.0.2</version>
</dependency>
<!-- Currently unsupported platform
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlib-natives-osx-amd64</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
-->
<dependency>
<groupId>it.cavallium</groupId>
<artifactId>concurrent-locks</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>it/tdlight/tdlight/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<pushChanges>false</pushChanges>
<remoteTagging>false</remoteTagging>
<preparationGoals>clean verify</preparationGoals>
<localCheckout>true</localCheckout>
<tagNameFormat>v@{project.version}</tagNameFormat>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filtering-java-templates</id>
<goals>
<goal>filter-sources</goal>
</goals>
<configuration>
<sourceDirectory>../src/main/java-templates-tdlib</sourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -63,8 +63,14 @@
<version>2.0.1-SNAPSHOT</version>
</dependency>
-->
<dependency>
<groupId>it.cavallium</groupId>
<artifactId>concurrent-locks</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
@ -108,6 +114,9 @@
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>it/tdlight/tdlib/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
@ -144,6 +153,9 @@
<goals>
<goal>filter-sources</goal>
</goals>
<configuration>
<sourceDirectory>../src/main/java-templates-tdlight</sourceDirectory>
</configuration>
</execution>
</executions>
</plugin>