Add fallback for unregistered clients

This commit is contained in:
Andrea Cavalli 2020-10-13 15:12:13 +02:00
parent a2edb8343b
commit bbedd3734b
2 changed files with 28 additions and 12 deletions

View File

@ -26,11 +26,12 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager;
clientManager.registerClient(this);
clientManager.preregisterClient(this);
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
}
public InternalClient(InternalClientManager clientManager,
@ -40,10 +41,11 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
this.defaultExceptionHandler = defaultExceptionHandler;
clientManager.registerClient(this);
clientManager.preregisterClient(this);
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
}
@Override

View File

@ -1,8 +1,7 @@
package it.tdlight.common;
import it.tdlight.jni.TdApi.Object;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
@ -12,7 +11,8 @@ public class InternalClientManager implements AutoCloseable {
private final String implementationName;
private final ResponseReceiver responseReceiver = new ResponseReceiver(this::handleClientEvents);
private final Int2ObjectMap<ClientEventsHandler> clientEventsHandlerMap = new Int2ObjectOpenHashMap<>();
private final ConcurrentHashMap<Integer, ClientEventsHandler> registeredClientEventHandlers = new ConcurrentHashMap<>();
private final ConcurrentHashMap<ClientEventsHandler, java.lang.Object> unregisteredClientEventHandlers = new ConcurrentHashMap<>();
private final AtomicLong currentQueryId = new AtomicLong();
private InternalClientManager(String implementationName) {
@ -30,7 +30,16 @@ public class InternalClientManager implements AutoCloseable {
}
private void handleClientEvents(int clientId, boolean isClosed, long[] clientEventIds, Object[] clientEvents) {
ClientEventsHandler handler = clientEventsHandlerMap.get(clientId);
ClientEventsHandler handler = registeredClientEventHandlers.get(clientId);
if (handler == null) {
handler = unregisteredClientEventHandlers
.keySet()
.stream()
.filter(item -> item.getClientId() == clientId)
.findAny()
.orElse(null);
}
if (handler != null) {
handler.handleEvents(isClosed, clientEventIds, clientEvents);
@ -39,12 +48,17 @@ public class InternalClientManager implements AutoCloseable {
}
if (isClosed) {
clientEventsHandlerMap.remove(clientId);
registeredClientEventHandlers.remove(clientId);
}
}
public void registerClient(ClientEventsHandler client) {
this.clientEventsHandlerMap.put(client.getClientId(), client);
public void preregisterClient(ClientEventsHandler client) {
this.unregisteredClientEventHandlers.put(client, new java.lang.Object());
}
public void registerClient(int clientId, InternalClient internalClient) {
registeredClientEventHandlers.put(clientId, internalClient);
unregisteredClientEventHandlers.remove(internalClient);
}
public String getImplementationName() {