Allow millisecond timeouts

This commit is contained in:
AsamK 2016-11-28 12:38:43 +01:00
parent 2351a89b00
commit c5cf78a50a
2 changed files with 9 additions and 8 deletions

View file

@ -46,6 +46,7 @@ import java.security.Security;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class Main { public class Main {
@ -365,9 +366,9 @@ public class Main {
System.err.println("User is not registered."); System.err.println("User is not registered.");
return 1; return 1;
} }
int timeout = 5; double timeout = 5;
if (ns.getInt("timeout") != null) { if (ns.getDouble("timeout") != null) {
timeout = ns.getInt("timeout"); timeout = ns.getDouble("timeout");
} }
boolean returnOnTimeout = true; boolean returnOnTimeout = true;
if (timeout < 0) { if (timeout < 0) {
@ -375,7 +376,7 @@ public class Main {
timeout = 3600; timeout = 3600;
} }
try { try {
m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m)); m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, new ReceiveMessageHandler(m));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); System.err.println("Error while receiving messages: " + e.getMessage());
return 3; return 3;
@ -549,7 +550,7 @@ public class Main {
return 2; return 2;
} }
try { try {
m.receiveMessages(3600, false, new DbusReceiveMessageHandler(m, conn)); m.receiveMessages(1, TimeUnit.HOURS, false, new DbusReceiveMessageHandler(m, conn));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); System.err.println("Error while receiving messages: " + e.getMessage());
return 3; return 3;
@ -719,7 +720,7 @@ public class Main {
Subparser parserReceive = subparsers.addParser("receive"); Subparser parserReceive = subparsers.addParser("receive");
parserReceive.addArgument("-t", "--timeout") parserReceive.addArgument("-t", "--timeout")
.type(int.class) .type(double.class)
.help("Number of seconds to wait for new messages (negative values disable timeout)"); .help("Number of seconds to wait for new messages (negative values disable timeout)");
Subparser parserDaemon = subparsers.addParser("daemon"); Subparser parserDaemon = subparsers.addParser("daemon");

View file

@ -1027,7 +1027,7 @@ class Manager implements Signal {
} }
} }
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException { public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
retryFailedReceivedMessages(handler); retryFailedReceivedMessages(handler);
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
SignalServiceMessagePipe messagePipe = null; SignalServiceMessagePipe messagePipe = null;
@ -1041,7 +1041,7 @@ class Manager implements Signal {
Exception exception = null; Exception exception = null;
final long now = new Date().getTime(); final long now = new Date().getTime();
try { try {
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS, new SignalServiceMessagePipe.MessagePipeCallback() { envelope = messagePipe.read(timeout, unit, new SignalServiceMessagePipe.MessagePipeCallback() {
@Override @Override
public void onMessage(SignalServiceEnvelope envelope) { public void onMessage(SignalServiceEnvelope envelope) {
// store message on disk, before acknowledging receipt to the server // store message on disk, before acknowledging receipt to the server