Implement close for DbusManagerImpl

This commit is contained in:
AsamK 2021-11-06 21:59:11 +01:00
parent fa9401d186
commit 510dc89e92
2 changed files with 21 additions and 10 deletions

View file

@ -344,11 +344,11 @@ public class App {
Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
if (command instanceof LocalCommand localCommand) { if (command instanceof LocalCommand localCommand) {
try { try (final var m = new DbusManagerImpl(ts, dBusConn)) {
localCommand.handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter); localCommand.handleCommand(ns, m, outputWriter);
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
throw new UserErrorException("Command is not yet implemented via dbus", e); throw new UserErrorException("Command is not yet implemented via dbus", e);
} catch (DBusExecutionException e) { } catch (IOException | DBusExecutionException e) {
throw new UnexpectedErrorException(e.getMessage(), e); throw new UnexpectedErrorException(e.getMessage(), e);
} }
} else { } else {

View file

@ -437,13 +437,7 @@ public class DbusManagerImpl implements Manager {
synchronized (messageHandlers) { synchronized (messageHandlers) {
messageHandlers.remove(handler); messageHandlers.remove(handler);
if (messageHandlers.size() == 0) { if (messageHandlers.size() == 0) {
try { uninstallMessageHandlers();
connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
} catch (DBusException e) {
e.printStackTrace();
}
} }
} }
} }
@ -583,6 +577,13 @@ public class DbusManagerImpl implements Manager {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
synchronized (this) {
this.notify();
}
synchronized (messageHandlers) {
messageHandlers.clear();
uninstallMessageHandlers();
}
} }
private SendMessageResults handleMessage( private SendMessageResults handleMessage(
@ -757,6 +758,16 @@ public class DbusManagerImpl implements Manager {
} }
} }
private void uninstallMessageHandlers() {
try {
connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
} catch (DBusException e) {
e.printStackTrace();
}
}
private List<MessageEnvelope.Data.Attachment> getAttachments(final Map<String, Variant<?>> extras) { private List<MessageEnvelope.Data.Attachment> getAttachments(final Map<String, Variant<?>> extras) {
if (!extras.containsKey("attachments")) { if (!extras.containsKey("attachments")) {
return List.of(); return List.of();