Move receive thread handling to manager

This commit is contained in:
AsamK 2021-10-21 22:59:52 +02:00
parent 5c389c875d
commit fc0a9b4102
5 changed files with 156 additions and 49 deletions

View file

@ -71,6 +71,9 @@ public class DaemonCommand implements MultiLocalCommand {
try {
t.join();
synchronized (this) {
wait();
}
} catch (InterruptedException ignored) {
}
} catch (DBusException | IOException e) {
@ -128,27 +131,11 @@ public class DaemonCommand implements MultiLocalCommand {
logger.info("Exported dbus object: " + objectPath);
final var thread = new Thread(() -> {
while (!Thread.interrupted()) {
try {
final var receiveMessageHandler = outputWriter instanceof JsonWriter
? new JsonDbusReceiveMessageHandler(m, (JsonWriter) outputWriter, conn, objectPath)
: new DbusReceiveMessageHandler(m, (PlainTextWriter) outputWriter, conn, objectPath);
m.receiveMessages(receiveMessageHandler);
break;
} catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e);
}
}
try {
initThread.join();
} catch (InterruptedException ignored) {
}
signal.close();
});
thread.start();
return thread;
final var receiveMessageHandler = outputWriter instanceof JsonWriter ? new JsonDbusReceiveMessageHandler(m,
(JsonWriter) outputWriter,
conn,
objectPath) : new DbusReceiveMessageHandler(m, (PlainTextWriter) outputWriter, conn, objectPath);
m.addReceiveHandler(receiveMessageHandler);
return initThread;
}
}

View file

@ -70,10 +70,11 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
final var objectMapper = Util.createJsonObjectMapper();
final var jsonRpcSender = new JsonRpcSender((JsonWriter) outputWriter);
final var receiveThread = receiveMessages(s -> jsonRpcSender.sendRequest(JsonRpcRequest.forNotification(
"receive",
objectMapper.valueToTree(s),
null)), m);
final var receiveMessageHandler = new JsonReceiveMessageHandler(m,
s -> jsonRpcSender.sendRequest(JsonRpcRequest.forNotification("receive",
objectMapper.valueToTree(s),
null)));
m.addReceiveHandler(receiveMessageHandler);
// Maybe this should be handled inside the Manager
while (!m.hasCaughtUpWithOldMessages()) {
@ -97,11 +98,7 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
jsonRpcReader.readRequests((method, params) -> handleRequest(m, objectMapper, method, params),
response -> logger.debug("Received unexpected response for id {}", response.getId()));
receiveThread.interrupt();
try {
receiveThread.join();
} catch (InterruptedException ignored) {
}
m.removeReceiveHandler(receiveMessageHandler);
}
private JsonNode handleRequest(
@ -166,22 +163,4 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
}
command.handleCommand(requestParams, m, outputWriter);
}
private Thread receiveMessages(JsonWriter jsonWriter, Manager m) {
final var thread = new Thread(() -> {
while (!Thread.interrupted()) {
try {
final var receiveMessageHandler = new JsonReceiveMessageHandler(m, jsonWriter);
m.receiveMessages(receiveMessageHandler);
break;
} catch (IOException e) {
logger.warn("Receiving messages failed, retrying", e);
}
}
});
thread.start();
return thread;
}
}

View file

@ -423,6 +423,21 @@ public class DbusManagerImpl implements Manager {
signal.sendSyncRequest();
}
@Override
public void addReceiveHandler(final ReceiveMessageHandler handler) {
throw new UnsupportedOperationException();
}
@Override
public void removeReceiveHandler(final ReceiveMessageHandler handler) {
throw new UnsupportedOperationException();
}
@Override
public boolean isReceiving() {
throw new UnsupportedOperationException();
}
@Override
public void receiveMessages(final ReceiveMessageHandler handler) throws IOException {
throw new UnsupportedOperationException();