diff --git a/src/DataStreamSuite/DataStreamPublisher/Program.cs b/src/DataStreamSuite/DataStreamPublisher/Program.cs index 82f117b5c..3af880dfa 100644 --- a/src/DataStreamSuite/DataStreamPublisher/Program.cs +++ b/src/DataStreamSuite/DataStreamPublisher/Program.cs @@ -30,7 +30,10 @@ async Task RunAsync(DataStreamPublisherConfig cfg) cfg.MergeWithConfigFile(); cfg.MergeWith(DataStreamPublisherConfig.Default()); PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub)); - Configuration.initialize(cfg.Sparkd); + if (!await Configuration.InitializeAsync(cfg.Sparkd)) { + Console.Error.WriteLine("Failed to initialize Spark config"); + return 1; + } Log.Gen.info("Starting DataStreamPublisher"); var service_ctx = ServiceContext.create(cfg.Sparkd); await service_ctx.login(cfg.User ?? "bts", cfg.Password ?? string.Empty); diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs index 12713c7e1..777f181f4 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/Program.cs @@ -26,7 +26,7 @@ static class Program { cfg.MergeWithConfigFile(); cfg.MergeWith(DataStreamQueryAnalyzerConfig.Default()); PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub)); - Configuration.initialize(cfg.Sparkd); + await Configuration.InitializeAsync(cfg.Sparkd); PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5)))); Log.Gen.info("Starting DataStreamQueryAnalyzer"); var mainForm = new MainForm(); diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 5b1be3303..f453d4234 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -17,14 +17,17 @@ using System.Threading; using System.Threading.Tasks; Parser.Default.ParseArguments(args) - .MapResult(Run, errs => 1); + .MapResult(Run, errs => Task.FromResult(1)); -int Run(PubSubServerConfig cfg) { +async Task Run(PubSubServerConfig cfg) { try { cfg.MergeWithConfigFile(); cfg.MergeWith(PubSubServerConfig.Default()); PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub)); - Configuration.initialize(cfg.Sparkd); + if (!await Configuration.InitializeAsync(cfg.Sparkd)) { + Console.Error.WriteLine("Failed to initialize Spark config"); + return 1; + } bool run_echo_server = false; bool echo_msg_back = false; cfg.SimpleEchoServer = run_echo_server; diff --git a/src/client/Spark.Console/src/Commands/ConfigCommand.cs b/src/client/Spark.Console/src/Commands/ConfigCommand.cs index f57a4f864..bc430c506 100644 --- a/src/client/Spark.Console/src/Commands/ConfigCommand.cs +++ b/src/client/Spark.Console/src/Commands/ConfigCommand.cs @@ -8,7 +8,7 @@ namespace Spark.Console { class ConfigCommand { public static int run(ConfigCommandOptions opts) { - Configuration.initialize(opts.sparkd); + Configuration.InitializeAsync(opts.sparkd).AsTask().Wait(); var keys = opts.keys?.ToArray() ?? Array.Empty(); var excluded_keys = opts.excluded_keys?.ToArray() ?? Array.Empty(); return Configuration.run_config_query( diff --git a/src/client/Spark.Console/src/Commands/TestCommand.cs b/src/client/Spark.Console/src/Commands/TestCommand.cs index a1cfae74a..597dfe0f3 100644 --- a/src/client/Spark.Console/src/Commands/TestCommand.cs +++ b/src/client/Spark.Console/src/Commands/TestCommand.cs @@ -8,6 +8,7 @@ namespace Spark.Console { using System; using System.Collections.Generic; using System.Linq; + using System.Threading.Tasks; static class TestCommand { static readonly Lazy _enumerator_backing = new( @@ -15,12 +16,15 @@ namespace Spark.Console { static TestEnumerator _enumerator => _enumerator_backing.Value; - public static int run(TestCommandOptions o) { + public static int run(TestCommandOptions o) => + RunAsync(o).GetAwaiter().GetResult(); + + public static async Task RunAsync(TestCommandOptions o) { if (o.type == TestType.Unit) { // Skip loading any local config - Configuration.initialize_for_test(); + await Configuration.ResetForTestAsync(); } else { - Configuration.initialize(o.sparkd); + await Configuration.InitializeAsync(o.sparkd); } ConsoleEx.WriteLine(JsonConvert.SerializeObject(o), ConsoleColor.DarkGray); diff --git a/src/client/spark/Daemons/MDDaemon.cs b/src/client/spark/Daemons/MDDaemon.cs index 9d44b19aa..169f70f51 100644 --- a/src/client/spark/Daemons/MDDaemon.cs +++ b/src/client/spark/Daemons/MDDaemon.cs @@ -13,6 +13,7 @@ namespace bts { using mbts.spark; using mbts.ui; + using Spark.Api.Services; using Spark.Core.Data; using Spark.Core.Disposables; using Spark.Core.Reactive; @@ -70,7 +71,7 @@ namespace bts { _relay_throttle = relay_throttle; _output_throttle = output_throttle; _security_metadata = security_metadata; - _md_source_backing = new(() => new(_user.Value, _relay_throttle, _output_throttle)); + _md_source_backing = new(() => new(UserDataPermissions.Create(_user.Value), _relay_throttle, _output_throttle)); } public int priority => 55; diff --git a/src/client/spark/Tests/ViewTests/WindowManagementTests.cs b/src/client/spark/Tests/ViewTests/WindowManagementTests.cs index cf23841f3..73d7a9b71 100644 --- a/src/client/spark/Tests/ViewTests/WindowManagementTests.cs +++ b/src/client/spark/Tests/ViewTests/WindowManagementTests.cs @@ -28,7 +28,7 @@ namespace mbts.ui.tests { // This is required to satisfy the StaticResources in various parts of the application if (Application.Current == null) { AutoResetEvent gonow = new AutoResetEvent(false); - Configuration.initialize(); + Configuration.InitializeAsync().AsTask().Wait(); Thread t = new Thread(() => { try { Assert.AreEqual(Thread.CurrentThread.GetApartmentState(), ApartmentState.STA); diff --git a/src/client/spark/view/Shell/Login/LoginModel.cs b/src/client/spark/view/Shell/Login/LoginModel.cs index fcfecf785..0baac331a 100644 --- a/src/client/spark/view/Shell/Login/LoginModel.cs +++ b/src/client/spark/view/Shell/Login/LoginModel.cs @@ -119,7 +119,7 @@ namespace mbts.ui.shell { public async Task init() { // Make sure config is initialized and working try { - Configuration.initialize(_config_source); + Configuration.InitializeAsync(_config_source).AsTask().Wait(); } catch (ApplicationException e) { ErrorMessage = (e.Message ?? string.Empty) + " " + (App.stunnel_job_error ?? string.Empty); HasFatalError = true; @@ -161,7 +161,7 @@ namespace mbts.ui.shell { } return result; } - return new LoginResult("Invalid username"); + return new() { reason = "Invalid username" }; } private void on_user_authenticated(string username, AuthTokenImmutable? token) { diff --git a/src/client/spark_api/MarketData/MDSource.cs b/src/client/spark_api/MarketData/MDSource.cs index 0cc6a4c3c..13f5c97e2 100644 --- a/src/client/spark_api/MarketData/MDSource.cs +++ b/src/client/spark_api/MarketData/MDSource.cs @@ -4,6 +4,7 @@ namespace bts.md { using mbts; using mbts.co2; + using Spark.Api.Services; using Spark.Core.Disposables; using System; @@ -18,18 +19,21 @@ namespace bts.md { public class MDSource : IDisposable { readonly IConnectableObservable> _securities_src; readonly Co2RelayControl _relay; - readonly UserTuple _user; readonly List _resources = new(); + readonly IUserDataPermissions userDataPermissions; State _state = State.Uninitialized; object _lock = new(); public MDSource(TimeSpan relay_throttle) - : this(UserTuple.admin, relay_throttle, TimeSpan.FromSeconds(1)) { } + : this(UserDataPermissions.Create(UserTuple.admin), relay_throttle) { } + + public MDSource(IUserDataPermissions userDataPermissions, TimeSpan? relayThrottle = null, TimeSpan? outputThrottle = null) + : this(userDataPermissions, relayThrottle ?? TimeSpan.FromSeconds(0.1), outputThrottle ?? TimeSpan.FromSeconds(1)) { } public MDSource( - UserTuple user, TimeSpan relay_throttle, TimeSpan output_throttle) { - _user = user; + IUserDataPermissions userDataPermissions, TimeSpan relay_throttle, TimeSpan output_throttle) { + this.userDataPermissions = userDataPermissions; _relay = new Co2RelayControl(consumer, relay_throttle); _relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore(); @@ -58,7 +62,7 @@ namespace bts.md { bool sec_filter(SecurityAndHandle sec) => sec.security.ContractCase != Security.ContractOneofCase.Synthetic || - _user.can_access_user_data(sec.security.Synthetic.UserId); + userDataPermissions.Allow(sec.security.Synthetic.UserId); } enum State { diff --git a/src/client/spark_api/Services/ISparkdLoginService.cs b/src/client/spark_api/Services/ISparkdLoginService.cs new file mode 100644 index 000000000..2dc6d973e --- /dev/null +++ b/src/client/spark_api/Services/ISparkdLoginService.cs @@ -0,0 +1,9 @@ +namespace Spark.Api.Services; + +using bts.api.core; + +using System.Threading.Tasks; + +public interface ISparkdLoginService { + ValueTask LoginAsync(); +} diff --git a/src/client/spark_api/Services/IUserDataPermissions.cs b/src/client/spark_api/Services/IUserDataPermissions.cs new file mode 100644 index 000000000..ddac30d06 --- /dev/null +++ b/src/client/spark_api/Services/IUserDataPermissions.cs @@ -0,0 +1,5 @@ +namespace Spark.Api.Services; + +public interface IUserDataPermissions { + bool Allow(uint userId); +} diff --git a/src/client/spark_api/Services/SparkApiServices.cs b/src/client/spark_api/Services/SparkApiServices.cs index 261624dcb..85b26452c 100644 --- a/src/client/spark_api/Services/SparkApiServices.cs +++ b/src/client/spark_api/Services/SparkApiServices.cs @@ -5,6 +5,8 @@ using bts.api.admin; using bts.api.core; using bts.api.orderman; using bts.api.position; +using bts.co2; +using bts.md; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -31,8 +33,8 @@ public static class SparkApiServices { Console.WriteLine("SparkApi services have been disabled"); return services; } - if (!services.Any(s => s.ServiceType == typeof(SparkService))) { - /// initializes configuration and logging (this should be done + if (!services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) { + /// initializes configuration and logging (this should be done /// before any api-related services run). throw new InvalidOperationException("Must add SparkService first"); } @@ -41,7 +43,9 @@ public static class SparkApiServices { .AddSingleton() .AddSingleton(sp => sp.GetRequiredService()) .AddSingleton() - .AddHostedService(sp => sp.GetRequiredService()); + .AddSingleton(sp => sp.GetRequiredService()) + .AddHostedService(sp => sp.GetRequiredService()) + .AddSingleton(); if (configurationSection.GetValue("EnableOrders", true)) { services.AddSparkdService(); services.TryAddSingleton(); @@ -50,6 +54,12 @@ public static class SparkApiServices { if (configurationSection.GetValue("EnablePosition", true)) { services.AddSparkdService(); } + if (configurationSection.GetValue("EnableMarketData", true)) { + services.AddSparkdService() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + } if (configurationSection.GetValue("EnableAdmin", true)) { services.AddSparkdService(); services.AddSparkdService(); diff --git a/src/client/spark_api/Services/SparkdLoginService.cs b/src/client/spark_api/Services/SparkdLoginService.cs index d41b9356f..cf30c74f0 100644 --- a/src/client/spark_api/Services/SparkdLoginService.cs +++ b/src/client/spark_api/Services/SparkdLoginService.cs @@ -7,30 +7,55 @@ using bts.api.core; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; +using System; using System.Threading; using System.Threading.Tasks; /// -/// +/// An that logs into Sparkd. Login is only attemped once. After that, +/// subsequent calls to simply return the result of the initial attempt. /// -public class SparkdLoginService : IHostedService { +public class SparkdLoginService : IHostedService, ISparkdLoginService { readonly ServiceContext serviceContext; readonly SparkApiCredentials config; + readonly object lockObj = new(); + LoginResult? loginResult; + Task? loginTask; - public SparkdLoginService(ServiceContext serviceContext, IOptions config) { + public SparkdLoginService(ServiceContext serviceContext, IOptions config) + : this(serviceContext, config.Value) { } + + public SparkdLoginService(ServiceContext serviceContext, SparkApiCredentials config) { this.serviceContext = serviceContext; - this.config = config.Value; + this.config = config; + } + + public ValueTask LoginAsync() { + if (loginResult is not null) { + return ValueTask.FromResult(loginResult); + } + if (string.IsNullOrEmpty(config.User)) { + return ValueTask.FromException(new InvalidOperationException("User is null/empty")); + } + lock (lockObj) { + if (loginResult is not null) { + return ValueTask.FromResult(loginResult); + } + loginTask ??= LoginImplAsync(); + return new(loginTask); + } + + async Task LoginImplAsync() { + loginResult = await serviceContext.login(config.User ?? string.Empty, config.Password ?? string.Empty); + if (!loginResult.success) { + Log.Gen.error($"Failed to login to sparkd. reason: {loginResult.reason}"); + } + return loginResult; + } } 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); + LoginAsync().AsTask(); public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; diff --git a/src/client/spark_api/Services/UserDataPermissions.cs b/src/client/spark_api/Services/UserDataPermissions.cs new file mode 100644 index 000000000..3fd380273 --- /dev/null +++ b/src/client/spark_api/Services/UserDataPermissions.cs @@ -0,0 +1,48 @@ +#nullable enable +namespace Spark.Api.Services; + +using bts; +using bts.api.core; + +using System.Diagnostics.CodeAnalysis; + +public class UserDataPermissions : IUserDataPermissions { + readonly object lockObj = new(); + readonly ISparkdLoginService? sparkdLoginService; + UserTuple? user; + + public UserDataPermissions(ISparkdLoginService sparkdLoginService) { + this.sparkdLoginService = sparkdLoginService; + } + + UserDataPermissions(UserTuple user) { + this.user = user; + } + + public static UserDataPermissions Create(UserTuple user) => + new(user); + + public bool Allow(uint userId) { + if (!user.HasValue) { + blockForUser(); + } + return user.Value.id == userId || user.Value.permissions == UserTuple.Permissions.Admin; + } + + [MemberNotNull(nameof(user))] + void blockForUser() { + Log.Gen.warn("UserDataPermissions waiting for login (blocked)."); + var loginRes = Login(); + Log.Gen.warn($"UserDataPermissions received login result (unblocked). {loginRes}"); + Log.assert(loginRes.success); + Log.notnull(loginRes.user); + user = loginRes.user; + + LoginResult Login() { + Log.notnull(sparkdLoginService); + lock (lockObj) { + return sparkdLoginService.LoginAsync().AsTask().GetAwaiter().GetResult(); + } + } + } +} diff --git a/src/client/spark_api/api/ManagedQuickbusWrapper.cs b/src/client/spark_api/api/ManagedQuickbusWrapper.cs new file mode 100644 index 000000000..0bdadf6f1 --- /dev/null +++ b/src/client/spark_api/api/ManagedQuickbusWrapper.cs @@ -0,0 +1,35 @@ +#nullable enable +namespace Spark.Core; + +using bts.api.core; + +using System; + +sealed class ManagedQuickbusWrapper : IDisposable { + readonly object lockObj = new(); + IntPtr handle; + + public ManagedQuickbusWrapper() + : this(ApiNativeMethods.api_create_quickbus(null, 0)) {} + + internal ManagedQuickbusWrapper(IntPtr handle) => + this.handle = handle; + + public static ManagedQuickbusWrapper None => new(IntPtr.Zero); + + public IntPtr Handle => handle; + + public bool IsDisposed => handle == IntPtr.Zero; + + public void Dispose() { + if (IsDisposed) { + return; + } + lock (lockObj) { + if (handle != IntPtr.Zero) { + ApiNativeMethods.api_destroy_quickbus(handle); + handle = IntPtr.Zero; + } + } + } +} diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index 7a7489f3b..8f38f0c21 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -2,36 +2,59 @@ namespace bts.api.core { using bts.api.user; - using Microsoft.Extensions.Options; - using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Reflection; + using Spark.Core.Services; + using Spark.Core; + using System.Threading; public sealed class ServiceContext : Spark.Api.IServiceProvider, IDisposable { - IntPtr _qbus; + readonly object lockObj = new(); + LazyOnce? quickbusBacking; + bool isDisposed; + AuthToken? _token; - public ServiceContext() { - _qbus = ApiNativeMethods.api_create_quickbus(null, 0); + public ServiceContext() + : this(new SparkConfigLoader(SparkConfigLoaderOpts.Default())) { } + + public ServiceContext(ISparkConfigLoader configLoader) { + quickbusBacking = new(QuickbusFactory, lockObj); + + ManagedQuickbusWrapper QuickbusFactory() { + if (isDisposed) { + return ManagedQuickbusWrapper.None; + } + configLoader.InitializeAsync(CancellationToken.None).Wait(); + return new(); + } } - internal ServiceContext(IntPtr qbus) => - _qbus = qbus; + internal ServiceContext(IntPtr qbus) { + quickbusBacking = new(QuickbusFactory, lockObj); + + ManagedQuickbusWrapper QuickbusFactory() { + // This code is executed in a lock (since we passed 'lockObj' in to the constructor), so + // no need to worry about concurrency when reading 'isDisposed'. + Log.assert(!isDisposed); + if (isDisposed) { + + } + return new(qbus); + } + } public AuthTokenImmutable? token => _token?.immutable(); public UserTuple? user { get; private set; } public bool is_authenticated => _token is not null; - internal IntPtr native_context => _qbus; + internal IntPtr native_context => quickbusBacking?.Value?.Handle ?? throw new ObjectDisposedException(nameof(ServiceContext)); - public static ServiceContext create(string? config_server = null) { - BTS.init(config_server); - var qbus = ApiNativeMethods.api_create_quickbus(null, 0); - return new ServiceContext(qbus); - } + public static ServiceContext create(string? configSource) => + new(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = configSource })); public T locate(string qualifier = "") where T : class, IApiClient { var ctr = typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance) @@ -71,12 +94,12 @@ namespace bts.api.core { if (result.value.IsValid) { _token = result.value.Token; user = new(result.value.UserId, username); - return new LoginResult(result.value.Reason, true); + return new() { reason = result.value.Reason, success = true, user = user }; } else { - return new LoginResult(result.value.Reason); + return new() { reason = result.value.Reason }; } } else { - return new LoginResult(result.error_message); + return new() { reason = result.error_message }; } } } @@ -87,9 +110,25 @@ namespace bts.api.core { } public void Dispose() { - if (_qbus != IntPtr.Zero) { - ApiNativeMethods.api_destroy_quickbus(_qbus); - _qbus = IntPtr.Zero; + if (isDisposed) { + return; + } + ManagedQuickbusWrapper? oldQuickbus; + lock (lockObj) { + if (isDisposed) { + return; + } + isDisposed = true; + if (quickbusBacking?.IsValueCreated ?? false) { + // IMPORTANT: Don't access quickbusBacking.Value unless you are SURE that the value + // has already been created (otherwise the factory function will deadlock, since it + // also locks 'lockObj'). + oldQuickbus = quickbusBacking.Value; + quickbusBacking.Value.Dispose(); + } + // Set quickbusBacking to null to prevent the quickbus from being created after dispose + // has been called (if it hasn't been created already). + quickbusBacking = null; } } @@ -101,10 +140,8 @@ namespace bts.api.core { } public class LoginResult { - public LoginResult() { success = true; } - public LoginResult(string reason) { success = false; this.reason = reason; } - public LoginResult(string reason, bool success) { this.success = success; this.reason = reason; } - public bool success { get; } - public string? reason { get; } + public bool success { get; init; } + public string? reason { get; init; } + public UserTuple? user { get; init; } } } diff --git a/src/client/spark_api_server/Program.cs b/src/client/spark_api_server/Program.cs index e6661c4ff..e27ede129 100644 --- a/src/client/spark_api_server/Program.cs +++ b/src/client/spark_api_server/Program.cs @@ -10,6 +10,8 @@ using CommandLine; using Spark.Api.Server; + using Spark.Api.Services; + using Spark.Core.Services; using System; using System.Reactive.Linq; @@ -39,11 +41,13 @@ Log.notnull(opts.user); Log.notnull(opts.password); PubSubConfig.SetGlobalConfig(Log.notnull(opts.PubSub)); - var service_ctx = ServiceContext.create(opts.sparkd); - await service_ctx.login(opts.user, opts.password); + var serviceCtx = new ServiceContext(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = opts.sparkd } )); + var loginService = new SparkdLoginService(serviceCtx, new SparkApiCredentials() { User = opts.user, Password = opts.password }); + await loginService.LoginAsync(); PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5)))); - var sync = new bts.websocket_core.WebsocketDataSync(service_ctx); + var userDataPermissions = new UserDataPermissions(loginService); + var sync = new bts.websocket_core.WebsocketDataSync(serviceCtx, userDataPermissions); var gwy = new bts.websocket_core.WebsocketConnectionGateway(); var prms = new WebsocketServerConfig { DefaultEndPoint = "/", diff --git a/src/client/spark_core/LazyOnce.cs b/src/client/spark_core/LazyOnce.cs index 4babc3ab3..498fc5b9e 100644 --- a/src/client/spark_core/LazyOnce.cs +++ b/src/client/spark_core/LazyOnce.cs @@ -8,10 +8,17 @@ namespace Spark.Core { /// public class LazyOnce { readonly Func _create; + readonly object syncRoot; Optional _value; - public LazyOnce(Func create) => + public LazyOnce(Func create) + : this(create, new()) { } + + public LazyOnce(Func create, object syncRoot) { _create = create; + ArgumentNullException.ThrowIfNull(syncRoot); + this.syncRoot = syncRoot; + } public bool IsValueCreated => _value.has_value; @@ -19,7 +26,7 @@ namespace Spark.Core { get { // check has_value outside of lock since, once set to true, it doesn't change if (!_value.has_value) { - lock (_create) { + lock (syncRoot) { // check has_value again (may have become true since first check) if (!_value.has_value) { _value = _create(); diff --git a/src/client/spark_core/Services/ISparkConfigLoader.cs b/src/client/spark_core/Services/ISparkConfigLoader.cs new file mode 100644 index 000000000..651176f19 --- /dev/null +++ b/src/client/spark_core/Services/ISparkConfigLoader.cs @@ -0,0 +1,12 @@ +#nullable enable +namespace Spark.Core.Services; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// Initializes Spark protobuf config. +/// +public interface ISparkConfigLoader { + Task InitializeAsync(CancellationToken cancellationToken); +} diff --git a/src/client/spark_core/Services/SparkConfig.cs b/src/client/spark_core/Services/SparkConfig.cs deleted file mode 100644 index 8f0976363..000000000 --- a/src/client/spark_core/Services/SparkConfig.cs +++ /dev/null @@ -1,6 +0,0 @@ -#nullable enable -namespace Spark.Core.Services; - -public class SparkConfig { - public string? ConfigServer { get; set; } -} diff --git a/src/client/spark_core/Services/SparkConfigLoader.cs b/src/client/spark_core/Services/SparkConfigLoader.cs new file mode 100644 index 000000000..64ae890c6 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoader.cs @@ -0,0 +1,27 @@ +#nullable enable +namespace Spark.Core.Services; + +using bts; + +using mbts.config; + +using Microsoft.Extensions.Options; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// Initializes Spark protobuf config. +/// +public class SparkConfigLoader : ISparkConfigLoader { + readonly SparkConfigLoaderOpts config; + + public SparkConfigLoader(IOptions config) + : this(config.Value) { } + + public SparkConfigLoader(SparkConfigLoaderOpts config) => + this.config = config; + + public async Task InitializeAsync(CancellationToken cancellationToken) => + Log.assert(await Configuration.InitializeAsync(config.ConfigServer, cancellationToken)); +} diff --git a/src/client/spark_core/Services/SparkConfigLoaderOpts.cs b/src/client/spark_core/Services/SparkConfigLoaderOpts.cs new file mode 100644 index 000000000..63db5e489 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoaderOpts.cs @@ -0,0 +1,8 @@ +#nullable enable +namespace Spark.Core.Services; + +public class SparkConfigLoaderOpts { + public string? ConfigServer { get; set; } + + public static SparkConfigLoaderOpts Default() => new(); +} diff --git a/src/client/spark_core/Services/SparkConfigLoaderService.cs b/src/client/spark_core/Services/SparkConfigLoaderService.cs new file mode 100644 index 000000000..6dc710242 --- /dev/null +++ b/src/client/spark_core/Services/SparkConfigLoaderService.cs @@ -0,0 +1,25 @@ +#nullable enable +namespace Spark.Core.Services; + +using Microsoft.Extensions.Hosting; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// An facade that initializes Spark protobuf config, logging, and +/// quickbus for both c# and cpp. +/// +public class SparkConfigLoaderService : IHostedService { + readonly ISparkConfigLoader loader; + + public SparkConfigLoaderService(ISparkConfigLoader loader) { + this.loader = loader; + } + + public Task StartAsync(CancellationToken cancellationToken) => + loader.InitializeAsync(cancellationToken); + + public Task StopAsync(CancellationToken cancellationToken) => + Task.CompletedTask; +} diff --git a/src/client/spark_core/Services/SparkCoreServices.cs b/src/client/spark_core/Services/SparkCoreServices.cs index 93e5fe2cb..e7cbbe944 100644 --- a/src/client/spark_core/Services/SparkCoreServices.cs +++ b/src/client/spark_core/Services/SparkCoreServices.cs @@ -8,11 +8,11 @@ using System.Linq; public static class SparkCoreServices { public static IServiceCollection AddSpark(this IServiceCollection services, IConfiguration configurationSection) { - if (services.Any(s => s.ServiceType == typeof(SparkService))) { + if (services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) { throw new InvalidOperationException("SparkService has already been added"); } - return services.Configure(configurationSection) - .AddSingleton() - .AddHostedService(sp => sp.GetRequiredService()); + return services.Configure(configurationSection) + .AddSingleton() + .AddHostedService(); } } diff --git a/src/client/spark_core/Services/SparkService.cs b/src/client/spark_core/Services/SparkService.cs deleted file mode 100644 index 8a5e6d9b6..000000000 --- a/src/client/spark_core/Services/SparkService.cs +++ /dev/null @@ -1,32 +0,0 @@ -#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/Services/SparkTestConfigLoader.cs b/src/client/spark_core/Services/SparkTestConfigLoader.cs new file mode 100644 index 000000000..cf12349ea --- /dev/null +++ b/src/client/spark_core/Services/SparkTestConfigLoader.cs @@ -0,0 +1,27 @@ +#nullable enable +namespace Spark.Core.Services; + +using bts; + +using mbts.config; + +using Microsoft.Extensions.Options; + +using System.Threading; +using System.Threading.Tasks; + +/// +/// Initializes Spark protobuf config. +/// +class SparkTestConfigLoader : ISparkConfigLoader { + readonly SparkTestConfigLoaderOpts config; + + public SparkTestConfigLoader(IOptions config) + : this(config.Value) { } + + public SparkTestConfigLoader(SparkTestConfigLoaderOpts config) => + this.config = config; + + public async Task InitializeAsync(CancellationToken cancellationToken) => + Log.assert(await Configuration.ResetForTestAsync(string.Empty, cancellationToken)); +} diff --git a/src/client/spark_core/Services/SparkTestConfigLoaderOpts.cs b/src/client/spark_core/Services/SparkTestConfigLoaderOpts.cs new file mode 100644 index 000000000..4eaeeb4ec --- /dev/null +++ b/src/client/spark_core/Services/SparkTestConfigLoaderOpts.cs @@ -0,0 +1,8 @@ +#nullable enable +namespace Spark.Core.Services; + +public class SparkTestConfigLoaderOpts { + public bool IsUnitTest { get; set; } + + public static SparkTestConfigLoaderOpts Default() => new(); +} diff --git a/src/client/spark_core/config/Configuration.cs b/src/client/spark_core/config/Configuration.cs index 446184364..e2067ba70 100644 --- a/src/client/spark_core/config/Configuration.cs +++ b/src/client/spark_core/config/Configuration.cs @@ -9,9 +9,12 @@ namespace mbts.config { using System.IO; using System.Runtime.InteropServices; using System.Text; + using System.Threading; + using System.Threading.Tasks; public static class Configuration { static object _config_lock = new(); + static TaskCompletionSource? initTcs; static State _state = State.Uninitialized; @@ -50,19 +53,90 @@ namespace mbts.config { } } - public static void initialize(string? config_source = null) { - if (_state == State.OK) { - return; + public static ValueTask InitializeAsync(string? configSource = null) => + InitializeAsync(configSource, CancellationToken.None); + + public static ValueTask InitializeAsync(CancellationToken cancellationToken) => + InitializeAsync(null, cancellationToken); + + public static ValueTask InitializeAsync(string? config_source, CancellationToken cancellationToken) { + return _state switch { + State.OK => ValueTask.FromResult(true), + State.Error => ValueTask.FromResult(false), + State.Uninitialized => Initialize(), + _ => throw new InvalidOperationException(), + }; + + ValueTask Initialize() { + lock (_config_lock) { + switch (_state) { + case State.OK: + return ValueTask.FromResult(true); + case State.Error: + return ValueTask.FromResult(false); + case State.Uninitialized: + if (initTcs is not null) { + return new(initTcs.Task); + } + initTcs = new(cancellationToken); + break; + default: + throw new InvalidOperationException(); + } + } + // Set 'initTcs' to null at the end so subsequent calls to InitializeAsync re-run BTS.init, + // rather than simply return the same completed task. + // Note: if BTS.init is successful, it won't rerun (see the switch case above). + var task = initTcs.Task; + try { + BTS.init(config_source); + // Guarentee BTS.init runs BEFORE we signal initialization is complete (this code won't + // run concurrently, so a full lock isn't necessary). + Thread.MemoryBarrier(); + _state = State.OK; + initTcs.SetResult(true); + } catch (Exception ex) { + if (ex is ApplicationException) { + lock (_config_lock) { + _state = State.Error; + initTcs.SetException(ex); + initTcs = null; + } + } + return new(false); + } + initTcs = null; + return new(task); } - // Considered doing this async on a background thread but it wouldn't do any good. The next - // guy down the line to ask for config will just block until this completes. - try { - BTS.init(config_source); - } catch (ApplicationException) { - _state = State.Error; - throw; + } + + public static Task ResetForTestAsync() => + ResetForTestAsync(string.Empty, CancellationToken.None); + + public static Task ResetForTestAsync(string config) => + ResetForTestAsync(config, CancellationToken.None); + + public static Task ResetForTestAsync(CancellationToken cancellationToken) => + ResetForTestAsync(string.Empty, cancellationToken); + + public static Task ResetForTestAsync(string config, CancellationToken cancellationToken) { + return Task.Run(ResetForTest, cancellationToken); + + bool ResetForTest() { + lock (_config_lock) { + _raw_initialize_for_test(config); + // still need init_spark_lib + try { + BTS.init(); + } catch (Exception ex) { + Console.Error.WriteLine($"Exception occurred resetting config for test: {ex}"); + _state = State.Error; + throw; + } + _state = State.OK; + } + return true; } - _state = State.OK; } public static void initialize_for_test(string config = "") { @@ -72,7 +146,7 @@ namespace mbts.config { _state = State.OK; } - public static unsafe bool try_initialize(out string error) { + public static bool try_initialize(out string error) { _reset(); try { BTS.init(); @@ -103,12 +177,22 @@ namespace mbts.config { $"Failed to deserialize config '{key}' as {type}: {error}"; static byte[]? _get_buffer(string key_name) { + switch (_state) { + case State.Uninitialized: + if (!InitializeAsync().GetAwaiter().GetResult()) { + return null; + } + break; + case State.Error: + return null; + case State.OK: + break; + default: + throw new InvalidOperationException($"Unexpected state: {_state}"); + } /// The native get_buffer () is not thread /// safe, so this call must be protected from re-entrancy. lock (_config_lock) { - if (_state == State.Uninitialized) { initialize(); } - if (_state == State.Error) { return null; } - int length = 0; byte[] buffer; IntPtr pbuffer = _raw_get_buffer(key_name, ref length); diff --git a/src/client/spark_core/logging/Log.cs b/src/client/spark_core/logging/Log.cs index 941407ae5..6978d72bf 100644 --- a/src/client/spark_core/logging/Log.cs +++ b/src/client/spark_core/logging/Log.cs @@ -54,7 +54,7 @@ namespace bts { } [return: NotNull] - public static T notnull([NotNull] T? obj, string msg = "") where T : class { + public static T notnull([NotNull] T? obj, string msg = "") { if (obj is null) { assert(false, msg); throw new InvalidOperationException("Assertion failed: expected obj to be non-null"); diff --git a/src/client/spark_sdk/Configurations/SdkSample.json b/src/client/spark_sdk/Configurations/SdkSample.json new file mode 100644 index 000000000..69b0e3a8f --- /dev/null +++ b/src/client/spark_sdk/Configurations/SdkSample.json @@ -0,0 +1,11 @@ +{ + "Spark": { + "ConfigServer": "" + }, + "SparkApi": { + "Credentials": { + "User": "bts", + "Password": "" + } + } +} diff --git a/src/client/spark_sdk/Program.cs b/src/client/spark_sdk/Program.cs new file mode 100644 index 000000000..243eb9ca1 --- /dev/null +++ b/src/client/spark_sdk/Program.cs @@ -0,0 +1,40 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +using Spark.Core.Services; +using Spark.Api.Services; + +using System; +using bts.sdk; + +var host = Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration(ConfigureApp) + .ConfigureServices(ConfigureServices) + .Build(); +try { + Console.Write($"Starting Application"); + host.Run(); + Console.Write($"Application exited gracefully."); +} catch (Exception ex) { + Console.Error.Write($"Application crashed: {ex}"); + return 1; +} +return 0; + +static void ConfigureApp(HostBuilderContext context, IConfigurationBuilder configBuilder) { + configBuilder.AddCommandLine(Environment.GetCommandLineArgs()); + var cfgRoot = configBuilder.Build(); + var cfg = cfgRoot["config"]; + if (cfg is not null) { + configBuilder.AddJsonFile(cfg, false); + } +} + +static void ConfigureServices(HostBuilderContext context, IServiceCollection services) { + /// Bind configuration json fields to strong types. This allows us to add services with + /// constructors that accept these types as arguments. + services.AddSpark(context.Configuration.GetSection("Spark")) + .AddSparkApi(context.Configuration.GetSection("SparkApi")) + .AddHostedService(); +} diff --git a/src/client/spark_sdk/samples/OrderSample.cs b/src/client/spark_sdk/samples/OrderSample.cs index 031f9d957..c8cfd652a 100644 --- a/src/client/spark_sdk/samples/OrderSample.cs +++ b/src/client/spark_sdk/samples/OrderSample.cs @@ -25,19 +25,20 @@ namespace bts.sdk readonly List _disps = new List(); IReadOnlyDictionary _securities = new Dictionary(0); - readonly Lazy _orders; - readonly Lazy _admin; + readonly OrderService _orders; + readonly AdminService _admin; public OrderSample( - ServiceContext serviceContext, OrdersClient orders_client, + OrderService orders, + AdminService admin, MarketDataClient md_client, IObservable> securities_src ) { _orders_client = orders_client; _md = md_client; - _orders = new(() => serviceContext.locate(), true); - _admin = new(() => serviceContext.locate(), true); + _orders = orders; + _admin = admin; _disps.Add(securities_src.Subscribe(secs => _securities = secs)); } @@ -76,16 +77,16 @@ namespace bts.sdk return null; } cr.NewOrder.SecurityKey = sec.Value.Id; - Response resp = _orders.Value.create_order(cr); + Response resp = _orders.create_order(cr); print_update(resp); //subscribei_ subscribes to a stream of Immutable (in this case FillImmutable) which is nice if you know you don't want to change the fills, which you dont' - _disps.Add(_orders.Value.subscribei_fills().Subscribe(x => + _disps.Add(_orders.subscribei_fills().Subscribe(x => { foreach(var xx in x.added) print_update(xx); })); - _disps.Add(_orders.Value.subscribei_orders().Subscribe(x => + _disps.Add(_orders.subscribei_orders().Subscribe(x => { foreach (var xx in x.removed) print_update(xx); @@ -96,7 +97,7 @@ namespace bts.sdk } void order_setup(Create cr, string username, string accountname) => - order_setup(cr, username, accountname, _admin.Value.get_admin_data(new VoidMessage())); + order_setup(cr, username, accountname, _admin.get_admin_data(new VoidMessage())); static void order_setup(Create cr, string username, string accountname, AdminData admin) { diff --git a/src/client/spark_sdk/samples/SDKMain.cs b/src/client/spark_sdk/samples/SDKMain.cs deleted file mode 100644 index 89649a50d..000000000 --- a/src/client/spark_sdk/samples/SDKMain.cs +++ /dev/null @@ -1,168 +0,0 @@ -namespace bts.sdk { - using bts.api.admin; - using bts.api.core; - using bts.api.orderman; - using bts.api.position; - using bts.co2; - using bts.md; - - using Spark.Api.Admin; - using Spark.Api.Orders; - using Spark.Api.Reactive; - - using System; - using System.Linq; - using System.Reactive.Disposables; - using System.Reactive.Linq; - - public class SDKMain - { - private static void user_test(ServiceContext ctx) - { - var s = ctx.locate(); - var r = new api.user.GetUsersRequest(); - var resp = s.get_users(r); - Console.WriteLine("{0}", resp); - } - public static void Main(string[] args) - { - BTS.init(); - Log.Sdk.info("Welcome to the BTS Client Side SDK example"); - var ctx = new ServiceContext(); - - string u = args.FirstOrDefault(x => x.Contains("--user=")); - string p = args.FirstOrDefault(x => x.Contains("--pass=")); - u = u?.Replace("--user=", string.Empty) ?? string.Empty; - p = p?.Replace("--pass=", string.Empty) ?? string.Empty; - - var lr = ctx.login(u,p).Result; - if (!lr.success) - { - Console.WriteLine("Login failure: " + lr.reason); - help(); - } - - if (args.FirstOrDefault() == "users") - { - user_test(ctx); - } - else - { - uint ms = 100; - string pu = args.FirstOrDefault(x => x.Contains("--pump=")); - if (pu != null) - ms = uint.Parse(p.Replace("--pump=", string.Empty)); - - var order_service = ctx.locate(); - var position_service = ctx.locate(); - var admin_service = ctx.locate(); - var md_relay = ctx.locate(); - var md_source = new MDSource(TimeSpan.FromSeconds(0.1)); - var md_client = new MarketDataClient(md_source); - var admin_client = new AdminClient(admin_service, UserTuple.admin); - var sec_resolver = new SecurityResolver(md_relay, md_source.subscribe_securities()); - var orders_client = new OrdersClient( - order_service, position_service, admin_client, sec_resolver); - - using(var disp = new CompositeDisposable(md_source)) { - if (args.Length > 0 && args.FirstOrDefault().Contains("md")) - { - var s = new MarketDataSample(md_client); - if (args.FirstOrDefault() == "md") - { - if (args.Length >= 5) - { - s.print_security(args[1], args[2], args[3], args[4]); - } - else if (args.Length >= 4) - { - s.print_product(args[1], args[2], args[3]); - } - else - md_help(); - } - else - md_help(); - } - else if (args.FirstOrDefault() == "order") - { - var os = new OrderSample( - ctx, - orders_client, - md_client, - md_source.subscribe_securities().Select(sec_map => - sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security))); - disp.Add(os); - order_sample(os, u, args); - } - else - help(); - - wait_for_stop(); - } - } - } - - static void order_sample(OrderSample os, string user, string[] args) - { - if (args.Length >= 10) //--user and --pass are in args[] - { - os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6], args[7]); - } - else if (args.Length >= 9) - { - os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6]); - } - else - order_help(); - } - - static void wait_for_stop() - { - Console.WriteLine("Hit q to stop"); - ConsoleKeyInfo c; - do - { - c = Console.ReadKey(); - Console.WriteLine(); - } while (c.KeyChar != 'q'); - } - - static void help() - { - Console.WriteLine("BTS Spark SDK"); - Console.WriteLine("Usages: spark_sdk [command] [arguments]"); - Console.WriteLine(); - Console.WriteLine("Commands:"); - Console.WriteLine(" md []"); - Console.WriteLine(" order []"); - Console.WriteLine("Arguments:"); - Console.WriteLine(" --user="); - Console.WriteLine(" --pass="); - Console.WriteLine(" --pump="); - Environment.Exit(1); - } - - static void order_help() - { - Console.WriteLine("order [] [] []"); - Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order"); - Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0"); - Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0"); - Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)"); - Environment.Exit(1); - } - - static void md_help() - { - Console.WriteLine("md "); - Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console."); - Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print"); - Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password"); - Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password"); - Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password"); - Environment.Exit(1); - } - - } -} diff --git a/src/client/spark_sdk/samples/SdkSampleConfig.cs b/src/client/spark_sdk/samples/SdkSampleConfig.cs new file mode 100644 index 000000000..2be018362 --- /dev/null +++ b/src/client/spark_sdk/samples/SdkSampleConfig.cs @@ -0,0 +1,29 @@ +#nullable enable +namespace Spark.Sdk; + +using bts.api; + +using Spark.Api.Services; + +public class SdkSampleConfig { + public SparkApiCredentials? Credentials { get; set; } + public SdkMarketDataSampleConfig? MarketData { get; set; } + public SdkOrderSampleConfig? Order { get; set; } +} + +public class SdkMarketDataSampleConfig { + public string? Exchange { get; set; } + public string? SecurityType { get; set; } + public string? Symbol { get; set; } + public string? SecurityDesc { get; set; } +} + +public class SdkOrderSampleConfig { + public string? Exchange { get; set; } + public string? SecurityType { get; set; } + public string? Symbol { get; set; } + public string? SecurityDesc { get; set; } + public string? Account { get; set; } + public string? Quantity { get; set; } + public string? Price { get; set; } +} diff --git a/src/client/spark_sdk/samples/SdkSampleService.cs b/src/client/spark_sdk/samples/SdkSampleService.cs new file mode 100644 index 000000000..0c036bb11 --- /dev/null +++ b/src/client/spark_sdk/samples/SdkSampleService.cs @@ -0,0 +1,176 @@ + +namespace bts.sdk { + using bts.api.admin; + using bts.api.core; + using bts.api.orderman; + using bts.api.position; + using bts.co2; + using bts.md; + + using Microsoft.Extensions.Hosting; + using Microsoft.Extensions.Options; + + using Spark.Api.Admin; + using Spark.Api.Orders; + using Spark.Api.Reactive; + using Spark.Api.Services; + using Spark.Core.Services; + using Spark.Sdk; + + using System; + using System.Linq; + using System.Reactive.Disposables; + using System.Reactive.Linq; + using System.Threading; + using System.Threading.Tasks; + + public class SdkSampleService : IHostedService { + readonly ISparkConfigLoader sparkConfigLoader; + readonly SdkSampleConfig sdkConfig; + readonly Spark.Api.IServiceProvider sparkServiceProvider; + readonly IUserDataPermissions userDataPermissions; + readonly ISparkdLoginService loginService; + + OrderService orderService; + OrdersClient ordersClient; + PositionService positionService; + AdminService adminService; + AdminClient adminClient; + MarketDataRelay mdRelay; + + public SdkSampleService( + ISparkConfigLoader sparkConfigLoader, + ISparkdLoginService loginService, + IUserDataPermissions userDataPermissions, + Spark.Api.IServiceProvider sparkServiceProvider, + OrderService orderService, + OrdersClient ordersClient, + PositionService positionService, + AdminService adminService, + AdminClient adminClient, + MarketDataRelay mdRelay, + IOptions sdkConfig) + : this(sparkConfigLoader, loginService, userDataPermissions, sparkServiceProvider, orderService, ordersClient, positionService, adminService, adminClient, mdRelay, sdkConfig.Value) { } + + public SdkSampleService( + ISparkConfigLoader sparkConfigLoader, + ISparkdLoginService loginService, + IUserDataPermissions userDataPermissions, + Spark.Api.IServiceProvider sparkServiceProvider, + OrderService orderService, + OrdersClient ordersClient, + PositionService positionService, + AdminService adminService, + AdminClient adminClient, + MarketDataRelay mdRelay, + SdkSampleConfig sdkConfig) { + this.sparkConfigLoader = sparkConfigLoader; + this.loginService = loginService; + this.sparkServiceProvider = sparkServiceProvider; + this.userDataPermissions = userDataPermissions; + this.orderService = orderService; + this.ordersClient = ordersClient; + this.positionService = positionService; + this.adminService = adminService; + this.adminClient = adminClient; + this.mdRelay = mdRelay; + this.sdkConfig = sdkConfig; + } + + UserTuple user { + get { + var loginTask = loginService.LoginAsync(); + if (!loginTask.IsCompletedSuccessfully) { + return loginTask.AsTask().GetAwaiter().GetResult().user.Value; + } + return loginTask.Result.user.Value; + } + } + + private static void user_test(ServiceContext ctx) { + var s = ctx.locate(); + var r = new api.user.GetUsersRequest(); + var resp = s.get_users(r); + Console.WriteLine("{0}", resp); + } + + public async Task StartAsync(CancellationToken cancellationToken) { + BTS.init(); + Log.Sdk.info("Welcome to the BTS Client Side SDK example"); + + await loginService.LoginAsync(); + var md_source = new MDSource(userDataPermissions); + var md_client = new MarketDataClient(md_source); + using (var disp = new CompositeDisposable(md_source)) { + if (sdkConfig.MarketData is not null) { + var s = new MarketDataSample(md_client); + var mdCfg = sdkConfig.MarketData; + s.print_security(mdCfg.Exchange, mdCfg.SecurityDesc, mdCfg.Symbol, mdCfg.SecurityDesc); + } else if (sdkConfig.Order is not null) { + var os = new OrderSample( + ordersClient, + orderService, + adminService, + md_client, + md_source.subscribe_securities().Select(sec_map => + sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security))); + disp.Add(os); + order_sample(os, user.name, sdkConfig.Order); + } else + help(); + + wait_for_stop(); + } + } + + public Task StopAsync(CancellationToken cancellationToken) => + Task.CompletedTask; + + static void order_sample(OrderSample os, string user, SdkOrderSampleConfig cfg) { + os.send_order(user, cfg.Account, cfg.Exchange, cfg.SecurityType, cfg.Symbol, cfg.SecurityDesc, cfg.Quantity, cfg.Price); + } + + static void wait_for_stop() { + Console.WriteLine("Hit q to stop"); + ConsoleKeyInfo c; + do { + c = Console.ReadKey(); + Console.WriteLine(); + } while (c.KeyChar != 'q'); + } + + static void help() { + Console.WriteLine("BTS Spark SDK"); + Console.WriteLine("Usages: spark_sdk [command] [arguments]"); + Console.WriteLine(); + Console.WriteLine("Commands:"); + Console.WriteLine(" md []"); + Console.WriteLine(" order []"); + Console.WriteLine("Arguments:"); + Console.WriteLine(" --user="); + Console.WriteLine(" --pass="); + Console.WriteLine(" --pump="); + Environment.Exit(1); + } + + static void order_help() { + Console.WriteLine("order [] [] []"); + Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order"); + Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0"); + Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0"); + Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)"); + Environment.Exit(1); + } + + static void md_help() { + Console.WriteLine("md "); + Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console."); + Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print"); + Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password"); + Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password"); + Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password"); + Environment.Exit(1); + } + + } +} diff --git a/src/client/spark_test/System/LimitsTests.cs b/src/client/spark_test/System/LimitsTests.cs index 3eb371776..dc647a535 100644 --- a/src/client/spark_test/System/LimitsTests.cs +++ b/src/client/spark_test/System/LimitsTests.cs @@ -16,27 +16,30 @@ using bts.co2; using bts.tests.utils; using Spark.Core.Disposables; +using Spark.Api.Services; namespace bts.tests { [TestGroup(TestType = TestType.System)] public class LimitsTests : IDisposable { - ServiceContext _context; OrderService _order_service; AdminService _admin_service; + ISparkdLoginService loginService; + MarketDataRelay mdRelay; IDisposable _order_subscription; Update _orders; - public LimitsTests (){} + + public LimitsTests(OrderService orderService, AdminService adminService, ISparkdLoginService loginService, MarketDataRelay mdRelay) { + _order_service = orderService; + _admin_service = adminService; + this.loginService = loginService; + this.mdRelay = mdRelay; + } private async Task setup_limits_tests() { await TestUtils.wait_for_system_ready(); - _context = new ServiceContext(); - _order_service = _context.locate(); - _admin_service = _context.locate(); - - var auth = _context.login("bts",""); - auth.Wait(3000); + loginService.LoginAsync().AsTask().Wait(3000); _order_subscription = _order_service.subscribei_orders().Subscribe(ords => { @@ -46,9 +49,6 @@ namespace bts.tests } public void Dispose() { - Disposable.dispose(ref _order_service); - Disposable.dispose(ref _admin_service); - Disposable.dispose(ref _context); Disposable.dispose(ref _order_subscription); } @@ -68,9 +68,8 @@ namespace bts.tests } }; - var relay = _context.locate(); await Task.Delay(100); - var result = await relay.requestmd_async(request); + var result = await mdRelay.requestmd_async(request); List securities = new List(); if (result.success) { diff --git a/src/client/spark_websocket/Core/WebsocketDataSync.cs b/src/client/spark_websocket/Core/WebsocketDataSync.cs index 5d1d2951f..e5010dbf9 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -1,21 +1,23 @@ namespace bts.websocket_core { + using bts.api.admin; + using bts.api.core; + using bts.api.orderman; + using bts.api.position; + using bts.api.synthetic; using bts.api; + using bts.co2; using bts.md; using mbts; + using Newtonsoft.Json; - using System.Collections.Concurrent; - using bts.api.core; - using bts.api.orderman; - using bts.api.admin; - using bts.co2; - using Spark.Api.Reactive; - using Spark.Api.Orders; + using Spark.Api.Admin; using Spark.Api.MarketData; - using bts.api.position; - using bts.api.synthetic; - + using Spark.Api.Orders; + using Spark.Api.Reactive; + using Spark.Api.Services; + using System.Collections.Concurrent; /// /// This does all the bookkeeping for connections and data, and passes @@ -24,9 +26,11 @@ namespace bts.websocket_core { public class WebsocketDataSync : IWebsocketDataSync { readonly AdminService adminService; readonly ServiceContext _service_ctx; + readonly IUserDataPermissions userDataPermissions; - public WebsocketDataSync(ServiceContext service_ctx) { + public WebsocketDataSync(ServiceContext service_ctx, IUserDataPermissions userDataPermissions) { _service_ctx = service_ctx; + this.userDataPermissions = userDataPermissions; adminService = service_ctx.locate(); } @@ -193,8 +197,7 @@ namespace bts.websocket_core { public async Task initAsync(WebsocketConnectionGateway gtwy) { Gateway = gtwy; clients = new ClientsStore(); - TimeSpan md_throttle = TimeSpan.FromSeconds(0.1); - MD_Source = new MDSource(md_throttle); + MD_Source = new MDSource(userDataPermissions); Md_Client = new(MD_Source); _securities_sub = MD_Source.subscribe_securities().Subscribe(sec_map => _securities = sec_map); var order_service = _service_ctx.locate();