From 034aaa46d9bdb9e8e433da2a236d3edfa4620c6e Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 18 Aug 2020 23:13:36 +0200 Subject: [PATCH] Renamed project files --- pom.xml | 11 +- .../tdlight}/utils/LibraryVersion.java | 2 +- .../java/it/ernytech/tdbot/package-info.java | 21 --- src/main/java/it/ernytech/tdlib/Client.java | 172 ------------------ .../it/ernytech/tdlib/utils/package-info.java | 21 --- src/main/java/it/tdlight/tdlight/Client.java | 150 +++++++++++++++ .../tdlight}/ConstructorDetector.java | 7 +- .../tdlight}/FatalErrorCallbackPtr.java | 2 +- .../tdlib/utils => tdlight/tdlight}/Init.java | 6 +- src/main/java/it/tdlight/tdlight/Log.java | 31 ++++ .../tdlib => tdlight/tdlight}/Request.java | 10 +- .../tdlib => tdlight/tdlight}/Response.java | 10 +- .../tdlight}/ResponseArray.java | 2 +- .../tdlib => tdlight/tdlight}/TdCallback.java | 8 +- .../tdlight}/TelegramClient.java | 2 +- .../tdlib => tdlight/tdlight}/utils/Arch.java | 2 +- .../tdlight}/utils/CantLoadLibrary.java | 2 +- .../tdlight}/utils/CloseCallback.java | 2 +- .../tdlight}/utils/ErrorCallback.java | 4 +- .../tdlight}/utils/LoadLibrary.java | 2 +- .../tdlight}/utils/NativeErrorCallback.java | 4 +- .../tdlib => tdlight/tdlight}/utils/Os.java | 2 +- .../tdlight}/utils/ReceiveCallback.java | 4 +- .../tdlight}/utils/ScannerUtils.java | 2 +- .../tdnatives/NativeClient.java} | 17 +- .../tdnatives/NativeLog.java} | 31 +--- .../tdlib => tdlight/tdnatives}/TdApi.java | 2 +- src/main/jni/build.sh | 4 +- src/main/jni/jtdlib/CMakeLists.txt | 6 +- src/main/jni/jtdlib/sources.txt | 18 +- 30 files changed, 253 insertions(+), 304 deletions(-) rename src/main/java-templates/it/{ernytech/tdlib => tdlight/tdlight}/utils/LibraryVersion.java (73%) delete mode 100644 src/main/java/it/ernytech/tdbot/package-info.java delete mode 100644 src/main/java/it/ernytech/tdlib/Client.java delete mode 100644 src/main/java/it/ernytech/tdlib/utils/package-info.java create mode 100644 src/main/java/it/tdlight/tdlight/Client.java rename src/main/java/it/{ernytech/tdbot => tdlight/tdlight}/ConstructorDetector.java (96%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/FatalErrorCallbackPtr.java (97%) rename src/main/java/it/{ernytech/tdlib/utils => tdlight/tdlight}/Init.java (90%) create mode 100644 src/main/java/it/tdlight/tdlight/Log.java rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/Request.java (90%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/Response.java (90%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/ResponseArray.java (97%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/TdCallback.java (96%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/TelegramClient.java (92%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/Arch.java (96%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/CantLoadLibrary.java (97%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/CloseCallback.java (96%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/ErrorCallback.java (93%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/LoadLibrary.java (99%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/NativeErrorCallback.java (93%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/Os.java (96%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/ReceiveCallback.java (93%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdlight}/utils/ScannerUtils.java (98%) rename src/main/java/it/{ernytech/tdlib/package-info.java => tdlight/tdnatives/NativeClient.java} (55%) rename src/main/java/it/{ernytech/tdlib/Log.java => tdlight/tdnatives/NativeLog.java} (66%) rename src/main/java/it/{ernytech/tdlib => tdlight/tdnatives}/TdApi.java (99%) diff --git a/pom.xml b/pom.xml index c239e55..2828e60 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,9 @@ 4.0.0 - it.ernytech - tdlib + it.tdlight + tdlight-java 2.168.0 - JTDLib + TDLight Java Wrapper jar UTF-8 @@ -57,6 +57,11 @@ common-utils 1.0.3 + + it.tdlight + tdlight-natives-linux-amd64 + 1.0.3 + diff --git a/src/main/java-templates/it/ernytech/tdlib/utils/LibraryVersion.java b/src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java similarity index 73% rename from src/main/java-templates/it/ernytech/tdlib/utils/LibraryVersion.java rename to src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java index 64c4da6..a614927 100644 --- a/src/main/java-templates/it/ernytech/tdlib/utils/LibraryVersion.java +++ b/src/main/java-templates/it/tdlight/tdlight/utils/LibraryVersion.java @@ -1,4 +1,4 @@ -package it.ernytech.tdlib.utils; +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/ernytech/tdbot/package-info.java b/src/main/java/it/ernytech/tdbot/package-info.java deleted file mode 100644 index 9359e6b..0000000 --- a/src/main/java/it/ernytech/tdbot/package-info.java +++ /dev/null @@ -1,21 +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 . - */ - -/** - * it.ernytech.tdbot is a group of class for to simplify the creation of bots or userbots. - */ -package it.ernytech.tdbot; \ No newline at end of file diff --git a/src/main/java/it/ernytech/tdlib/Client.java b/src/main/java/it/ernytech/tdlib/Client.java deleted file mode 100644 index ab8ab0c..0000000 --- a/src/main/java/it/ernytech/tdlib/Client.java +++ /dev/null @@ -1,172 +0,0 @@ -package it.ernytech.tdlib; -/* - * 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 . - */ - - - -import it.ernytech.tdlib.utils.Init; - -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 implements TelegramClient { - private long clientId; - private final ReentrantLock receiveLock = new ReentrantLock(); - private final StampedLock executionLock = new StampedLock(); - private volatile Long stampedLockValue = 1L; - private static native long createNativeClient(); - private static native void nativeClientSend(long nativeClientId, long eventId, TdApi.Function function); - private static native int nativeClientReceive(long nativeClientId, long[] eventIds, TdApi.Object[] events, double timeout); - private static native TdApi.Object nativeClientExecute(TdApi.Function function); - private static native void destroyNativeClient(long nativeClientId); - - /** - * Creates a new TDLib client. - */ - public Client() { - 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 TdApi.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 TdApi.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"); - } - - TdApi.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/ernytech/tdlib/utils/package-info.java b/src/main/java/it/ernytech/tdlib/utils/package-info.java deleted file mode 100644 index 707839c..0000000 --- a/src/main/java/it/ernytech/tdlib/utils/package-info.java +++ /dev/null @@ -1,21 +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 . - */ - -/** - * it.ernytech.tdlib.utils is a group of utils necessary to complete the essential operation of tdlib. - */ -package it.ernytech.tdlib.utils; \ 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 new file mode 100644 index 0000000..63f7d6c --- /dev/null +++ b/src/main/java/it/tdlight/tdlight/Client.java @@ -0,0 +1,150 @@ +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/ernytech/tdbot/ConstructorDetector.java b/src/main/java/it/tdlight/tdlight/ConstructorDetector.java similarity index 96% rename from src/main/java/it/ernytech/tdbot/ConstructorDetector.java rename to src/main/java/it/tdlight/tdlight/ConstructorDetector.java index 74b1125..2bedbf2 100644 --- a/src/main/java/it/ernytech/tdbot/ConstructorDetector.java +++ b/src/main/java/it/tdlight/tdlight/ConstructorDetector.java @@ -15,10 +15,9 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdbot; - -import it.ernytech.tdlib.TdApi; +package it.tdlight.tdlight; +import it.tdlight.tdnatives.TdApi; import java.lang.reflect.Field; import java.util.concurrent.ConcurrentHashMap; @@ -30,7 +29,7 @@ public class ConstructorDetector { static { // Call this to load static methods and prevent errors during startup! try { - it.ernytech.tdlib.utils.Init.start(); + Init.start(); } catch (Throwable throwable) { throwable.printStackTrace(); } diff --git a/src/main/java/it/ernytech/tdlib/FatalErrorCallbackPtr.java b/src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java similarity index 97% rename from src/main/java/it/ernytech/tdlib/FatalErrorCallbackPtr.java rename to src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java index 7e4bd8f..d76f101 100644 --- a/src/main/java/it/ernytech/tdlib/FatalErrorCallbackPtr.java +++ b/src/main/java/it/tdlight/tdlight/FatalErrorCallbackPtr.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; +package it.tdlight.tdlight; /** * A type of callback function that will be called when a fatal error happens. diff --git a/src/main/java/it/ernytech/tdlib/utils/Init.java b/src/main/java/it/tdlight/tdlight/Init.java similarity index 90% rename from src/main/java/it/ernytech/tdlib/utils/Init.java rename to src/main/java/it/tdlight/tdlight/Init.java index 5be9dbb..b882619 100644 --- a/src/main/java/it/ernytech/tdlib/utils/Init.java +++ b/src/main/java/it/tdlight/tdlight/Init.java @@ -15,9 +15,11 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight; -import it.ernytech.tdbot.ConstructorDetector; +import it.tdlight.tdlight.utils.CantLoadLibrary; +import it.tdlight.tdlight.utils.LoadLibrary; +import it.tdlight.tdlight.utils.Os; /** * Init class to successfully initialize Tdlib diff --git a/src/main/java/it/tdlight/tdlight/Log.java b/src/main/java/it/tdlight/tdlight/Log.java new file mode 100644 index 0000000..c879dbe --- /dev/null +++ b/src/main/java/it/tdlight/tdlight/Log.java @@ -0,0 +1,31 @@ +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/ernytech/tdlib/Request.java b/src/main/java/it/tdlight/tdlight/Request.java similarity index 90% rename from src/main/java/it/ernytech/tdlib/Request.java rename to src/main/java/it/tdlight/tdlight/Request.java index 6c56dae..43a2f3c 100644 --- a/src/main/java/it/ernytech/tdlib/Request.java +++ b/src/main/java/it/tdlight/tdlight/Request.java @@ -15,21 +15,23 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; +package it.tdlight.tdlight; + +import it.tdlight.tdnatives.TdApi.Function; /** * A request to the TDLib. */ public class Request { private long id; - private TdApi.Function function; + 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, TdApi.Function function) { + public Request(long id, Function function) { this.id = id; this.function = function; } @@ -46,7 +48,7 @@ public class Request { * Get TDLib API function. * @return TDLib API function representing a request to TDLib. */ - public TdApi.Function getFunction() { + public Function getFunction() { return this.function; } } diff --git a/src/main/java/it/ernytech/tdlib/Response.java b/src/main/java/it/tdlight/tdlight/Response.java similarity index 90% rename from src/main/java/it/ernytech/tdlib/Response.java rename to src/main/java/it/tdlight/tdlight/Response.java index bb762c7..3975809 100644 --- a/src/main/java/it/ernytech/tdlib/Response.java +++ b/src/main/java/it/tdlight/tdlight/Response.java @@ -15,21 +15,23 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; +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 TdApi.Object object; + 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, TdApi.Object object) { + public Response(long id, Object object) { this.id = id; this.object = object; } @@ -46,7 +48,7 @@ public class Response { * Get TDLib API object. * @return TDLib API object representing a response to a TDLib request or an incoming update. */ - public TdApi.Object getObject() { + public Object getObject() { return this.object; } } diff --git a/src/main/java/it/ernytech/tdlib/ResponseArray.java b/src/main/java/it/tdlight/tdlight/ResponseArray.java similarity index 97% rename from src/main/java/it/ernytech/tdlib/ResponseArray.java rename to src/main/java/it/tdlight/tdlight/ResponseArray.java index 0207224..2dd2226 100644 --- a/src/main/java/it/ernytech/tdlib/ResponseArray.java +++ b/src/main/java/it/tdlight/tdlight/ResponseArray.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; +package it.tdlight.tdlight; /** * An array of incoming updates from TDLib. diff --git a/src/main/java/it/ernytech/tdlib/TdCallback.java b/src/main/java/it/tdlight/tdlight/TdCallback.java similarity index 96% rename from src/main/java/it/ernytech/tdlib/TdCallback.java rename to src/main/java/it/tdlight/tdlight/TdCallback.java index 20bd92b..153462a 100644 --- a/src/main/java/it/ernytech/tdlib/TdCallback.java +++ b/src/main/java/it/tdlight/tdlight/TdCallback.java @@ -15,11 +15,11 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; +package it.tdlight.tdlight; -import it.ernytech.tdlib.utils.CloseCallback; -import it.ernytech.tdlib.utils.ErrorCallback; -import it.ernytech.tdlib.utils.ReceiveCallback; +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. diff --git a/src/main/java/it/ernytech/tdlib/TelegramClient.java b/src/main/java/it/tdlight/tdlight/TelegramClient.java similarity index 92% rename from src/main/java/it/ernytech/tdlib/TelegramClient.java rename to src/main/java/it/tdlight/tdlight/TelegramClient.java index 8e2978d..85096f7 100644 --- a/src/main/java/it/ernytech/tdlib/TelegramClient.java +++ b/src/main/java/it/tdlight/tdlight/TelegramClient.java @@ -1,4 +1,4 @@ -package it.ernytech.tdlib; +package it.tdlight.tdlight; import java.io.IOException; import java.util.List; diff --git a/src/main/java/it/ernytech/tdlib/utils/Arch.java b/src/main/java/it/tdlight/tdlight/utils/Arch.java similarity index 96% rename from src/main/java/it/ernytech/tdlib/utils/Arch.java rename to src/main/java/it/tdlight/tdlight/utils/Arch.java index 177c80e..b1b5484 100644 --- a/src/main/java/it/ernytech/tdlib/utils/Arch.java +++ b/src/main/java/it/tdlight/tdlight/utils/Arch.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; /** * Enumeration with all architectures recognized by this library. diff --git a/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java b/src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java similarity index 97% rename from src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java rename to src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java index 34bbe82..2213647 100644 --- a/src/main/java/it/ernytech/tdlib/utils/CantLoadLibrary.java +++ b/src/main/java/it/tdlight/tdlight/utils/CantLoadLibrary.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; /** * An exception that is thrown when the LoadLibrary class fails to load the library. diff --git a/src/main/java/it/ernytech/tdlib/utils/CloseCallback.java b/src/main/java/it/tdlight/tdlight/utils/CloseCallback.java similarity index 96% rename from src/main/java/it/ernytech/tdlib/utils/CloseCallback.java rename to src/main/java/it/tdlight/tdlight/utils/CloseCallback.java index 2dcf188..23de821 100644 --- a/src/main/java/it/ernytech/tdlib/utils/CloseCallback.java +++ b/src/main/java/it/tdlight/tdlight/utils/CloseCallback.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; /** * Interface of callback for receive notification of closing Tdlib. diff --git a/src/main/java/it/ernytech/tdlib/utils/ErrorCallback.java b/src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java similarity index 93% rename from src/main/java/it/ernytech/tdlib/utils/ErrorCallback.java rename to src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java index c799cd0..670ba3e 100644 --- a/src/main/java/it/ernytech/tdlib/utils/ErrorCallback.java +++ b/src/main/java/it/tdlight/tdlight/utils/ErrorCallback.java @@ -15,9 +15,9 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; -import it.ernytech.tdlib.Response; +import it.tdlight.tdlight.Response; /** * Interface of callback for receive incoming error response. diff --git a/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java b/src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java similarity index 99% rename from src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java rename to src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java index 7bd6f51..a17cff6 100644 --- a/src/main/java/it/ernytech/tdlib/utils/LoadLibrary.java +++ b/src/main/java/it/tdlight/tdlight/utils/LoadLibrary.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; import java.io.IOException; import java.nio.ByteOrder; diff --git a/src/main/java/it/ernytech/tdlib/utils/NativeErrorCallback.java b/src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java similarity index 93% rename from src/main/java/it/ernytech/tdlib/utils/NativeErrorCallback.java rename to src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java index dab3392..f5a8835 100644 --- a/src/main/java/it/ernytech/tdlib/utils/NativeErrorCallback.java +++ b/src/main/java/it/tdlight/tdlight/utils/NativeErrorCallback.java @@ -15,9 +15,9 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; -import it.ernytech.tdlib.Response; +import it.tdlight.tdlight.Response; /** * Interface of callback for receive incoming error response. diff --git a/src/main/java/it/ernytech/tdlib/utils/Os.java b/src/main/java/it/tdlight/tdlight/utils/Os.java similarity index 96% rename from src/main/java/it/ernytech/tdlib/utils/Os.java rename to src/main/java/it/tdlight/tdlight/utils/Os.java index b47397b..c50a3cd 100644 --- a/src/main/java/it/ernytech/tdlib/utils/Os.java +++ b/src/main/java/it/tdlight/tdlight/utils/Os.java @@ -15,7 +15,7 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; /** * Enumeration with all operating systems recognized by this library. diff --git a/src/main/java/it/ernytech/tdlib/utils/ReceiveCallback.java b/src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java similarity index 93% rename from src/main/java/it/ernytech/tdlib/utils/ReceiveCallback.java rename to src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java index 20085d0..78d8df3 100644 --- a/src/main/java/it/ernytech/tdlib/utils/ReceiveCallback.java +++ b/src/main/java/it/tdlight/tdlight/utils/ReceiveCallback.java @@ -15,9 +15,9 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; -import it.ernytech.tdlib.Response; +import it.tdlight.tdlight.Response; /** * Interface of callback for receive incoming update or request response. diff --git a/src/main/java/it/ernytech/tdlib/utils/ScannerUtils.java b/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java similarity index 98% rename from src/main/java/it/ernytech/tdlib/utils/ScannerUtils.java rename to src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java index 056b473..b690fe1 100644 --- a/src/main/java/it/ernytech/tdlib/utils/ScannerUtils.java +++ b/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java @@ -1,4 +1,4 @@ -package it.ernytech.tdlib.utils; +package it.tdlight.tdlight.utils; import java.io.PrintStream; import java.util.Scanner; diff --git a/src/main/java/it/ernytech/tdlib/package-info.java b/src/main/java/it/tdlight/tdnatives/NativeClient.java similarity index 55% rename from src/main/java/it/ernytech/tdlib/package-info.java rename to src/main/java/it/tdlight/tdnatives/NativeClient.java index d10b689..23796d8 100644 --- a/src/main/java/it/ernytech/tdlib/package-info.java +++ b/src/main/java/it/tdlight/tdnatives/NativeClient.java @@ -1,3 +1,4 @@ +package it.tdlight.tdlight.natives; /* * Copyright (c) 2018. Ernesto Castellotti * This file is part of JTdlib. @@ -15,7 +16,15 @@ * along with JTdlib. If not, see . */ -/** - * it.ernytech.tdlib is a group of inspired by the classic native C ++ classes of Tdlib Client and Log to interact with the Tdlib API. - */ -package it.ernytech.tdlib; \ No newline at end of file + + +import it.tdlight.tdnatives.TdApi.Function; +import it.tdlight.tdnatives.TdApi.Object; + +public class NativeClient { + protected static native long createNativeClient(); + protected static native void nativeClientSend(long nativeClientId, long eventId, Function function); + protected static native int nativeClientReceive(long nativeClientId, long[] eventIds, Object[] events, double timeout); + protected static native Object nativeClientExecute(Function function); + protected static native void destroyNativeClient(long nativeClientId); +} diff --git a/src/main/java/it/ernytech/tdlib/Log.java b/src/main/java/it/tdlight/tdnatives/NativeLog.java similarity index 66% rename from src/main/java/it/ernytech/tdlib/Log.java rename to src/main/java/it/tdlight/tdnatives/NativeLog.java index c96bf10..3176dc8 100644 --- a/src/main/java/it/ernytech/tdlib/Log.java +++ b/src/main/java/it/tdlight/tdnatives/NativeLog.java @@ -15,27 +15,12 @@ * along with JTdlib. If not, see . */ -package it.ernytech.tdlib; - -import it.ernytech.tdlib.utils.Init; - -import java.util.Objects; +package it.tdlight.tdlight.natives; /** * Interface for managing the internal logging of TDLib. By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5. */ -public class Log { - 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; +public class NativeLog { /** * Sets the path to the file to where the internal TDLib log will be written. By default TDLib writes logs to stderr or an OS specific log. Use this method to write the log to a file instead. @@ -55,16 +40,4 @@ public class Log { * @param verbosityLevel New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1 corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value greater than 5 and up to 1024 can be used to enable even more logging. */ public static native void setVerbosityLevel(int verbosityLevel); - - /** - * 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/ernytech/tdlib/TdApi.java b/src/main/java/it/tdlight/tdnatives/TdApi.java similarity index 99% rename from src/main/java/it/ernytech/tdlib/TdApi.java rename to src/main/java/it/tdlight/tdnatives/TdApi.java index 9ac7aee..ed84eea 100644 --- a/src/main/java/it/ernytech/tdlib/TdApi.java +++ b/src/main/java/it/tdlight/tdnatives/TdApi.java @@ -1,4 +1,4 @@ -package it.ernytech.tdlib; +package it.tdlight.tdnatives; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; diff --git a/src/main/jni/build.sh b/src/main/jni/build.sh index bf6fab4..ebea1e3 100755 --- a/src/main/jni/build.sh +++ b/src/main/jni/build.sh @@ -2,8 +2,8 @@ #rm -r jtdlib/jnibuild #rm -r jtdlib/build -rm ../java/it/ernytech/tdlib/TdApi.java || true -rm ../java/it/ernytech/tdlib/new_TdApi.java || true +rm ../java/it/tdlight/tdlight/natives/TdApi.java || true +rm ../java/it/tdlight/tdlight/natives/new_TdApi.java || true export TD_SRC_DIR=${PWD}/td export TD_BIN_DIR=${PWD}/jtdlib/td diff --git a/src/main/jni/jtdlib/CMakeLists.txt b/src/main/jni/jtdlib/CMakeLists.txt index dd6f1b7..0c82867 100644 --- a/src/main/jni/jtdlib/CMakeLists.txt +++ b/src/main/jni/jtdlib/CMakeLists.txt @@ -12,6 +12,8 @@ endif() find_package(Td REQUIRED) if (NOT JNI_FOUND) + set(JAVA_AWT_LIBRARY NotNeeded) + set(JAVA_AWT_INCLUDE_PATH NotNeeded) find_package(JNI REQUIRED) endif() message(STATUS "Found JNI: ${JNI_INCLUDE_DIRS} ${JNI_LIBRARIES}") @@ -26,7 +28,7 @@ message(STATUS "Java Source Directory: ${JAVA_SRC_DIR}") # Generating TdApi.java find_program(PHP_EXECUTABLE php) -set(TD_API_JAVA_PACKAGE "it/ernytech/tdlib") +set(TD_API_JAVA_PACKAGE "it/tdlight/tdnatives") set(TD_API_JAVA_PATH ${JAVA_SRC_DIR}) set(TD_API_TLO_PATH ${CMAKE_CURRENT_SOURCE_DIR}/td/bin/td/generate/scheme/td_api.tlo) set(TD_API_TL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/td/bin/td/generate/scheme/td_api.tl) @@ -51,7 +53,7 @@ add_custom_target(build_java ) add_custom_target(generate_javadoc - COMMAND ${Java_JAVADOC_EXECUTABLE} -d ${JAVA_OUTPUT_DIRECTORY}/../docs it.ernytech.tdlib + COMMAND ${Java_JAVADOC_EXECUTABLE} -d ${JAVA_OUTPUT_DIRECTORY}/../docs it.tdlight.tdnatives WORKING_DIRECTORY ${TD_API_JAVA_PATH} COMMENT "Generating Javadoc documentation" DEPENDS td_generate_java_api diff --git a/src/main/jni/jtdlib/sources.txt b/src/main/jni/jtdlib/sources.txt index 89ae3a2..b0e6431 100644 --- a/src/main/jni/jtdlib/sources.txt +++ b/src/main/jni/jtdlib/sources.txt @@ -1,15 +1,3 @@ -../../../java/it/ernytech/tdlib/Client.java -../../../java/it/ernytech/tdlib/TelegramClient.java -../../../java/it/ernytech/tdlib/utils/Init.java -../../../java/it/ernytech/tdlib/utils/LoadLibrary.java -../../../java/it/ernytech/tdlib/utils/Os.java -../../../java/it/ernytech/tdlib/Log.java -../../../java/it/ernytech/tdlib/Request.java -../../../java/it/ernytech/tdlib/Response.java -../../../java/it/ernytech/tdlib/utils/Arch.java -../../../java/it/ernytech/tdlib/utils/CantLoadLibrary.java -../../../java/it/ernytech/tdlib/FatalErrorCallbackPtr.java -../../../java/it/ernytech/tdbot/ConstructorDetector.java -../../../java/it/ernytech/tdlib/TdApi.java -../../../java-templates/it/ernytech/tdlib/utils/LibraryVersion.java - +../../../java/it/tdlight/tdnatives/NativeClient.java +../../../java/it/tdlight/tdnatives/NativeLog.java +../../../java/it/tdlight/tdnatives/TdApi.java \ No newline at end of file