Update API

This commit is contained in:
Andrea Cavalli 2020-10-12 18:48:49 +02:00
parent 05311ef763
commit 4d51b63737
7 changed files with 51 additions and 81 deletions

@ -1 +1 @@
Subproject commit be83b71452454bbdf1e73db58025256a93a6b320
Subproject commit f5ecc4b89655ddfaa1e05b77d6740e8d9cdfcaea

View File

@ -0,0 +1,16 @@
package it.tdlight.common;
/**
* Interface for handler of exceptions thrown while invoking ResultHandler.
* By default, all such exceptions are ignored.
* All exceptions thrown from ExceptionHandler are ignored.
*/
public interface ExceptionHandler {
/**
* Callback called on exceptions thrown while invoking ResultHandler.
*
* @param e Exception thrown by ResultHandler.
*/
void onException(Throwable e);
}

View File

@ -1,9 +1,8 @@
package it.tdlight.common;
import it.tdlight.common.Init;
import it.tdlight.jni.FatalErrorCallbackPtr;
import it.tdlight.jni.NativeLog;
import it.tdlight.jni.TdApi;
import java.util.function.Consumer;
/**
* Class used for managing internal TDLib logging.
@ -70,7 +69,7 @@ public class Log {
* 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 synchronized void setFatalErrorCallback(FatalErrorCallbackPtr fatalErrorCallback) {
public static synchronized void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
NativeLog.setFatalErrorCallback(fatalErrorCallback);
}
}

View File

@ -0,0 +1,16 @@
package it.tdlight.common;
import it.tdlight.jni.TdApi.Object;
/**
* Interface for handler for results of queries to TDLib and incoming updates from TDLib.
*/
public interface ResultHandler {
/**
* Callback called on result of query to TDLib or incoming update from TDLib.
*
* @param object Result of query or update of type TdApi.Update about new events.
*/
void onResult(Object object);
}

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.jni;
/**
* 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,31 +1,14 @@
package it.tdlight.jni;
/*
* 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/>.
*/
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.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 int nativeClientReceive(long nativeClientId, long[] eventIds, Object[] events, double timeout, boolean include_responses, boolean include_updates);
protected static native Object nativeClientExecute(Function function);
protected static native void destroyNativeClient(long nativeClientId);
protected static native int createNativeClient();
protected static native void nativeClientSend(int nativeClientId, long eventId, TdApi.Function function);
protected static native int nativeClientReceive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout);
protected static native int nativeClientReceive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout, boolean include_responses, boolean include_updates);
protected static native TdApi.Object nativeClientExecute(TdApi.Function function);
}

View File

@ -1,30 +1,15 @@
/*
* 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.jni;
import java.util.function.Consumer;
/**
* Class used for managing internal TDLib logging.
* Use TdApi.*Log* methods instead.
*/
public class NativeLog {
private static final FatalErrorCallbackPtr defaultFatalErrorCallbackPtr = System.err::println;
private static FatalErrorCallbackPtr fatalErrorCallback = defaultFatalErrorCallbackPtr;
private static final Consumer<String> defaultFatalErrorCallbackPtr = System.err::println;
private static Consumer<String> fatalErrorCallback = defaultFatalErrorCallbackPtr;
/**
* Sets file path for writing TDLib internal log. By default TDLib writes logs to the System.err.
@ -72,14 +57,14 @@ public class NativeLog {
* @param errorMessage Error message.
*/
private static synchronized void onFatalError(String errorMessage) {
new Thread(() -> NativeLog.fatalErrorCallback.onFatalError(errorMessage)).start();
new Thread(() -> NativeLog.fatalErrorCallback.accept(errorMessage)).start();
}
/**
* 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 synchronized void setFatalErrorCallback(FatalErrorCallbackPtr fatalErrorCallback) {
public static synchronized void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
NativeLog.fatalErrorCallback = ObjectsUtils.requireNonNullElse(fatalErrorCallback, defaultFatalErrorCallbackPtr);
}
}