]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java
Implement close for DbusManagerImpl
[signal-cli] / src / main / java / org / asamk / signal / dbus / DbusManagerImpl.java
index 0d5ed98e5a662be69821dc4090993d9a457ecd6f..7eb7c4dcbbef9c9e9e6d5208cc4c7f19bf537c79 100644 (file)
@@ -437,13 +437,7 @@ public class DbusManagerImpl implements Manager {
         synchronized (messageHandlers) {
             messageHandlers.remove(handler);
             if (messageHandlers.size() == 0) {
-                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();
-                }
+                uninstallMessageHandlers();
             }
         }
     }
@@ -583,6 +577,13 @@ public class DbusManagerImpl implements Manager {
 
     @Override
     public void close() throws IOException {
+        synchronized (this) {
+            this.notify();
+        }
+        synchronized (messageHandlers) {
+            messageHandlers.clear();
+            uninstallMessageHandlers();
+        }
     }
 
     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) {
         if (!extras.containsKey("attachments")) {
             return List.of();