2023-04-11 21:29:23 +00:00
|
|
|
package dev.toad;
|
|
|
|
|
2023-04-17 01:01:54 +00:00
|
|
|
import dev.toad.msg.Message;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
|
public final class Client implements AutoCloseable {
|
2023-04-11 21:29:23 +00:00
|
|
|
|
|
|
|
final Toad toad;
|
|
|
|
|
|
|
|
Client(Toad toad) {
|
|
|
|
this.toad = toad;
|
|
|
|
}
|
2023-04-17 01:01:54 +00:00
|
|
|
|
|
|
|
public CompletableFuture<Message> send(Message message) {
|
|
|
|
if (message.addr().isEmpty()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"Message destination address must be set"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Async
|
|
|
|
.pollCompletable(() -> this.toad.sendMessage(message))
|
|
|
|
.thenCompose((Toad.IdAndToken sent) ->
|
|
|
|
Async.pollCompletable(() ->
|
|
|
|
this.toad.pollResp(sent.token, message.addr().get())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.thenApply(msg -> msg.toOwned());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
this.toad.close();
|
|
|
|
}
|
2023-04-11 21:29:23 +00:00
|
|
|
}
|