diff --git a/src/client/spark_api/Services/SparkApiConfig.cs b/src/client/spark_api/Services/SparkApiConfig.cs new file mode 100644 index 000000000..f58c7c47e --- /dev/null +++ b/src/client/spark_api/Services/SparkApiConfig.cs @@ -0,0 +1,11 @@ +#nullable enable +namespace Spark.Api.Services; + +public class SparkApiConfig { + public string? ConfigServer { get; set; } + public string? User { get; set; } + public string? Password { get; set; } + public bool EnableAdmin { get; set; } + public bool EnableOrders { get; set; } + public bool EnablePosition { get; set; } +} diff --git a/src/client/spark_api/Services/SparkApiServices.cs b/src/client/spark_api/Services/SparkApiServices.cs new file mode 100644 index 000000000..ed7674769 --- /dev/null +++ b/src/client/spark_api/Services/SparkApiServices.cs @@ -0,0 +1,54 @@ +#nullable enable +namespace Spark.Api.Services; + +using bts.api.admin; +using bts.api.core; +using bts.api.orderman; +using bts.api.position; + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +using Spark.Api.Admin; +using Spark.Api.Orders; +using Spark.Api.Reactive; +using Spark.Core.Services; + +using System; +using System.Linq; + +public static class SparkApiServices { + /// + /// Connects to Sparkd. To configure, define 'SparkdCredentials.User' and + /// 'SparkdCredentials.Password' in the input --config json file. + /// + /// The to add the service to. + /// A reference to this instance after the operation has completed. + public static IServiceCollection AddSparkApi(this IServiceCollection services, IConfiguration configurationSection) { + if (!services.Any(s => s.ServiceType == typeof(SparkService))) { + /// initializes configuration and logging (this should be done + /// before any api-related services run). + throw new InvalidOperationException("Must add SparkService first"); + } + services + .Configure(configurationSection.GetSection("Credentials")) + .AddSingleton() + .AddHostedService() + .AddSparkdService(); + if (configurationSection.GetValue("EnableOrders", true)) { + services.AddSparkdService() + .AddSingleton(); + } + if (configurationSection.GetValue("EnablePosition", true)) { + services.AddSparkdService(); + } + if (configurationSection.GetValue("EnableAdmin", true)) { + services.AddSparkdService(); + services.AddSparkdService(); + } + return services; + } + + internal static IServiceCollection AddSparkdService(this IServiceCollection services) where T : class => + services.AddSingleton(sp => sp.GetRequiredService().GetRequiredService()); +} diff --git a/src/client/spark_api/Services/SparkdCredentials.cs b/src/client/spark_api/Services/SparkdCredentials.cs new file mode 100644 index 000000000..165bed73d --- /dev/null +++ b/src/client/spark_api/Services/SparkdCredentials.cs @@ -0,0 +1,7 @@ +#nullable enable +namespace Spark.Api.Services; + +public class SparkApiCredentials { + public string? User { get; set; } + public string? Password { get; set; } +} diff --git a/src/client/spark_api/Services/SparkdLoginService.cs b/src/client/spark_api/Services/SparkdLoginService.cs new file mode 100644 index 000000000..d41b9356f --- /dev/null +++ b/src/client/spark_api/Services/SparkdLoginService.cs @@ -0,0 +1,37 @@ +#nullable enable +namespace Spark.Api.Services; + +using bts; +using bts.api.core; + +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// +/// +public class SparkdLoginService : IHostedService { + readonly ServiceContext serviceContext; + readonly SparkApiCredentials config; + + public SparkdLoginService(ServiceContext serviceContext, IOptions config) { + this.serviceContext = serviceContext; + this.config = config.Value; + } + + public Task StartAsync(CancellationToken cancellationToken) => + string.IsNullOrEmpty(config.User) + ? Task.CompletedTask + : Task.Run(() => { + var res = serviceContext.login(config.User, config.Password ?? string.Empty).GetAwaiter().GetResult(); + if (!res.success) { + Log.Gen.error("Failed to login to sparkd."); + } + }, cancellationToken); + + public Task StopAsync(CancellationToken cancellationToken) => + Task.CompletedTask; +} diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index 9e4d0fa8c..7a7489f3b 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -2,6 +2,8 @@ namespace bts.api.core { using bts.api.user; + using Microsoft.Extensions.Options; + using System; using System.Collections.Generic; using System.Linq; @@ -12,8 +14,7 @@ namespace bts.api.core { IntPtr _qbus; AuthToken? _token; - public ServiceContext(string? config_source = null) { - BTS.init(config_source); + public ServiceContext() { _qbus = ApiNativeMethods.api_create_quickbus(null, 0); } diff --git a/src/client/spark_api/spark_api.csproj b/src/client/spark_api/spark_api.csproj index c72702948..b5b1032ff 100644 --- a/src/client/spark_api/spark_api.csproj +++ b/src/client/spark_api/spark_api.csproj @@ -20,6 +20,7 @@ all + diff --git a/src/client/spark_core/Protobuf/IProtobuf.cs b/src/client/spark_core/Protobuf/IProtobuf.cs index ce7ae25d8..eb088caa5 100644 --- a/src/client/spark_core/Protobuf/IProtobuf.cs +++ b/src/client/spark_core/Protobuf/IProtobuf.cs @@ -4,7 +4,7 @@ namespace Spark.Core.Protobuf { using System; - public interface IProtobuf : ICloneable, IEquatable { + public interface IProtobuf : ICloneable, IEquatable where T : notnull { object ICloneable.Clone() => Clone() ?? throw new InvalidOperationException(); } diff --git a/src/client/spark_core/Services/SparkConfig.cs b/src/client/spark_core/Services/SparkConfig.cs new file mode 100644 index 000000000..8f0976363 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfig.cs @@ -0,0 +1,6 @@ +#nullable enable +namespace Spark.Core.Services; + +public class SparkConfig { + public string? ConfigServer { get; set; } +} diff --git a/src/client/spark_core/Services/SparkCoreServices.cs b/src/client/spark_core/Services/SparkCoreServices.cs new file mode 100644 index 000000000..93e5fe2cb --- /dev/null +++ b/src/client/spark_core/Services/SparkCoreServices.cs @@ -0,0 +1,18 @@ +namespace Spark.Core.Services; + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +using System; +using System.Linq; + +public static class SparkCoreServices { + public static IServiceCollection AddSpark(this IServiceCollection services, IConfiguration configurationSection) { + if (services.Any(s => s.ServiceType == typeof(SparkService))) { + throw new InvalidOperationException("SparkService has already been added"); + } + return services.Configure(configurationSection) + .AddSingleton() + .AddHostedService(sp => sp.GetRequiredService()); + } +} diff --git a/src/client/spark_core/Services/SparkService.cs b/src/client/spark_core/Services/SparkService.cs new file mode 100644 index 000000000..8a5e6d9b6 --- /dev/null +++ b/src/client/spark_core/Services/SparkService.cs @@ -0,0 +1,32 @@ +#nullable enable +namespace Spark.Core.Services; + +using mbts.config; + +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// An facade that initializes Spark protobuf config, logging, and +/// quickbus for both c# and cpp. +/// +public class SparkService : IHostedService { + readonly SparkConfig config; + + public SparkService(IOptions config) + : this(config.Value) { } + + public SparkService(SparkConfig config) => + this.config = config; + + public Task StartAsync(CancellationToken cancellationToken) { + Configuration.initialize(config.ConfigServer); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) => + Task.CompletedTask; +} diff --git a/src/client/spark_core/spark_core.csproj b/src/client/spark_core/spark_core.csproj index 5e2719681..c73f84959 100644 --- a/src/client/spark_core/spark_core.csproj +++ b/src/client/spark_core/spark_core.csproj @@ -15,6 +15,7 @@ all + diff --git a/src/client/spark_core/utils_api/BTS.cs b/src/client/spark_core/utils_api/BTS.cs index bad682c8a..5fe04efc9 100644 --- a/src/client/spark_core/utils_api/BTS.cs +++ b/src/client/spark_core/utils_api/BTS.cs @@ -9,7 +9,7 @@ namespace bts { static int _needs_init_spark_lib = 0; - public static void init(string config_source = null) { + public static bool init(string config_source = null) { var version = Assembly.GetExecutingAssembly().GetName().Version; Console.WriteLine( $"Configuring Spark {version} with {(config_source is null ? "defaults" : $"'{config_source}'")}"); @@ -26,6 +26,7 @@ namespace bts if (Interlocked.CompareExchange(ref _needs_init_spark_lib,1,0) == 0) { utils.BTSNativeMethods.init_spark_lib(); } + return true; // This seems like a good idea but it crashes due to thread shutdown order // AppDomain.CurrentDomain.ProcessExit += (o, e) => { diff --git a/src/client/spark_core/utils_api/ICloneable.cs b/src/client/spark_core/utils_api/ICloneable.cs index ef9cc1c36..ccd09ee15 100644 --- a/src/client/spark_core/utils_api/ICloneable.cs +++ b/src/client/spark_core/utils_api/ICloneable.cs @@ -2,7 +2,8 @@ namespace bts { using System; - public interface ICloneable : ICloneable { + public interface ICloneable : ICloneable where T : notnull { new T Clone(); + object ICloneable.Clone() => Clone(); } }