230 lines
8.0 KiB
C#
230 lines
8.0 KiB
C#
namespace Bts.DataStream.Websocket;
|
|
|
|
using BCG;
|
|
|
|
using bts;
|
|
using bts.api.core;
|
|
using bts.collections;
|
|
|
|
using Bts.DataStreamExpressions;
|
|
using Bts.Websocket;
|
|
|
|
using mbts.utils;
|
|
|
|
using Spark.Core.Disposables;
|
|
|
|
using System.Reactive.Subjects;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
/// <summary>
|
|
/// Represents an I/O model (<see cref="ISubject{TSource, TResult}"/>), that accepts websocket
|
|
/// requests (from clients) as input, and emits data/notifications (to clients) as output.
|
|
/// This class's chief role is managing data subscriptions to sparkd on the clients' behalf.
|
|
/// </summary>
|
|
public class DataSync(ServiceContext serviceCtx, JsonSerializerOptions? serializerOpts = null) :
|
|
ISubject<WebsocketConnectionUpdate, IEnumerable<WebsocketOutgoingMessage>> {
|
|
readonly ServiceContext serviceCtx = serviceCtx;
|
|
readonly JsonSerializerOptions serializerOpts = serializerOpts ?? JsonSerializerOptions.Default;
|
|
readonly Dictionary<WebsocketClientID, List<ClientSubscription>> clients = [];
|
|
readonly object lockObj = new();
|
|
|
|
event Action<IEnumerable<WebsocketOutgoingMessage>>? requestSend;
|
|
|
|
public void OnCompleted() =>
|
|
Log.Api.info("DataSync incoming request stream has completed.");
|
|
|
|
public void OnError(Exception error) =>
|
|
Log.Api.error($"DataSync error: {error}");
|
|
|
|
public void OnNext(WebsocketConnectionUpdate value) {
|
|
switch (value.Type) {
|
|
case WebsocketConnectionUpdateType.IncomingMessage:
|
|
onIncomingMessage(value.IncomingMessage);
|
|
break;
|
|
case WebsocketConnectionUpdateType.Connect:
|
|
onConnect(value.Connect);
|
|
break;
|
|
case WebsocketConnectionUpdateType.Disconnect:
|
|
onDisconnect(value.Disconnect);
|
|
break;
|
|
default:
|
|
Log.assert(false, $"Unexpected WebsocketConnectionUpdate type: {value.Type}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
public IDisposable Subscribe(IObserver<IEnumerable<WebsocketOutgoingMessage>> observer) {
|
|
lock (lockObj) {
|
|
requestSend += observer.OnNext;
|
|
return Disposable.Create(dispose);
|
|
}
|
|
void dispose() {
|
|
lock (lockObj) {
|
|
requestSend -= observer.OnNext;
|
|
}
|
|
}
|
|
}
|
|
|
|
static JsonRpcId? extractRpcId(string messageBody) {
|
|
try {
|
|
return JsonNode.Parse(messageBody)
|
|
?.AsObject()
|
|
?.GetValueOrDefault("id")
|
|
?.Deserialize<JsonRpcId>();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
void onConnect(WebsocketConnectNotification notification) {
|
|
var id = notification.Id;
|
|
lock (lockObj) {
|
|
clients.Add(id, new());
|
|
}
|
|
Log.Api.info($"DataStreamWebsocketDataSync client connected. clientId: {notification.Id}");
|
|
if (notification.Message is not null) {
|
|
onIncomingMessage(notification.Message);
|
|
}
|
|
}
|
|
|
|
void onDisconnect(WebsocketDisconnectNotification notification) {
|
|
Log.Api.warn($"DataStreamWebsocketDataSync client disconnected. clientId: {notification.Id}");
|
|
unsubscribeAll(notification.Id);
|
|
clients.Remove(notification.Id);
|
|
}
|
|
|
|
void onIncomingMessage(WebsocketIncomingMessage message) {
|
|
var id = message.sender_client_id;
|
|
if (!JsonRpcRequest.TryDeserialize(message, out var req, out var err)) {
|
|
onBadRequest(message, err);
|
|
return;
|
|
}
|
|
switch (req.Method!.ToLower()) {
|
|
case "subscribe":
|
|
subscribe(id, req);
|
|
break;
|
|
case "unsubscribe":
|
|
unsubscribe(id, req);
|
|
break;
|
|
case "unsubscribe_all":
|
|
unsubscribeAll(id);
|
|
break;
|
|
default:
|
|
Log.Api.error($"Received request of unknown type: {req}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
void sendNotification<T>(WebsocketClientID id, string method, T rpcParams) {
|
|
var rpcNotification = JsonRpcRequest.CreateNotification(method, rpcParams);
|
|
var rpcNotificationStr = JsonSerializer.Serialize(rpcNotification, serializerOpts);
|
|
send(new WebsocketOutgoingMessage[] { new(id, rpcNotificationStr) });
|
|
}
|
|
|
|
void sendResponse<T>(WebsocketClientID id, JsonRpcId rpcId, T rpcParams) {
|
|
var rpcRes = JsonRpcResponse.Create(rpcId, rpcParams);
|
|
var rpcResStr = JsonSerializer.Serialize(rpcRes, serializerOpts);
|
|
send(new WebsocketOutgoingMessage[] { new(id, rpcResStr )});
|
|
}
|
|
|
|
void sendError<T>(WebsocketClientID id, JsonRpcId rpcId, JsonRpcError<T> error) {
|
|
var rpcRes = JsonRpcResponse.CreateError(rpcId, error);
|
|
var rpcResStr = JsonSerializer.Serialize(rpcRes, serializerOpts);
|
|
send(new WebsocketOutgoingMessage[] { new(id, rpcResStr) });
|
|
}
|
|
|
|
bool send(IEnumerable<WebsocketOutgoingMessage> messages) {
|
|
lock (lockObj) {
|
|
if (requestSend is null) {
|
|
Log.Api.warn(
|
|
$"Unable to send message(s) because '{nameof(requestSend)}' handler is null");
|
|
return false;
|
|
}
|
|
requestSend(messages);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void subscribe(WebsocketClientID id, JsonRpcRequest<JsonObject?> req) {
|
|
var subParams = req.Params!.Deserialize<SubscriptionParams>()!;
|
|
var exp = DataStreamExpressionsInterpreter.Evaluate(subParams.Query!);
|
|
var stream = exp.getView(new Dictionary<string, StatementExpression>(0));
|
|
var sub = new ClientSubscription(id, subParams, Disposable.Create(dispose));
|
|
lock (lockObj) {
|
|
clients[id].Add(sub);
|
|
sendResponse(id, req.Id!.Value, req.Params);
|
|
stream.onChange += onChange;
|
|
}
|
|
|
|
void onChange(object? newValue) {
|
|
var dp = ExceptionAssert.of_type<DataPacket>(newValue);
|
|
var rpcParams = new {
|
|
addedRows = dp.addedRows,
|
|
deletedRows = dp.deletedRows
|
|
};
|
|
sendNotification(id, "subscription", rpcParams);
|
|
}
|
|
|
|
void dispose() =>
|
|
stream.onChange -= onChange;
|
|
}
|
|
|
|
void unsubscribe(WebsocketClientID id, JsonRpcRequest<JsonObject?> req) {
|
|
var subParams = req.Params!.Deserialize<SubscriptionParams>();
|
|
var subParamsStr = JsonSerializer.Serialize(subParams, serializerOpts);
|
|
// Minimize amount of code inside lock (just need it to find/remove the subscription)
|
|
List<ClientSubscription>? subs = null;
|
|
ClientSubscription? sub = null;
|
|
lock (lockObj) {
|
|
subs = clients.GetValueOrDefault(id);
|
|
subs?.TryRemoveFirst(s => s!.Params.Equals(subParams), out sub);
|
|
}
|
|
if (subs is null) {
|
|
// Didn't find client (log/throw).
|
|
var msg = $"Unable to unsubscribe: did not find client. Id: {id}.\nParams:\n{subParamsStr}";
|
|
Log.Api.error(msg);
|
|
throw new Exception(msg);
|
|
}
|
|
if (sub is null) {
|
|
// Found client, but didn't find subscription (log/throw).
|
|
var msg = $"Unable to unsubscribe: did not find the specified subscription. " +
|
|
$"Id: {id}. Request:\n{subParams}";
|
|
Log.Api.error(msg);
|
|
throw new Exception(msg);
|
|
}
|
|
// Found client and subscription (dispose/remove subscription).
|
|
Log.Api.info($"Unsubscribing client. Id: {id}.\nParams:\n{subParamsStr}");
|
|
sub.Dispose();
|
|
}
|
|
|
|
void unsubscribeAll(WebsocketClientID id) {
|
|
lock (lockObj) {
|
|
if (!clients.TryGetValue(id, out var subs)) {
|
|
throw new Exception(
|
|
$"Unable to unsubscribe: did not find client with id {id}");
|
|
}
|
|
subs.DisposeAll();
|
|
subs.Clear();
|
|
}
|
|
}
|
|
|
|
void onBadRequest(WebsocketIncomingMessage req, string? errorMsg) {
|
|
var rpcId = extractRpcId(req.message_body);
|
|
var logMsg = $"Recieved invalid request:\n{req.message_body}";
|
|
if (errorMsg is not null) {
|
|
logMsg += $"\n\n**Exception**\n{errorMsg}";
|
|
}
|
|
Log.Api.error(logMsg);
|
|
if (rpcId is null) {
|
|
return;
|
|
}
|
|
// Send error back to client
|
|
var rpcErr = JsonRpcError.Create(
|
|
(long)JsonRpcErrorCode.InvalidRequest,
|
|
$"Received invalid request: {errorMsg ?? "unknown error"}",
|
|
req.message_body);
|
|
sendError(req.sender_client_id, rpcId.Value, rpcErr);
|
|
}
|
|
}
|