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/Daemons/ShellServices.cs b/src/client/spark/Daemons/ShellServices.cs index 330b8b99a..61763c6f8 100644 --- a/src/client/spark/Daemons/ShellServices.cs +++ b/src/client/spark/Daemons/ShellServices.cs @@ -12,12 +12,14 @@ using bts.md; using bts.metrics; using bts.support; +using Spark.Core.Services; + using System.Composition; namespace Spark { class ShellServices { public ShellServices([Import("config_source")]string config_source) => - context = new(config_source); + context = new(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = config_source })); [Export] public ServiceContext context { get; } 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 30b413e47..9733c7401 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; @@ -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/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index e8d0290ee..8f38f0c21 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -18,6 +18,9 @@ namespace bts.api.core { AuthToken? _token; + public ServiceContext() + : this(new SparkConfigLoader(SparkConfigLoaderOpts.Default())) { } + public ServiceContext(ISparkConfigLoader configLoader) { quickbusBacking = new(QuickbusFactory, lockObj); @@ -50,6 +53,9 @@ namespace bts.api.core { internal IntPtr native_context => quickbusBacking?.Value?.Handle ?? throw new ObjectDisposedException(nameof(ServiceContext)); + 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) .FirstOrDefault(ci => ci.GetParameters() @@ -88,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 }; } } } @@ -134,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 9af427a31..d79e8d7d7 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; @@ -38,11 +40,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/Services/SparkConfigLoader.cs b/src/client/spark_core/Services/SparkConfigLoader.cs index 1a0fb9aa4..64ae890c6 100644 --- a/src/client/spark_core/Services/SparkConfigLoader.cs +++ b/src/client/spark_core/Services/SparkConfigLoader.cs @@ -13,7 +13,7 @@ using System.Threading.Tasks; /// /// Initializes Spark protobuf config. /// -class SparkConfigLoader : ISparkConfigLoader { +public class SparkConfigLoader : ISparkConfigLoader { readonly SparkConfigLoaderOpts config; public SparkConfigLoader(IOptions config) diff --git a/src/client/spark_core/Services/SparkCoreServices.cs b/src/client/spark_core/Services/SparkCoreServices.cs index 9cbac82a7..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(SparkConfigLoader))) { + 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()); + .AddSingleton() + .AddHostedService(); } } 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 3def9b1b5..e2067ba70 100644 --- a/src/client/spark_core/config/Configuration.cs +++ b/src/client/spark_core/config/Configuration.cs @@ -110,6 +110,35 @@ namespace mbts.config { } } + 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; + } + } + public static void initialize_for_test(string config = "") { _raw_initialize_for_test(config); // still need init_spark_lib 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 3b97c8119..4cb7a4d45 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -15,6 +15,7 @@ namespace bts.websocket_core { using Spark.Api.MarketData; using bts.api.position; using bts.api.synthetic; + using Spark.Api.Services; /// @@ -24,9 +25,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 +196,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();