fix build: Fix Spark build after making config init async

This commit is contained in:
John Colvin committed 2024-10-30 19:31:08 +00:00
1 parent 49e4e05a3c
commit 2cf8a3963d
28 files changed
+509 -238

No files matched your search

@@ -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<string>();
var excluded_keys = opts.excluded_keys?.ToArray() ?? Array.Empty<string>();
return Configuration.run_config_query(
@@ -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<TestEnumerator> _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<int> 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);
+2 -1
View File
@@ -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;
+3 -1
View File
@@ -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; }
@@ -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);
@@ -119,7 +119,7 @@ namespace mbts.ui.shell {
public async Task<bool> 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) {
+9 -5
View File
@@ -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<ConcurrentDictionary<SecurityKey, SecurityMD>> _securities_src;
readonly Co2RelayControl _relay;
readonly UserTuple _user;
readonly List<IDisposable> _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 {
@@ -0,0 +1,9 @@
namespace Spark.Api.Services;
using bts.api.core;
using System.Threading.Tasks;
public interface ISparkdLoginService {
ValueTask<LoginResult> LoginAsync();
}
@@ -0,0 +1,5 @@
namespace Spark.Api.Services;
public interface IUserDataPermissions {
bool Allow(uint userId);
}
@@ -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;
@@ -30,15 +32,18 @@ public static class SparkApiServices {
Console.WriteLine("SparkApi services have been disabled");
return services;
}
if (!services.Any(s => s.ServiceType == typeof(SparkService))) {
/// <see cref="SparkService"/> initializes configuration and logging (this should be done
if (!services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) {
/// <see cref="SparkConfigLoader"/> initializes configuration and logging (this should be done
/// before any api-related services run).
throw new InvalidOperationException("Must add SparkService first");
}
services
.Configure<SparkApiCredentials>(configurationSection.GetSection("Credentials"))
.AddSingleton<ServiceContext>()
.AddHostedService<SparkdLoginService>()
.AddSingleton<Api.IServiceProvider, ServiceContext>(sp => sp.GetRequiredService<ServiceContext>())
.AddSingleton<SparkdLoginService>()
.AddSingleton<ISparkdLoginService, SparkdLoginService>(sp => sp.GetRequiredService<SparkdLoginService>())
.AddHostedService(sp => sp.GetRequiredService<SparkdLoginService>())
.AddSparkdService<SecurityResolver>();
if (configurationSection.GetValue("EnableOrders", true)) {
services.AddSparkdService<OrderService>()
@@ -47,6 +52,12 @@ public static class SparkApiServices {
if (configurationSection.GetValue("EnablePosition", true)) {
services.AddSparkdService<PositionService>();
}
if (configurationSection.GetValue("EnableMarketData", true)) {
services.AddSparkdService<MarketDataRelay>()
.AddSingleton<IUserDataPermissions, UserDataPermissions>()
.AddSingleton<MDSource>()
.AddSparkdService<MarketDataClient>();
}
if (configurationSection.GetValue("EnableAdmin", true)) {
services.AddSparkdService<AdminService>();
services.AddSparkdService<AdminClient>();
@@ -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;
/// <summary>
///
/// An <see cref="IHostedService"/> that logs into Sparkd. Login is only attemped once. After that,
/// subsequent calls to <see cref="LoginAsync"/> simply return the result of the initial attempt.
/// </summary>
public class SparkdLoginService : IHostedService {
public class SparkdLoginService : IHostedService, ISparkdLoginService {
readonly ServiceContext serviceContext;
readonly SparkApiCredentials config;
readonly object lockObj = new();
LoginResult? loginResult;
Task<LoginResult>? loginTask;
public SparkdLoginService(ServiceContext serviceContext, IOptions<SparkApiCredentials> config) {
public SparkdLoginService(ServiceContext serviceContext, IOptions<SparkApiCredentials> config)
: this(serviceContext, config.Value) { }
public SparkdLoginService(ServiceContext serviceContext, SparkApiCredentials config) {
this.serviceContext = serviceContext;
this.config = config.Value;
this.config = config;
}
public ValueTask<LoginResult> LoginAsync() {
if (loginResult is not null) {
return ValueTask.FromResult(loginResult);
}
if (string.IsNullOrEmpty(config.User)) {
return ValueTask.FromException<LoginResult>(new InvalidOperationException("User is null/empty"));
}
lock (lockObj) {
if (loginResult is not null) {
return ValueTask.FromResult(loginResult);
}
loginTask ??= LoginImplAsync();
return new(loginTask);
}
async Task<LoginResult> 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;
@@ -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();
}
}
}
}
+12 -8
View File
@@ -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<T>(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; }
}
}
+7 -3
View File
@@ -6,6 +6,8 @@
using CommandLine;
using Spark.Api.Server;
using Spark.Api.Services;
using Spark.Core.Services;
using System;
using System.Reactive.Linq;
@@ -30,10 +32,12 @@
Console.WriteLine("Starting spark_api_server... ");
Log.notnull(opts.user);
Log.notnull(opts.password);
var service_ctx = ServiceContext.create(opts.sparkd);
await service_ctx.login(opts.user, opts.password);
var service_ctx = new ServiceContext(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = opts.sparkd } ));
var loginService = new SparkdLoginService(service_ctx, new SparkApiCredentials() { User = opts.user, Password = opts.password });
await loginService.LoginAsync();
var userDataPermissions = new UserDataPermissions(loginService);
var sync = new bts.websocket_core.WebsocketDataSync(service_ctx);
var sync = new bts.websocket_core.WebsocketDataSync(service_ctx, userDataPermissions);
var gwy = new bts.websocket_core.WebsocketConnectionGateway();
var prms = new WebsocketServerConfig {
DefaultEndPoint = "/",
@@ -13,7 +13,7 @@ using System.Threading.Tasks;
/// <summary>
/// Initializes Spark protobuf config.
/// </summary>
class SparkConfigLoader : ISparkConfigLoader {
public class SparkConfigLoader : ISparkConfigLoader {
readonly SparkConfigLoaderOpts config;
public SparkConfigLoader(IOptions<SparkConfigLoaderOpts> config)
@@ -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<SparkConfigLoaderOpts>(configurationSection)
.AddSingleton<SparkConfigLoader>()
.AddHostedService(sp => sp.GetRequiredService<SparkConfigLoaderService>());
.AddSingleton<ISparkConfigLoader, SparkConfigLoader>()
.AddHostedService<SparkConfigLoaderService>();
}
}
@@ -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;
/// <summary>
/// Initializes Spark protobuf config.
/// </summary>
class SparkTestConfigLoader : ISparkConfigLoader {
readonly SparkTestConfigLoaderOpts config;
public SparkTestConfigLoader(IOptions<SparkTestConfigLoaderOpts> 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));
}
@@ -0,0 +1,8 @@
#nullable enable
namespace Spark.Core.Services;
public class SparkTestConfigLoaderOpts {
public bool IsUnitTest { get; set; }
public static SparkTestConfigLoaderOpts Default() => new();
}
@@ -110,6 +110,35 @@ namespace mbts.config {
}
}
public static Task<bool> ResetForTestAsync() =>
ResetForTestAsync(string.Empty, CancellationToken.None);
public static Task<bool> ResetForTestAsync(string config) =>
ResetForTestAsync(config, CancellationToken.None);
public static Task<bool> ResetForTestAsync(CancellationToken cancellationToken) =>
ResetForTestAsync(string.Empty, cancellationToken);
public static Task<bool> 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
+1 -1
View File
@@ -54,7 +54,7 @@ namespace bts {
}
[return: NotNull]
public static T notnull<T>([NotNull] T? obj, string msg = "") where T : class {
public static T notnull<T>([NotNull] T? obj, string msg = "") {
if (obj is null) {
assert(false, msg);
throw new InvalidOperationException("Assertion failed: expected obj to be non-null");
@@ -0,0 +1,11 @@
{
"Spark": {
"ConfigServer": ""
},
"SparkApi": {
"Credentials": {
"User": "bts",
"Password": ""
}
}
}
+40
View File
@@ -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<SdkSampleService>();
}
+10 -9
View File
@@ -25,19 +25,20 @@ namespace bts.sdk
readonly List<IDisposable> _disps = new List<IDisposable>();
IReadOnlyDictionary<uint, SecurityImmutable> _securities =
new Dictionary<uint, SecurityImmutable>(0);
readonly Lazy<OrderService> _orders;
readonly Lazy<AdminService> _admin;
readonly OrderService _orders;
readonly AdminService _admin;
public OrderSample(
ServiceContext serviceContext,
OrdersClient orders_client,
OrderService orders,
AdminService admin,
MarketDataClient md_client,
IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> securities_src
) {
_orders_client = orders_client;
_md = md_client;
_orders = new(() => serviceContext.locate<OrderService>(), true);
_admin = new(() => serviceContext.locate<AdminService>(), 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)
{
-168
View File
@@ -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<api.user.AuthService>();
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<OrderService>();
var position_service = ctx.locate<PositionService>();
var admin_service = ctx.locate<AdminService>();
var md_relay = ctx.locate<MarketDataRelay>();
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 <exchange> <security_type> <symbol> [<security_desc>]");
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
Console.WriteLine("Arguments:");
Console.WriteLine(" --user=<bts user name>");
Console.WriteLine(" --pass=<password>");
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
Environment.Exit(1);
}
static void order_help()
{
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
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 <exchange> <security_type> <symbol>");
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);
}
}
}
@@ -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; }
}
@@ -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<SdkSampleConfig> 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<api.user.AuthService>();
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 <exchange> <security_type> <symbol> [<security_desc>]");
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
Console.WriteLine("Arguments:");
Console.WriteLine(" --user=<bts user name>");
Console.WriteLine(" --pass=<password>");
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
Environment.Exit(1);
}
static void order_help() {
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
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 <exchange> <security_type> <symbol>");
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);
}
}
}
+12 -13
View File
@@ -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<OrderRecordImmutable, uint> _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<OrderService>();
_admin_service = _context.locate<AdminService>();
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<MarketDataRelay>();
await Task.Delay(100);
var result = await relay.requestmd_async(request);
var result = await mdRelay.requestmd_async(request);
List<SecurityImmutable> securities = new List<SecurityImmutable>();
if (result.success) {
@@ -15,6 +15,7 @@ namespace bts.websocket_core {
using Spark.Api.MarketData;
using bts.api.position;
using bts.api.synthetic;
using Spark.Api.Services;
/// <summary>
@@ -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<AdminService>();
}
@@ -196,8 +199,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<OrderService>();