Skip to content

Commit 41e83aa

Browse files
committed
Update dependencies, add sendMessage, add CompletableFuture variants to SimpleClient methods.
1 parent 6de20e1 commit 41e83aa

11 files changed

Lines changed: 237 additions & 43 deletions

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ If you are using Maven, edit your `pom.xml` file as below:
8686
<!-- Java 8 is supported if you use the following dependency classifier: <classifier>jdk8</classifier> -->
8787
<!-- don't specify the version here -->
8888
</dependency>
89-
<!-- Example linux amd64 (GCC) ssl1 natives -->
89+
<!-- Example linux amd64 (GNU GCC) ssl1 natives -->
9090
<dependency>
9191
<groupId>it.tdlight</groupId>
9292
<artifactId>tdlight-natives</artifactId>
93-
<classifier>linux_amd64_gcc_ssl1</classifier>
93+
<classifier>linux_amd64_gnu_ssl1</classifier>
9494
<!-- don't specify the version here -->
9595
</dependency>
9696
<!-- Example windows amd64 natives -->
@@ -124,8 +124,8 @@ dependencies {
124124
125125
// do not specify the versions on the dependencies below!
126126
implementation group: 'it.tdlight', name: 'tdlight-java' // Java 8 is supported if you use the following dependency classifier: `jdk8`
127-
implementation group: 'it.tdlight', name: 'tdlight-natives', classifier: 'linux_amd64_gcc_ssl1'
128-
// Include other native classifiers, for example linux_amd64_ssl1, linux_amd64_ssl3, macos_amd64, ... -->
127+
implementation group: 'it.tdlight', name: 'tdlight-natives', classifier: 'linux_amd64_gnu_ssl1'
128+
// Include other native classifiers, for example linux_amd64_clang_ssl3, macos_amd64, ... -->
129129
}
130130
```
131131

@@ -136,13 +136,16 @@ it [here](https://github.com/tdlight-team/tdlight-java/releases).
136136

137137
To use TDLight Java you need to include the native libraries, by specifying one of the following classifier for each tdlight-natives dependency:
138138

139-
- `linux_amd64_ssl1`
140-
- `linux_amd64_ssl3`
141-
- `linux_amd64_gcc_ssl1`
142-
- `linux_amd64_gcc_ssl3`
143-
- `linux_arm64_ssl1`
144-
- `linux_arm64_ssl3`
145-
- `linux_ppc64le_ssl3`
139+
- `linux_amd64_clang_ssl3`
140+
- `linux_amd64_gnu_ssl1`
141+
- `linux_amd64_gnu_ssl3`
142+
- `linux_arm64_clang_ssl3`
143+
- `linux_arm64_gnu_ssl1`
144+
- `linux_arm64_gnu_ssl3`
145+
- `linux_armhf_gnu_ssl1`
146+
- `linux_armhf_gnu_ssl3`
147+
- `linux_ppc64el_gnu_ssl3`
148+
- `linux_riscv64_gnu_ssl3`
146149
- `windows_amd64`
147150
- `macos_amd64`
148151

bom/pom.xml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<name>TDLight Java BOM</name>
99
<properties>
1010
<revision>3.0.0.0-SNAPSHOT</revision>
11-
<tdlight.natives.version>4.0.440</tdlight.natives.version>
12-
<tdlight.api.version>4.0.410</tdlight.api.version>
11+
<tdlight.natives.version>4.0.480</tdlight.natives.version>
12+
<tdlight.api.version>4.0.450</tdlight.api.version>
1313
<maven.compiler.source>1.8</maven.compiler.source>
1414
<maven.compiler.target>1.8</maven.compiler.target>
1515
</properties>
@@ -82,43 +82,61 @@
8282
<groupId>it.tdlight</groupId>
8383
<artifactId>tdlight-natives</artifactId>
8484
<version>${tdlight.natives.version}</version>
85-
<classifier>linux_amd64_ssl1</classifier>
85+
<classifier>linux_amd64_clang_ssl3</classifier>
8686
</dependency>
8787
<dependency>
8888
<groupId>it.tdlight</groupId>
8989
<artifactId>tdlight-natives</artifactId>
9090
<version>${tdlight.natives.version}</version>
91-
<classifier>linux_amd64_ssl3</classifier>
91+
<classifier>linux_amd64_gnu_ssl1</classifier>
9292
</dependency>
9393
<dependency>
9494
<groupId>it.tdlight</groupId>
9595
<artifactId>tdlight-natives</artifactId>
9696
<version>${tdlight.natives.version}</version>
97-
<classifier>linux_amd64_gcc_ssl1</classifier>
97+
<classifier>linux_amd64_gnu_ssl3</classifier>
9898
</dependency>
9999
<dependency>
100100
<groupId>it.tdlight</groupId>
101101
<artifactId>tdlight-natives</artifactId>
102102
<version>${tdlight.natives.version}</version>
103-
<classifier>linux_amd64_gcc_ssl3</classifier>
103+
<classifier>linux_arm64_clang_ssl3</classifier>
104104
</dependency>
105105
<dependency>
106106
<groupId>it.tdlight</groupId>
107107
<artifactId>tdlight-natives</artifactId>
108108
<version>${tdlight.natives.version}</version>
109-
<classifier>linux_arm64_ssl1</classifier>
109+
<classifier>linux_arm64_gnu_ssl1</classifier>
110110
</dependency>
111111
<dependency>
112112
<groupId>it.tdlight</groupId>
113113
<artifactId>tdlight-natives</artifactId>
114114
<version>${tdlight.natives.version}</version>
115-
<classifier>linux_arm64_ssl3</classifier>
115+
<classifier>linux_arm64_gnu_ssl3</classifier>
116116
</dependency>
117117
<dependency>
118118
<groupId>it.tdlight</groupId>
119119
<artifactId>tdlight-natives</artifactId>
120120
<version>${tdlight.natives.version}</version>
121-
<classifier>linux_ppc64le_ssl3</classifier>
121+
<classifier>linux_armhf_gnu_ssl1</classifier>
122+
</dependency>
123+
<dependency>
124+
<groupId>it.tdlight</groupId>
125+
<artifactId>tdlight-natives</artifactId>
126+
<version>${tdlight.natives.version}</version>
127+
<classifier>linux_armhf_gnu_ssl3</classifier>
128+
</dependency>
129+
<dependency>
130+
<groupId>it.tdlight</groupId>
131+
<artifactId>tdlight-natives</artifactId>
132+
<version>${tdlight.natives.version}</version>
133+
<classifier>linux_ppc64el_gnu_ssl3</classifier>
134+
</dependency>
135+
<dependency>
136+
<groupId>it.tdlight</groupId>
137+
<artifactId>tdlight-natives</artifactId>
138+
<version>${tdlight.natives.version}</version>
139+
<classifier>linux_riscv64_gnu_ssl3</classifier>
122140
</dependency>
123141
<dependency>
124142
<groupId>it.tdlight</groupId>

example/pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,17 @@
4444
<dependency>
4545
<groupId>it.tdlight</groupId>
4646
<artifactId>tdlight-natives</artifactId>
47-
<classifier>linux_amd64_ssl1</classifier>
47+
<classifier>linux_amd64_gnu_ssl1</classifier>
4848
</dependency>
4949
<dependency>
5050
<groupId>it.tdlight</groupId>
5151
<artifactId>tdlight-natives</artifactId>
52-
<classifier>linux_amd64_gcc_ssl1</classifier>
52+
<classifier>linux_amd64_clang_ssl3</classifier>
5353
</dependency>
5454
<dependency>
5555
<groupId>it.tdlight</groupId>
5656
<artifactId>tdlight-natives</artifactId>
57-
<classifier>linux_amd64_ssl3</classifier>
58-
</dependency>
59-
<dependency>
60-
<groupId>it.tdlight</groupId>
61-
<artifactId>tdlight-natives</artifactId>
62-
<classifier>linux_amd64_gcc_ssl3</classifier>
57+
<classifier>linux_amd64_gnu_ssl3</classifier>
6358
</dependency>
6459
<dependency>
6560
<groupId>it.tdlight</groupId>

tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitForExit.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
final class AuthorizationStateWaitForExit implements GenericUpdateHandler<TdApi.UpdateAuthorizationState> {
99

10-
private final CountDownLatch closed;
10+
private final Runnable setClosed;
1111

12-
public AuthorizationStateWaitForExit(CountDownLatch closed) {
13-
this.closed = closed;
12+
public AuthorizationStateWaitForExit(Runnable setClosed) {
13+
this.setClosed = setClosed;
1414
}
1515

1616
@Override
1717
public void onUpdate(UpdateAuthorizationState update) {
1818
if (update.authorizationState.getConstructor() == AuthorizationStateClosed.CONSTRUCTOR) {
19-
closed.countDown();
19+
setClosed.run();
2020
}
2121
}
2222
}

tdlight-java/src/main/java/it/tdlight/client/MutableTelegramClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface MutableTelegramClient {
99

1010
<T extends TdApi.Update> void addCommandHandler(String commandName, CommandHandler handler);
1111

12-
<T extends TdApi.Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<T> handler);
12+
<T extends TdApi.Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<? super T> handler);
1313

1414
void addUpdatesHandler(GenericUpdateHandler<TdApi.Update> handler);
1515

tdlight-java/src/main/java/it/tdlight/client/SimpleResultHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class SimpleResultHandler<T extends Update> implements ResultHandler<Update> {
99

1010
private final int updateConstructor;
11-
private final GenericUpdateHandler<T> handler;
11+
private final GenericUpdateHandler<? super T> handler;
1212

13-
public SimpleResultHandler(Class<T> type, GenericUpdateHandler<T> handler) {
13+
public SimpleResultHandler(Class<T> type, GenericUpdateHandler<? super T> handler) {
1414
this.updateConstructor = ConstructorDetector.getConstructor(type);
1515
this.handler = handler;
1616
}

tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import it.tdlight.Init;
77
import it.tdlight.ResultHandler;
88
import it.tdlight.TelegramClient;
9+
import it.tdlight.jni.TdApi.Message;
910
import it.tdlight.jni.TdApi.Update;
1011
import it.tdlight.util.UnsupportedNativeLibraryException;
1112
import it.tdlight.jni.TdApi;
@@ -20,7 +21,12 @@
2021
import java.util.Objects;
2122
import java.util.Set;
2223
import java.util.concurrent.CompletableFuture;
24+
import java.util.concurrent.CompletionStage;
25+
import java.util.concurrent.ConcurrentHashMap;
26+
import java.util.concurrent.ConcurrentMap;
2327
import java.util.concurrent.CountDownLatch;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.Future;
2430
import java.util.concurrent.RejectedExecutionException;
2531
import org.slf4j.Logger;
2632
import org.slf4j.LoggerFactory;
@@ -54,7 +60,8 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
5460
private final AuthorizationStateReadyLoadChats mainChatsLoader;
5561
private final AuthorizationStateReadyLoadChats archivedChatsLoader;
5662

57-
private final CountDownLatch closed = new CountDownLatch(1);
63+
private final CompletableFuture<Void> closed = new CompletableFuture<>();
64+
private final ConcurrentMap<TemporaryMessageURL, CompletableFuture<Message>> temporaryMessages = new ConcurrentHashMap<>();
5865

5966
SimpleTelegramClient(ClientFactory clientFactory,
6067
TDLibSettings settings,
@@ -109,12 +116,15 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
109116
this::handleDefaultException
110117
)
111118
);
112-
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class, new AuthorizationStateWaitForExit(this.closed));
119+
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class, new AuthorizationStateWaitForExit(this::onCloseUpdate));
113120
this.mainChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListMain());
114121
this.archivedChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListArchive());
115122
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
116123
this.meGetter = new AuthorizationStateReadyGetMe(client, mainChatsLoader, archivedChatsLoader));
117124
this.addUpdateHandler(TdApi.UpdateNewMessage.class, new CommandsHandler(client, this.commandHandlers, this::getMe));
125+
var temporaryMessageHandler = new TemporaryMessageHandler(this.temporaryMessages);
126+
this.addUpdateHandler(TdApi.UpdateMessageSendSucceeded.class, temporaryMessageHandler);
127+
this.addUpdateHandler(TdApi.UpdateMessageSendFailed.class, temporaryMessageHandler);
118128
this.authenticationData = authenticationData;
119129
createDirectories();
120130
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
@@ -191,7 +201,7 @@ public <T extends TdApi.Update> void addCommandHandler(String commandName, Comma
191201
}
192202

193203
@Override
194-
public <T extends TdApi.Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<T> handler) {
204+
public <T extends TdApi.Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<? super T> handler) {
195205
this.updateHandlers.put(new SimpleResultHandler<>(updateType, handler), null);
196206
}
197207

@@ -257,6 +267,44 @@ public <R extends TdApi.Object> void send(TdApi.Function<R> function, GenericRes
257267
client.send(function, result -> resultHandler.onResult(Result.of(result)), resultHandlerExceptionHandler);
258268
}
259269

270+
/**
271+
* Sends a request to TDLib and get the result.
272+
*
273+
* @param function The request to TDLib.
274+
* @throws NullPointerException if function is null.
275+
*/
276+
@SuppressWarnings("unchecked")
277+
public <R extends TdApi.Object> CompletableFuture<R> send(TdApi.Function<R> function) {
278+
CompletableFuture<R> future = new CompletableFuture<>();
279+
client.send(function, result -> {
280+
if (result instanceof TdApi.Error) {
281+
future.completeExceptionally(new TelegramError((TdApi.Error) result));
282+
} else {
283+
future.complete((R) result);
284+
}
285+
}, future::completeExceptionally);
286+
return future;
287+
}
288+
289+
/**
290+
* Sends a message
291+
*
292+
* @param function The request to TDLib.
293+
* @param wait Wait until the message has been sent.
294+
* If false, the returned message will not represent the real message, but it will be a temporary message.
295+
* @throws NullPointerException if function is null.
296+
*/
297+
public CompletableFuture<TdApi.Message> sendMessage(TdApi.SendMessage function, boolean wait) {
298+
return this.send(function).thenCompose(msg -> {
299+
CompletableFuture<TdApi.Message> future = new CompletableFuture<>();
300+
CompletableFuture<?> prev = temporaryMessages.put(new TemporaryMessageURL(msg.chatId, msg.id), future);
301+
if (prev != null) {
302+
prev.completeExceptionally(new IllegalStateException("Another temporary message has the same id"));
303+
}
304+
return future;
305+
});
306+
}
307+
260308
/**
261309
* Execute a synchronous function.
262310
* <strong>Please note that only some functions can be executed using this method.</strong>
@@ -266,6 +314,13 @@ public <R extends TdApi.Object> Result<R> execute(TdApi.Function<R> function) {
266314
return Result.of(client.execute(function));
267315
}
268316

317+
/**
318+
* Send the close signal but don't wait
319+
*/
320+
public CompletableFuture<Void> close() {
321+
return this.send(new TdApi.Close()).thenCompose(x -> this.waitForExitAsync());
322+
}
323+
269324
/**
270325
* Send the close signal but don't wait
271326
*/
@@ -293,7 +348,23 @@ public void closeAndWait() throws InterruptedException {
293348
* Wait until TDLight is closed
294349
*/
295350
public void waitForExit() throws InterruptedException {
296-
closed.await();
351+
try {
352+
closed.get();
353+
} catch (ExecutionException e) {
354+
throw new RuntimeException(e);
355+
}
356+
}
357+
358+
/**
359+
* Wait until TDLight is closed
360+
*/
361+
public CompletableFuture<Void> waitForExitAsync() {
362+
return closed.copy();
363+
}
364+
365+
private void onCloseUpdate() {
366+
this.closed.complete(null);
367+
this.temporaryMessages.clear();
297368
}
298369

299370
private final class SimpleTelegramClientInteraction implements ClientInteraction {

tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public <T extends Update> void addCommandHandler(String commandName, CommandHand
3939
}
4040

4141
@Override
42-
public <T extends Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<T> handler) {
42+
public <T extends Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<? super T> handler) {
4343
this.updateHandlers.add(new SimpleResultHandler<>(updateType, handler));
4444
}
4545

0 commit comments

Comments
 (0)