2018-01-28 11:58:33 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-01-28 11:58:33 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
package org.drinkless.tdlib;
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main class for interaction with the TDLib.
|
|
|
|
*/
|
2020-10-11 20:28:33 +02:00
|
|
|
public final class Client {
|
2018-01-28 11:58:33 +01:00
|
|
|
/**
|
|
|
|
* 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(TdApi.Object object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to the TDLib.
|
|
|
|
*
|
|
|
|
* @param query Object representing a query to the TDLib.
|
|
|
|
* @param resultHandler Result handler with onResult method which will be called with result
|
|
|
|
* of the query or with TdApi.Error as parameter. If it is null, nothing
|
|
|
|
* will be called.
|
|
|
|
* @param exceptionHandler Exception handler with onException method which will be called on
|
|
|
|
* exception thrown from resultHandler. If it is null, then
|
|
|
|
* defaultExceptionHandler will be called.
|
|
|
|
* @throws NullPointerException if query is null.
|
|
|
|
*/
|
|
|
|
public void send(TdApi.Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
|
2020-10-11 20:28:33 +02:00
|
|
|
long queryId = currentQueryId.incrementAndGet();
|
|
|
|
if (resultHandler != null) {
|
2018-01-28 11:58:33 +01:00
|
|
|
handlers.put(queryId, new Handler(resultHandler, exceptionHandler));
|
|
|
|
}
|
2020-10-11 20:28:33 +02:00
|
|
|
nativeClientSend(nativeClientId, queryId, query);
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to the TDLib with an empty ExceptionHandler.
|
|
|
|
*
|
|
|
|
* @param query Object representing a query to the TDLib.
|
|
|
|
* @param resultHandler Result handler with onResult method which will be called with result
|
|
|
|
* of the query or with TdApi.Error as parameter. If it is null, then
|
|
|
|
* defaultExceptionHandler will be called.
|
|
|
|
* @throws NullPointerException if query is null.
|
|
|
|
*/
|
|
|
|
public void send(TdApi.Function query, ResultHandler resultHandler) {
|
|
|
|
send(query, resultHandler, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously.
|
|
|
|
*
|
|
|
|
* @param query Object representing a query to the TDLib.
|
|
|
|
* @return request result.
|
|
|
|
* @throws NullPointerException if query is null.
|
|
|
|
*/
|
|
|
|
public static TdApi.Object execute(TdApi.Function query) {
|
|
|
|
return nativeClientExecute(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new Client.
|
|
|
|
*
|
2020-10-05 19:13:15 +02:00
|
|
|
* @param updateHandler Handler for incoming updates.
|
|
|
|
* @param updateExceptionHandler Handler for exceptions thrown from updateHandler. If it is null, exceptions will be iggnored.
|
2018-01-28 11:58:33 +01:00
|
|
|
* @param defaultExceptionHandler Default handler for exceptions thrown from all ResultHandler. If it is null, exceptions will be iggnored.
|
|
|
|
* @return created Client
|
|
|
|
*/
|
2020-10-05 19:13:15 +02:00
|
|
|
public static Client create(ResultHandler updateHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) {
|
|
|
|
Client client = new Client(updateHandler, updateExceptionHandler, defaultExceptionHandler);
|
2020-10-11 20:28:33 +02:00
|
|
|
synchronized (responseReceiver) {
|
|
|
|
if (!responseReceiver.isRun) {
|
|
|
|
responseReceiver.isRun = true;
|
|
|
|
|
|
|
|
Thread receiverThread = new Thread(responseReceiver, "TDLib thread");
|
|
|
|
receiverThread.setDaemon(true);
|
|
|
|
receiverThread.start();
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 11:58:33 +01:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static class ResponseReceiver implements Runnable {
|
|
|
|
public boolean isRun = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
while (true) {
|
|
|
|
int resultN = nativeClientReceive(clientIds, eventIds, events, 100000.0 /*seconds*/);
|
|
|
|
for (int i = 0; i < resultN; i++) {
|
|
|
|
processResult(clientIds[i], eventIds[i], events[i]);
|
|
|
|
events[i] = null;
|
|
|
|
}
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
2020-10-11 20:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void processResult(int clientId, long id, TdApi.Object object) {
|
|
|
|
boolean isClosed = false;
|
|
|
|
if (id == 0 && object instanceof TdApi.UpdateAuthorizationState) {
|
|
|
|
TdApi.AuthorizationState authorizationState = ((TdApi.UpdateAuthorizationState) object).authorizationState;
|
|
|
|
if (authorizationState instanceof TdApi.AuthorizationStateClosed) {
|
|
|
|
isClosed = true;
|
|
|
|
}
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
2020-10-11 20:28:33 +02:00
|
|
|
|
|
|
|
Handler handler = id == 0 ? updateHandlers.get(clientId) : handlers.remove(id);
|
|
|
|
if (handler != null) {
|
|
|
|
try {
|
|
|
|
handler.resultHandler.onResult(object);
|
|
|
|
} catch (Throwable cause) {
|
|
|
|
ExceptionHandler exceptionHandler = handler.exceptionHandler;
|
|
|
|
if (exceptionHandler == null) {
|
|
|
|
exceptionHandler = defaultExceptionHandlers.get(clientId);
|
|
|
|
}
|
|
|
|
if (exceptionHandler != null) {
|
|
|
|
try {
|
|
|
|
exceptionHandler.onException(cause);
|
|
|
|
} catch (Throwable ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
2020-10-11 20:28:33 +02:00
|
|
|
|
|
|
|
if (isClosed) {
|
|
|
|
updateHandlers.remove(clientId); // there will be no more updates
|
|
|
|
defaultExceptionHandlers.remove(clientId); // ignore further exceptions
|
2020-10-13 14:39:17 +02:00
|
|
|
clientCount.decrementAndGet();
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static final int MAX_EVENTS = 1000;
|
|
|
|
private final int[] clientIds = new int[MAX_EVENTS];
|
|
|
|
private final long[] eventIds = new long[MAX_EVENTS];
|
|
|
|
private final TdApi.Object[] events = new TdApi.Object[MAX_EVENTS];
|
|
|
|
}
|
2018-01-28 11:58:33 +01:00
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private final int nativeClientId;
|
2020-10-05 17:58:38 +02:00
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static final ConcurrentHashMap<Integer, ExceptionHandler> defaultExceptionHandlers = new ConcurrentHashMap<Integer, ExceptionHandler>();
|
|
|
|
private static final ConcurrentHashMap<Integer, Handler> updateHandlers = new ConcurrentHashMap<Integer, Handler>();
|
|
|
|
private static final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
|
|
|
|
private static final AtomicLong currentQueryId = new AtomicLong();
|
2020-10-13 14:39:17 +02:00
|
|
|
private static final AtomicLong clientCount = new AtomicLong();
|
2018-01-28 11:58:33 +01:00
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static final ResponseReceiver responseReceiver = new ResponseReceiver();
|
2018-01-28 11:58:33 +01:00
|
|
|
|
|
|
|
private static class Handler {
|
|
|
|
final ResultHandler resultHandler;
|
|
|
|
final ExceptionHandler exceptionHandler;
|
|
|
|
|
|
|
|
Handler(ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
|
|
|
|
this.resultHandler = resultHandler;
|
|
|
|
this.exceptionHandler = exceptionHandler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:13:15 +02:00
|
|
|
private Client(ResultHandler updateHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) {
|
2020-10-13 14:39:17 +02:00
|
|
|
clientCount.incrementAndGet();
|
2018-01-28 11:58:33 +01:00
|
|
|
nativeClientId = createNativeClient();
|
2020-10-11 20:28:33 +02:00
|
|
|
if (updateHandler != null) {
|
|
|
|
updateHandlers.put(nativeClientId, new Handler(updateHandler, updateExceptionHandler));
|
|
|
|
}
|
2020-10-05 20:31:27 +02:00
|
|
|
if (defaultExceptionHandler != null) {
|
2020-10-11 20:28:33 +02:00
|
|
|
defaultExceptionHandlers.put(nativeClientId, defaultExceptionHandler);
|
2020-10-05 20:31:27 +02:00
|
|
|
}
|
2020-11-14 20:57:59 +01:00
|
|
|
send(new TdApi.GetOption("version"), null, null);
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void finalize() throws Throwable {
|
2020-10-11 20:28:33 +02:00
|
|
|
send(new TdApi.Close(), null, null);
|
2018-01-28 11:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static native int createNativeClient();
|
2018-01-28 11:58:33 +01:00
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static native void nativeClientSend(int nativeClientId, long eventId, TdApi.Function function);
|
2018-01-28 11:58:33 +01:00
|
|
|
|
2020-10-11 20:28:33 +02:00
|
|
|
private static native int nativeClientReceive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout);
|
2018-01-28 11:58:33 +01:00
|
|
|
|
|
|
|
private static native TdApi.Object nativeClientExecute(TdApi.Function function);
|
|
|
|
}
|