Update to version 4. Change client creation method, add .initialize(handlers)

This commit is contained in:
Andrea Cavalli 2021-01-24 13:19:13 +01:00
parent 1492b63c34
commit 35e3b7748d
8 changed files with 72 additions and 87 deletions

View File

@ -159,7 +159,8 @@ public final class Example {
case TdApi.AuthorizationStateClosed.CONSTRUCTOR: case TdApi.AuthorizationStateClosed.CONSTRUCTOR:
print("Closed"); print("Closed");
if (!needQuit) { if (!needQuit) {
client = ClientManager.create(new UpdateHandler(), new ErrorHandler(), new ErrorHandler()); // recreate client after previous has closed client = ClientManager.create(); // recreate client after previous has closed
client.initialize(new UpdateHandler(), new ErrorHandler(), new ErrorHandler());
} else { } else {
canQuit = true; canQuit = true;
} }
@ -282,7 +283,7 @@ public final class Example {
java.util.Iterator<OrderedChat> iter = mainChatList.iterator(); java.util.Iterator<OrderedChat> iter = mainChatList.iterator();
System.out.println(); System.out.println();
System.out.println("First " + limit + " chat(s) out of " + mainChatList.size() + " known chat(s):"); System.out.println("First " + limit + " chat(s) out of " + mainChatList.size() + " known chat(s):");
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit && iter.hasNext(); i++) {
long chatId = iter.next().chatId; long chatId = iter.next().chatId;
TdApi.Chat chat = chats.get(chatId); TdApi.Chat chat = chats.get(chatId);
synchronized (chat) { synchronized (chat) {
@ -306,7 +307,8 @@ public final class Example {
// create client // create client
Init.start(); Init.start();
client = ClientManager.create(new UpdateHandler(), new ErrorHandler(), new ErrorHandler()); client = ClientManager.create();
client.initialize(new UpdateHandler(), new ErrorHandler(), new ErrorHandler());
client.execute(new TdApi.SetLogVerbosityLevel(0)); client.execute(new TdApi.SetLogVerbosityLevel(0));
// disable TDLib log // disable TDLib log

View File

@ -1,7 +1,5 @@
package it.tdlight.common; package it.tdlight.common;
import it.tdlight.jni.TdApi;
public abstract class CommonClientManager { public abstract class CommonClientManager {
private static InternalClientManager getClientManager(String implementationName) { private static InternalClientManager getClientManager(String implementationName) {
@ -9,33 +7,12 @@ public abstract class CommonClientManager {
return InternalClientManager.get(implementationName); return InternalClientManager.get(implementationName);
} }
protected static TelegramClient create(String implementationName, protected static TelegramClient create(String implementationName) {
ResultHandler updateHandler, InternalClient client = new InternalClient(getClientManager(implementationName));
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
InternalClient client = new InternalClient(getClientManager(implementationName),
updateHandler,
updateExceptionHandler,
defaultExceptionHandler
);
return create(client);
}
protected static TelegramClient create(String implementationName,
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
InternalClient client = new InternalClient(getClientManager(implementationName),
updatesHandler,
updateExceptionHandler,
defaultExceptionHandler
);
return create(client); return create(client);
} }
private static TelegramClient create(InternalClient internalClient) { private static TelegramClient create(InternalClient internalClient) {
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
internalClient.send(new TdApi.GetAuthorizationState(), (result) -> {}, ex -> {});
return internalClient; return internalClient;
} }
} }

View File

@ -13,38 +13,16 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>(); private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
private final int clientId; private int clientId;
private final InternalClientManager clientManager; private final InternalClientManager clientManager;
private final Handler updateHandler; private Handler updateHandler;
private final MultiHandler updatesHandler; private MultiHandler updatesHandler;
private final ExceptionHandler defaultExceptionHandler; private ExceptionHandler defaultExceptionHandler;
private final AtomicBoolean isClosed = new AtomicBoolean(); private final AtomicBoolean isClosed = new AtomicBoolean();
public InternalClient(InternalClientManager clientManager, public InternalClient(InternalClientManager clientManager) {
ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientManager = clientManager; this.clientManager = clientManager;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
}
public InternalClient(InternalClientManager clientManager,
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
} }
@Override @Override
@ -131,6 +109,35 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
} }
} }
@Override
public void initialize(UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.defaultExceptionHandler = defaultExceptionHandler;
clientManager.registerClient(clientId, this);
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
this.send(new TdApi.GetAuthorizationState(), (result) -> {}, ex -> {});
}
@Override
public void initialize(ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.updateHandler = new Handler(updateHandler, updateExceptionHandler);
this.updatesHandler = null;
this.defaultExceptionHandler = defaultExceptionHandler;
this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this);
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
this.send(new TdApi.GetAuthorizationState(), (result) -> {}, ex -> {});
}
@Override @Override
public void send(Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) { public void send(Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
if (isClosedAndMaybeThrow(query)) { if (isClosedAndMaybeThrow(query)) {

View File

@ -4,6 +4,31 @@ import it.tdlight.jni.TdApi;
public interface TelegramClient { public interface TelegramClient {
/**
* Initialize the client synchronously.
* @param updatesHandler Handler in which the updates are received
* @param updateExceptionHandler Handler in which the errors from updates are received
* @param defaultExceptionHandler Handler that receives exceptions triggered in a handler
*/
void initialize(UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler);
/**
* Initialize the client synchronously.
* @param updateHandler Handler in which the updates are received
* @param updateExceptionHandler Handler in which the errors from updates are received
* @param defaultExceptionHandler Handler that receives exceptions triggered in a handler
*/
default void initialize(ResultHandler updateHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
this.initialize((UpdatesHandler) updates -> updates.forEach(updateHandler::onResult),
updateExceptionHandler,
defaultExceptionHandler
);
}
/** /**
* Sends a request to the TDLib. * Sends a request to the TDLib.
* *

View File

@ -1,10 +1,7 @@
package it.tdlight.tdlib; package it.tdlight.tdlib;
import it.tdlight.common.CommonClientManager; import it.tdlight.common.CommonClientManager;
import it.tdlight.common.ExceptionHandler;
import it.tdlight.common.ResultHandler;
import it.tdlight.common.TelegramClient; import it.tdlight.common.TelegramClient;
import it.tdlight.common.UpdatesHandler;
/** /**
* Interface for interaction with TDLib. * Interface for interaction with TDLib.
@ -13,17 +10,7 @@ public class ClientManager extends CommonClientManager {
private static final String implementationName = "tdlib"; private static final String implementationName = "tdlib";
public static TelegramClient create( public static TelegramClient create() {
ResultHandler updateHandler, return CommonClientManager.create(implementationName);
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
return CommonClientManager.create(implementationName, updateHandler, updateExceptionHandler, defaultExceptionHandler);
}
public static TelegramClient create(
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
return CommonClientManager.create(implementationName, updatesHandler, updateExceptionHandler, defaultExceptionHandler);
} }
} }

View File

@ -1,10 +1,7 @@
package it.tdlight.tdlight; package it.tdlight.tdlight;
import it.tdlight.common.CommonClientManager; import it.tdlight.common.CommonClientManager;
import it.tdlight.common.ExceptionHandler;
import it.tdlight.common.ResultHandler;
import it.tdlight.common.TelegramClient; import it.tdlight.common.TelegramClient;
import it.tdlight.common.UpdatesHandler;
/** /**
* Interface for interaction with TDLight. * Interface for interaction with TDLight.
@ -13,17 +10,7 @@ public class ClientManager extends CommonClientManager {
private static final String implementationName = "tdlight"; private static final String implementationName = "tdlight";
public static TelegramClient create( public static TelegramClient create() {
ResultHandler updateHandler, return CommonClientManager.create(implementationName);
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
return CommonClientManager.create(implementationName, updateHandler, updateExceptionHandler, defaultExceptionHandler);
}
public static TelegramClient create(
UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) {
return CommonClientManager.create(implementationName, updatesHandler, updateExceptionHandler, defaultExceptionHandler);
} }
} }

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlib-java</artifactId> <artifactId>tdlib-java</artifactId>
<version>3.171.${revision}</version> <version>4.171.${revision}</version>
<name>TDLib Java Wrapper</name> <name>TDLib Java Wrapper</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId> <artifactId>tdlight-java</artifactId>
<version>3.171.${revision}</version> <version>4.171.${revision}</version>
<name>TDLight Java Wrapper</name> <name>TDLight Java Wrapper</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>