From 98ee20f4072d4942689868200e7ee76ba78d2994 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 18 Aug 2020 23:26:02 +0200 Subject: [PATCH] Restructuring developement branch --- .gitmodules | 12 +- src/main/jni/build.sh => build.sh | 0 common-utils | 1 - dependencies/tdlib-serializer | 1 + dependencies/tdlight | 1 + pom.xml | 112 ------------ .../tdlight/tdlight/utils/LibraryVersion.java | 4 - src/main/java/it/tdlight/tdlight/Client.java | 150 ---------------- .../tdlight/tdlight/ConstructorDetector.java | 91 ---------- .../tdlight/FatalErrorCallbackPtr.java | 29 --- src/main/java/it/tdlight/tdlight/Init.java | 51 ------ src/main/java/it/tdlight/tdlight/Log.java | 31 ---- src/main/java/it/tdlight/tdlight/Request.java | 54 ------ .../java/it/tdlight/tdlight/Response.java | 54 ------ .../it/tdlight/tdlight/ResponseArray.java | 33 ---- .../java/it/tdlight/tdlight/TdCallback.java | 146 --------------- .../it/tdlight/tdlight/TelegramClient.java | 21 --- .../java/it/tdlight/tdlight/utils/Arch.java | 30 ---- .../tdlight/utils/CantLoadLibrary.java | 30 ---- .../tdlight/tdlight/utils/CloseCallback.java | 28 --- .../tdlight/tdlight/utils/ErrorCallback.java | 31 ---- .../it/tdlight/tdlight/utils/LoadLibrary.java | 166 ------------------ .../tdlight/utils/NativeErrorCallback.java | 31 ---- .../java/it/tdlight/tdlight/utils/Os.java | 28 --- .../tdlight/utils/ReceiveCallback.java | 31 ---- .../tdlight/tdlight/utils/ScannerUtils.java | 164 ----------------- .../jtdlib => tdnatives-cpp}/CMakeLists.txt | 0 .../jni/jtdlib => tdnatives-cpp}/sources.txt | 0 .../jni/jtdlib => tdnatives-cpp}/td_jni.cpp | 0 .../it/tdlight/tdnatives/NativeClient.java | 0 .../it/tdlight/tdnatives/NativeLog.java | 0 .../it/tdlight/tdnatives/TdApi.java | 0 32 files changed, 7 insertions(+), 1323 deletions(-) rename src/main/jni/build.sh => build.sh (100%) delete mode 160000 common-utils create mode 160000 dependencies/tdlib-serializer create mode 160000 dependencies/tdlight delete mode 100644 pom.xml delete mode 100644 src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java delete mode 100644 src/main/java/it/tdlight/tdlight/Client.java delete mode 100644 src/main/java/it/tdlight/tdlight/ConstructorDetector.java delete mode 100644 src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java delete mode 100644 src/main/java/it/tdlight/tdlight/Init.java delete mode 100644 src/main/java/it/tdlight/tdlight/Log.java delete mode 100644 src/main/java/it/tdlight/tdlight/Request.java delete mode 100644 src/main/java/it/tdlight/tdlight/Response.java delete mode 100644 src/main/java/it/tdlight/tdlight/ResponseArray.java delete mode 100644 src/main/java/it/tdlight/tdlight/TdCallback.java delete mode 100644 src/main/java/it/tdlight/tdlight/TelegramClient.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/Arch.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/CloseCallback.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/Os.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java delete mode 100644 src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java rename src/{main/jni/jtdlib => tdnatives-cpp}/CMakeLists.txt (100%) rename src/{main/jni/jtdlib => tdnatives-cpp}/sources.txt (100%) rename src/{main/jni/jtdlib => tdnatives-cpp}/td_jni.cpp (100%) rename src/{main/java => tdnatives-java}/it/tdlight/tdnatives/NativeClient.java (100%) rename src/{main/java => tdnatives-java}/it/tdlight/tdnatives/NativeLog.java (100%) rename src/{main/java => tdnatives-java}/it/tdlight/tdnatives/TdApi.java (100%) diff --git a/.gitmodules b/.gitmodules index d9d2f4a..5b55e57 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/src/main/jni/build.sh b/build.sh similarity index 100% rename from src/main/jni/build.sh rename to build.sh diff --git a/common-utils b/common-utils deleted file mode 160000 index 8da6756..0000000 --- a/common-utils +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8da67564c5a006dd31c2d0528768af8f790c1a67 diff --git a/dependencies/tdlib-serializer b/dependencies/tdlib-serializer new file mode 160000 index 0000000..57bf307 --- /dev/null +++ b/dependencies/tdlib-serializer @@ -0,0 +1 @@ +Subproject commit 57bf30757952daafcd28352e38fda21e260ce664 diff --git a/dependencies/tdlight b/dependencies/tdlight new file mode 160000 index 0000000..b93239f --- /dev/null +++ b/dependencies/tdlight @@ -0,0 +1 @@ +Subproject commit b93239f6d4315e9823076390d5e16db1379e9058 diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 2828e60..0000000 --- a/pom.xml +++ /dev/null @@ -1,112 +0,0 @@ - - 4.0.0 - it.tdlight - tdlight-java - 2.168.0 - TDLight Java Wrapper - jar - - UTF-8 - - 10 - 10 - - - - - github - GitHub TDLight Team Apache Maven Packages - https://maven.pkg.github.com/tdlight-team/tdlight-java - - - github-alternative - GitHub Cavallium Apache Maven Packages - https://maven.pkg.github.com/Cavallium/common-utils - - - - - - github - GitHub TDLight Team Apache Maven Packages - https://maven.pkg.github.com/tdlight-team/tdlight-java - - - - - - junit - junit - 4.12 - test - - - org.jetbrains - annotations - 17.0.0 - compile - - - org.jetbrains - annotations - 17.0.0 - compile - - - org.warp - common-utils - 1.0.3 - - - it.tdlight - tdlight-natives-linux-amd64 - 1.0.3 - - - - - - - maven-clean-plugin - 3.0.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.7.0 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - - - org.codehaus.mojo - templating-maven-plugin - 1.0.0 - - - filtering-java-templates - - filter-sources - - - - - - - diff --git a/src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java b/src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java deleted file mode 100644 index a614927..0000000 --- a/src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java +++ /dev/null @@ -1,4 +0,0 @@ -package it.tdlight.tdlight.utils; -public final class LibraryVersion { - public static final String VERSION = "${project.version}"; -} \ No newline at end of file diff --git a/src/main/java/it/tdlight/tdlight/Client.java b/src/main/java/it/tdlight/tdlight/Client.java deleted file mode 100644 index 63f7d6c..0000000 --- a/src/main/java/it/tdlight/tdlight/Client.java +++ /dev/null @@ -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 receive(double timeout, int eventSize) { - if (this.executionLock.isWriteLocked()) { - throw new IllegalStateException("ClientActor is destroyed"); - } - - var responseList = new ArrayList(); - 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(); - } -} diff --git a/src/main/java/it/tdlight/tdlight/ConstructorDetector.java b/src/main/java/it/tdlight/tdlight/ConstructorDetector.java deleted file mode 100644 index 2bedbf2..0000000 --- a/src/main/java/it/tdlight/tdlight/ConstructorDetector.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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 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 getTDConstructorsUnsafe() { - return constructorHashMap; - } - - public static void registerExternalClass(int constructor, Class clazz) { - constructorHashMap.put(constructor, clazz); - } -} diff --git a/src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java b/src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java deleted file mode 100644 index d76f101..0000000 --- a/src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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); -} diff --git a/src/main/java/it/tdlight/tdlight/Init.java b/src/main/java/it/tdlight/tdlight/Init.java deleted file mode 100644 index b882619..0000000 --- a/src/main/java/it/tdlight/tdlight/Init.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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; - } - } -} diff --git a/src/main/java/it/tdlight/tdlight/Log.java b/src/main/java/it/tdlight/tdlight/Log.java deleted file mode 100644 index c879dbe..0000000 --- a/src/main/java/it/tdlight/tdlight/Log.java +++ /dev/null @@ -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(); - } -} diff --git a/src/main/java/it/tdlight/tdlight/Request.java b/src/main/java/it/tdlight/tdlight/Request.java deleted file mode 100644 index 43a2f3c..0000000 --- a/src/main/java/it/tdlight/tdlight/Request.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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; - } -} diff --git a/src/main/java/it/tdlight/tdlight/Response.java b/src/main/java/it/tdlight/tdlight/Response.java deleted file mode 100644 index 3975809..0000000 --- a/src/main/java/it/tdlight/tdlight/Response.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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; - } -} diff --git a/src/main/java/it/tdlight/tdlight/ResponseArray.java b/src/main/java/it/tdlight/tdlight/ResponseArray.java deleted file mode 100644 index 2dd2226..0000000 --- a/src/main/java/it/tdlight/tdlight/ResponseArray.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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; - } -} diff --git a/src/main/java/it/tdlight/tdlight/TdCallback.java b/src/main/java/it/tdlight/tdlight/TdCallback.java deleted file mode 100644 index 153462a..0000000 --- a/src/main/java/it/tdlight/tdlight/TdCallback.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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; - } -} diff --git a/src/main/java/it/tdlight/tdlight/TelegramClient.java b/src/main/java/it/tdlight/tdlight/TelegramClient.java deleted file mode 100644 index 85096f7..0000000 --- a/src/main/java/it/tdlight/tdlight/TelegramClient.java +++ /dev/null @@ -1,21 +0,0 @@ -package it.tdlight.tdlight; - -import java.io.IOException; -import java.util.List; - -public interface TelegramClient { - - void send(Request request); - - List receive(double timeout, int eventSize); - - Response receive(double timeout); - - Response execute(Request request); - - void destroyClient(); - - void initializeClient() throws IOException; - - boolean isDestroyed(); -} diff --git a/src/main/java/it/tdlight/tdlight/utils/Arch.java b/src/main/java/it/tdlight/tdlight/utils/Arch.java deleted file mode 100644 index b1b5484..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/Arch.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -package it.tdlight.tdlight.utils; - -/** - * Enumeration with all architectures recognized by this library. - */ -public enum Arch { - amd64, - i386, - armhf, - aarch64, - ppc64le, - unknown -} diff --git a/src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java b/src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java deleted file mode 100644 index 2213647..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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"); - } -} diff --git a/src/main/java/it/tdlight/tdlight/utils/CloseCallback.java b/src/main/java/it/tdlight/tdlight/utils/CloseCallback.java deleted file mode 100644 index 23de821..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/CloseCallback.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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(); -} diff --git a/src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java b/src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java deleted file mode 100644 index 670ba3e..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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); -} diff --git a/src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java b/src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java deleted file mode 100644 index a17cff6..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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 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(); - } -} diff --git a/src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java b/src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java deleted file mode 100644 index f5a8835..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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); -} diff --git a/src/main/java/it/tdlight/tdlight/utils/Os.java b/src/main/java/it/tdlight/tdlight/utils/Os.java deleted file mode 100644 index c50a3cd..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/Os.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -package it.tdlight.tdlight.utils; - -/** - * Enumeration with all operating systems recognized by this library. - */ -public enum Os { - linux, - win, - mac, - unknown -} diff --git a/src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java b/src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java deleted file mode 100644 index 78d8df3..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2018. Ernesto Castellotti - * 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 . - */ - -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); -} diff --git a/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java b/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java deleted file mode 100644 index b690fe1..0000000 --- a/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java +++ /dev/null @@ -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(); - } - } -} diff --git a/src/main/jni/jtdlib/CMakeLists.txt b/src/tdnatives-cpp/CMakeLists.txt similarity index 100% rename from src/main/jni/jtdlib/CMakeLists.txt rename to src/tdnatives-cpp/CMakeLists.txt diff --git a/src/main/jni/jtdlib/sources.txt b/src/tdnatives-cpp/sources.txt similarity index 100% rename from src/main/jni/jtdlib/sources.txt rename to src/tdnatives-cpp/sources.txt diff --git a/src/main/jni/jtdlib/td_jni.cpp b/src/tdnatives-cpp/td_jni.cpp similarity index 100% rename from src/main/jni/jtdlib/td_jni.cpp rename to src/tdnatives-cpp/td_jni.cpp diff --git a/src/main/java/it/tdlight/tdnatives/NativeClient.java b/src/tdnatives-java/it/tdlight/tdnatives/NativeClient.java similarity index 100% rename from src/main/java/it/tdlight/tdnatives/NativeClient.java rename to src/tdnatives-java/it/tdlight/tdnatives/NativeClient.java diff --git a/src/main/java/it/tdlight/tdnatives/NativeLog.java b/src/tdnatives-java/it/tdlight/tdnatives/NativeLog.java similarity index 100% rename from src/main/java/it/tdlight/tdnatives/NativeLog.java rename to src/tdnatives-java/it/tdlight/tdnatives/NativeLog.java diff --git a/src/main/java/it/tdlight/tdnatives/TdApi.java b/src/tdnatives-java/it/tdlight/tdnatives/TdApi.java similarity index 100% rename from src/main/java/it/tdlight/tdnatives/TdApi.java rename to src/tdnatives-java/it/tdlight/tdnatives/TdApi.java