+ }
+
+ private SignalJsonRpcDispatcherHandler getSignalJsonRpcDispatcherHandler(
+ final SocketChannel c, final boolean noReceiveOnStart
+ ) {
+ final var lineSupplier = IOUtils.getLineSupplier(Channels.newReader(c, StandardCharsets.UTF_8));
+ final var jsonOutputWriter = new JsonWriterImpl(Channels.newWriter(c, StandardCharsets.UTF_8));
+
+ return new SignalJsonRpcDispatcherHandler(jsonOutputWriter, lineSupplier, noReceiveOnStart);
+ }
+
+ private void runDbusSingleAccount(
+ final Manager m, final boolean isDbusSystem, final boolean noReceiveOnStart
+ ) throws UnexpectedErrorException {
+ runDbus(isDbusSystem, (conn, objectPath) -> {
+ try {
+ exportDbusObject(conn, objectPath, m, noReceiveOnStart).join();
+ } catch (InterruptedException ignored) {
+ }
+ });
+ }
+
+ private void runDbusMultiAccount(
+ final MultiAccountManager c, final boolean noReceiveOnStart, final boolean isDbusSystem
+ ) throws UnexpectedErrorException {
+ runDbus(isDbusSystem, (connection, objectPath) -> {
+ final var signalControl = new DbusSignalControlImpl(c, objectPath);
+ connection.exportObject(signalControl);
+
+ c.addOnManagerAddedHandler(m -> {
+ final var thread = exportMultiAccountManager(connection, m, noReceiveOnStart);
+ if (thread != null) {
+ 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) {
+ }
+ connection.unExportObject(path);
+ });
+
+ final var initThreads = c.getManagers()
+ .stream()
+ .map(m -> exportMultiAccountManager(connection, m, noReceiveOnStart))
+ .filter(Objects::nonNull)
+ .toList();
+
+ for (var t : initThreads) {
+ try {
+ t.join();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ });
+ }
+
+ private void runDbus(
+ final boolean isDbusSystem, DbusRunner dbusRunner
+ ) throws UnexpectedErrorException {
+ DBusConnection.DBusBusType busType;
+ if (isDbusSystem) {
+ busType = DBusConnection.DBusBusType.SYSTEM;
+ } else {
+ busType = DBusConnection.DBusBusType.SESSION;
+ }
+ try {
+ var conn = DBusConnection.getConnection(busType);
+ dbusRunner.run(conn, DbusConfig.getObjectPath());
+
+ conn.requestBusName(DbusConfig.getBusname());
+
+ logger.info("DBus daemon running on {} bus: {}", busType, DbusConfig.getBusname());
+ } catch (DBusException e) {
+ logger.error("Dbus command failed", e);
+ throw new UnexpectedErrorException("Dbus command failed", e);
+ }
+ }
+
+ private Thread exportMultiAccountManager(
+ final DBusConnection conn, final Manager m, final boolean noReceiveOnStart
+ ) {
+ try {
+ final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
+ return exportDbusObject(conn, objectPath, m, noReceiveOnStart);
+ } catch (DBusException e) {
+ logger.error("Failed to export object", e);
+ return null;
+ }
+ }
+
+ private Thread exportDbusObject(
+ final DBusConnection conn, final String objectPath, final Manager m, final boolean noReceiveOnStart
+ ) throws DBusException {
+ final var signal = new DbusSignalImpl(m, conn, objectPath, noReceiveOnStart);
+ conn.exportObject(signal);
+ final var initThread = new Thread(signal::initObjects);
+ initThread.setName("dbus-init");
+ initThread.start();
+
+ logger.debug("Exported dbus object: " + objectPath);
+
+ return initThread;
+ }
+
+ interface DbusRunner {