Implement socket/tcp for daemon command

This commit is contained in:
AsamK 2021-11-10 10:30:57 +01:00
parent 7706a02e1b
commit 81a11dc977
19 changed files with 785 additions and 240 deletions

View file

@ -55,6 +55,7 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* This class implements the Manager interface using the DBus Signal interface, where possible.
@ -65,6 +66,7 @@ public class DbusManagerImpl implements Manager {
private final Signal signal;
private final DBusConnection connection;
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
private DBusSigHandler<Signal.MessageReceivedV2> dbusMsgHandler;
private DBusSigHandler<Signal.ReceiptReceivedV2> dbusRcptHandler;
@ -424,18 +426,23 @@ public class DbusManagerImpl implements Manager {
}
@Override
public void addReceiveHandler(final ReceiveMessageHandler handler) {
public void addReceiveHandler(final ReceiveMessageHandler handler, final boolean isWeakListener) {
synchronized (messageHandlers) {
if (messageHandlers.size() == 0) {
installMessageHandlers();
if (isWeakListener) {
weakHandlers.add(handler);
} else {
if (messageHandlers.size() == 0) {
installMessageHandlers();
}
messageHandlers.add(handler);
}
messageHandlers.add(handler);
}
}
@Override
public void removeReceiveHandler(final ReceiveMessageHandler handler) {
synchronized (messageHandlers) {
weakHandlers.remove(handler);
messageHandlers.remove(handler);
if (messageHandlers.size() == 0) {
uninstallMessageHandlers();
@ -582,8 +589,11 @@ public class DbusManagerImpl implements Manager {
this.notify();
}
synchronized (messageHandlers) {
if (messageHandlers.size() > 0) {
uninstallMessageHandlers();
}
weakHandlers.clear();
messageHandlers.clear();
uninstallMessageHandlers();
}
}
@ -664,11 +674,7 @@ public class DbusManagerImpl implements Manager {
List.of())),
Optional.empty(),
Optional.empty());
synchronized (messageHandlers) {
for (final var messageHandler : messageHandlers) {
messageHandler.handleMessage(envelope, null);
}
}
notifyMessageHandlers(envelope);
};
connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
@ -693,11 +699,7 @@ public class DbusManagerImpl implements Manager {
Optional.empty(),
Optional.empty(),
Optional.empty());
synchronized (messageHandlers) {
for (final var messageHandler : messageHandlers) {
messageHandler.handleMessage(envelope, null);
}
}
notifyMessageHandlers(envelope);
};
connection.addSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
@ -747,20 +749,26 @@ public class DbusManagerImpl implements Manager {
Optional.empty(),
Optional.empty())),
Optional.empty());
synchronized (messageHandlers) {
for (final var messageHandler : messageHandlers) {
messageHandler.handleMessage(envelope, null);
}
}
notifyMessageHandlers(envelope);
};
connection.addSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
} catch (DBusException e) {
e.printStackTrace();
}
signal.subscribeReceive();
}
private void notifyMessageHandlers(final MessageEnvelope envelope) {
synchronized (messageHandlers) {
Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> {
h.handleMessage(envelope, null);
});
}
}
private void uninstallMessageHandlers() {
try {
signal.unsubscribeReceive();
connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);

View file

@ -10,74 +10,26 @@ import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.PinLockedException;
import org.freedesktop.dbus.DBusPath;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DbusSignalControlImpl implements org.asamk.SignalControl {
private final SignalCreator c;
private final Function<Manager, Thread> newManagerRunner;
private final List<Pair<Manager, Thread>> receiveThreads = new ArrayList<>();
private final Object stopTrigger = new Object();
private final String objectPath;
public DbusSignalControlImpl(
final SignalCreator c, final Function<Manager, Thread> newManagerRunner, final String objectPath
) {
public DbusSignalControlImpl(final SignalCreator c, final String objectPath) {
this.c = c;
this.newManagerRunner = newManagerRunner;
this.objectPath = objectPath;
}
public void addManager(Manager m) {
var thread = newManagerRunner.apply(m);
if (thread == null) {
return;
}
synchronized (receiveThreads) {
receiveThreads.add(new Pair<>(m, thread));
}
}
public void run() {
synchronized (stopTrigger) {
try {
stopTrigger.wait();
} catch (InterruptedException ignored) {
}
}
synchronized (receiveThreads) {
for (var t : receiveThreads) {
t.second().interrupt();
}
}
while (true) {
final Thread thread;
synchronized (receiveThreads) {
if (receiveThreads.size() == 0) {
break;
}
var pair = receiveThreads.remove(0);
thread = pair.second();
}
try {
thread.join();
} catch (InterruptedException ignored) {
}
}
}
@Override
public boolean isRemote() {
return false;
@ -124,7 +76,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
) throws Error.Failure, Error.InvalidNumber {
try (final RegistrationManager registrationManager = c.getNewRegistrationManager(number)) {
final Manager manager = registrationManager.verifyAccount(verificationCode, pin);
addManager(manager);
c.addManager(manager);
} catch (IOException | PinLockedException | IncorrectPinException e) {
throw new SignalControl.Error.Failure(e.getClass().getSimpleName() + " " + e.getMessage());
}
@ -138,7 +90,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
new Thread(() -> {
try {
final Manager manager = provisioningManager.finishDeviceLink(newDeviceName);
addManager(manager);
c.addManager(manager);
} catch (IOException | TimeoutException | UserAlreadyExists e) {
e.printStackTrace();
}
@ -156,12 +108,9 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
@Override
public List<DBusPath> listAccounts() {
synchronized (receiveThreads) {
return receiveThreads.stream()
.map(Pair::first)
.map(Manager::getSelfNumber)
.map(u -> new DBusPath(DbusConfig.getObjectPath(u)))
.collect(Collectors.toList());
}
return c.getAccountNumbers()
.stream()
.map(u -> new DBusPath(DbusConfig.getObjectPath(u)))
.collect(Collectors.toList());
}
}

View file

@ -2,6 +2,7 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.BaseConfig;
import org.asamk.signal.DbusReceiveMessageHandler;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
@ -60,26 +61,40 @@ public class DbusSignalImpl implements Signal {
private final Manager m;
private final DBusConnection connection;
private final String objectPath;
private final boolean noReceiveOnStart;
private DBusPath thisDevice;
private final List<StructDevice> devices = new ArrayList<>();
private final List<StructGroup> groups = new ArrayList<>();
private DbusReceiveMessageHandler dbusMessageHandler;
private int subscriberCount;
private final static Logger logger = LoggerFactory.getLogger(DbusSignalImpl.class);
public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
public DbusSignalImpl(
final Manager m, DBusConnection connection, final String objectPath, final boolean noReceiveOnStart
) {
this.m = m;
this.connection = connection;
this.objectPath = objectPath;
this.noReceiveOnStart = noReceiveOnStart;
}
public void initObjects() {
if (!noReceiveOnStart) {
subscribeReceive();
}
updateDevices();
updateGroups();
updateConfiguration();
}
public void close() {
if (dbusMessageHandler != null) {
m.removeReceiveHandler(dbusMessageHandler);
dbusMessageHandler = null;
}
unExportDevices();
unExportGroups();
unExportConfiguration();
@ -95,6 +110,24 @@ public class DbusSignalImpl implements Signal {
return m.getSelfNumber();
}
@Override
public void subscribeReceive() {
if (dbusMessageHandler == null) {
dbusMessageHandler = new DbusReceiveMessageHandler(m, connection, objectPath);
m.addReceiveHandler(dbusMessageHandler);
}
subscriberCount++;
}
@Override
public void unsubscribeReceive() {
subscriberCount = Math.max(0, subscriberCount - 1);
if (subscriberCount == 0 && dbusMessageHandler != null) {
m.removeReceiveHandler(dbusMessageHandler);
dbusMessageHandler = null;
}
}
@Override
public void submitRateLimitChallenge(String challenge, String captchaString) {
final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");