From 4397df4628824959c0ceafd16fbe0a6c8d24153e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 2 Aug 2025 11:45:13 -0400 Subject: [PATCH] refactor: Use HostBuilder to launch spark w/ streams --- .gitignore | 1 + .vscode/launch.json | 16 + spark_client.sln | 307 ++++++++++-------- .../Primitives/Misc/WebScrapingStream.cs | 16 +- src/client/spark/Daemons/DiagnosticDaemon.cs | 2 +- src/client/spark/Daemons/MDDaemon.cs | 5 + src/client/spark/Daemons/ShellServices.cs | 62 +++- src/client/spark/Program.cs | 25 +- .../spark/Services/ISparkLoginGuiService.cs | 16 + src/client/spark/Services/SparkAppService.cs | 73 +++++ .../spark/Services/SparkLoginGuiService.cs | 66 ++++ src/client/spark/Services/SparkServices.cs | 38 +++ src/client/spark/ShellBootstrapper.cs | 33 +- src/client/spark/Spark.csproj | 6 + src/client/spark/SparkApp.xaml.cs | 86 ++--- src/client/spark/appsettings.json | 104 ++++++ .../ShellDiagnosticsServiceServer.cs | 5 +- .../Controls/DialogSkirts/DialogSkirt.xaml.cs | 2 +- .../spark/view/Shell/Login/LoginModel.cs | 53 +-- .../spark/view/Shell/Login/LoginView.xaml | 24 +- .../spark/view/Shell/Login/LoginViewModel.cs | 8 +- src/client/spark/view/Shell/ShellModel.cs | 59 +++- .../Services/CachedServiceProvider.cs | 49 +++ .../spark_api/Services/SparkApiServices.cs | 28 +- src/client/spark_api/api/LoginException.cs | 5 + .../spark_core/Services/ISparkConfigLoader.cs | 1 + .../spark_core/Services/SparkConfigLoader.cs | 2 + .../Services/SparkTestConfigLoader.cs | 2 + 28 files changed, 780 insertions(+), 314 deletions(-) create mode 100644 src/client/spark/Services/ISparkLoginGuiService.cs create mode 100644 src/client/spark/Services/SparkAppService.cs create mode 100644 src/client/spark/Services/SparkLoginGuiService.cs create mode 100644 src/client/spark/Services/SparkServices.cs create mode 100644 src/client/spark/appsettings.json create mode 100644 src/client/spark_api/Services/CachedServiceProvider.cs create mode 100644 src/client/spark_api/api/LoginException.cs diff --git a/.gitignore b/.gitignore index 5b9bfee57..0811d7e7d 100644 --- a/.gitignore +++ b/.gitignore @@ -55,6 +55,7 @@ trace_data/ .cache/ /src/Spark.Admin.WebApp/tsconfig.*.g.json /src/Spark.Admin.WebApp/src/bin/ +/src/client/**/*appsettings.Local.json **/CMakeDeps.cmake **/CMakeDeps_win.cmake .devcontainer/ diff --git a/.vscode/launch.json b/.vscode/launch.json index 125b1a7fa..46b55ee00 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -236,6 +236,9 @@ "preLaunchTask": "Build Spark (Debug)", "program": "Spark.exe", "cwd": "build/Debug/bin/net8.0-windows", + "env": { + "DOTNET_ENVIRONMENT": "${input:sparkEnvironment}", + } }, "stopAtEntry": false, "console": "integratedTerminal" @@ -248,6 +251,9 @@ "windows": { "program": "Spark.exe", "cwd": "build/Debug/bin/net8.0-windows", + "env": { + "DOTNET_ENVIRONMENT": "${input:sparkEnvironment}", + } }, "stopAtEntry": false, "console": "integratedTerminal" @@ -406,6 +412,16 @@ ], "default": "Development", }, + { + "id": "sparkEnvironment", + "type": "pickString", + "description": "Spark Configuration", + "options": [ + "Development", + "Local", + ], + "default": "Development", + }, { "id": "sparkConfigServer", "type": "promptString", diff --git a/spark_client.sln b/spark_client.sln index e2b1c7687..3bc43c95e 100644 --- a/spark_client.sln +++ b/spark_client.sln @@ -1,143 +1,164 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32922.545 -MinimumVisualStudioVersion = 15.0.26124.0 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api", "src\client\spark_api\spark_api.csproj", "{DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spark", "src\client\spark\Spark.csproj", "{9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spark.Console", "src\client\Spark.Console\Spark.Console.csproj", "{387566C6-C6F6-412C-A30C-9855BBCB4816}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_core", "src\client\spark_core\spark_core.csproj", "{6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_sdk", "src\client\spark_sdk\spark_sdk.csproj", "{6963C314-83C4-4667-B0D0-C2F7AE208C29}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_test", "src\client\spark_test\spark_test.csproj", "{ECD78CC5-138F-4809-A764-2358C2AC2E82}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_websocket", "src\client\spark_websocket\spark_websocket.csproj", "{3201359D-C071-4089-9EDE-B04D759F1508}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api_server", "src\client\spark_api_server\spark_api_server.csproj", "{2B8C5D01-FF78-4BFA-B332-5E430586DAF9}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStreamCore", "src\DataStreamSuite\DataStreamCore\DataStreamCore.csproj", "{4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStream", "src\DataStreamSuite\DataStream\DataStream.csproj", "{2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedStreams", "src\DataStreamSuite\SharedStreams\SharedStreams.csproj", "{4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSub", "src\DataStreamSuite\PubSub\PubSub.csproj", "{51C40BA9-F226-4A35-939D-1ED6F5FF7299}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - MinSizeRel|x64 = MinSizeRel|x64 - Release|x64 = Release|x64 - RelWithDebInfo|x64 = RelWithDebInfo|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Debug|x64.ActiveCfg = Debug|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Debug|x64.Build.0 = Debug|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.MinSizeRel|x64.ActiveCfg = Release|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.MinSizeRel|x64.Build.0 = Release|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Release|x64.ActiveCfg = Release|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Release|x64.Build.0 = Release|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x64.Build.0 = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.ActiveCfg = Debug|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.Build.0 = Debug|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.MinSizeRel|x64.ActiveCfg = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.MinSizeRel|x64.Build.0 = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Release|x64.ActiveCfg = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Release|x64.Build.0 = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.RelWithDebInfo|x64.Build.0 = Release|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.Debug|x64.ActiveCfg = Debug|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.Debug|x64.Build.0 = Debug|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.MinSizeRel|x64.ActiveCfg = Debug|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.MinSizeRel|x64.Build.0 = Debug|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.Release|x64.ActiveCfg = Release|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.Release|x64.Build.0 = Release|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {387566C6-C6F6-412C-A30C-9855BBCB4816}.RelWithDebInfo|x64.Build.0 = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Debug|x64.ActiveCfg = Debug|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Debug|x64.Build.0 = Debug|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.MinSizeRel|x64.ActiveCfg = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.MinSizeRel|x64.Build.0 = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Release|x64.ActiveCfg = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Release|x64.Build.0 = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x64.Build.0 = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.ActiveCfg = Debug|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.Build.0 = Debug|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.MinSizeRel|x64.ActiveCfg = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.MinSizeRel|x64.Build.0 = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Release|x64.ActiveCfg = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Release|x64.Build.0 = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {6963C314-83C4-4667-B0D0-C2F7AE208C29}.RelWithDebInfo|x64.Build.0 = Release|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Debug|x64.ActiveCfg = Debug|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Debug|x64.Build.0 = Debug|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.MinSizeRel|x64.ActiveCfg = Debug|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.MinSizeRel|x64.Build.0 = Debug|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Release|x64.ActiveCfg = Release|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Release|x64.Build.0 = Release|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x64.Build.0 = Release|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.Debug|x64.ActiveCfg = Debug|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.Debug|x64.Build.0 = Debug|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.MinSizeRel|x64.ActiveCfg = Debug|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.MinSizeRel|x64.Build.0 = Debug|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.Release|x64.ActiveCfg = Release|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.Release|x64.Build.0 = Release|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {3201359D-C071-4089-9EDE-B04D759F1508}.RelWithDebInfo|x64.Build.0 = Release|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Debug|x64.ActiveCfg = Debug|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Debug|x64.Build.0 = Debug|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.MinSizeRel|x64.ActiveCfg = Debug|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.MinSizeRel|x64.Build.0 = Debug|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Release|x64.ActiveCfg = Release|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Release|x64.Build.0 = Release|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.RelWithDebInfo|x64.Build.0 = Release|x64 - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Debug|x64.ActiveCfg = Debug|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Debug|x64.Build.0 = Debug|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.MinSizeRel|x64.Build.0 = Debug|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Release|x64.ActiveCfg = Release|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Release|x64.Build.0 = Release|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU - {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Debug|x64.ActiveCfg = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Debug|x64.Build.0 = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.MinSizeRel|x64.Build.0 = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Release|x64.ActiveCfg = Release|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Release|x64.Build.0 = Release|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU - {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Debug|x64.ActiveCfg = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Debug|x64.Build.0 = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.MinSizeRel|x64.Build.0 = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Release|x64.ActiveCfg = Release|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Release|x64.Build.0 = Release|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU - {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Debug|x64.ActiveCfg = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Debug|x64.Build.0 = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.MinSizeRel|x64.Build.0 = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Release|x64.ActiveCfg = Release|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Release|x64.Build.0 = Release|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU - {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {422D1620-0166-4C26-9A81-CA8A2EB1AB58} - EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection -EndGlobal +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32922.545 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api", "src\client\spark_api\spark_api.csproj", "{DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spark", "src\client\spark\Spark.csproj", "{9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spark.Console", "src\client\Spark.Console\Spark.Console.csproj", "{387566C6-C6F6-412C-A30C-9855BBCB4816}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_core", "src\client\spark_core\spark_core.csproj", "{6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_sdk", "src\client\spark_sdk\spark_sdk.csproj", "{6963C314-83C4-4667-B0D0-C2F7AE208C29}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_test", "src\client\spark_test\spark_test.csproj", "{ECD78CC5-138F-4809-A764-2358C2AC2E82}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_websocket", "src\client\spark_websocket\spark_websocket.csproj", "{3201359D-C071-4089-9EDE-B04D759F1508}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api_server", "src\client\spark_api_server\spark_api_server.csproj", "{2B8C5D01-FF78-4BFA-B332-5E430586DAF9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStreamCore", "src\DataStreamSuite\DataStreamCore\DataStreamCore.csproj", "{4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStream", "src\DataStreamSuite\DataStream\DataStream.csproj", "{2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedStreams", "src\DataStreamSuite\SharedStreams\SharedStreams.csproj", "{4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSub", "src\DataStreamSuite\PubSub\PubSub.csproj", "{51C40BA9-F226-4A35-939D-1ED6F5FF7299}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4564E677-343A-46CB-AF02-AFC4E38BF94B}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "client", "client", "{56A8A70A-DA03-4CED-A144-EC10F1379FD8}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataStreamSuite", "DataStreamSuite", "{915EF238-CA0D-4E73-AF50-88CB3F29C7AD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bts.Extensions.Hosting", "src\DataStreamSuite\Bts.Extensions.Hosting\Bts.Extensions.Hosting.csproj", "{4E0E51AA-7B59-4808-B7CC-9EAEDF397460}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + MinSizeRel|x64 = MinSizeRel|x64 + Release|x64 = Release|x64 + RelWithDebInfo|x64 = RelWithDebInfo|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Debug|x64.ActiveCfg = Debug|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Debug|x64.Build.0 = Debug|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.MinSizeRel|x64.ActiveCfg = Release|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.MinSizeRel|x64.Build.0 = Release|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Release|x64.ActiveCfg = Release|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.Release|x64.Build.0 = Release|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}.RelWithDebInfo|x64.Build.0 = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.ActiveCfg = Debug|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Debug|x64.Build.0 = Debug|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.MinSizeRel|x64.ActiveCfg = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.MinSizeRel|x64.Build.0 = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Release|x64.ActiveCfg = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.Release|x64.Build.0 = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {9A4D4D07-1AB6-4B2B-B111-A4F34D6C267E}.RelWithDebInfo|x64.Build.0 = Release|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.Debug|x64.ActiveCfg = Debug|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.Debug|x64.Build.0 = Debug|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.MinSizeRel|x64.ActiveCfg = Debug|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.MinSizeRel|x64.Build.0 = Debug|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.Release|x64.ActiveCfg = Release|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.Release|x64.Build.0 = Release|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {387566C6-C6F6-412C-A30C-9855BBCB4816}.RelWithDebInfo|x64.Build.0 = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Debug|x64.ActiveCfg = Debug|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Debug|x64.Build.0 = Debug|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.MinSizeRel|x64.ActiveCfg = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.MinSizeRel|x64.Build.0 = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Release|x64.ActiveCfg = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.Release|x64.Build.0 = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {6E449EB6-20AC-4FA6-B8D9-10F4BB7E1FEE}.RelWithDebInfo|x64.Build.0 = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.ActiveCfg = Debug|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Debug|x64.Build.0 = Debug|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.MinSizeRel|x64.ActiveCfg = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.MinSizeRel|x64.Build.0 = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Release|x64.ActiveCfg = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.Release|x64.Build.0 = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {6963C314-83C4-4667-B0D0-C2F7AE208C29}.RelWithDebInfo|x64.Build.0 = Release|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Debug|x64.ActiveCfg = Debug|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Debug|x64.Build.0 = Debug|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.MinSizeRel|x64.ActiveCfg = Debug|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.MinSizeRel|x64.Build.0 = Debug|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Release|x64.ActiveCfg = Release|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.Release|x64.Build.0 = Release|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {ECD78CC5-138F-4809-A764-2358C2AC2E82}.RelWithDebInfo|x64.Build.0 = Release|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.Debug|x64.ActiveCfg = Debug|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.Debug|x64.Build.0 = Debug|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.MinSizeRel|x64.ActiveCfg = Debug|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.MinSizeRel|x64.Build.0 = Debug|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.Release|x64.ActiveCfg = Release|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.Release|x64.Build.0 = Release|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {3201359D-C071-4089-9EDE-B04D759F1508}.RelWithDebInfo|x64.Build.0 = Release|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Debug|x64.ActiveCfg = Debug|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Debug|x64.Build.0 = Debug|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.MinSizeRel|x64.ActiveCfg = Debug|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.MinSizeRel|x64.Build.0 = Debug|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Release|x64.ActiveCfg = Release|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.Release|x64.Build.0 = Release|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.RelWithDebInfo|x64.ActiveCfg = Release|x64 + {2B8C5D01-FF78-4BFA-B332-5E430586DAF9}.RelWithDebInfo|x64.Build.0 = Release|x64 + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Debug|x64.ActiveCfg = Debug|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Debug|x64.Build.0 = Debug|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Release|x64.ActiveCfg = Release|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.Release|x64.Build.0 = Release|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {4E4F491F-C688-43C4-88DA-AC92BC5FA1B8}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Debug|x64.ActiveCfg = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Debug|x64.Build.0 = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Release|x64.ActiveCfg = Release|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.Release|x64.Build.0 = Release|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {2AC54ADC-3246-427E-9CA9-FEA2DBCB9432}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Debug|x64.ActiveCfg = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Debug|x64.Build.0 = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Release|x64.ActiveCfg = Release|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.Release|x64.Build.0 = Release|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {4E96E5CC-7791-40B3-A8A1-C34FBB736B3B}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Debug|x64.ActiveCfg = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Debug|x64.Build.0 = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Release|x64.ActiveCfg = Release|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.Release|x64.Build.0 = Release|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {51C40BA9-F226-4A35-939D-1ED6F5FF7299}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.Debug|x64.ActiveCfg = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.Debug|x64.Build.0 = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.Release|x64.ActiveCfg = Release|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.Release|x64.Build.0 = Release|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {422D1620-0166-4C26-9A81-CA8A2EB1AB58} + EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {56A8A70A-DA03-4CED-A144-EC10F1379FD8} = {4564E677-343A-46CB-AF02-AFC4E38BF94B} + {915EF238-CA0D-4E73-AF50-88CB3F29C7AD} = {4564E677-343A-46CB-AF02-AFC4E38BF94B} + {4E0E51AA-7B59-4808-B7CC-9EAEDF397460} = {915EF238-CA0D-4E73-AF50-88CB3F29C7AD} + EndGlobalSection +EndGlobal diff --git a/src/DataStreamSuite/DataStream/Primitives/Misc/WebScrapingStream.cs b/src/DataStreamSuite/DataStream/Primitives/Misc/WebScrapingStream.cs index 9f70da850..d201005fa 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Misc/WebScrapingStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Misc/WebScrapingStream.cs @@ -10,15 +10,15 @@ using HtmlAgilityPack; namespace BCG { class Program { - static void Main() { - string url = "http://quotes.toscrape.com"; - List quotes = ScrapeQuotes(url); + // static void Main() { + // string url = "http://quotes.toscrape.com"; + // List quotes = ScrapeQuotes(url); - Console.WriteLine("Quotes from http://quotes.toscrape.com:"); - foreach (var quote in quotes) { - Console.WriteLine(quote); - } - } + // Console.WriteLine("Quotes from http://quotes.toscrape.com:"); + // foreach (var quote in quotes) { + // Console.WriteLine(quote); + // } + // } static List ScrapeQuotes(string url) { List quotes = new List(); diff --git a/src/client/spark/Daemons/DiagnosticDaemon.cs b/src/client/spark/Daemons/DiagnosticDaemon.cs index 530eea964..32fb8254f 100644 --- a/src/client/spark/Daemons/DiagnosticDaemon.cs +++ b/src/client/spark/Daemons/DiagnosticDaemon.cs @@ -11,7 +11,7 @@ namespace bts.spark { readonly ShellDiagnosticsServiceServer _service; [ImportingConstructor] - public DiagnosticDaemon(ServiceContext context, CompositionContext container) { + public DiagnosticDaemon([Import("internal")]ServiceContext context, CompositionContext container) { _service = new ShellDiagnosticsServiceServer(container, context); } } diff --git a/src/client/spark/Daemons/MDDaemon.cs b/src/client/spark/Daemons/MDDaemon.cs index 169f70f51..5e96b8839 100644 --- a/src/client/spark/Daemons/MDDaemon.cs +++ b/src/client/spark/Daemons/MDDaemon.cs @@ -38,6 +38,7 @@ namespace bts { readonly TimeSpan _relay_throttle; readonly TimeSpan _output_throttle; readonly List _resources = new(2); + readonly Lazy client; [ImportingConstructor] public MDDaemon( @@ -72,6 +73,7 @@ namespace bts { _output_throttle = output_throttle; _security_metadata = security_metadata; _md_source_backing = new(() => new(UserDataPermissions.Create(_user.Value), _relay_throttle, _output_throttle)); + client = new(() => new(_md_source)); } public int priority => 55; @@ -79,6 +81,9 @@ namespace bts { [Export] public MDConsumer consumer => _md_source.consumer; + [Export] + public Lazy Client => client; + MDSource _md_source => _md_source_backing.Value; IBus _bus => _bus_backing.Value; diff --git a/src/client/spark/Daemons/ShellServices.cs b/src/client/spark/Daemons/ShellServices.cs index 61763c6f8..2231b1fa3 100644 --- a/src/client/spark/Daemons/ShellServices.cs +++ b/src/client/spark/Daemons/ShellServices.cs @@ -12,55 +12,81 @@ using bts.md; using bts.metrics; using bts.support; -using Spark.Core.Services; - using System.Composition; namespace Spark { + [Export] class ShellServices { - public ShellServices([Import("config_source")]string config_source) => - context = new(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = config_source })); + PersistenceService genericSettingsService; + PersistenceService settingsService; + ErrorReportService errorReportService; + OrderService orderService; + AdminService adminService; + MarketDataRelay marketDataRelay; + PositionService positionService; + AnalyticsService analyticsService; + SyntheticSecurityService syntheticSecurityService; + AlgoDefinitionService algoDefinitionService; + MarketDataAuxiliaryService marketDataAuxiliaryService; + ShellLatencyService shellLatencyService; + MBOService mboService; + + [Export("internal")] + public ServiceContext InternalServiceContext { get; set; } [Export] - public ServiceContext context { get; } + public Api.IServiceProvider ServiceProvider { get; set; } [Export("settings")] - public PersistenceService settings_service => context.locate("settings"); + public PersistenceService SettingsService => + settingsService ??= ServiceProvider.GetService("settings"); [Export] - public PersistenceService generic_settings => context.locate(); + public PersistenceService GenericSettingsService => + genericSettingsService ??= ServiceProvider.GetService(); [Export] - public ErrorReportService error_service => context.locate(); + public ErrorReportService ErrorReportService => + errorReportService ??= ServiceProvider.GetService(); [Export] - public OrderService order_service => context.locate(); + public OrderService OrderService => + orderService ??= ServiceProvider.GetService(); [Export] - public AdminService admin_service => context.locate(); + public AdminService AdminService => + adminService ??= ServiceProvider.GetService(); [Export] - public SyntheticSecurityService synthetic_security_service => context.locate(); + public SyntheticSecurityService SyntheticSecurityService => + syntheticSecurityService ??= ServiceProvider.GetService(); [Export] - public ShellLatencyService shell_latency_service => context.locate(); + public ShellLatencyService ShellLatencyService => + shellLatencyService ??= ServiceProvider.GetService(); [Export] - public PositionService position_service => context.locate(); + public PositionService PositionService => + positionService ??= ServiceProvider.GetService(); [Export] - public MarketDataAuxiliaryService md_auxiliary_service => context.locate(); + public MarketDataAuxiliaryService MarketDataAuxiliaryService => + marketDataAuxiliaryService ??= ServiceProvider.GetService(); [Export] - public MarketDataRelay md_replay => context.locate(); + public MarketDataRelay MarketDataRelay => + marketDataRelay ??= ServiceProvider.GetService(); [Export] - public AlgoDefinitionService algo_definition_service => context.locate(); + public AlgoDefinitionService AlgoDefinitionService => + algoDefinitionService ??= ServiceProvider.GetService(); [Export] - public MBOService mbo_service => context.locate(); + public MBOService MboService => + mboService ??= ServiceProvider.GetService(); [Export] - public AnalyticsService analytics_service => context.locate(); + public AnalyticsService AnalyticsService => + analyticsService ??= ServiceProvider.GetService(); } } diff --git a/src/client/spark/Program.cs b/src/client/spark/Program.cs index 241fda561..c3e11a737 100644 --- a/src/client/spark/Program.cs +++ b/src/client/spark/Program.cs @@ -1,14 +1,21 @@ -namespace Spark; +using Bts.Extensions.Hosting; -using mbts.ui.shell; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +using Spark.Services; using System; -static class Program { - [STAThread] - public static int Main(string[] args) { - var app = new SparkApp(); - app.InitializeComponent(); - return app.Run(); - } +var host = HostBuilderExtensions.CreateBtsBuilder(args) + .ConfigureServices(ConfigureServices) + .Build(); +try { + host.Run(); +} catch (Exception ex) { + Console.Error.WriteLine($"Host.Run threw an exception: {ex}"); + Environment.Exit(1); } + +static void ConfigureServices(HostBuilderContext context, IServiceCollection services) => + services.AddSparkApp(context.Configuration); diff --git a/src/client/spark/Services/ISparkLoginGuiService.cs b/src/client/spark/Services/ISparkLoginGuiService.cs new file mode 100644 index 000000000..b63e4387e --- /dev/null +++ b/src/client/spark/Services/ISparkLoginGuiService.cs @@ -0,0 +1,16 @@ +#nullable enable +namespace Spark.Services; + +using bts.api; +using bts.api.core; + +using Spark.Api.Services; + +using System.Threading.Tasks; + +public interface ISparkLoginGuiService : ISparkdLoginService { + new ValueTask<(LoginResult Result, AuthTokenImmutable? Token, 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 new file mode 100644 index 000000000..f60b8bf87 --- /dev/null +++ b/src/client/spark/Services/SparkAppService.cs @@ -0,0 +1,73 @@ +#nullable enable +namespace Spark.Services; + +using bts.api.core; + +using Bts.DataStreamCore; + +using mbts.ui.shell; + +using Microsoft.Extensions.Hosting; + +using Spark.Core; +using Spark.Core.Services; + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +class SparkAppService : IHostedService { + readonly ISparkConfigLoader sparkConfigLoader; + readonly List streamFactories; + readonly ISparkLoginGuiService loginService; + readonly Api.IServiceProvider serviceProvider; + readonly ServiceContext internalServiceContext; + SparkApp? sparkApp; + + public SparkAppService( + ISparkConfigLoader sparkConfigLoader, ISparkLoginGuiService loginService, Api.IServiceProvider serviceProvider, ServiceContext internalServiceContext, IEnumerable streamFactories) { + this.sparkConfigLoader = sparkConfigLoader; + this.loginService = loginService; + this.serviceProvider = serviceProvider; + this.internalServiceContext = internalServiceContext; + this.streamFactories = new(streamFactories); + } + + public async Task StartAsync(CancellationToken cancellationToken) { + var (loginResult, token, workspace) = await loginService.LoginAsync(); + var sparkAppThread = new Thread(RunSparkApp) { + Name = "SparkApp", + }; + sparkAppThread.SetApartmentState(ApartmentState.STA); + sparkAppThread.Start(); + + void RunSparkApp() { + int exitCode = 0; + try { + sparkApp = new() { + ConfigLoader = sparkConfigLoader, + StreamFactories = streamFactories, + ServiceProvider = serviceProvider, + InternalServiceContext = internalServiceContext, + UserTuple = loginResult.user.assert_value(), + Workspace = workspace, + Token = token, + }; + exitCode = sparkApp.Run(); + } catch (Exception ex) { + Console.Error.WriteLine($"SparkApp.Run threw an exception: {ex}"); + } + if (exitCode != 0) { + throw new Exception($"SparkApp.Run returned non-zero exit code ({exitCode})"); + } + } + } + + public async Task StopAsync(CancellationToken cancellationToken) { + var disp = sparkApp?.Dispatcher; + if (disp is not null) { + await disp.InvokeAsync(() => sparkApp?.Shutdown()); + } + } +} diff --git a/src/client/spark/Services/SparkLoginGuiService.cs b/src/client/spark/Services/SparkLoginGuiService.cs new file mode 100644 index 000000000..cccc728e0 --- /dev/null +++ b/src/client/spark/Services/SparkLoginGuiService.cs @@ -0,0 +1,66 @@ +#nullable enable +namespace Spark.Services; + +using bts.api; +using bts.api.core; + +using mbts; +using mbts.ui.shell; + +using Microsoft.Extensions.Hosting; + +using Spark.Core.Services; + +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Threading; + +class SparkLoginGuiService : IHostedService, ISparkLoginGuiService { + readonly ISparkConfigLoader configLoader; + readonly TaskCompletionSource<(LoginResult Result, AuthTokenImmutable? Token, string? Workspace)> loginTcs = new(); + readonly ServiceContext serviceProvider; + + public SparkLoginGuiService(ISparkConfigLoader configLoader, ServiceContext serviceProvider) { + this.configLoader = configLoader; + this.serviceProvider = serviceProvider; + } + + public async ValueTask<(LoginResult Result, AuthTokenImmutable? Token, string? Workspace)> LoginAsync() => + await loginTcs.Task; + + public async Task StartAsync(CancellationToken cancellationToken) { + await configLoader.InitializeAsync(CancellationToken.None); + var model = new LoginModel(); + model.provide_services(serviceProvider); + var loginGuiThread = new Thread(RunLoginGui) { + IsBackground = true, + Name = "Login" + }; + loginGuiThread.SetApartmentState(ApartmentState.STA); + loginGuiThread.Start(); + model.await_login(); + + void RunLoginGui() { + var vm = new LoginViewModel(model, Dispatcher.CurrentDispatcher); + var view = new LoginView(vm); + vm.LoggedIn += OnLoggedIn; + model.init().ignore(); + view.ShowDialog(); + // ShowDialog returns when the window is closed. If the login is successful, + // the result will already be set and TrySetCancel will do nothing. If not, + // it will signal the login failed to all services waiting for an auth token. + loginTcs.TrySetException(new LoginException("Login failed")); + + void OnLoggedIn() { + var (userName, userId, token) = model.get_user(); + var workspace = model.get_workspace(); + loginTcs.SetResult((new LoginResult() { user = new(userId, userName), success = true }, token, workspace)); + view.Close(); + } + } + } + + public Task StopAsync(CancellationToken cancellationToken) { + throw new System.NotImplementedException(); + } +} diff --git a/src/client/spark/Services/SparkServices.cs b/src/client/spark/Services/SparkServices.cs new file mode 100644 index 000000000..b9cdddaa0 --- /dev/null +++ b/src/client/spark/Services/SparkServices.cs @@ -0,0 +1,38 @@ +#nullable enable +namespace Spark.Services; + +using Bts.DataStream.Services; +using Bts.DataStreamCore.Services; +using Bts.PubSub.Services; +using Bts.SharedStreams.Services; + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +using Spark.Api.Services; +using Spark.Core.Services; + +using System; + +public static class SparkServices { + public static IServiceCollection AddSparkLoginGui(this IServiceCollection services) => + services.AddSingleton() + .AddSingleton(sp => sp.GetRequiredService()) + .AddSingleton(sp => sp.GetRequiredService()) + .AddHostedService(sp => sp.GetRequiredService()); + + public static IServiceCollection AddSparkApp(this IServiceCollection services, IConfiguration configurationSection) => + services + .AddSpark(configurationSection.GetSection("Spark")) + .AddSparkApi(configurationSection.GetSection("SparkApi")) + .AddSparkLoginGui() + .AddMasterWorker(configurationSection.GetSection("MasterWorker")) + .AddViewStreamDatabase() + .AddPubSubClient(configurationSection.GetSection("PubSub")) + .AddPubTableBroker() + .AddRemoteQueryManager() + .AddPrimitiveStreams(configurationSection.GetSection("PrimitiveStream")) + .AddMarketDataStreams() + .AddGarbageCollector(configurationSection.GetSection("GarbageCollector")) + .AddHostedService(); +} diff --git a/src/client/spark/ShellBootstrapper.cs b/src/client/spark/ShellBootstrapper.cs index bc6bed434..07a1a8752 100644 --- a/src/client/spark/ShellBootstrapper.cs +++ b/src/client/spark/ShellBootstrapper.cs @@ -1,12 +1,18 @@ namespace mbts.ui.shell { using bts; + using bts.api; + using bts.api.core; + + using Bts.DataStreamCore; using mbts.config; using mbts.ui.extensions; using Spark.Core.Disposables; + using Spark.Core.Services; using System; + using System.Collections.Generic; using System.Collections.Specialized; using System.Runtime.InteropServices; using System.Threading; @@ -19,7 +25,6 @@ /// and the collection of open windows) and displaying them. /// class ShellBootstrapper : IDisposable { - readonly string _config_source; ShellModel _model; Window _login; @@ -28,6 +33,14 @@ Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_HandleClipboardError; } + public ISparkConfigLoader ConfigLoader { get; set; } + public IEnumerable StreamFactories { get; set; } + public ServiceContext InternalServiceContext { get; set; } + public Spark.Api.IServiceProvider ServiceProvider { get; set; } + public UserTuple UserTuple { get; set; } + public AuthTokenImmutable? Token { get; set; } + public string Workspace { get; set; } + public static void CurrentDispatcher_HandleClipboardError(object sender, DispatcherUnhandledExceptionEventArgs e) { // Dont' allow clipboard failure to bring down the shell if (e.Exception is COMException com_exeption && com_exeption.ErrorCode == -2147221040) { @@ -43,13 +56,7 @@ } } - public ShellBootstrapper( - string user = null, - string password = null, - string config_source = null, - bool? restore_workspace = null - ) { - _config_source = config_source; + public void LaunchLoginGui(string config_source = null, string user = null, string password = null, bool? restore_workspace = null) { // Get the LoginModel instance on this thread to make sure it performs syncronous config initialization. var login_model = LoginModel.init(config_source); // Show the dialog on a background thread so animations aren't blocked during the main application startup @@ -121,8 +128,14 @@ } public void Run() { - _model = new ShellModel(); - _model.Initialize(_config_source); + _model = new ShellModel() { + UserTuple_Export = new(() => UserTuple), + Token_Export = new(() => Token.Value), + Workspace = Workspace, + ConfigLoader = ConfigLoader, + StreamFactories = StreamFactories, + }; + _model.Initialize(ServiceProvider, InternalServiceContext); if (Configuration.get().SoftwareOnly) { System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; } diff --git a/src/client/spark/Spark.csproj b/src/client/spark/Spark.csproj index 8e98f023e..e9e63bee4 100644 --- a/src/client/spark/Spark.csproj +++ b/src/client/spark/Spark.csproj @@ -53,6 +53,7 @@ + @@ -80,4 +81,9 @@ + + + + + diff --git a/src/client/spark/SparkApp.xaml.cs b/src/client/spark/SparkApp.xaml.cs index 55ebb55a1..dedd93e00 100644 --- a/src/client/spark/SparkApp.xaml.cs +++ b/src/client/spark/SparkApp.xaml.cs @@ -1,35 +1,49 @@ namespace mbts.ui.shell { using bts; - using bts.utils; - using CommandLine; + using Bts.DataStreamCore; using mbts.ui.native; - - using Spark.Cli; + + using Spark.Core.Services; using System; + using System.Collections.Generic; using System.ComponentModel; using System.Globalization; - using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; public partial class SparkApp : Application { - static readonly Mutex _mutex; - + internal static readonly Mutex Mutex; + readonly bool allowMultipleShells; ShellBootstrapper _runtime; - SparkCmdOpts _opts; static SparkApp() { // Don't let the designer take the mutex. - if (!_is_in_designer) { - _mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); + if (!IsInDesigner) { + Mutex = new Mutex(true, "5BA5BA1C-9AAC-4844-B7E5-094691CD5105"); } } - static bool _is_in_designer => + public SparkApp() + : this(false) { } + + public SparkApp(bool allowMultipleShells) { + this.allowMultipleShells = allowMultipleShells; + InitializeComponent(); + } + + public ISparkConfigLoader ConfigLoader { get; set; } + public IEnumerable StreamFactories { get; set; } + public bts.api.core.ServiceContext InternalServiceContext { get; set; } + public Spark.Api.IServiceProvider ServiceProvider { get; set; } + public UserTuple UserTuple { get; set; } + public bts.api.AuthTokenImmutable? Token { get; set; } + public string Workspace { get; set; } + + internal static bool IsInDesigner => #if DEBUG (bool)DependencyPropertyDescriptor.FromProperty( DesignerProperties.IsInDesignModeProperty, @@ -39,32 +53,21 @@ namespace mbts.ui.shell { #endif public new int Run() { - var argv = SparkCmdOpts.conform_opts(Environment.GetCommandLineArgs().Skip(1)); - - int exit_code = 1; - Parser.Default.ParseArguments(argv).WithParsed(opts => { - _opts = opts; - var opt_desc = TableFormatter.fmt_table( - typeof(SparkCmdOpts).GetProperties() - .Select(p => (p.Name, p.GetValue(opts))), field_separator: "\n\t"); - Log.Ui.info($"Running spark_client with options:\n\t{opt_desc}"); - try { - if (_mutex is not null && !_mutex.WaitOne(TimeSpan.Zero, true) && !opts.allow_multiple_shells) { - // Second instance. Activate the main window and return - NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); - Log.Ui.info("Bringing running shell to the foreground..."); - exit_code = 0; - return; - } - exit_code = base.Run(); - } catch (AbandonedMutexException ame) { - Log.Ui.warn($"Shell mutex was abandoned. Releasing the mutex and trying again. \n{ame}"); - _mutex.ReleaseMutex(); - // Keep trying until stack overflow - exit_code = Run(); + try { + if (Mutex is not null && !Mutex.WaitOne(TimeSpan.Zero, true) && !allowMultipleShells) { + // Second instance. Activate the main window and return + NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); + Log.Ui.info("Bringing running shell to the foreground..."); + return 0; + } else { + return base.Run(); } - }); - return exit_code; + } catch (AbandonedMutexException ame) { + Log.Ui.warn($"Shell mutex was abandoned. Releasing the mutex and trying again. \n{ame}"); + Mutex.ReleaseMutex(); + // Keep trying until stack overflow + return Run(); + } } protected override void OnStartup(StartupEventArgs e) { @@ -76,8 +79,15 @@ namespace mbts.ui.shell { CultureInfo.DefaultThreadCurrentUICulture = us_culture; base.OnStartup(e); - _runtime = new ShellBootstrapper( - _opts?.user, _opts?.password, _opts?.sparkd, _opts?.restore_workspace); + _runtime = new ShellBootstrapper { + ConfigLoader = ConfigLoader, + StreamFactories = StreamFactories, + InternalServiceContext = InternalServiceContext, + ServiceProvider = ServiceProvider, + UserTuple = UserTuple, + Workspace = Workspace, + Token = Token, + }; _runtime.Run(); // Select text on tab in diff --git a/src/client/spark/appsettings.json b/src/client/spark/appsettings.json new file mode 100644 index 000000000..2768d0308 --- /dev/null +++ b/src/client/spark/appsettings.json @@ -0,0 +1,104 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "Spark": { + "ConfigServer": "http://127.0.0.1:8000/config" + }, + "SparkApi": { + "Credentials": { + "User": "bts", + "Password": "" + }, + "EnableAdmin": true, + "EnableOrders": true, + "EnablePosition": true + }, + "MasterWorker": { + "Interval": "00:00:00.1" + }, + "ViewStreamDatabase": { + "Interval": "00:00:00.1" + }, + "PrimitiveStream": { + "Assemblies": [ + "DataStream.dll", + "SharedStreams.dll" + ], + "ExcludedTypes": [ + "SettlementPriceStream", + "OHLCVStream" + ] + }, + "PubSub": { + "HeartbeatInterval": 15000, + "MissingHeartbeatsBeforeGc": 50, + "DisposedConnectionCheckPeriod": 1000, + "ServerPort": 22019, + "Routing": { + "PublisherRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://127.0.0.1:22019", + "TagRegExp": ".*" + } + ], + "SubscriberRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://127.0.0.1:22019", + "TagRegExp": ".*" + } + ] + }, + "TagTypeToAssemblyMap": { + "Entries": [ + { + "HandlerClass": "BCG.PubSubServerTagHandler", + "Assembly": "DataStreamCore.dll" + } + ] + }, + "TagHandlerFactory": { + "Handlers": [ + { + "TagType": "DataStream", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerStreamTagHandler" + }, + { + "TagType": "System_Sub", + "Assembly": "PubSub", + "Class": "BCG.SystemSubscriptionMapPublisher" + }, + { + "TagType": "System_Stats", + "Assembly": "PubSub", + "Class": "BCG.SystemStatsPublisher" + }, + { + "TagType": ".*", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerGenericDataHandler" + } + ] + } + }, + "DataStreamPublisher": { + "PublisherNodes": [ + { + "Name": "test_data", + "Streams": [ + { + "Name": "test_literal", + "Expression": "literal(\"x\", \"420.0\")", + "IsOnWebsocket": true + } + ] + } + ] + } +} diff --git a/src/client/spark/core/Diagnostics/ShellDiagnosticsServiceServer.cs b/src/client/spark/core/Diagnostics/ShellDiagnosticsServiceServer.cs index 3a3dbe439..f8ffddad3 100644 --- a/src/client/spark/core/Diagnostics/ShellDiagnosticsServiceServer.cs +++ b/src/client/spark/core/Diagnostics/ShellDiagnosticsServiceServer.cs @@ -22,7 +22,10 @@ namespace bts.api.diagnostic { class ShellDiagnosticsServiceServer : ShellDiagnosticServiceBase { readonly ServiceContext _context; readonly List _hosts = new List(); - public ShellDiagnosticsServiceServer(CompositionContext container, ServiceContext context) : base(context, Environment.MachineName) { + + [ImportingConstructor] + public ShellDiagnosticsServiceServer(CompositionContext container, [Import("internal")]ServiceContext context) + : base(context, Environment.MachineName) { Console.WriteLine($"Started {Environment.MachineName}"); _context = context; this.container = container; diff --git a/src/client/spark/view/Controls/DialogSkirts/DialogSkirt.xaml.cs b/src/client/spark/view/Controls/DialogSkirts/DialogSkirt.xaml.cs index 13b7251f0..7229b37cf 100644 --- a/src/client/spark/view/Controls/DialogSkirts/DialogSkirt.xaml.cs +++ b/src/client/spark/view/Controls/DialogSkirts/DialogSkirt.xaml.cs @@ -22,7 +22,7 @@ namespace mbts.ui.controls { _find_style(typeof(Button)); Style _find_style(object key) => - Application.Current.TryFindResource(key) as Style; + Application.Current?.TryFindResource(key) as Style; } return _dialog_button_style_backing; } diff --git a/src/client/spark/view/Shell/Login/LoginModel.cs b/src/client/spark/view/Shell/Login/LoginModel.cs index 2f065d62d..3024a01cd 100644 --- a/src/client/spark/view/Shell/Login/LoginModel.cs +++ b/src/client/spark/view/Shell/Login/LoginModel.cs @@ -1,4 +1,4 @@ -using bts; +using bts; using bts.api; using bts.api.core; using bts.api.persistence; @@ -15,39 +15,6 @@ using System.Threading.Tasks; using System.Windows.Threading; namespace mbts.ui.shell { - // The LoginContainer participates in the startup Import/Export dependency system - // Since the login form should not depend on *anything* it will be shown before this is created - // Once this is created it access the global LoginModel and retrieves the username - [Export] - public class LoginContainer { - [ImportingConstructor] - public LoginContainer(ServiceContext context) { - var model = LoginModel.instance; - model.provide_services(context); - // await_login pumps the dispatcher but doesn't allow control to return to MEF until the username and workspace are available - model.await_login(); - (UserName, UserId, Token) = model.get_user(); - Workspace = model.get_workspace(); - model.Dispose(); - UserTuple_Export = new Lazy(() => UserTuple); - Token_Export = new Lazy(() => Token); - } - - public string UserName { get; private set; } - public uint UserId { get; private set; } - public UserTuple UserTuple => new UserTuple(UserId, UserName); - public AuthTokenImmutable Token { get; private set; } - - [Export] - public Lazy UserTuple_Export { get; private set; } - - [Export(typeof(Lazy))] - public Lazy Token_Export { get; private set; } - - [Export("workspace")] - public string Workspace { get; private set; } - } - public struct User { public User(string name, uint id) { this.name = name; @@ -61,18 +28,12 @@ namespace mbts.ui.shell { // Singleton to help bootstrap the system static Lazy _instance; - readonly string _config_source; private string _workspace; private PreloginWorkspaceEnumerator _enum_workspace; ServiceContext _ctx; DispatcherFrame _frame = new DispatcherFrame(); - LoginModel(string config_source) { - _config_source = config_source; - init().ignore(); - } - // Properties used in binding to the UI public event Action Ready; @@ -87,7 +48,7 @@ namespace mbts.ui.shell { public static LoginModel init(string config_source) { // '_instance' is lazy to guarentee it is only instantiated once Interlocked.CompareExchange( - ref _instance, new(() => new(config_source)), null); + ref _instance, new(() => new()), null); return _instance.Value; } @@ -117,15 +78,6 @@ namespace mbts.ui.shell { // Check that the logon dependencies are valid. // Initialization is complete when UserNames is populated public async Task init() { - // Make sure config is initialized and working - try { - Configuration.InitializeAsync(_config_source).AsTask().Wait(); - } catch (ApplicationException e) { - ErrorMessage = e.Message; - HasFatalError = true; - return false; - } - // Wait for the hub to create the login doodad _ctx = await _hub_src.Task; // Populate the UI @@ -181,6 +133,7 @@ namespace mbts.ui.shell { #region provide hub internal void provide_services(ServiceContext ctx) { _hub_src.TrySetResult(ctx); + _ctx = ctx; } private TaskCompletionSource _hub_src = new TaskCompletionSource(); #endregion diff --git a/src/client/spark/view/Shell/Login/LoginView.xaml b/src/client/spark/view/Shell/Login/LoginView.xaml index 12b983970..f1b709666 100644 --- a/src/client/spark/view/Shell/Login/LoginView.xaml +++ b/src/client/spark/view/Shell/Login/LoginView.xaml @@ -29,6 +29,9 @@ + + + 10 10 22 @@ -157,7 +160,10 @@ + Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" + BorderThickness="0" + Foreground="{ui:BTSBrush UI.TextBox.normal_foreground}" + Background="{ui:BTSBrush UI.TextBox.normal_background}" /> + Foreground="{ui:BTSBrush UI.default_text}" /> + Foreground="{ui:BTSBrush UI.default_text}"> Load last workspace @@ -225,10 +231,10 @@ - + Margin="0,0,0,10" /> + Please call BTS Support at (919) 913-0860 or email @@ -237,12 +243,12 @@ + Foreground="{ui:BTSBrush UI.default_text}" /> + Foreground="{ui:BTSBrush UI.default_text}" /> @@ -257,7 +263,7 @@ > + Text="Loading..." HorizontalAlignment="Center" Foreground="{ui:BTSBrush UI.default_text}" /> diff --git a/src/client/spark/view/Shell/Login/LoginViewModel.cs b/src/client/spark/view/Shell/Login/LoginViewModel.cs index 1aa22be43..b3adcdfac 100644 --- a/src/client/spark/view/Shell/Login/LoginViewModel.cs +++ b/src/client/spark/view/Shell/Login/LoginViewModel.cs @@ -36,6 +36,7 @@ namespace mbts.ui.shell { public event Action ready; public event Action begin_login; + public event Action LoggedIn; private void OnSendMail(object obj) { string uri_base = obj as string; @@ -155,7 +156,10 @@ namespace mbts.ui.shell { string selected_workspace = _resume_workspace && SelectedWorkspace != NEW_WORKSPACE_NAME ? SelectedWorkspace : null; var result = await _model.do_login(Name, selected_workspace, Password); - if (result.success) { login_status = LoginStatus.LoggingIn; } + if (result.success) { + login_status = LoginStatus.LoggingIn; + LoggedIn?.Invoke(); + } if (!result.success && !string.IsNullOrEmpty(result.reason)) { WarningMessage = result.reason; login_status = LoginStatus.Rejected; @@ -166,7 +170,7 @@ namespace mbts.ui.shell { } private void OnCancel(object unused) { CloseView(); - Application.Current.Dispatcher.InvokeAsync(() => Application.Current.Shutdown(0)); + Application.Current?.Dispatcher?.InvokeAsync(() => Application.Current.Shutdown(0)); } private void CloseView() { diff --git a/src/client/spark/view/Shell/ShellModel.cs b/src/client/spark/view/Shell/ShellModel.cs index c173a0dc2..723193254 100644 --- a/src/client/spark/view/Shell/ShellModel.cs +++ b/src/client/spark/view/Shell/ShellModel.cs @@ -1,11 +1,13 @@ namespace mbts.ui { using bts; + using bts.api; using bts.api.core; - using bts.config; using bts.core; using bts.shell; using bts.spark; + using Bts.DataStreamCore; + using mbts.config; using mbts.persist; using mbts.spark; @@ -13,9 +15,11 @@ using mbts.ui.settings; using mbts.ui.shell; + using Spark; using Spark.Api.Data; using Spark.Api.Reactive; using Spark.Core.Data; + using Spark.Core.Services; using System; using System.Collections.Generic; @@ -30,9 +34,24 @@ using System.Text; [Export] - public class ConfigSourceExporter { - [Export("config_source")] - public string config_source { get; set; } + public class CredentialsExporter { + [Export] + public Lazy UserTuple_Export { get; set; } + + [Export(typeof(Lazy))] + public Lazy Token_Export { get; set; } + + [Export("workspace")] + public string Workspace { get; set; } + + [Export] + public ISparkConfigLoader ConfigLoader { get; set; } + } + + [Export] + public class StreamFactoryExporter { + [Export] + public IEnumerable StreamFactories { get; set; } } // Models a generic multi-window host @@ -43,12 +62,6 @@ public CompositionHost container => _container.Value; - [Import] - public ui.shell.LoginContainer login { get; set; } - - [Import] - public Lazy user_tuple { get; set; } - [ImportMany] public IEnumerable services { get; set; } @@ -65,8 +78,14 @@ [Export] public IExecFeeCalculator exec_fee_calculator => _exec_fee_calculator.Value; - public void Initialize(string config_source) { - PerformMEFComposition(config_source); + public Lazy UserTuple_Export { get; set; } + public Lazy Token_Export { get; set; } + public string Workspace { get; set; } + public ISparkConfigLoader ConfigLoader { get; set; } + public IEnumerable StreamFactories { get; set; } + + public void Initialize(Spark.Api.IServiceProvider serviceProvider, ServiceContext internalServiceContext) { + PerformMEFComposition(serviceProvider, internalServiceContext); InitInitializers(); } @@ -105,12 +124,20 @@ return value; } - void PerformMEFComposition(string config_source) { + void PerformMEFComposition(Spark.Api.IServiceProvider serviceProvider, ServiceContext internalServiceContext) { Log.Ui.info($"Generating MEF dependencies for the shell" + $"({typeof(IInitializable)}s, {typeof(IShellModule)}s, " + $"{typeof(IShellDaemon)}s, etc.)"); try { - container.GetExport().config_source = config_source; + var shellServices = container.GetExport(); + shellServices.InternalServiceContext = internalServiceContext; + shellServices.ServiceProvider = serviceProvider; + var credentials = container.GetExport(); + credentials.UserTuple_Export = UserTuple_Export; + credentials.Token_Export = Token_Export; + credentials.Workspace = Workspace; + credentials.ConfigLoader = ConfigLoader; + container.GetExport().StreamFactories = StreamFactories; container.SatisfyImports(this); } catch (ReflectionTypeLoadException rtlex) { var message = new StringBuilder(rtlex.ToString()); @@ -142,10 +169,10 @@ // specifically when a task calls for adding particular types to the // composition host, rather than all types in this assembly. static HashSet _base_dep_injection_types() => new() { - typeof(shell.LoginContainer), + // typeof(shell.LoginContainer), typeof(WindowFactory), typeof(ServiceContext), - typeof(Spark.ShellServices), + // typeof(Spark.ShellServices), typeof(shell.LauncherVM), typeof(OrdersModel), typeof(SimpleBus), diff --git a/src/client/spark_api/Services/CachedServiceProvider.cs b/src/client/spark_api/Services/CachedServiceProvider.cs new file mode 100644 index 000000000..b88ac8cfb --- /dev/null +++ b/src/client/spark_api/Services/CachedServiceProvider.cs @@ -0,0 +1,49 @@ +#nullable enable +namespace Spark.Api.Services; + +using bts.api.core; + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +public class CachedServiceProvider : Api.IServiceProvider { + readonly object locker = new(); + readonly Api.IServiceProvider serviceProvider; + readonly Dictionary> cachedServices = new(); + + public CachedServiceProvider(Api.IServiceProvider serviceProvider) { + this.serviceProvider = serviceProvider; + } + + public T GetService(string? qualifier) where T : class, IApiClient { + Lazy? service = null; + lock (locker) { + service = cachedServices.GetOrAdd(new(typeof(T), qualifier), ServiceFactory); + } + return (T)service.Value; + + Lazy ServiceFactory(ServiceKey key) => + new(() => serviceProvider.GetService(key.Qualifier)); + } + + readonly struct ServiceKey : IEquatable { + public ServiceKey(Type type, string? qualifier) { + Type = type; + Qualifier = qualifier; + } + + public Type Type { get; } + public string? Qualifier { get; } + + public bool Equals(ServiceKey other) => + other.Type == Type && other.Qualifier == Qualifier; + + public override bool Equals([NotNullWhen(true)] object? obj) => + obj is ServiceKey other && Equals(other); + + public override int GetHashCode() { + return HashCode.Combine(Type.FullName, Qualifier); + } + } +} diff --git a/src/client/spark_api/Services/SparkApiServices.cs b/src/client/spark_api/Services/SparkApiServices.cs index 9733c7401..d2ff3f87a 100644 --- a/src/client/spark_api/Services/SparkApiServices.cs +++ b/src/client/spark_api/Services/SparkApiServices.cs @@ -38,14 +38,12 @@ public static class SparkApiServices { /// before any api-related services run). throw new InvalidOperationException("Must add SparkService first"); } - services - .Configure(configurationSection.GetSection("Credentials")) - .AddSingleton() - .AddSingleton(sp => sp.GetRequiredService()) - .AddSingleton() - .AddSingleton(sp => sp.GetRequiredService()) - .AddHostedService(sp => sp.GetRequiredService()) - .AddSingleton(); + services.AddSingleton() + .AddSingleton(sp => new CachedServiceProvider(sp.GetRequiredService())); + var credentials = configurationSection.GetSection("Credentials"); + if (credentials.GetChildren().Any()) { + services.AddSparkLogin(credentials); + } if (configurationSection.GetValue("EnableOrders", true)) { services.AddSparkdService(); services.TryAddSingleton(); @@ -67,6 +65,20 @@ public static class SparkApiServices { return services; } + public static IServiceCollection AddSparkLogin(this IServiceCollection services, IConfiguration configurationSection) => + services.Configure(configurationSection) + .AddSingleton() + .AddSingleton(sp => sp.GetRequiredService()) + .AddHostedService(sp => sp.GetRequiredService()); + + public static IServiceCollection AddSparkLogin(this IServiceCollection services, SparkApiCredentials configurationSection) => + services.AddSingleton() + .AddSingleton(sp => sp.GetRequiredService()) + .AddHostedService(sp => sp.GetRequiredService()); + internal static IServiceCollection AddSparkdService(this IServiceCollection services) where T : class => services.AddSingleton(sp => sp.GetRequiredService().GetRequiredService()); + + internal static IServiceCollection AddQualifiedSparkdService(this IServiceCollection services, string qualifier) where T : class, IApiClient => + services.AddSingleton(sp => sp.GetRequiredService().locate(qualifier)); } diff --git a/src/client/spark_api/api/LoginException.cs b/src/client/spark_api/api/LoginException.cs new file mode 100644 index 000000000..9ded5eb90 --- /dev/null +++ b/src/client/spark_api/api/LoginException.cs @@ -0,0 +1,5 @@ +namespace bts.api.core; + +public class LoginException : ApiException { + public LoginException(string msg) : base(msg) { } +} diff --git a/src/client/spark_core/Services/ISparkConfigLoader.cs b/src/client/spark_core/Services/ISparkConfigLoader.cs index 651176f19..c63021610 100644 --- a/src/client/spark_core/Services/ISparkConfigLoader.cs +++ b/src/client/spark_core/Services/ISparkConfigLoader.cs @@ -8,5 +8,6 @@ using System.Threading.Tasks; /// Initializes Spark protobuf config. /// public interface ISparkConfigLoader { + string? ConfigServer { get; } Task InitializeAsync(CancellationToken cancellationToken); } diff --git a/src/client/spark_core/Services/SparkConfigLoader.cs b/src/client/spark_core/Services/SparkConfigLoader.cs index d2041db20..e4ae13545 100644 --- a/src/client/spark_core/Services/SparkConfigLoader.cs +++ b/src/client/spark_core/Services/SparkConfigLoader.cs @@ -27,6 +27,8 @@ public class SparkConfigLoader : ISparkConfigLoader { this.logger = logger; } + public string? ConfigServer => config.ConfigServer; + public async Task InitializeAsync(CancellationToken cancellationToken) { bool success = false; var task = Configuration.InitializeAsync(config.ConfigServer, cancellationToken); diff --git a/src/client/spark_core/Services/SparkTestConfigLoader.cs b/src/client/spark_core/Services/SparkTestConfigLoader.cs index cf12349ea..4aeec820d 100644 --- a/src/client/spark_core/Services/SparkTestConfigLoader.cs +++ b/src/client/spark_core/Services/SparkTestConfigLoader.cs @@ -22,6 +22,8 @@ class SparkTestConfigLoader : ISparkConfigLoader { public SparkTestConfigLoader(SparkTestConfigLoaderOpts config) => this.config = config; + public string? ConfigServer => null; + public async Task InitializeAsync(CancellationToken cancellationToken) => Log.assert(await Configuration.ResetForTestAsync(string.Empty, cancellationToken)); }