Add dbus SignalControl interface to register/verify/link accounts

This commit is contained in:
AsamK 2021-04-03 19:13:12 +02:00
parent 48b2c7ff8a
commit 8f781c019f
9 changed files with 272 additions and 22 deletions

View file

@ -13,6 +13,7 @@ import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.dbus.DbusSignalControlImpl;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager;
import org.freedesktop.dbus.connections.impl.DBusConnection;
@ -21,7 +22,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -78,7 +78,9 @@ public class DaemonCommand implements MultiLocalCommand {
}
@Override
public void handleCommand(final Namespace ns, final List<Manager> managers) throws CommandException {
public void handleCommand(
final Namespace ns, final List<Manager> managers, SignalCreator c
) throws CommandException {
boolean ignoreAttachments = ns.getBoolean("ignore-attachments");
DBusConnection.DBusBusType busType;
@ -89,21 +91,24 @@ public class DaemonCommand implements MultiLocalCommand {
}
try (var conn = DBusConnection.getConnection(busType)) {
var receiveThreads = new ArrayList<Thread>();
final var signalControl = new DbusSignalControlImpl(c, m -> {
try {
final var objectPath = DbusConfig.getObjectPath(m.getUsername());
return run(conn, objectPath, m, ignoreAttachments);
} catch (DBusException e) {
logger.error("Failed to export object", e);
return null;
}
}, DbusConfig.getObjectPath());
conn.exportObject(signalControl);
for (var m : managers) {
var objectPath = DbusConfig.getObjectPath(m.getUsername());
var thread = run(conn, objectPath, m, ignoreAttachments);
receiveThreads.add(thread);
signalControl.addManager(m);
}
conn.requestBusName(DbusConfig.getBusname());
for (var t : receiveThreads) {
try {
t.join();
} catch (InterruptedException ignored) {
}
}
signalControl.run();
} catch (DBusException | IOException e) {
logger.error("Dbus command failed", e);
throw new UnexpectedErrorException("Dbus command failed");
@ -113,7 +118,9 @@ public class DaemonCommand implements MultiLocalCommand {
private Thread run(
DBusConnection conn, String objectPath, Manager m, boolean ignoreAttachments
) throws DBusException {
conn.exportObject(objectPath, new DbusSignalImpl(m));
conn.exportObject(new DbusSignalImpl(m, objectPath));
logger.info("Exported dbus object: " + objectPath);
final var thread = new Thread(() -> {
while (true) {
@ -128,8 +135,6 @@ public class DaemonCommand implements MultiLocalCommand {
}
});
logger.info("Exported dbus object: " + objectPath);
thread.start();
return thread;