+ return new SignalJsonRpcDispatcherHandler(jsonOutputWriter,
+ lineSupplier,
+ receiveMode == ReceiveMode.MANUAL);
+ }
+
+ protected Thread exportDbusObject(final DBusConnection conn, final String objectPath, final Manager m) {
+ final var signal = new DbusSignalImpl(m, conn, objectPath, receiveMode != ReceiveMode.ON_START);
+
+ return Thread.ofPlatform().name("dbus-init-" + m.getSelfNumber()).start(signal::initObjects);
+ }
+
+ protected void runDbus(
+ final boolean isDbusSystem, MultiAccountDaemonHandler.DbusRunner dbusRunner
+ ) throws CommandException {
+ DBusConnection.DBusBusType busType;
+ if (isDbusSystem) {
+ busType = DBusConnection.DBusBusType.SYSTEM;
+ } else {
+ busType = DBusConnection.DBusBusType.SESSION;
+ }
+ DBusConnection conn;
+ try {
+ conn = DBusConnectionBuilder.forType(busType).build();
+ dbusRunner.run(conn, DbusConfig.getObjectPath());
+ } catch (DBusException e) {
+ throw new UnexpectedErrorException("Dbus command failed: " + e.getMessage(), e);
+ } catch (UnsupportedOperationException e) {
+ throw new UserErrorException("Failed to connect to Dbus: " + e.getMessage(), e);
+ }
+
+ try {
+ conn.requestBusName(DbusConfig.getBusname());
+ } catch (DBusException e) {
+ throw new UnexpectedErrorException(
+ "Dbus command failed, maybe signal-cli dbus daemon is already running: " + e.getMessage(),
+ e);
+ }
+
+ logger.info("DBus daemon running on {} bus: {}", busType, DbusConfig.getBusname());
+ }
+
+ @Override
+ public void close() {
+ // TODO
+ }
+ }
+
+ private static final class SingleAccountDaemonHandler extends DaemonHandler {
+
+ private final Manager m;
+
+ private SingleAccountDaemonHandler(final Manager m, final ReceiveMode receiveMode) {
+ super(receiveMode);
+ this.m = m;
+ }
+
+ @Override
+ public void runSocket(final ServerSocketChannel serverChannel) {
+ runSocket(serverChannel, channel -> {
+ final var handler = getSignalJsonRpcDispatcherHandler(channel);
+ handler.handleConnection(m);
+ });
+ }
+
+ @Override
+ public void runDbus(final boolean isDbusSystem) throws CommandException {
+ runDbus(isDbusSystem, (conn, objectPath) -> {
+ try {
+ exportDbusObject(conn, objectPath, m).join();
+ } catch (InterruptedException ignored) {
+ }
+ });
+ }
+
+ @Override
+ public void runHttp(InetSocketAddress address) throws CommandException {
+ final var handler = new HttpServerHandler(address, m);
+ try {
+ handler.init();
+ } catch (IOException ex) {
+ throw new IOErrorException("Failed to initialize HTTP Server", ex);
+ }
+ }
+ }
+
+ private static final class MultiAccountDaemonHandler extends DaemonHandler {
+
+ private final MultiAccountManager c;
+
+ private MultiAccountDaemonHandler(final MultiAccountManager c, final ReceiveMode receiveMode) {
+ super(receiveMode);
+ this.c = c;
+ }
+
+ public void runSocket(final ServerSocketChannel serverChannel) {
+ runSocket(serverChannel, channel -> {
+ final var handler = getSignalJsonRpcDispatcherHandler(channel);
+ handler.handleConnection(c);
+ });
+ }
+
+ public void runDbus(final boolean isDbusSystem) throws CommandException {
+ runDbus(isDbusSystem, (connection, objectPath) -> {
+ final var signalControl = new DbusSignalControlImpl(c, objectPath);
+ connection.exportObject(signalControl);
+
+ c.addOnManagerAddedHandler(m -> {
+ final var thread = exportManager(connection, m);
+ try {
+ thread.join();
+ } catch (InterruptedException ignored) {
+ }
+ });
+ c.addOnManagerRemovedHandler(m -> {
+ final var path = DbusConfig.getObjectPath(m.getSelfNumber());
+ try {
+ final var object = connection.getExportedObject(null, path);
+ if (object instanceof DbusSignalImpl dbusSignal) {
+ dbusSignal.close();
+ }
+ } catch (DBusException ignored) {
+ }
+ });
+
+ final var initThreads = c.getManagers().stream().map(m -> exportManager(connection, m)).toList();
+
+ for (var t : initThreads) {
+ try {
+ t.join();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ });
+ }
+
+ @Override
+ public void runHttp(final InetSocketAddress address) throws CommandException {
+ final var handler = new HttpServerHandler(address, c);
+ try {
+ handler.init();
+ } catch (IOException ex) {
+ throw new IOErrorException("Failed to initialize HTTP Server", ex);
+ }
+ }
+
+ private Thread exportManager(
+ final DBusConnection conn, final Manager m
+ ) {
+ final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
+ return exportDbusObject(conn, objectPath, m);
+ }
+
+ interface DbusRunner {
+
+ void run(DBusConnection connection, String objectPath) throws DBusException;
+ }