From 82128be9ca829c39f95fa6e8564a20a14e10ae76 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 3 Oct 2023 10:29:30 -0400 Subject: [PATCH] refactor: Use events to avoid circular deps for ws sync/gateway --- src/client/spark_api_server/Program.cs | 1 - .../Core/IWebsocketConnectionGateway.cs | 8 ++--- .../Core/WebsocketConnectionGateway.cs | 29 +++++-------------- .../spark_websocket/Core/WebsocketDataSync.cs | 3 ++ 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/client/spark_api_server/Program.cs b/src/client/spark_api_server/Program.cs index b3f3cb8ec..fe6f0d778 100644 --- a/src/client/spark_api_server/Program.cs +++ b/src/client/spark_api_server/Program.cs @@ -43,7 +43,6 @@ Port = "8080", KeepAliveIntervalSecs = 0.1, }; - gwy.init(sync); await sync.initAsync(gwy); var errors = Errors.None; Log.Api.info("Starting websocket server..."); diff --git a/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs b/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs index e7e699d22..7a6b8f4fb 100644 --- a/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs +++ b/src/client/spark_websocket/Core/IWebsocketConnectionGateway.cs @@ -14,10 +14,10 @@ namespace bts.websocket_core { // 2. // Sends data to the client public interface IWebsocketConnectionGateway { - // This is the first call after construction. This - // will only be called once, and the provided sync will be used - // to send connection, disconnection and data back to the sync - public void init(IWebsocketDataSync sync); + event Action? Connected; + event Action? Disconnected; + event Action? MessageReceived; + // This function tells the class to start listening to client connections. // All the parameters (ports to open, security parameters etc) are // passed through the WebsocketServerParams arguments diff --git a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs index f5e49cee2..fee4a79d2 100644 --- a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs +++ b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs @@ -329,19 +329,13 @@ namespace bts.websocket_core { } + public event Action? Connected; + public event Action? Disconnected; + public event Action? MessageReceived; private WebApplication? app = null; private WebsocketServerConfig? _server_params = null; private WebSocketConnectionStore Connections { get; } = new(); - private IWebsocketDataSync? _sync = null; - private IWebsocketDataSync Sync { - get => Log.notnull(_sync); - set { - bts.Log.isnull(_sync); - _sync = value; - } - - } /// /// creates a new connections dictionary and a new WebSocketHandler. /// @@ -350,13 +344,6 @@ namespace bts.websocket_core { _WebSocketHandler = new WebSocketHandler(this, Connections); } /// - /// Initializes the websocket gateway with a sync object - /// - /// The object that synchronizes the data between the websocket server and other parts of the system - public void init(IWebsocketDataSync sync) { - Sync = sync; - } - /// /// Creates a unique identifier for a websocket client and adds the client's connection to the list of active connections. /// /// The connection of the client @@ -370,14 +357,15 @@ namespace bts.websocket_core { /// forwards an incoming message from a websocket client to sync. /// /// The incoming message to be handled - public void HandleMessage(WebsocketIncomingMessage message) => Sync.handle_client_message(message); + public void HandleMessage(WebsocketIncomingMessage message) => + MessageReceived?.Invoke(message); /// /// Notifies of a client disconnection and removes the client's connection from the list of active connections. /// /// The unique identifier of the disconnected client public void NotifyDisconnection(WebsocketClientID id) { - Sync.handle_disconnection(id); + Disconnected?.Invoke(id); Connections.RemoveConnection(id); } @@ -389,7 +377,7 @@ namespace bts.websocket_core { /// A unique identifier for the client public async Task HandleConnection(Connection conn, WebsocketIncomingMessage? optional_message) { var id = createID(conn); - await Task.Run(() => Sync.handle_connection(id, optional_message)); + await Task.Run(() => Connected?.Invoke(id, optional_message)); return id; } @@ -406,7 +394,7 @@ namespace bts.websocket_core { if (conn == null) { // If the client connection is not found, log the error and notify sync about the disconnection through the Sync object logger.warn(" could not find socket in dictionary"); - Sync.handle_disconnection(id); + Disconnected?.Invoke(id); return; } // Check if the connection needs to be disconnected @@ -425,7 +413,6 @@ namespace bts.websocket_core { /// The parameters for the websocket server includind the URL and endpoint, public Task start_listening(WebsocketServerConfig server_params) { // start listening, assert that init has been called already - bts.Log.notnull(_sync); bts.Log.isnull(_server_params); bts.Log.isnull(app); diff --git a/src/client/spark_websocket/Core/WebsocketDataSync.cs b/src/client/spark_websocket/Core/WebsocketDataSync.cs index 3b97c8119..58f5355be 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -169,6 +169,9 @@ namespace bts.websocket_core { set { Log.isnull(gateway); gateway = value; + gateway.Connected += handle_connection; + gateway.Disconnected += handle_disconnection; + gateway.MessageReceived += handle_client_message; } } ///