Implement unregister command for jsonrpc and dbus daemon

This commit is contained in:
AsamK 2021-11-12 16:07:35 +01:00
parent b78573021d
commit c73c58723c
11 changed files with 98 additions and 9 deletions

View file

@ -171,6 +171,10 @@ public interface Signal extends DBusInterface {
void submitRateLimitChallenge(String challenge, String captchaString) throws Error.Failure;
void unregister() throws Error.Failure;
void deleteAccount() throws Error.Failure;
class MessageReceivedV2 extends DBusSignal {
private final long timestamp;

View file

@ -100,7 +100,7 @@ public class Main {
}
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST);
// java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST);
}
private static int getStatusForError(final CommandException e) {

View file

@ -132,6 +132,12 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
runDbusSingleAccount(m, false, receiveMode != ReceiveMode.ON_START);
}
m.addClosedListener(() -> {
synchronized (this) {
notifyAll();
}
});
synchronized (this) {
try {
wait();
@ -230,7 +236,6 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
}
private void runSocket(final ServerSocketChannel serverChannel, Consumer<SocketChannel> socketHandler) {
final var mainThread = Thread.currentThread();
new Thread(() -> {
while (true) {
final SocketChannel channel;
@ -241,7 +246,9 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
logger.info("Accepted new client: " + clientString);
} catch (IOException e) {
logger.error("Failed to accept new socket connection", e);
mainThread.notifyAll();
synchronized (this) {
notifyAll();
}
break;
}
new Thread(() -> {
@ -292,6 +299,17 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
}
}
});
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.getAccountNumbers()
.stream()

View file

@ -11,7 +11,7 @@ import org.asamk.signal.manager.Manager;
import java.io.IOException;
public class UnregisterCommand implements LocalCommand {
public class UnregisterCommand implements JsonRpcLocalCommand {
@Override
public String getName() {

View file

@ -68,6 +68,7 @@ public class DbusManagerImpl implements Manager {
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
private final List<Runnable> closedListeners = new ArrayList<>();
private DBusSigHandler<Signal.MessageReceivedV2> dbusMsgHandler;
private DBusSigHandler<Signal.ReceiptReceivedV2> dbusRcptHandler;
private DBusSigHandler<Signal.SyncMessageReceivedV2> dbusSyncHandler;
@ -583,6 +584,13 @@ public class DbusManagerImpl implements Manager {
throw new UnsupportedOperationException();
}
@Override
public void addClosedListener(final Runnable listener) {
synchronized (closedListeners) {
closedListeners.add(listener);
}
}
@Override
public void close() throws IOException {
synchronized (this) {
@ -595,6 +603,10 @@ public class DbusManagerImpl implements Manager {
weakHandlers.clear();
messageHandlers.clear();
}
synchronized (closedListeners) {
closedListeners.forEach(Runnable::run);
closedListeners.clear();
}
}
private SendMessageResults handleMessage(

View file

@ -138,6 +138,24 @@ public class DbusSignalImpl implements Signal {
}
@Override
public void unregister() throws Error.Failure {
try {
m.unregister();
} catch (IOException e) {
throw new Error.Failure("Failed to unregister: " + e.getMessage());
}
}
@Override
public void deleteAccount() throws Error.Failure {
try {
m.deleteAccount();
} catch (IOException e) {
throw new Error.Failure("Failed to delete account: " + e.getMessage());
}
}
@Override
public void addDevice(String uri) {
try {

View file

@ -64,6 +64,8 @@ public class SignalJsonRpcDispatcherHandler {
if (!noReceiveOnStart) {
c.getAccountNumbers().stream().map(c::getManager).filter(Objects::nonNull).forEach(this::subscribeReceive);
c.addOnManagerAddedHandler(this::subscribeReceive);
c.addOnManagerRemovedHandler(this::unsubscribeReceive);
}
handleConnection();
@ -76,6 +78,9 @@ public class SignalJsonRpcDispatcherHandler {
subscribeReceive(m);
}
final var currentThread = Thread.currentThread();
m.addClosedListener(currentThread::interrupt);
handleConnection();
}