Split receiveMessages method

This commit is contained in:
AsamK 2021-10-21 21:19:14 +02:00
parent 430c155f7e
commit 5c389c875d
6 changed files with 32 additions and 15 deletions

View file

@ -193,9 +193,15 @@ public interface Manager extends Closeable {
void requestAllSyncData() throws IOException; void requestAllSyncData() throws IOException;
void receiveMessages( /**
long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
) throws IOException; */
void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException;
/**
* Receive new messages from server, returns only if the thread is interrupted.
*/
void receiveMessages(ReceiveMessageHandler handler) throws IOException;
void setIgnoreAttachments(boolean ignoreAttachments); void setIgnoreAttachments(boolean ignoreAttachments);

View file

@ -873,7 +873,16 @@ public class ManagerImpl implements Manager {
} }
@Override @Override
public void receiveMessages( public void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException {
receiveMessages(timeout, unit, true, handler);
}
@Override
public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
receiveMessages(1L, TimeUnit.HOURS, false, handler);
}
private void receiveMessages(
long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler long timeout, TimeUnit unit, boolean returnOnTimeout, ReceiveMessageHandler handler
) throws IOException { ) throws IOException {
retryFailedReceivedMessages(handler); retryFailedReceivedMessages(handler);

View file

@ -23,7 +23,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
public class DaemonCommand implements MultiLocalCommand { public class DaemonCommand implements MultiLocalCommand {
@ -135,7 +134,7 @@ public class DaemonCommand implements MultiLocalCommand {
final var receiveMessageHandler = outputWriter instanceof JsonWriter final var receiveMessageHandler = outputWriter instanceof JsonWriter
? new JsonDbusReceiveMessageHandler(m, (JsonWriter) outputWriter, conn, objectPath) ? new JsonDbusReceiveMessageHandler(m, (JsonWriter) outputWriter, conn, objectPath)
: new DbusReceiveMessageHandler(m, (PlainTextWriter) outputWriter, conn, objectPath); : new DbusReceiveMessageHandler(m, (PlainTextWriter) outputWriter, conn, objectPath);
m.receiveMessages(1, TimeUnit.HOURS, false, receiveMessageHandler); m.receiveMessages(receiveMessageHandler);
break; break;
} catch (IOException e) { } catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e); logger.warn("Receiving messages failed, retrying", e);

View file

@ -33,7 +33,6 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
public class JsonRpcDispatcherCommand implements LocalCommand { public class JsonRpcDispatcherCommand implements LocalCommand {
@ -173,7 +172,7 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
try { try {
final var receiveMessageHandler = new JsonReceiveMessageHandler(m, jsonWriter); final var receiveMessageHandler = new JsonReceiveMessageHandler(m, jsonWriter);
m.receiveMessages(1, TimeUnit.HOURS, false, receiveMessageHandler); m.receiveMessages(receiveMessageHandler);
break; break;
} catch (IOException e) { } catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e); logger.warn("Receiving messages failed, retrying", e);

View file

@ -142,17 +142,16 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
double timeout = ns.getDouble("timeout"); double timeout = ns.getDouble("timeout");
var returnOnTimeout = true;
if (timeout < 0) {
returnOnTimeout = false;
timeout = 3600;
}
boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
m.setIgnoreAttachments(ignoreAttachments); m.setIgnoreAttachments(ignoreAttachments);
try { try {
final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m, final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
(JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter); (JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);
m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, handler); if (timeout < 0) {
m.receiveMessages(handler);
} else {
m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, handler);
}
} catch (IOException e) { } catch (IOException e) {
throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e); throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
} }

View file

@ -423,9 +423,14 @@ public class DbusManagerImpl implements Manager {
signal.sendSyncRequest(); signal.sendSyncRequest();
} }
@Override
public void receiveMessages(final ReceiveMessageHandler handler) throws IOException {
throw new UnsupportedOperationException();
}
@Override @Override
public void receiveMessages( public void receiveMessages(
final long timeout, final TimeUnit unit, final boolean returnOnTimeout, final ReceiveMessageHandler handler final long timeout, final TimeUnit unit, final ReceiveMessageHandler handler
) throws IOException { ) throws IOException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }