372 lines
15 KiB
C#
372 lines
15 KiB
C#
namespace bts.websocket_core {
|
|
using bts.api;
|
|
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;
|
|
|
|
|
|
/// <summary>
|
|
/// 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
|
|
/// </summary>
|
|
public class WebsocketDataSync : IWebsocketDataSync {
|
|
readonly ServiceContext _service_ctx;
|
|
|
|
public WebsocketDataSync(ServiceContext service_ctx) =>
|
|
_service_ctx = service_ctx;
|
|
|
|
/// <summary>
|
|
/// class to manage thread safe client store, will change this to blocking collection
|
|
/// </summary>
|
|
class ClientsStore {
|
|
private ConcurrentDictionary<WebsocketClientID, ClientState> Clients { get; } = new();
|
|
/// <summary>
|
|
/// adds client to dictionary is thread safe
|
|
/// </summary>
|
|
/// <param name="client_id"></param>
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// removes client from dictionary is thread safe
|
|
/// </summary>
|
|
/// <param name="client_id"></param>
|
|
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<KeyValuePair<WebsocketClientID, ClientState>> 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;
|
|
}
|
|
/// <summary>
|
|
/// Immutable to store the state of the client
|
|
/// </summary>
|
|
public readonly struct ClientState : IDisposable {
|
|
|
|
private readonly Dictionary<IUniqueID, IWebsocketRequestHandler?> _requests = new();
|
|
public ClientState() {
|
|
|
|
}
|
|
public ClientState(Dictionary<IUniqueID, IWebsocketRequestHandler?> requests) {
|
|
_requests = new Dictionary<IUniqueID, IWebsocketRequestHandler?>(requests);
|
|
}
|
|
public ClientState Add(IUniqueID id, IWebsocketRequestHandler? dis) {
|
|
var newIds = new Dictionary<IUniqueID, IWebsocketRequestHandler?>(_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<IUniqueID, IWebsocketRequestHandler?>(_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<ServiceContext>(service_ctx);
|
|
// set {
|
|
// Log.isnull(service_ctx);
|
|
// service_ctx = value;
|
|
// }
|
|
// }
|
|
|
|
MarketDataClient? mdClient;
|
|
MarketDataClient Md_Client {
|
|
get => Log.notnull<MarketDataClient>(mdClient);
|
|
set {
|
|
Log.isnull(mdClient);
|
|
mdClient = value;
|
|
}
|
|
}
|
|
MDSource? _md_source;
|
|
MDSource MD_Source {
|
|
get => Log.notnull<MDSource>(_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<OrdersClient>(_orders_client);
|
|
set {
|
|
Log.isnull<OrdersClient>(_orders_client);
|
|
_orders_client = value;
|
|
}
|
|
}
|
|
|
|
IReadOnlyDictionary<string, DynamicOrderDescriptor>? _dynamic_descriptors;
|
|
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors{
|
|
get => Log.notnull<IReadOnlyDictionary<string, DynamicOrderDescriptor>>(_dynamic_descriptors);
|
|
set {
|
|
Log.isnull<IReadOnlyDictionary<string, DynamicOrderDescriptor>>(_dynamic_descriptors);
|
|
_dynamic_descriptors=value.ToDictionary(k=>k.Key.ToLower(),v => v.Value);
|
|
}
|
|
}
|
|
WebsocketConnectionGateway? gateway;
|
|
WebsocketConnectionGateway Gateway {
|
|
get => Log.notnull<WebsocketConnectionGateway>(gateway);
|
|
set {
|
|
Log.isnull(gateway);
|
|
gateway = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// stores the current connected clients, can be converted the concurrent dictionary
|
|
/// </summary>
|
|
private ClientsStore? clients = null;
|
|
private ClientsStore Clients {
|
|
get => Log.notnull<ClientsStore>(clients);
|
|
set {
|
|
Log.isnull(clients);
|
|
clients = value;
|
|
}
|
|
}
|
|
|
|
IReadOnlyDictionary<uint, SecurityMD> _securities = new Dictionary<uint, SecurityMD>();
|
|
IDisposable? _securities_sub;
|
|
|
|
/// <summary>
|
|
/// Should be called in the start, stores pointer for gtwy and creates dictionary for clients, starts broadcast thread
|
|
/// </summary>
|
|
/// <param name="gtwy"></param>
|
|
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_Client = new(MD_Source);
|
|
_securities_sub = MD_Source.subscribe_securities().Subscribe(sec_map => _securities = sec_map);
|
|
var order_service = _service_ctx.locate<OrderService>();
|
|
var position_service = _service_ctx.locate<PositionService>();
|
|
var admin_service = _service_ctx.locate<AdminService>();
|
|
var synth_sec_service = _service_ctx.locate<SyntheticSecurityService>();
|
|
var admin_client = new AdminClient(admin_service, UserTuple.admin);
|
|
var md_relay = _service_ctx.locate<MarketDataRelay>();
|
|
var sec_resolver = new SecurityResolver(md_relay, MD_Source.subscribe_securities());
|
|
Orders_Client = new OrdersClient(
|
|
order_service, position_service, admin_client, sec_resolver, TimeSpan.FromSeconds(0.1));
|
|
var sec_table_src = MD_Source.subscribe_securities()
|
|
.Table(s => s.security.Id)
|
|
.Select(s => s.security, s => s.Id);
|
|
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();
|
|
}
|
|
private uint get_security(Venue v, string sec_name) {
|
|
var securites = _securities;
|
|
if (securites.Values.TryGetFirst(sc_md =>
|
|
(
|
|
sc_md.security.Exchange == v &&
|
|
sc_md.security.ToString() == sec_name),
|
|
out SecurityMD sc_md
|
|
)) {
|
|
return sc_md.id;
|
|
}
|
|
throw new ArgumentException($"{sec_name} at {v} not found in securites");
|
|
}
|
|
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<WebsocketRequest, OrdersClient, Task<(bool success, uint? id, string status)>> 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, _securities));}
|
|
public async void handle_enter_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.EnterOrderAsync(
|
|
req, Orders_Client, _securities,Dynamic_Descriptors
|
|
));}
|
|
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,
|
|
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<WebsocketOutgoingMessage>() {
|
|
new WebsocketOutgoingMessage(id, requestJson)
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
}
|