X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/8f781c019f5f451c9d6323659bb248be335ad0e5..cd29144e81701698092f3334bee0c99c0f77f202:/src/main/java/org/asamk/signal/commands/DaemonCommand.java diff --git a/src/main/java/org/asamk/signal/commands/DaemonCommand.java b/src/main/java/org/asamk/signal/commands/DaemonCommand.java index 7988c8ef..a121c7e9 100644 --- a/src/main/java/org/asamk/signal/commands/DaemonCommand.java +++ b/src/main/java/org/asamk/signal/commands/DaemonCommand.java @@ -23,15 +23,18 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; -import java.util.Set; -import java.util.concurrent.TimeUnit; public class DaemonCommand implements MultiLocalCommand { private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class); - private final OutputWriter outputWriter; - public static void attachToSubparser(final Subparser subparser) { + @Override + public String getName() { + return "daemon"; + } + + @Override + public void attachToSubparser(final Subparser subparser) { subparser.help("Run in daemon mode and provide an experimental dbus interface."); subparser.addArgument("--system") .action(Arguments.storeTrue()) @@ -41,21 +44,20 @@ public class DaemonCommand implements MultiLocalCommand { .action(Arguments.storeTrue()); } - public DaemonCommand(final OutputWriter outputWriter) { - this.outputWriter = outputWriter; - } - @Override - public Set getSupportedOutputTypes() { - return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON); + public List getSupportedOutputTypes() { + return List.of(OutputType.PLAIN_TEXT, OutputType.JSON); } @Override - public void handleCommand(final Namespace ns, final Manager m) throws CommandException { - boolean ignoreAttachments = ns.getBoolean("ignore-attachments"); + public void handleCommand( + final Namespace ns, final Manager m, final OutputWriter outputWriter + ) throws CommandException { + boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); + m.setIgnoreAttachments(ignoreAttachments); DBusConnection.DBusBusType busType; - if (ns.getBoolean("system")) { + if (Boolean.TRUE.equals(ns.getBoolean("system"))) { busType = DBusConnection.DBusBusType.SYSTEM; } else { busType = DBusConnection.DBusBusType.SESSION; @@ -63,28 +65,31 @@ public class DaemonCommand implements MultiLocalCommand { try (var conn = DBusConnection.getConnection(busType)) { var objectPath = DbusConfig.getObjectPath(); - var t = run(conn, objectPath, m, ignoreAttachments); + var t = run(conn, objectPath, m, outputWriter); conn.requestBusName(DbusConfig.getBusname()); try { t.join(); + synchronized (this) { + wait(); + } } catch (InterruptedException ignored) { } } catch (DBusException | IOException e) { logger.error("Dbus command failed", e); - throw new UnexpectedErrorException("Dbus command failed"); + throw new UnexpectedErrorException("Dbus command failed", e); } } @Override public void handleCommand( - final Namespace ns, final List managers, SignalCreator c + final Namespace ns, final List 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; - if (ns.getBoolean("system")) { + if (Boolean.TRUE.equals(ns.getBoolean("system"))) { busType = DBusConnection.DBusBusType.SYSTEM; } else { busType = DBusConnection.DBusBusType.SESSION; @@ -92,9 +97,10 @@ public class DaemonCommand implements MultiLocalCommand { try (var conn = DBusConnection.getConnection(busType)) { final var signalControl = new DbusSignalControlImpl(c, m -> { + m.setIgnoreAttachments(ignoreAttachments); try { - final var objectPath = DbusConfig.getObjectPath(m.getUsername()); - return run(conn, objectPath, m, 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; @@ -111,32 +117,25 @@ public class DaemonCommand implements MultiLocalCommand { signalControl.run(); } catch (DBusException | IOException e) { logger.error("Dbus command failed", e); - throw new UnexpectedErrorException("Dbus command failed"); + throw new UnexpectedErrorException("Dbus command failed", e); } } private Thread run( - DBusConnection conn, String objectPath, Manager m, boolean ignoreAttachments + DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter ) 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); - final var thread = new Thread(() -> { - while (true) { - 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); - } 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; } }