From 12a3e7a2f485f2d7e0a262bb10ab80c8d4e08541 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 25 Aug 2025 16:31:04 -0400 Subject: [PATCH] feat (ui): Add ConfigServerPicker dialog So we can connect to different config servers without manually editing spark.conf, or changing command line args. This will run before the Login dialog. --- .../spark/Services/ISparkLoginGuiService.cs | 3 +- src/client/spark/Services/SparkAppService.cs | 15 +++- .../spark/Services/SparkConfigGuiLoader.cs | 67 +++++++++++++++ .../spark/Services/SparkLoginGuiService.cs | 6 +- src/client/spark/Services/SparkServices.cs | 11 ++- .../Shell/Login/ConfigServerPickerView.xaml | 81 +++++++++++++++++++ .../Login/ConfigServerPickerView.xaml.cs | 20 +++++ .../Login/ConfigServerPickerViewModel.cs | 26 ++++++ .../Services/SparkConfigLoaderOpts.cs | 5 +- .../spark_core/Services/SparkCoreServices.cs | 9 +++ 10 files changed, 231 insertions(+), 12 deletions(-) create mode 100644 src/client/spark/Services/SparkConfigGuiLoader.cs create mode 100644 src/client/spark/view/Shell/Login/ConfigServerPickerView.xaml create mode 100644 src/client/spark/view/Shell/Login/ConfigServerPickerView.xaml.cs create mode 100644 src/client/spark/view/Shell/Login/ConfigServerPickerViewModel.cs diff --git a/src/client/spark/Services/ISparkLoginGuiService.cs b/src/client/spark/Services/ISparkLoginGuiService.cs index b63e4387e..3debae87b 100644 --- a/src/client/spark/Services/ISparkLoginGuiService.cs +++ b/src/client/spark/Services/ISparkLoginGuiService.cs @@ -1,7 +1,6 @@ #nullable enable namespace Spark.Services; -using bts.api; using bts.api.core; using Spark.Api.Services; @@ -9,7 +8,7 @@ using Spark.Api.Services; using System.Threading.Tasks; public interface ISparkLoginGuiService : ISparkdLoginService { - new ValueTask<(LoginResult Result, AuthTokenImmutable? Token, string? Workspace)> LoginAsync(); + new ValueTask<(LoginResult Result, string? Workspace)> LoginAsync(); async ValueTask ISparkdLoginService.LoginAsync() => (await LoginAsync()).Result; diff --git a/src/client/spark/Services/SparkAppService.cs b/src/client/spark/Services/SparkAppService.cs index f62689bd0..b2edcabfd 100644 --- a/src/client/spark/Services/SparkAppService.cs +++ b/src/client/spark/Services/SparkAppService.cs @@ -10,6 +10,7 @@ using mbts.ui.shell; using Microsoft.Extensions.Hosting; using Spark.Api.MarketData; +using Spark.Api.Services; using Spark.Core; using Spark.Core.Services; @@ -21,7 +22,7 @@ using System.Threading.Tasks; class SparkAppService : IHostedService { readonly ISparkConfigLoader sparkConfigLoader; readonly List streamFactories; - readonly ISparkLoginGuiService loginService; + readonly ISparkdLoginService loginService; readonly Api.IServiceProvider serviceProvider; readonly ServiceContext internalServiceContext; readonly IMarketDataClient marketDataClient; @@ -29,7 +30,7 @@ class SparkAppService : IHostedService { public SparkAppService( ISparkConfigLoader sparkConfigLoader, - ISparkLoginGuiService loginService, + ISparkdLoginService loginService, Api.IServiceProvider serviceProvider, ServiceContext internalServiceContext, IMarketDataClient marketDataClient, @@ -44,7 +45,13 @@ class SparkAppService : IHostedService { } public async Task StartAsync(CancellationToken cancellationToken) { - var (loginResult, token, workspace) = await loginService.LoginAsync(); + LoginResult? loginResult; + string? workspace = null; + if (loginService is ISparkLoginGuiService loginGuiService) { + (loginResult, workspace) = await loginGuiService.LoginAsync(); + } else { + loginResult = await loginService.LoginAsync(); + } var sparkAppThread = new Thread(RunSparkApp) { Name = "SparkApp", }; @@ -62,7 +69,7 @@ class SparkAppService : IHostedService { MarketDataClient = marketDataClient, UserTuple = loginResult.user.assert_value(), Workspace = workspace, - Token = token, + Token = loginResult.Token, }; exitCode = sparkApp.Run(); } catch (Exception ex) { diff --git a/src/client/spark/Services/SparkConfigGuiLoader.cs b/src/client/spark/Services/SparkConfigGuiLoader.cs new file mode 100644 index 000000000..f71a6217d --- /dev/null +++ b/src/client/spark/Services/SparkConfigGuiLoader.cs @@ -0,0 +1,67 @@ +#nullable enable +namespace Spark.Services; + +using bts.collections; + +using mbts.ui.shell; + +using Microsoft.Extensions.Options; + +using Spark.Core.Services; + +using System; +using System.Threading; +using System.Threading.Tasks; + +/// +/// Launches dialog to pick config server. +/// +public class SparkConfigGuiLoader : ISparkConfigLoader { + readonly object syncRoot = new(); + readonly SparkConfigLoaderOpts config; + TaskCompletionSource? configServerTcs; + + public SparkConfigGuiLoader(IOptions config) + : this(config.Value) { } + + public SparkConfigGuiLoader(SparkConfigLoaderOpts config) { + this.config = config; + } + + public string? ConfigServer => config.ConfigServer; + + public async Task InitializeAsync(CancellationToken cancellationToken) { + bool launchGui = false; + lock (syncRoot) { + if (configServerTcs is null) { + launchGui = true; + configServerTcs = new(); + } + } + if (launchGui) { + var loginGuiThread = new Thread(RunConfigGui) { + IsBackground = true, + Name = "Login" + }; + loginGuiThread.SetApartmentState(ApartmentState.STA); + loginGuiThread.Start(); + } + var loader = await configServerTcs.Task; + await loader.InitializeAsync(cancellationToken); + + void RunConfigGui() { + var vm = new ConfigServerPickerViewModel(); + vm.ConfigServers.AddRange(config.ConfigServers); + vm.OkCmd.executed += s => { + config.ConfigServer = s; + configServerTcs.SetResult(new SparkConfigLoader(config)); + }; + var view = new ConfigServerPickerView() { + DataContext = vm + }; + vm.RequestClose += (s, e) => view.Close(); + view.ShowDialog(); + configServerTcs.TrySetException(new Exception("Failed to pick config server")); + } + } +} diff --git a/src/client/spark/Services/SparkLoginGuiService.cs b/src/client/spark/Services/SparkLoginGuiService.cs index 37f688422..6c1e0dce3 100644 --- a/src/client/spark/Services/SparkLoginGuiService.cs +++ b/src/client/spark/Services/SparkLoginGuiService.cs @@ -17,7 +17,7 @@ using System.Windows.Threading; class SparkLoginGuiService : IHostedService, ISparkLoginGuiService { readonly ISparkConfigLoader configLoader; - readonly TaskCompletionSource<(LoginResult Result, AuthTokenImmutable? Token, string? Workspace)> loginTcs = new(); + readonly TaskCompletionSource<(LoginResult Result, string? Workspace)> loginTcs = new(); readonly ServiceContext serviceProvider; public SparkLoginGuiService(ISparkConfigLoader configLoader, ServiceContext serviceProvider) { @@ -25,7 +25,7 @@ class SparkLoginGuiService : IHostedService, ISparkLoginGuiService { this.serviceProvider = serviceProvider; } - public async ValueTask<(LoginResult Result, AuthTokenImmutable? Token, string? Workspace)> LoginAsync() => + public async ValueTask<(LoginResult Result, string? Workspace)> LoginAsync() => await loginTcs.Task; public async Task StartAsync(CancellationToken cancellationToken) { @@ -54,7 +54,7 @@ class SparkLoginGuiService : IHostedService, ISparkLoginGuiService { void OnLoggedIn() { var (userName, userId, token) = model.get_user(); var workspace = model.get_workspace(); - loginTcs.SetResult((new LoginResult() { user = new(userId, userName), Token = token, success = true, }, token, workspace)); + loginTcs.SetResult((new() { user = new(userId, userName), Token = token, success = true, }, workspace)); view.Close(); } } diff --git a/src/client/spark/Services/SparkServices.cs b/src/client/spark/Services/SparkServices.cs index 599bea4de..1c681a389 100644 --- a/src/client/spark/Services/SparkServices.cs +++ b/src/client/spark/Services/SparkServices.cs @@ -22,8 +22,15 @@ public static class SparkServices { .AddHostedService(sp => sp.GetRequiredService()); public static IServiceCollection AddSparkApp(this IServiceCollection services, IConfiguration configurationSection) { - services.AddSpark(configurationSection.GetSection("Spark")) - .AddSparkApi(configurationSection.GetSection("SparkApi")) + var sparkCfgSection = configurationSection.GetSection("Spark"); + var sparkCfg = sparkCfgSection?.Get(); + if (sparkCfg?.ConfigServers?.Count > 0) { + // Launch gui to let the user pick a config server from the listed options. + services.AddSpark(sparkCfgSection); + } else { + services.AddSpark(sparkCfgSection); + } + services.AddSparkApi(configurationSection.GetSection("SparkApi")) .AddSparkLoginGui() .AddMasterWorker(configurationSection.GetSection("MasterWorker")) .AddViewStreamDatabase() diff --git a/src/client/spark/view/Shell/Login/ConfigServerPickerView.xaml b/src/client/spark/view/Shell/Login/ConfigServerPickerView.xaml new file mode 100644 index 000000000..65c3cd87e --- /dev/null +++ b/src/client/spark/view/Shell/Login/ConfigServerPickerView.xaml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + +