namespace bts.websocket_core { using bts.api; using bts.md; using Newtonsoft.Json; using System.Collections.Concurrent; using bts.api.orderman; using bts.api.admin; using bts.co2; using Spark.Api.Orders; using Spark.Api.Admin; using Spark.Api.MarketData; using bts.api.position; using bts.api.synthetic; using Bts.DataStreamCore; using System.Diagnostics.CodeAnalysis; /// /// This does all the bookkeeping for connections and data, and passes /// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY /// public class WebsocketDataSync : IWebsocketDataSync { readonly AdminService adminService; readonly Spark.Api.IServiceProvider _service_ctx; readonly ISecurityResolver securityResolver; readonly IDataStreamFactory streamFactory; public WebsocketDataSync(Spark.Api.IServiceProvider service_ctx, IMarketDataClient mdClient, ISecurityResolver securityResolver, IDataStreamFactory streamFactory) { _service_ctx = service_ctx; this.securityResolver = securityResolver; this.mdClient = mdClient; this.streamFactory = streamFactory; adminService = service_ctx.GetService(); } /// /// class to manage thread safe client store, will change this to blocking collection /// class ClientsStore { private ConcurrentDictionary Clients { get; } = new(); /// /// adds client to dictionary is thread safe /// /// public void AddClient(WebsocketClientID client_id) { if (Clients.TryAdd(client_id, new ClientState())) { return; } var log_txt = $"ClientsStore.AddClient client_id: {client_id.ToString()} Already exits"; bts.Log.Gen.warn(log_txt); Clients[client_id] = new ClientState(); } /// /// removes client from dictionary is thread safe /// /// public void RemoveClient(WebsocketClientID client_id) { if (Clients.TryRemove(client_id, out ClientState state)) { state.Dispose(); } } public bool ContainsKey(WebsocketClientID client_id) => Clients.ContainsKey(client_id); public IEnumerator> GetEnumerator() => Clients.GetEnumerator(); public ClientState this[WebsocketClientID clientId] { get => Clients.TryGetValue( clientId, out var clientState) ? clientState : throw new ArgumentException($"{nameof(clientId)} not found in the Clients dictionary"); set => Clients.AddOrUpdate( clientId, value, (key, old_value) => value); } public int Count() => Clients.Count; } /// /// Immutable to store the state of the client /// public readonly struct ClientState : IDisposable { private readonly Dictionary _requests = new(); public ClientState() { } public ClientState(Dictionary requests) { _requests = new Dictionary(requests); } public ClientState Add(IUniqueID id, IWebsocketRequestHandler? dis) { var newIds = new Dictionary(_requests); newIds.TryAdd(id, dis); return new ClientState(newIds); } public void Dispose() { foreach (var dis in _requests.Values) { if (dis != null) { dis.Dispose(); } } } public bool ContainsKey(IUniqueID id) => _requests.ContainsKey(id); public ClientState Remove(IUniqueID id) { var newIds = new Dictionary(_requests); newIds.TryPopValue(id, out IWebsocketRequestHandler? dis); if (dis != null) { dis.Dispose(); } return new ClientState(newIds); } public ClientState RemoveAll() { Dispose(); return new ClientState(); } } // ServiceContext? service_ctx; // ServiceContext Service_Ctx{ // get => Log.notnull(service_ctx); // set { // Log.isnull(service_ctx); // service_ctx = value; // } // } IMarketDataClient? mdClient; IMarketDataClient Md_Client { [MemberNotNull(nameof(mdClient))] get => Log.notnull(mdClient); set { Log.isnull(mdClient); mdClient = value; } } MDSource? _md_source; MDSource MD_Source { get => Log.notnull(_md_source); set { Log.isnull(_md_source); _md_source = value; } } SyntheticSecurityClient? _synth_sec_client; SyntheticSecurityClient Synth_Sec_Client { get => Log.notnull(_synth_sec_client); set { Log.isnull(_synth_sec_client); _synth_sec_client = value; } } OrdersClient? _orders_client; OrdersClient Orders_Client { get => Log.notnull(_orders_client); set { Log.isnull(_orders_client); _orders_client = value; } } IReadOnlyDictionary? _dynamic_descriptors; IReadOnlyDictionary Dynamic_Descriptors{ get => Log.notnull>(_dynamic_descriptors); set { Log.isnull>(_dynamic_descriptors); _dynamic_descriptors=value.ToDictionary(k=>k.Key.ToLower(),v => v.Value); } } WebsocketConnectionGateway? gateway; WebsocketConnectionGateway Gateway { get => Log.notnull(gateway); set { Log.isnull(gateway); gateway = value; } } /// /// stores the current connected clients, can be converted the concurrent dictionary /// private ClientsStore? clients = null; private ClientsStore Clients { get => Log.notnull(clients); set { Log.isnull(clients); clients = value; } } IReadOnlyDictionary _securities = new Dictionary(0); IDisposable? _securities_sub; /// /// Should be called in the start, stores pointer for gtwy and creates dictionary for clients, starts broadcast thread /// /// public async Task initAsync(WebsocketConnectionGateway gtwy) { Gateway = gtwy; clients = new ClientsStore(); _securities_sub = Md_Client.Securities().Subscribe(secTable => _securities = secTable.state); var order_service = _service_ctx.GetService(); var position_service = _service_ctx.GetService(); var admin_service = _service_ctx.GetService(); var synth_sec_service = _service_ctx.GetService(); var admin_client = new AdminClient(admin_service, UserTuple.admin); var md_relay = _service_ctx.GetService(); var sec_table_src = mdClient.Securities(); Orders_Client = new OrdersClient( order_service, position_service, admin_client, securityResolver, TimeSpan.FromSeconds(0.1)); var r = await Orders_Client.GetRegisteredDynamicOrderTypesAsync(); Dynamic_Descriptors = r .DynamicOrderDesc .ToDictionary(d => d.DynamicOrderType); Synth_Sec_Client = new(synth_sec_service, sec_table_src); // broadcast.Start(); } public void handle_connection(WebsocketClientID client_id, WebsocketIncomingMessage? optional_message) { Clients.AddClient(client_id); var log_txt = $"WebsocketDataSync.handle_connection client_id: {client_id} " + $"message: {(optional_message == null ? "null" : optional_message.ToString())}"; bts.Log.Gen.info(log_txt); System.Console.WriteLine(log_txt); if (optional_message != null) { handle_client_message(optional_message); } } // Called when a client is disconnected public void handle_disconnection(WebsocketClientID client_id) { handle_unsubscribe_all(client_id); Clients.RemoveClient(client_id); var log_txt = $"WebsocketDataSync.handle_disconnection client_id: {client_id}"; bts.Log.Gen.info(log_txt); System.Console.WriteLine(log_txt); } // Called when a client sends data public void handle_client_message(WebsocketIncomingMessage message) { WebsocketRequest? req; string error; if (!WebsocketRequest.TryGetWebSocketRequest(message.sender_client_id, message.message_body, out IUniqueID id, out req, out error)) { handle_incorrect_request(id, message.sender_client_id, error); return; } handle_request(req); var log_txt = $"WebsocketDataSync.handle_client_message message: {req.type}"; bts.Log.Gen.info(log_txt); System.Console.WriteLine(log_txt); } public void handle_request(WebsocketRequest req) { switch (req.type) { case WebsocketRequest.REQUEST_TYPES.SUBSCRIBE: { handle_subscription(req); break; } case WebsocketRequest.REQUEST_TYPES.UNSUBSCRIBE_ALL: { handle_unsubscribe_all(req.client_id); break; } case WebsocketRequest.REQUEST_TYPES.ENTER_ORDER: { handle_enter_order(req); break; } case WebsocketRequest.REQUEST_TYPES.DELETE_ORDER: { handle_delete_order(req); break; } case WebsocketRequest.REQUEST_TYPES.UPDATE_ORDER: { handle_update_order(req); break; } case WebsocketRequest.REQUEST_TYPES.SET_PRECOMPUTED_THEOS: { handle_set_theos(req); break; } case WebsocketRequest.REQUEST_TYPES.UNKOWN: { ///send error break; } default: break; } } public void handle_unsubscribe_all(WebsocketClientID client_id) { try { Clients[client_id] = Clients[client_id].RemoveAll(); } catch (ArgumentException ex) { System.Console.WriteLine("error for req", client_id, ex.ToString()); } } public async Task HandleRestRequest(WebsocketRequest req, Func> handlerAsync) { (bool success, uint? id, string status) = await handlerAsync(req, Orders_Client); if (!success){ handle_incorrect_request(req, status); return; } handle_correct_request(req.id, status, id != null ? new { order_key = id } : null, req.client_id); } public async void handle_delete_order(WebsocketRequest req) { await HandleRestRequest(req,WebsocketRestHandler.DeleteOrderAsync);} public async void handle_set_theos(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.SetTheosAsync(req, Orders_Client, getSecurityId));} public async void handle_enter_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.EnterOrderAsync( req, Orders_Client, adminService, Dynamic_Descriptors, getSecurityId ));} public async void handle_update_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.UpdateOrderAsync(req,Orders_Client,Dynamic_Descriptors));} public void handle_subscription(WebsocketRequest req) { WebsocketSubscriptionHandler? hdlr; string status; if (!WebsocketSubscriptionHandler.TryCreate( req, Md_Client, Orders_Client, streamFactory, obj => send_message(obj, req.client_id), out hdlr, out status)) { handle_incorrect_request(req, status); return; } if (Clients.ContainsKey(req.client_id) && Clients[req.client_id].ContainsKey(hdlr.ID)) { handle_incorrect_request(req, "subscription id already in use"); return; } handle_correct_request(req.id, status, new { subscription_id = hdlr.ID.ID }, req.client_id); hdlr.subscribe(); Clients[req.client_id] = Clients[req.client_id].Add(hdlr.ID, hdlr); } public void handle_incorrect_request(WebsocketRequest req, string message = "") => handle_incorrect_request(req.id, req.client_id, message); public void handle_incorrect_request(IUniqueID rid, WebsocketClientID cid, string message = "") { System.Console.WriteLine($"request {rid} from {cid} failed, message: {message}"); var ms = new { jsonrpc = 2.0, id = rid.ID, error = message }; send_message(ms, cid); } public void handle_correct_request(IUniqueID rid, string status, Object? data, WebsocketClientID cid) { System.Console.WriteLine($"request {rid} from {cid} suceeded, message: {status}"); var ms = new { jsonrpc = 2.0, id = rid.ID, status = status, result = data }; send_message(ms, cid); } public void send_message(object value, WebsocketClientID id) { var requestJson = JsonConvert.SerializeObject(value); Gateway.send_to_clients( new List() { new WebsocketOutgoingMessage(id, requestJson) }); } uint? getSecurityId(Venue venue, string? name) => _securities.Values.TryGetFirst(s => s.Exchange == venue && s.ToString() == name, out var s) ? s.Id : null; } }