Removed unused files
This commit is contained in:
parent
cd746caec5
commit
f7247d0fa0
@ -1,172 +0,0 @@
|
||||
package it.ernytech.tdlib;
|
||||
/*
|
||||
* 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.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<Response> receive(double timeout, int eventSize) {
|
||||
if (this.executionLock.isWriteLocked()) {
|
||||
throw new IllegalStateException("ClientActor is destroyed");
|
||||
}
|
||||
|
||||
var responseList = new ArrayList<Response>();
|
||||
if (eventIds == null) {
|
||||
eventIds = new long[eventSize];
|
||||
events = new 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();
|
||||
}
|
||||
}
|
@ -1,49 +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.ernytech.tdlib.utils;
|
||||
|
||||
import it.ernytech.tdbot.ConstructorDetector;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +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.ernytech.tdlib;
|
||||
|
||||
import it.ernytech.tdlib.utils.Init;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param filePath Path to a file where the internal TDLib log will be written. Use an empty path to switch back to the default logging behaviour.
|
||||
* @return True on success, or false otherwise, i.e. if the file can't be opened for writing.
|
||||
*/
|
||||
public static native boolean setFilePath(String filePath);
|
||||
|
||||
/**
|
||||
* Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Unused if log is not written to a file. Defaults to 10 MB.
|
||||
* @param maxFileSize Maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Should be positive.
|
||||
*/
|
||||
public static native void setMaxFileSize(long maxFileSize);
|
||||
|
||||
/**
|
||||
* Sets the verbosity level of the internal logging of TDLib. By default the TDLib uses a verbosity level of 5 for logging.
|
||||
* @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();
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package it.tdlight.tdlight.natives;
|
||||
/*
|
||||
* 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.ernytech.tdlib.TdApi.Function;
|
||||
import it.ernytech.tdlib.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);
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
|
||||
* This file is part of JTdlib.
|
||||
*
|
||||
* JTdlib is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* JTdlib is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package it.tdlight.tdlight.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 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.
|
||||
* @param filePath Path to a file where the internal TDLib log will be written. Use an empty path to switch back to the default logging behaviour.
|
||||
* @return True on success, or false otherwise, i.e. if the file can't be opened for writing.
|
||||
*/
|
||||
public static native boolean setFilePath(String filePath);
|
||||
|
||||
/**
|
||||
* Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Unused if log is not written to a file. Defaults to 10 MB.
|
||||
* @param maxFileSize Maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Should be positive.
|
||||
*/
|
||||
public static native void setMaxFileSize(long maxFileSize);
|
||||
|
||||
/**
|
||||
* Sets the verbosity level of the internal logging of TDLib. By default the TDLib uses a verbosity level of 5 for logging.
|
||||
* @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);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user