mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Add optional message limit for receive command
This commit is contained in:
parent
5ed9db4f08
commit
de2bfc7f79
6 changed files with 59 additions and 43 deletions
|
@ -58,6 +58,7 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
@ -497,39 +498,46 @@ public class DbusManagerImpl implements Manager {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(final ReceiveMessageHandler handler) throws IOException {
|
||||
addReceiveHandler(handler);
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
removeReceiveHandler(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(
|
||||
final Duration timeout, final ReceiveMessageHandler handler
|
||||
Optional<Duration> timeout, Optional<Integer> maxMessages, ReceiveMessageHandler handler
|
||||
) throws IOException {
|
||||
final var remainingMessages = new AtomicInteger(maxMessages.orElse(-1));
|
||||
final var lastMessage = new AtomicLong(System.currentTimeMillis());
|
||||
final var thread = Thread.currentThread();
|
||||
|
||||
final ReceiveMessageHandler receiveHandler = (envelope, e) -> {
|
||||
lastMessage.set(System.currentTimeMillis());
|
||||
handler.handleMessage(envelope, e);
|
||||
if (remainingMessages.get() > 0) {
|
||||
if (remainingMessages.decrementAndGet() <= 0) {
|
||||
remainingMessages.set(0);
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
};
|
||||
addReceiveHandler(receiveHandler);
|
||||
while (true) {
|
||||
try {
|
||||
final var sleepTimeRemaining = timeout.toMillis() - (System.currentTimeMillis() - lastMessage.get());
|
||||
if (sleepTimeRemaining < 0) {
|
||||
break;
|
||||
if (timeout.isPresent()) {
|
||||
while (remainingMessages.get() != 0) {
|
||||
try {
|
||||
final var passedTime = System.currentTimeMillis() - lastMessage.get();
|
||||
final var sleepTimeRemaining = timeout.get().toMillis() - passedTime;
|
||||
if (sleepTimeRemaining < 0) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(sleepTimeRemaining);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
Thread.sleep(sleepTimeRemaining);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
removeReceiveHandler(receiveHandler);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue