Default path

This commit is contained in:
Andrea Cavalli 2022-12-31 05:53:16 +01:00
parent 236bcbf628
commit 917f498034
2 changed files with 44 additions and 5 deletions

View File

@ -97,7 +97,7 @@ public class Example {
@Command(name = "instance", mixinStandardHelpOptions = true, version = "1.0",
description = {"Instance-related commands."},
subcommands = {ConfigureCommand.class, StartCommand.class, CommandLine.HelpCommand.class})
subcommands = {ConfigureCommand.class, StartCommand.class, StopCommand.class, CommandLine.HelpCommand.class})
static class InstanceCommand implements Runnable {
@Option(names = {"-v", "--verbose"},
description = { "Specify multiple -v options to increase verbosity.",
@ -128,7 +128,7 @@ public class Example {
return;
}
currentInstance = name;
instanceMap.put(name, new TDLibInstance(interactionRequests));
instanceMap.put(name, new TDLibInstance(name, interactionRequests));
}
@Command(mixinStandardHelpOptions = true, subcommands = {CommandLine.HelpCommand.class},
@ -172,8 +172,7 @@ public class Example {
var instance = getInstance();
var settings = instance.getSettings();
if (path != null) {
settings.setDatabaseDirectoryPath(path.resolve("data"));
settings.setDownloadedFilesDirectoryPath(path.resolve("downloads"));
instance.setPath(currentInstance);
System.out.printf("Path set to \"%s\".%n", path);
}
if (useMessagesDb != null) {
@ -240,6 +239,31 @@ public class Example {
}
}
@Command(name = "stop", mixinStandardHelpOptions = true, version = "1.0",
description = {"Stop the instance."},
subcommands = {CommandLine.HelpCommand.class})
static class StopCommand implements Runnable {
@ParentCommand InstanceCommand parent;
public void run() {
if (currentInstance == null) {
System.out.println("No instance is active.");
return;
}
var instance = getInstance();
System.out.println("Stopping client...");
try {
instance.stop();
} catch (InterruptedException e) {
throw new RuntimeException("Failed to close", e);
}
System.out.println("Client stopped.");
instanceMap.remove(currentInstance);
currentInstance = null;
}
}
public static void main(String[] args) {
AnsiConsole.systemInstall();
try {

View File

@ -11,6 +11,7 @@ import it.tdlight.jni.TdApi.AuthorizationStateClosed;
import it.tdlight.jni.TdApi.AuthorizationStateClosing;
import it.tdlight.jni.TdApi.AuthorizationStateLoggingOut;
import it.tdlight.jni.TdApi.AuthorizationStateReady;
import java.nio.file.Path;
import java.util.Queue;
public class TDLibInstance {
@ -20,15 +21,22 @@ public class TDLibInstance {
private SimpleTelegramClient client;
private AuthenticationData authenticationData;
public TDLibInstance(Queue<ClientInteractionRequest> interactionRequests) throws CantLoadLibrary {
public TDLibInstance(String name, Queue<ClientInteractionRequest> interactionRequests) throws CantLoadLibrary {
Init.start();
var apiToken = APIToken.example();
settings = TDLibSettings.create(apiToken);
setPath(name);
this.interactionRequests = interactionRequests;
}
public void setPath(String name) {
var path = Path.of(".").resolve(name);
settings.setDatabaseDirectoryPath(path.resolve("data"));
settings.setDownloadedFilesDirectoryPath(path.resolve("downloads"));
}
public TDLibSettings getSettings() {
if (client != null) {
throw new UnsupportedOperationException("Client is already started");
@ -46,6 +54,9 @@ public class TDLibInstance {
interactionRequests.offer(new ClientInteractionRequest(TDLibInstance.this, parameter, parameterInfo, result)));
client.addUpdateHandler(TdApi.UpdateAuthorizationState.class, updateAuthorizationState -> {
var state = updateAuthorizationState.authorizationState.getConstructor();
if (state == AuthorizationStateReady.CONSTRUCTOR) {
System.out.println("Logged in");
}
switch (state) {
case AuthorizationStateReady.CONSTRUCTOR,
AuthorizationStateLoggingOut.CONSTRUCTOR,
@ -59,4 +70,8 @@ public class TDLibInstance {
public AuthenticationData getAuthenticationData() {
return authenticationData;
}
public void stop() throws InterruptedException {
client.closeAndWait();
}
}