]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/DaemonCommand.java
Merge branch 'master' into dbus_sendviewed
[signal-cli] / src / main / java / org / asamk / signal / commands / DaemonCommand.java
index 4a322b993e82b4d2870156be2368ecceea35f7fe..a121c7e93de4f31bc9dfb60b7fe480baf8299169 100644 (file)
@@ -23,7 +23,6 @@ import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.util.List;
 
 import java.io.IOException;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
 
 public class DaemonCommand implements MultiLocalCommand {
 
 
 public class DaemonCommand implements MultiLocalCommand {
 
@@ -54,10 +53,11 @@ public class DaemonCommand implements MultiLocalCommand {
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+        boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
+        m.setIgnoreAttachments(ignoreAttachments);
 
         DBusConnection.DBusBusType busType;
 
         DBusConnection.DBusBusType busType;
-        if (ns.getBoolean("system")) {
+        if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
@@ -65,12 +65,15 @@ public class DaemonCommand implements MultiLocalCommand {
 
         try (var conn = DBusConnection.getConnection(busType)) {
             var objectPath = DbusConfig.getObjectPath();
 
         try (var conn = DBusConnection.getConnection(busType)) {
             var objectPath = DbusConfig.getObjectPath();
-            var t = run(conn, objectPath, m, outputWriter, ignoreAttachments);
+            var t = run(conn, objectPath, m, outputWriter);
 
             conn.requestBusName(DbusConfig.getBusname());
 
             try {
                 t.join();
 
             conn.requestBusName(DbusConfig.getBusname());
 
             try {
                 t.join();
+                synchronized (this) {
+                    wait();
+                }
             } catch (InterruptedException ignored) {
             }
         } catch (DBusException | IOException e) {
             } catch (InterruptedException ignored) {
             }
         } catch (DBusException | IOException e) {
@@ -83,10 +86,10 @@ public class DaemonCommand implements MultiLocalCommand {
     public void handleCommand(
             final Namespace ns, final List<Manager> managers, final SignalCreator c, final OutputWriter outputWriter
     ) throws CommandException {
     public void handleCommand(
             final Namespace ns, final List<Manager> managers, final SignalCreator c, final OutputWriter outputWriter
     ) throws CommandException {
-        boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
+        boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
 
         DBusConnection.DBusBusType busType;
 
         DBusConnection.DBusBusType busType;
-        if (ns.getBoolean("system")) {
+        if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
             busType = DBusConnection.DBusBusType.SYSTEM;
         } else {
             busType = DBusConnection.DBusBusType.SESSION;
@@ -94,9 +97,10 @@ public class DaemonCommand implements MultiLocalCommand {
 
         try (var conn = DBusConnection.getConnection(busType)) {
             final var signalControl = new DbusSignalControlImpl(c, m -> {
 
         try (var conn = DBusConnection.getConnection(busType)) {
             final var signalControl = new DbusSignalControlImpl(c, m -> {
+                m.setIgnoreAttachments(ignoreAttachments);
                 try {
                 try {
-                    final var objectPath = DbusConfig.getObjectPath(m.getUsername());
-                    return run(conn, objectPath, m, outputWriter, ignoreAttachments);
+                    final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
+                    return run(conn, objectPath, m, outputWriter);
                 } catch (DBusException e) {
                     logger.error("Failed to export object", e);
                     return null;
                 } catch (DBusException e) {
                     logger.error("Failed to export object", e);
                     return null;
@@ -118,28 +122,20 @@ public class DaemonCommand implements MultiLocalCommand {
     }
 
     private Thread run(
     }
 
     private Thread run(
-            DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter, boolean ignoreAttachments
+            DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter
     ) throws DBusException {
     ) throws DBusException {
-        conn.exportObject(new DbusSignalImpl(m, objectPath));
+        final var signal = new DbusSignalImpl(m, conn, objectPath);
+        conn.exportObject(signal);
+        final var initThread = new Thread(signal::initObjects);
+        initThread.start();
 
         logger.info("Exported dbus object: " + objectPath);
 
 
         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(1, TimeUnit.HOURS, false, ignoreAttachments, receiveMessageHandler);
-                    break;
-                } catch (IOException e) {
-                    logger.warn("Receiving messages failed, retrying", e);
-                }
-            }
-        });
-
-        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;
     }
 }
     }
 }