package org.asamk.signal.dbus;
import org.asamk.signal.DbusConfig;
+import org.asamk.signal.Shutdown;
import org.asamk.signal.commands.exceptions.CommandException;
+import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
+import org.freedesktop.dbus.connections.IDisconnectCallback;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
private final boolean isDbusSystem;
private DBusConnection dBusConnection;
+ private final String busname;
private final List<AutoCloseable> closeables = new ArrayList<>();
private final DbusRunner dbusRunner;
private final boolean noReceiveOnStart;
- public DbusHandler(final boolean isDbusSystem, final Manager m, final boolean noReceiveOnStart) {
+ public DbusHandler(
+ final boolean isDbusSystem,
+ final String busname,
+ final Manager m,
+ final boolean noReceiveOnStart
+ ) {
this.isDbusSystem = isDbusSystem;
this.dbusRunner = (connection) -> {
try {
}
};
this.noReceiveOnStart = noReceiveOnStart;
+ this.busname = busname;
}
- public DbusHandler(final boolean isDbusSystem, final MultiAccountManager c, final boolean noReceiveOnStart) {
+ public DbusHandler(
+ final boolean isDbusSystem,
+ final String busname,
+ final MultiAccountManager c,
+ final boolean noReceiveOnStart
+ ) {
this.isDbusSystem = isDbusSystem;
this.dbusRunner = (connection) -> {
final var signalControl = new DbusSignalControlImpl(c, DbusConfig.getObjectPath());
}
};
this.noReceiveOnStart = noReceiveOnStart;
+ this.busname = busname;
}
public void init() throws CommandException {
throw new AssertionError("DbusHandler already initialized");
}
final var busType = isDbusSystem ? DBusConnection.DBusBusType.SYSTEM : DBusConnection.DBusBusType.SESSION;
- logger.debug("Starting DBus server on {} bus: {}", busType, DbusConfig.getBusname());
+ logger.debug("Starting DBus server on {} bus: {}", busType, busname);
try {
- dBusConnection = DBusConnectionBuilder.forType(busType).build();
+ dBusConnection = DBusConnectionBuilder.forType(busType)
+ .withDisconnectCallback(new DisconnectCallback())
+ .build();
dbusRunner.run(dBusConnection);
} catch (DBusException e) {
throw new UnexpectedErrorException("Dbus command failed: " + e.getMessage(), e);
}
try {
- dBusConnection.requestBusName(DbusConfig.getBusname());
+ dBusConnection.requestBusName(busname);
} catch (DBusException e) {
throw new UnexpectedErrorException("Dbus command failed, maybe signal-cli dbus daemon is already running: "
+ e.getMessage(), e);
}
- logger.info("Started DBus server on {} bus: {}", busType, DbusConfig.getBusname());
+ logger.info("Started DBus server on {} bus: {}", busType, busname);
}
@Override
void run(DBusConnection connection) throws DBusException;
}
+
+ private static final class DisconnectCallback implements IDisconnectCallback {
+
+ @Override
+ public void disconnectOnError(IOException ex) {
+ logger.debug("DBus daemon disconnected unexpectedly, shutting down");
+ Shutdown.triggerShutdown(new IOErrorException("Unexpected dbus daemon disconnect", ex));
+ }
+ }
}