Restructuring developement branch

This commit is contained in:
Andrea Cavalli 2020-08-18 23:26:02 +02:00
parent f7247d0fa0
commit 98ee20f407
32 changed files with 7 additions and 1323 deletions

12
.gitmodules vendored
View File

@ -1,9 +1,7 @@
[submodule "src/main/jni/td"]
path = src/main/jni/td
[submodule "dependencies/tdlight"]
path = dependencies/tdlight
url = https://git.ignuranza.net/tdlight-team/tdlight.git
[submodule "common-utils"]
path = common-utils
url = https://git.ignuranza.net/tdlight-team/common-utils.git
[submodule "src/main/jni/tdlib-serializer"]
path = src/main/jni/tdlib-serializer
[submodule "dependencies/tdlib-serializer"]
path = dependencies/tdlib-serializer
url = https://git.ignuranza.net/windoz/tdlib-serializer.git

@ -1 +0,0 @@
Subproject commit 8da67564c5a006dd31c2d0528768af8f790c1a67

1
dependencies/tdlib-serializer vendored Submodule

@ -0,0 +1 @@
Subproject commit 57bf30757952daafcd28352e38fda21e260ce664

1
dependencies/tdlight vendored Submodule

@ -0,0 +1 @@
Subproject commit b93239f6d4315e9823076390d5e16db1379e9058

112
pom.xml
View File

@ -1,112 +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</artifactId>
<version>2.168.0</version>
<name>TDLight Java Wrapper</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- required for jdk9 -->
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>github</id>
<name>GitHub TDLight Team Apache Maven Packages</name>
<url>https://maven.pkg.github.com/tdlight-team/tdlight-java</url>
</repository>
<repository>
<id>github-alternative</id>
<name>GitHub Cavallium Apache Maven Packages</name>
<url>https://maven.pkg.github.com/Cavallium/common-utils</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub TDLight Team Apache Maven Packages</name>
<url>https://maven.pkg.github.com/tdlight-team/tdlight-java</url>
</repository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.warp</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-amd64</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<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>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

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

View File

@ -1,150 +0,0 @@
package it.tdlight.tdlight;
import it.tdlight.tdnatives.TdApi.Object;
import it.tdlight.tdlight.natives.NativeClient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.StampedLock;
/**
* Interface for interaction with TDLib.
*/
public class Client extends NativeClient implements TelegramClient {
private long clientId;
private final ReentrantLock receiveLock = new ReentrantLock();
private final StampedLock executionLock = new StampedLock();
private volatile Long stampedLockValue = 1L;
/**
* Creates a new TDLib client.
*/
public Client() {
super();
try {
Init.start();
} catch (Throwable throwable) {
throwable.printStackTrace();
System.exit(1);
}
this.clientId = createNativeClient();
}
/**
* Sends request to TDLib. May be called from any thread.
* @param request Request to TDLib.
*/
@Override
public void send(Request request) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
}
nativeClientSend(this.clientId, request.getId(), request.getFunction());
}
private long[] eventIds;
private Object[] events;
/**
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
* @param eventSize Maximum number of events allowed in list.
* @return An incoming update or request response list. The object returned in the response may be an empty list if the timeout expires.
*/
@Override
public List<Response> receive(double timeout, int eventSize) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
}
var responseList = new ArrayList<Response>();
if (eventIds == null) {
eventIds = new long[eventSize];
events = new Object[eventSize];
} else if (eventIds.length != eventSize) {
throw new IllegalArgumentException("EventSize can't change! Previous value = " + eventIds.length + " New value = " + eventSize);
} else {
Arrays.fill(eventIds, 0);
Arrays.fill(events, null);
}
if (this.receiveLock.isLocked()) {
throw new IllegalThreadStateException("Thread: " + Thread.currentThread().getName() + " trying receive incoming updates but shouldn't be called simultaneously from two different threads!");
}
int resultSize;
this.receiveLock.lock();
try {
resultSize = nativeClientReceive(this.clientId, eventIds, events, timeout);
} finally {
this.receiveLock.unlock();
}
for (int i = 0; i < resultSize; i++) {
responseList.add(new Response(eventIds[i], events[i]));
}
return responseList;
}
/**
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
* @return An incoming update or request response. The object returned in the response may be a nullptr if the timeout expires.
*/
@Override
public Response receive(double timeout) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
}
var responseList = receive(timeout, 1);
if (responseList.size() < 1) {
return null;
}
return responseList.get(0);
}
/**
* Synchronously executes TDLib requests. Only a few requests can be executed synchronously. May be called from any thread.
* @param request Request to the TDLib.
* @return The request response.
*/
@Override
public Response execute(Request request) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
}
Object object = nativeClientExecute(request.getFunction());
return new Response(0, object);
}
/**
* Destroys the client and TDLib instance.
*/
@Override
public void destroyClient() {
stampedLockValue = this.executionLock.tryWriteLock();
destroyNativeClient(this.clientId);
}
/**
* Destroys the client and TDLib instance.
*/
@Override
public void initializeClient() {
this.executionLock.tryUnlockWrite();
stampedLockValue = null;
this.clientId = createNativeClient();
}
@Override
public boolean isDestroyed() {
return this.executionLock.isWriteLocked();
}
}

View File

@ -1,91 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
import it.tdlight.tdnatives.TdApi;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
/**
* Identify the class by using the Constructor.
*/
public class ConstructorDetector {
static {
// Call this to load static methods and prevent errors during startup!
try {
Init.start();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private static ConcurrentHashMap<Integer, Class> constructorHashMap;
/**
* Initialize the ConstructorDetector, it is called from the Init class.
*/
public static void init() {
if (constructorHashMap != null) {
return;
}
Class[] classes = TdApi.class.getDeclaredClasses();
setConstructorHashMap(classes);
}
/**
* Identify the class.
* @param CONSTRUCTOR CONSTRUCTOR of the Tdlib API.
* @return The class related to CONSTRUCTOR.
*/
public static Class getClass(int CONSTRUCTOR) {
return constructorHashMap.getOrDefault(CONSTRUCTOR, null);
}
private static void setConstructorHashMap(Class[] tdApiClasses) {
constructorHashMap = new ConcurrentHashMap<>();
for (Class apiClass : tdApiClasses) {
Field CONSTRUCTORField;
int CONSTRUCTOR;
try {
CONSTRUCTORField = apiClass.getDeclaredField("CONSTRUCTOR");
} catch (NoSuchFieldException e) {
continue;
}
try {
CONSTRUCTOR = CONSTRUCTORField.getInt(null);
} catch (IllegalAccessException e) {
continue;
}
constructorHashMap.put(CONSTRUCTOR, apiClass);
}
}
public static ConcurrentHashMap<Integer, Class> getTDConstructorsUnsafe() {
return constructorHashMap;
}
public static void registerExternalClass(int constructor, Class<?> clazz) {
constructorHashMap.put(constructor, clazz);
}
}

View File

@ -1,29 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
/**
* A type of callback function that will be called when a fatal error happens.
*/
public interface FatalErrorCallbackPtr {
/**
* Send error message to callback.
* @param error_message String with a description of a happened fatal error.
*/
void onFatalError(String error_message);
}

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
import it.tdlight.tdlight.utils.CantLoadLibrary;
import it.tdlight.tdlight.utils.LoadLibrary;
import it.tdlight.tdlight.utils.Os;
/**
* Init class to successfully initialize Tdlib
*/
public class Init {
private static boolean started = false;
/**
* Initialize Tdlib
*
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/
public synchronized static void start() throws Throwable {
if (!started) {
var os = LoadLibrary.getOs();
if (os == Os.win) {
LoadLibrary.load("libeay32");
LoadLibrary.load("ssleay32");
LoadLibrary.load("zlib1");
}
LoadLibrary.load("tdjni");
ConstructorDetector.init();
started = true;
}
}
}

View File

@ -1,31 +0,0 @@
package it.tdlight.tdlight;
import it.tdlight.tdlight.natives.NativeLog;
import java.util.Objects;
public class Log extends NativeLog {
static {
try {
Init.start();
} catch (Throwable throwable) {
throwable.printStackTrace();
System.exit(0);
}
}
private static final FatalErrorCallbackPtr defaultFatalErrorCallbackPtr = System.err::println;
private static FatalErrorCallbackPtr fatalErrorCallback = defaultFatalErrorCallbackPtr;
/**
* Sets the callback that will be called when a fatal error happens. None of the TDLib methods can be called from the callback. The TDLib will crash as soon as callback returns. By default the callback set to print in stderr.
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default callback.
*/
public static void setFatalErrorCallback(FatalErrorCallbackPtr fatalErrorCallback) {
Log.fatalErrorCallback = Objects.requireNonNullElse(fatalErrorCallback, defaultFatalErrorCallbackPtr);
}
private static void onFatalError(String errorMessage) {
new Thread(() -> Log.fatalErrorCallback.onFatalError(errorMessage)).start();
}
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
import it.tdlight.tdnatives.TdApi.Function;
/**
* A request to the TDLib.
*/
public class Request {
private long id;
private Function function;
/**
* Creates a request with eventId and function.
* @param id Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
* @param function TDLib API function representing a request to TDLib.
*/
public Request(long id, Function function) {
this.id = id;
this.function = function;
}
/**
* Get request identifier.
* @return Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
*/
public long getId() {
return this.id;
}
/**
* Get TDLib API function.
* @return TDLib API function representing a request to TDLib.
*/
public Function getFunction() {
return this.function;
}
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
import it.tdlight.tdnatives.TdApi.Object;
/**
* A response to a request, or an incoming update from TDLib.
*/
public class Response {
private long id;
private Object object;
/**
* Creates a response with eventId and object, do not create answers explicitly! you must receive the reply through a client.
* @param id TDLib request identifier, which corresponds to the response or 0 for incoming updates from TDLib.
* @param object TDLib API object representing a response to a TDLib request or an incoming update.
*/
public Response(long id, Object object) {
this.id = id;
this.object = object;
}
/**
* Get TDLib request identifier.
* @return TDLib request identifier, which corresponds to the response or 0 for incoming updates from TDLib.
*/
public long getId() {
return this.id;
}
/**
* Get TDLib API object.
* @return TDLib API object representing a response to a TDLib request or an incoming update.
*/
public Object getObject() {
return this.object;
}
}

View File

@ -1,33 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
/**
* An array of incoming updates from TDLib.
*/
public class ResponseArray {
private Response[] responses;
public ResponseArray(Response[] responses) {
this.responses = responses;
}
public Response[] getResponses() {
return responses;
}
}

View File

@ -1,146 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
import it.tdlight.tdlight.utils.CloseCallback;
import it.tdlight.tdlight.utils.ErrorCallback;
import it.tdlight.tdlight.utils.ReceiveCallback;
/**
* Interface of callback for interaction with TDLib.
*/
public class TdCallback {
private ReceiveCallback receiveCallback;
private ErrorCallback errorCallback;
private CloseCallback closeCallback;
/**
* Creates a new TdCallback.
*
* @param receiveCallback Interface of callback for receive incoming update or request response.
* @param errorCallback Interface of callback for receive incoming error response.
* @param closeCallback Interface of callback for receive notification of closing Tdlib.
*/
public TdCallback(ReceiveCallback receiveCallback, ErrorCallback errorCallback, CloseCallback closeCallback) {
this.receiveCallback = receiveCallback;
this.errorCallback = errorCallback;
this.closeCallback = closeCallback;
}
/**
* Creates a new TdCallback.
*
* @param receiveCallback Interface of callback for receive incoming update or request response.
*/
public TdCallback(ReceiveCallback receiveCallback) {
this.receiveCallback = receiveCallback;
}
/**
* Creates a new TdCallback.
*
* @param errorCallback Interface of callback for receive incoming error response.
*/
public TdCallback(ErrorCallback errorCallback) {
this.errorCallback = errorCallback;
}
/**
* Creates a new TdCallback.
*
* @param closeCallback Interface of callback for receive notification of closing Tdlib.
*/
public TdCallback(CloseCallback closeCallback) {
this.closeCallback = closeCallback;
}
/**
* Creates a new TdCallback.
*
* @param receiveCallback Interface of callback for receive incoming update or request response.
* @param errorCallback Interface of callback for receive incoming error response.
*/
public TdCallback(ReceiveCallback receiveCallback, ErrorCallback errorCallback) {
this.receiveCallback = receiveCallback;
this.errorCallback = errorCallback;
}
/**
* Creates a new TdCallback.
*
* @param errorCallback Interface of callback for receive incoming error response.
* @param closeCallback Interface of callback for receive notification of closing Tdlib.
*/
public TdCallback(ErrorCallback errorCallback, CloseCallback closeCallback) {
this.errorCallback = errorCallback;
this.closeCallback = closeCallback;
}
/**
* Creates a new TdCallback.
*
* @param receiveCallback Interface of callback for receive incoming update or request response.
* @param closeCallback Interface of callback for receive notification of closing Tdlib.
*/
public TdCallback(ReceiveCallback receiveCallback, CloseCallback closeCallback) {
this.receiveCallback = receiveCallback;
this.closeCallback = closeCallback;
}
/**
* Get ReceiveCallback.
*
* @return This method return ReceiveCallback or "null callback" (a callback that receives records from tdlib but does not perform any operation) if is null.
*/
public ReceiveCallback getReceiveCallback() {
if (this.receiveCallback == null) {
return response -> {
};
}
return this.receiveCallback;
}
/**
* Get ErrorCallback.
*
* @return This method return ErrorCallback or "null callback" (a callback that receives records from tdlib but does not perform any operation) if is null.
*/
public ErrorCallback getErrorCallback() {
if (this.errorCallback == null) {
return error -> {
};
}
return this.errorCallback;
}
/**
* Get CloseCallback.
*
* @return This method return CloseCallback or "null callback" (a callback that receives records from tdlib but does not perform any operation) if is null.
*/
public CloseCallback getCloseCallback() {
if (this.closeCallback == null) {
return () -> {
};
}
return this.closeCallback;
}
}

View File

@ -1,21 +0,0 @@
package it.tdlight.tdlight;
import java.io.IOException;
import java.util.List;
public interface TelegramClient {
void send(Request request);
List<Response> receive(double timeout, int eventSize);
Response receive(double timeout);
Response execute(Request request);
void destroyClient();
void initializeClient() throws IOException;
boolean isDestroyed();
}

View File

@ -1,30 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
/**
* Enumeration with all architectures recognized by this library.
*/
public enum Arch {
amd64,
i386,
armhf,
aarch64,
ppc64le,
unknown
}

View File

@ -1,30 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
/**
* An exception that is thrown when the LoadLibrary class fails to load the library.
*/
public class CantLoadLibrary extends RuntimeException {
/**
* Creates a new CantLoadLibrary exception.
*/
CantLoadLibrary() {
super("FATAL: org.ernytech.tdlib.utils.Init failled when load tdlib library, execution can't continue");
}
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
/**
* Interface of callback for receive notification of closing Tdlib.
*/
public interface CloseCallback {
/**
* This method is called when tdlib is closing
*/
void onClosed();
}

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
import it.tdlight.tdlight.Response;
/**
* Interface of callback for receive incoming error response.
*/
public interface ErrorCallback {
/**
* This method is called when the library receives error responses
* @param error The incoming error response.
*/
void onError(Response error);
}

View File

@ -1,166 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
import java.io.IOException;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
/**
* The class to load the libraries needed to run Tdlib
*/
public class LoadLibrary {
private static ConcurrentHashMap<String, Boolean> libraryLoaded = new ConcurrentHashMap<>();
private static Path librariesPath = Paths.get(".JTDLibLibraries");
private static final String libsVersion = LibraryVersion.VERSION;
static {
if (Files.notExists(librariesPath)) {
try {
Files.createDirectories(librariesPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Load a library installed in the system (priority choice) or a library included in the jar.
*
* @param libname The name of the library.
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/
public static void load(String libname) throws Throwable {
if (libname == null || libname.trim().isEmpty()) {
throw new IllegalArgumentException();
}
if (libraryLoaded.containsKey(libname)) {
if (libraryLoaded.get(libname)) {
return;
}
}
loadLibrary(libname);
libraryLoaded.put(libname, true);
}
private static void loadLibrary(String libname) throws Throwable {
if (loadSysLibrary(libname)) {
return;
}
var arch = getCpuArch();
var os = getOs();
if (arch == Arch.unknown) {
throw new CantLoadLibrary().initCause(new IllegalStateException("Arch: \"" + System.getProperty("os.arch") + "\" is unknown"));
}
if (os == Os.unknown) {
throw new CantLoadLibrary().initCause(new IllegalStateException("Os: \"" + System.getProperty("os.name") + "\" is unknown"));
}
try {
loadJarLibrary(libname, arch, os);
} catch (IOException | CantLoadLibrary | UnsatisfiedLinkError e) {
throw new CantLoadLibrary().initCause(e);
}
}
private static boolean loadSysLibrary(String libname) {
try {
System.loadLibrary(libname);
} catch (UnsatisfiedLinkError e) {
return false;
}
return true;
}
private static void loadJarLibrary(String libname, Arch arch, Os os) throws IOException, CantLoadLibrary {
Path tempPath = Files.createDirectories(librariesPath.resolve("version-" + libsVersion).resolve(libname));
Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os));
var libInputStream = LoadLibrary.class.getResourceAsStream(createPath("libs", os.name(), arch.name(), libname) + getExt(os));
if (Files.notExists(tempFile)) {
Files.copy(libInputStream, tempFile);
}
libInputStream.close();
System.load(tempFile.toFile().getAbsolutePath());
}
private static Arch getCpuArch() {
var architecture = System.getProperty("os.arch").trim();
switch (architecture) {
case "amd64":
case "x86_64":
return Arch.amd64;
case "i386":
case "x86":
return Arch.i386;
case "arm":
return Arch.armhf;
case "arm64":
case "aarch64":
return Arch.aarch64;
case "ppc64":
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) // Java always returns ppc64 for all 64-bit powerpc but
return Arch.ppc64le; // powerpc64le (our target) is very different, it uses this condition to accurately identify the architecture
else
return Arch.unknown;
default:
return Arch.unknown;
}
}
public static Os getOs() {
var os = System.getProperty("os.name").toLowerCase().trim();
if (os.contains("linux"))
return Os.linux;
if (os.contains("windows"))
return Os.win;
if (os.contains("mac"))
return Os.mac;
if (os.contains("darwin"))
return Os.mac;
return Os.unknown;
}
private static String getExt(Os os) {
return (os == Os.win) ? ".dll" : ".so";
}
private static String createPath(String... path) {
var stringBuilder = new StringBuilder();
stringBuilder.append("/");
for (int i = 0; i < path.length; i++) {
stringBuilder.append(path[i]);
if (i < path.length - 1) {
stringBuilder.append("/");
}
}
return stringBuilder.toString();
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
import it.tdlight.tdlight.Response;
/**
* Interface of callback for receive incoming error response.
*/
public interface NativeErrorCallback {
/**
* This method is called when the library receives error responses
* @param error The incoming error response.
*/
void onNativeError(Response error);
}

View File

@ -1,28 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
/**
* Enumeration with all operating systems recognized by this library.
*/
public enum Os {
linux,
win,
mac,
unknown
}

View File

@ -1,31 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight.utils;
import it.tdlight.tdlight.Response;
/**
* Interface of callback for receive incoming update or request response.
*/
public interface ReceiveCallback {
/**
* This method is called when the library receives update or request response.
* @param response The incoming update or request response.
*/
void onResult(Response response);
}

View File

@ -1,164 +0,0 @@
package it.tdlight.tdlight.utils;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;
public class ScannerUtils {
private static final Scanner scanner = new Scanner(System.in);
private static final ReentrantLock lock = new ReentrantLock();
private static final PrintStream emptyOut = new java.io.PrintStream(new java.io.OutputStream() {
@Override
public void write(int b) {
}
}) {
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) {
}
@Override
public void write(byte[] b) {
}
@Override
public void write(byte[] buf, int off, int len) {
}
@Override
public void print(boolean b) {
}
@Override
public void print(char c) {
}
@Override
public void print(int i) {
}
@Override
public void print(long l) {
}
@Override
public void print(float f) {
}
@Override
public void print(double d) {
}
@Override
public void print(char[] s) {
}
@Override
public void print(String s) {
}
@Override
public void print(Object obj) {
}
@Override
public void println() {
}
@Override
public void println(boolean x) {
}
@Override
public void println(char x) {
}
@Override
public void println(int x) {
}
@Override
public void println(long x) {
}
@Override
public void println(float x) {
}
@Override
public void println(double x) {
}
@Override
public void println(char[] x) {
}
@Override
public void println(String x) {
}
@Override
public void println(Object x) {
}
@Override
public java.io.PrintStream printf(String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream printf(java.util.Locale l, String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream format(String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream format(java.util.Locale l, String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream append(CharSequence csq) {
return this;
}
@Override
public java.io.PrintStream append(CharSequence csq, int start, int end) {
return this;
}
@Override
public java.io.PrintStream append(char c) {
return this;
}
};
public static String askParameter(String displayName, String question) {
try {
lock.lock();
StringBuilder toPrint = new StringBuilder();
var oldOut = System.out;
var oldErr = System.err;
System.setOut(emptyOut);
System.setErr(emptyOut);
oldOut.print("[" + displayName + "] " + question + ": ");
var result = scanner.nextLine();
System.setOut(oldOut);
System.setErr(oldErr);
return result;
} finally {
lock.unlock();
}
}
}