tdlight-java/src/main/java/it/tdlight/common/InternalClient.java

162 lines
4.8 KiB
Java
Raw Normal View History

2020-10-13 01:31:32 +02:00
package it.tdlight.common;
2020-11-12 13:01:22 +01:00
import it.tdlight.jni.TdApi.Error;
2020-10-13 01:31:32 +02:00
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.TdApi.Object;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.concurrent.ConcurrentHashMap;
2020-11-12 13:01:22 +01:00
import java.util.concurrent.atomic.AtomicBoolean;
2020-11-15 20:10:54 +01:00
import java.util.concurrent.locks.ReentrantReadWriteLock;
2020-10-13 01:31:32 +02:00
public class InternalClient implements ClientEventsHandler, TelegramClient {
2020-11-15 20:10:54 +01:00
static final ReentrantReadWriteLock clientInitializationLock = new ReentrantReadWriteLock(true);
2020-10-13 01:31:32 +02:00
private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
private final int clientId;
private final InternalClientManager clientManager;
private final Handler updateHandler;
private final MultiHandler updatesHandler;
private final ExceptionHandler defaultExceptionHandler;
2020-11-12 13:01:22 +01:00
private final AtomicBoolean isClosed = new AtomicBoolean();
2020-10-13 01:31:32 +02:00
public InternalClient(InternalClientManager clientManager,
ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
2020-11-15 20:10:54 +01:00
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
2020-10-13 01:31:32 +02:00
}
public InternalClient(InternalClientManager clientManager,
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
2020-11-14 11:12:52 +01:00
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientId = NativeClientAccess.create();
2020-10-13 18:33:06 +02:00
2020-11-14 11:12:52 +01:00
clientManager.registerClient(clientId, this);
2020-10-13 01:31:32 +02:00
}
@Override
public int getClientId() {
return clientId;
}
@Override
public void handleEvents(boolean isClosed, long[] eventIds, Object[] events) {
if (updatesHandler != null) {
LongArrayList idsToFilter = new LongArrayList(eventIds);
ObjectArrayList<Object> eventsToFilter = new ObjectArrayList<>(events);
for (int i = eventIds.length - 1; i >= 0; i--) {
if (eventIds[i] != 0) {
idsToFilter.removeLong(i);
eventsToFilter.remove(i);
long eventId = eventIds[i];
Object event = events[i];
Handler handler = handlers.remove(eventId);
handleResponse(eventId, event, handler);
}
}
try {
2020-10-13 23:22:21 +02:00
updatesHandler.getUpdatesHandler().onUpdates(eventsToFilter);
2020-10-13 01:31:32 +02:00
} catch (Throwable cause) {
handleException(updatesHandler.getExceptionHandler(), cause);
}
} else {
for (int i = 0; i < eventIds.length; i++) {
handleEvent(eventIds[i], events[i]);
}
}
if (isClosed) {
2020-11-12 13:01:22 +01:00
if (this.isClosed.compareAndSet(false, true)) {
handleClose();
}
2020-10-13 01:31:32 +02:00
}
}
2020-11-12 13:01:22 +01:00
private void handleClose() {
handlers.forEach((eventId, handler) -> {
handleResponse(eventId, new Error(500, "Instance closed"), handler);
});
handlers.clear();
}
2020-10-13 01:31:32 +02:00
/**
* Handles only a response (not an update!)
*/
private void handleResponse(long eventId, Object event, Handler handler) {
if (handler != null) {
try {
2020-10-13 23:22:21 +02:00
handler.getResultHandler().onResult(event);
2020-10-13 01:31:32 +02:00
} catch (Throwable cause) {
handleException(handler.getExceptionHandler(), cause);
}
} else {
System.err.println("Unknown event id " + eventId + ", the event has been dropped!");
}
}
/**
* Handles a response or an update
*/
private void handleEvent(long eventId, Object event) {
if (updatesHandler != null || updateHandler == null) throw new IllegalStateException();
Handler handler = eventId == 0 ? updateHandler : handlers.remove(eventId);
handleResponse(eventId, event, handler);
}
private void handleException(ExceptionHandler exceptionHandler, Throwable cause) {
if (exceptionHandler == null) {
exceptionHandler = defaultExceptionHandler;
}
if (exceptionHandler != null) {
try {
exceptionHandler.onException(cause);
} catch (Throwable ignored) {}
}
}
@Override
public void send(Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
ensureOpen();
long queryId = clientManager.getNextQueryId();
if (resultHandler != null) {
handlers.put(queryId, new Handler(resultHandler, exceptionHandler));
}
NativeClientAccess.send(clientId, queryId, query);
}
@Override
public Object execute(Function query) {
ensureOpen();
return NativeClientAccess.execute(query);
}
private void ensureOpen() {
2020-11-12 13:01:22 +01:00
if (isClosed.get()) {
2020-10-13 01:31:32 +02:00
throw new IllegalStateException("The client is closed!");
}
}
}