]> nmode's Git Repositories - signal-cli/commitdiff
Use Duration for timeout
authorAsamK <asamk@gmx.de>
Mon, 20 Dec 2021 15:12:37 +0000 (16:12 +0100)
committerAsamK <asamk@gmx.de>
Mon, 20 Dec 2021 15:12:37 +0000 (16:12 +0100)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
src/main/java/org/asamk/signal/commands/ReceiveCommand.java
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java

index 6e9c86ad41c8c6e4c4f13b198d4077f07e703a9c..8b373e38dac8371f8f381017b2b54f35ba1e98c4 100644 (file)
@@ -33,6 +33,7 @@ import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
+import java.time.Duration;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -219,7 +220,7 @@ public interface Manager extends Closeable {
     /**
      * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
      */
-    void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException;
+    void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException;
 
     /**
      * Receive new messages from server, returns only if the thread is interrupted.
index 5c3f0e0b691c2483ff046e2df9078f7646799c3c..cb9e473052fbb0572f0315ad480146e7c11b6332 100644 (file)
@@ -97,6 +97,7 @@ import java.net.URISyntaxException;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.security.SignatureException;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -1005,7 +1006,7 @@ public class ManagerImpl implements Manager {
             logger.debug("Starting receiving messages");
             while (!Thread.interrupted()) {
                 try {
-                    receiveMessagesInternal(1L, TimeUnit.HOURS, false, (envelope, e) -> {
+                    receiveMessagesInternal(Duration.ofMinutes(1), false, (envelope, e) -> {
                         synchronized (messageHandlers) {
                             Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> {
                                 try {
@@ -1072,17 +1073,17 @@ public class ManagerImpl implements Manager {
     }
 
     @Override
-    public void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException {
-        receiveMessages(timeout, unit, true, handler);
+    public void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException {
+        receiveMessages(timeout, true, handler);
     }
 
     @Override
     public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
-        receiveMessages(1L, TimeUnit.HOURS, false, handler);
+        receiveMessages(Duration.ofMinutes(1), false, handler);
     }
 
     private void receiveMessages(
-            long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler
+            Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
     ) throws IOException {
         if (isReceiving()) {
             throw new IllegalStateException("Already receiving message.");
@@ -1090,7 +1091,7 @@ public class ManagerImpl implements Manager {
         isReceivingSynchronous = true;
         receiveThread = Thread.currentThread();
         try {
-            receiveMessagesInternal(timeout, unit, returnOnTimeout, handler);
+            receiveMessagesInternal(timeout, returnOnTimeout, handler);
         } finally {
             receiveThread = null;
             hasCaughtUpWithOldMessages = false;
@@ -1099,7 +1100,7 @@ public class ManagerImpl implements Manager {
     }
 
     private void receiveMessagesInternal(
-            long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler
+            Duration timeout, boolean returnOnTimeout, ReceiveMessageHandler handler
     ) throws IOException {
         retryFailedReceivedMessages(handler);
 
@@ -1128,7 +1129,7 @@ public class ManagerImpl implements Manager {
             }
             logger.debug("Checking for new message from server");
             try {
-                var result = signalWebSocket.readOrEmpty(unit.toMillis(timeout), envelope1 -> {
+                var result = signalWebSocket.readOrEmpty(timeout.toMillis(), envelope1 -> {
                     final var recipientId = envelope1.hasSourceUuid()
                             ? resolveRecipient(envelope1.getSourceAddress())
                             : null;
index 0a38ad4e60db096de10ddd79a6a5f70ed2090842..0cd8b77bfdaded5a6a179a1cc5574dada0c0c1e6 100644 (file)
@@ -17,8 +17,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.time.Duration;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
 
 public class ReceiveCommand implements LocalCommand {
 
@@ -59,7 +59,7 @@ public class ReceiveCommand implements LocalCommand {
             if (timeout < 0) {
                 m.receiveMessages(handler);
             } else {
-                m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, handler);
+                m.receiveMessages(Duration.ofMillis((long) (timeout * 1000)), handler);
             }
         } catch (IOException e) {
             throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
index df21d94d61722ad6c8767ad02e324dc256b62c07..73c17f07185f000432de19bb27faa5af577b6a90 100644 (file)
@@ -42,6 +42,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -50,7 +51,6 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.UUID;
-import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
@@ -494,11 +494,11 @@ public class DbusManagerImpl implements Manager {
 
     @Override
     public void receiveMessages(
-            final long timeout, final TimeUnit unit, final ReceiveMessageHandler handler
+            final Duration timeout, final ReceiveMessageHandler handler
     ) throws IOException {
         addReceiveHandler(handler);
         try {
-            Thread.sleep(unit.toMillis(timeout));
+            Thread.sleep(timeout.toMillis());
         } catch (InterruptedException ignored) {
         }
         removeReceiveHandler(handler);