diff --git a/src/client/spark_api_server/Program.cs b/src/client/spark_api_server/Program.cs index b44c4ebe9..848aeb31f 100644 --- a/src/client/spark_api_server/Program.cs +++ b/src/client/spark_api_server/Program.cs @@ -4,13 +4,16 @@ class Program { static int Main(string[] args) { - Console.WriteLine("Starting spark_api_server..."); + Console.WriteLine("Starting spark_api_server... just checking build"); // TODO bts.websocket_core.WebsocketDataSync sync = new bts.websocket_core.WebsocketDataSync(); bts.websocket_core.WebsocketConnectionGateway gwy = new bts.websocket_core.WebsocketConnectionGateway(); + bts.websocket_core.WebsocketServerParams prms = new bts.websocket_core.WebsocketServerParams(); gwy.init(sync); + sync.init(gwy); + gwy.start_listening(prms); while (true) { System.Threading.Thread.Sleep(100); } diff --git a/src/client/spark_websocket/Core/WebsocketClientID.cs b/src/client/spark_websocket/Core/WebsocketClientID.cs index 8cf26a7c7..161dd0a23 100644 --- a/src/client/spark_websocket/Core/WebsocketClientID.cs +++ b/src/client/spark_websocket/Core/WebsocketClientID.cs @@ -3,26 +3,23 @@ using System.Collections.Generic; using System.Reflection; namespace bts.websocket_core { - // Represents a unique client + // Represents a unique client, a little redundant public class WebsocketClientID { - readonly string ip; - readonly string port; - // not sure if we need this, lets go with this for now - readonly string uuid; - public WebsocketClientID(string ip, string port, string uuid) { - this.ip = ip; - this.port = port; - this.uuid = uuid; + + public Guid Id { get; } + + public WebsocketClientID() { + Id = Guid.NewGuid(); } public override int GetHashCode() { - return uuid.GetHashCode(); + return this.Id.GetHashCode(); } public override string ToString() { - return uuid; + return this.Id.ToString(); } public override bool Equals(object? obj) { var other = obj == null ? null : obj as WebsocketClientID; - return other != null && other.uuid == uuid; + return other != null && other.Id == Id; } } } \ No newline at end of file diff --git a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs index 393dcf9e5..9b4a0b9f1 100644 --- a/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs +++ b/src/client/spark_websocket/Core/WebsocketConnectionGateway.cs @@ -1,28 +1,259 @@ -using System; -using System.Collections.Generic; -using System.Reflection; - +// #nullable enable +using System.Net.WebSockets; +using System.Text; +using System.Collections.Concurrent; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; namespace bts.websocket_core { - + /// + /// Handles all websocket related processing, interfaces with datasync, gets initial paramters from WebsocketServerParams + /// public class WebsocketConnectionGateway : IWebsocketConnectionGateway { private IWebsocketDataSync? _sync = null; private WebsocketServerParams? _server_params = null; + private WebApplication? app = null; + // IDictionary clients = new Dictionary(); + private ConcurrentDictionary? clients = null; + + /// + /// To Avoid null reference warning, only called when object is not null, should only be called inside getNonNull, is separate function so can be changed later on + /// + /// non nullable version of object + private T getObject(T? obj) { + return obj!; + } + + /// + /// as explained in c# documentation this can be used to instruct the compiler that obj is not null, for now this is redundant + /// + /// + /// + private static bool IsNotNull([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) => obj != null; + + /// + /// will assert that object is not null and return the non nullable version + /// + /// non nullable version of object + private object GetNonNull(object? obj) { + if (IsNotNull(obj)) { + return getObject(obj); + } else { + bts.Log.assert(false); + return new Object(); + } + } + /// + /// Should be called in the start, stores pointer for sync and creates dictionary for clients, does not start server + /// + /// public void init(IWebsocketDataSync sync) { // first call bts.Log.assert(_sync == null); + bts.Log.assert(clients == null); _sync = sync; + clients = new ConcurrentDictionary(); } + /// + /// stores the current clients, one client corresponds to one open socket which corresponds to one request, will update getter to c# convention + /// + private ConcurrentDictionary GetClients() => (ConcurrentDictionary)GetNonNull(clients); + + /// + /// will update getter to c# convention + /// + private IWebsocketDataSync GetSync() => (IWebsocketDataSync)GetNonNull(_sync); + + /// + /// creates ID and adds it to clients, in case of collision just updates the previous value + /// + WebsocketClientID createID(WebSocket ws) { + var id = new WebsocketClientID(); + var check = GetClients().TryAdd(id, ws); + if (!check) { + var log_txt = $"WebsocketDataSync.createID client_id: {id.ToString()} Already exits"; + bts.Log.Gen.warn(log_txt); + GetClients()[id] = ws; + } + return id; + } + + /// + /// wrapper to gets socket from clients dictionary against given id, returns null if not found + /// + private WebSocket? GetSocket(WebsocketClientID id) { + return GetClients().ContainsKey(id) ? GetClients()[id] : null; + + } + + /// + /// wrapper to remove socket from clients dictionary against given id, cleans up removed socket as well + /// + private void RemoveSocket(WebsocketClientID id) { + if (GetClients().ContainsKey(id)) { + if (GetClients().TryRemove(id, out WebSocket? ws)) { + ws.Dispose(); + } + } + } + + /// + /// Manages client disconnection, does the following three things: + /// 1. tries sending close request to client + /// 2. removes client from dictionary of active clients + /// 3. asks sync to handle disconnection + /// + public async Task HandleDisconnection(WebsocketClientID id, string optional_message = "") { + await SendDisconnectRequestAsync(id, optional_message); + RemoveSocket(id); + GetSync().handle_disconnection(id); + } + + /// + /// should be called after init + /// creates server using the server params provided and starts listening for socket based requests on that URI + /// keeps running async + /// + /// public void start_listening(WebsocketServerParams server_params) { // start listening, assert that init has been called already bts.Log.assert(_sync != null); bts.Log.assert(_server_params == null); + bts.Log.assert(clients != null); + bts.Log.assert(app == null); + + _server_params = server_params; + var builder = WebApplication.CreateBuilder(); + builder.WebHost.UseUrls(_server_params.Uri); + app = builder.Build(); + app.UseWebSockets(_server_params.get_ws_options()); + + app.Map(_server_params.Default_end_point, async context => { + if (context.WebSockets.IsWebSocketRequest) { + using (var ws = await context.WebSockets.AcceptWebSocketAsync()) { + await ProcessWebSocketConnectionAsync(ws); + } + } else { + context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; + } + }); + app.RunAsync(); + } - // Messages are sent to the client through this + /// + /// sends the list of messafes to respectives sockets, does not wait for messages to be sent + /// + /// list of messages each containing a string based message and client ID public void send_to_clients( IEnumerable messages) { + var tasks = new List(); foreach (var om in messages) { + tasks.Add(SendWebSocketRequestAsync(om.receiver_client_id, om.message_body)); + } + } + /// + /// After adding new socket to dictionary send the new client id to sync to store and process + /// while socket stays open this keeps waiting for requests + /// + /// + /// Task that keeps waiting for requests until the socket is closed + public async Task ProcessWebSocketConnectionAsync(WebSocket ws) { + var request = createID(ws); + GetSync().handle_connection(request, null);//for now thinking this is async/ no IO + while (ws != null && ws.State == WebSocketState.Open) { + await ProcessWebSocketRequestAsync(request); + } + } + /// + /// 1. makes sure state is consistent + /// 2. listens for request + /// 3. if request is the close, calls handleDisconnection + /// 4. otherwise sends request to sync to process + /// catches exception of client closes without handshake + /// + /// id of client from whom the message is to be recieved + /// Task that runs until socket is open + public async Task ProcessWebSocketRequestAsync(WebsocketClientID id) { + var socket = GetSocket(id); + if (socket == null || socket.State != WebSocketState.Open) { + await HandleDisconnection(id, "WebsocketGateway.ProcessWebSocketRequestAsync could not receive Request as client closed"); + return; + } + try { + ArraySegment buffer = new ArraySegment(new byte[256]); + WebSocketReceiveResult result; + string request = ""; + do { + result = await socket.ReceiveAsync(buffer, CancellationToken.None); + if (result.MessageType == WebSocketMessageType.Close) { + var log_txt = $"WebsocketGateway.ProcessWebSocketRequestAsync connection closed client_id: {id}"; + await HandleDisconnection(id, log_txt); + return; + } + if (buffer.Array != null)// added because of warning + { + request += Encoding.UTF8.GetString(buffer.Array, 0, result.Count); + } else { + bts.Log.assert(false);//this should never happen + } + if (result.EndOfMessage) { + GetSync().handle_client_message(new WebsocketIncomingMessage(id, request)); + request = ""; + } + } while (socket.State == WebSocketState.Open); + + } catch (WebSocketException ex) { + var ex_str = ex.ToString(); + var log_txt = $"WebsocketGateway.ProcessWebSocketRequestAsync connection closed client_id: {id} Error: {ex_str}"; + await HandleDisconnection(id, log_txt); + } catch (OperationCanceledException) {//not sure about this read that this should just be ignored + } + } + /// + /// tries to send request to client to disconnect + /// + /// id of client to whom the Disconnect message should be sent + /// + /// task that completes if client disconnects or if there is an error + public async Task SendDisconnectRequestAsync(WebsocketClientID id, string message) { + var log_txt = $"WebsocketGateway.SendDisconnectRequestAsync tried closing client_id: {id} with message: {message}"; + var socket = GetSocket(id); + if (socket != null && socket.State == WebSocketState.Open) { + try { + await socket.CloseAsync((WebSocketCloseStatus)1000, "", CancellationToken.None); + log_txt += " done succesfully"; + } catch { + log_txt += " failed as client didnt respond"; + } + } else if (socket == null) { + log_txt += " failed as could not find socket in dictionary"; + } else if (socket.State != WebSocketState.Open) { + log_txt += " Socket already closed"; + } + bts.Log.Gen.info(log_txt); + Console.WriteLine(log_txt); + } + + /// + /// sends message iver socket correspoding to id + /// + /// id of client to whom the message should be sent + /// message to be sent + /// + public async Task SendWebSocketRequestAsync(WebsocketClientID id, string message) { + var socket = GetSocket(id); + if (socket == null || socket.State != WebSocketState.Open) { + await HandleDisconnection(id, "WebsocketGateway.SendWebSocketRequestAsync could not send Request as client closed"); + return; + } + try { + await socket.SendAsync(System.Text.Encoding.ASCII.GetBytes(message), System.Net.WebSockets.WebSocketMessageType.Text, true, CancellationToken.None); + } catch (WebSocketException ex) { + var ex_str = ex.ToString(); + var log_txt = $"WebsocketGateway.SendWebSocketRequestAsync connection closed client_id: {id} Error: {ex_str}"; + await HandleDisconnection(id, log_txt); + return; } } diff --git a/src/client/spark_websocket/Core/WebsocketDataSync.cs b/src/client/spark_websocket/Core/WebsocketDataSync.cs index db0a2152d..531905b73 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -1,30 +1,161 @@ -using System; -using System.Collections.Generic; -using System.Reflection; - namespace bts.websocket_core { - // This does all the bookkeeping for conenctions and data, and passes - // it over to the business layer + /// + /// Immutable to store the state of the client + /// + public class ClientState { + public UInt64 MessageCount { get; } + public ClientState(UInt64 messageCount) { + MessageCount = messageCount; + } + public ClientState IncrementMessages(ClientState st) => new ClientState(st.MessageCount + 1); + + + } + /// + /// This does all the bookkeeping for conenctions and data, and passes + /// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY + /// public class WebsocketDataSync : IWebsocketDataSync { + WebsocketConnectionGateway? Gateway; + /// + /// stores the current connected clients, can be converted the concurrent dictionary + /// + private Dictionary? clients; + /// + /// to access the dictionary + /// + private static object _clientsAccessLock = new object(); + /// + /// Thread that will broadcast messages to all connected messages + /// + private Thread? broadcast; + /// + /// number of messages after which broadcast thread will disconnect that particular socket + /// + public uint BROADCAST_LIMIT { get; } = 20; + /// + /// will update getter to c# convention + /// + /// non nullable version + private WebsocketConnectionGateway GetGateway() => (WebsocketConnectionGateway)GetNonNull(Gateway); + /// + /// will update getter to c# convention + /// + /// non nullable version + private Dictionary GetClients() => (Dictionary)GetNonNull(clients); + /// + /// adds client to dictionary is thread safe + /// + /// + void AddClient(WebsocketClientID client_id) { + lock (_clientsAccessLock) { + GetClients().Add(client_id, new ClientState(0)); + } + } + /// + /// removes client from dictionary is thread safe + /// + /// + void RemoveClient(WebsocketClientID client_id) { + lock (_clientsAccessLock) { + if (GetClients().ContainsKey(client_id)) + GetClients().Remove(client_id); + } + } + /// + /// will update getter to c# convention + /// + /// non nullable version + private Thread GetBroadcast() => (Thread)GetNonNull(broadcast); + /// + /// To Avoid null reference warning, only called when object is not null, should only be called inside getNonNull, is separate function so can be changed later on + /// + /// non nullable version of object + private T getObject(T? obj) { + return obj!; + } + /// + /// as explained in c# documentation this can be used to instruct the compiler that obj is not null, for now this is redundant + /// + /// + /// + private static bool IsNotNull([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? obj) => obj != null; + /// + /// will assert that object is not null and return the non nullable version + /// + /// non nullable version of object + private object GetNonNull(object? obj) { + if (IsNotNull(obj)) { + return getObject(obj); + } else { + bts.Log.assert(false); + return new Object(); + } + } + /// + /// Should be called in the start, stores pointer for gtwy and creates dictionary for clients, starts broadcast thread + /// + /// + public void init(WebsocketConnectionGateway gtwy) { + Gateway = gtwy; + clients = new Dictionary(); + broadcast = new Thread(() => BroadcastToClients()); + broadcast.Start(); + } + /// + /// Runs on different thread, wakes after given duration sends a message to all connected sockets, will close sockets that have been sent messages equal to limit + /// + /// wakes up after given duration in ms + private void BroadcastToClients(int duration = 1000) { + while (true) { + List messages = new List(); + List toRemove = new List(); + lock (_clientsAccessLock) { + foreach (var client in GetClients()) { + messages.Add(new WebsocketOutgoingMessage(client.Key, $"New Message number: {client.Value.MessageCount}, clients connected: {GetClients().Count}")); + GetClients()[client.Key] = client.Value.IncrementMessages(client.Value); + if (GetClients()[client.Key].MessageCount == BROADCAST_LIMIT) { + toRemove.Add(client.Key); + } + } + } + //example on how to send multiple messages to multiple sockets + GetGateway().send_to_clients(messages); + foreach (var key in toRemove) { + ///example on how to disconnect, this should ideally happen in its own thread + GetGateway().HandleDisconnection(key, "closing client").Wait(); + } + Thread.Sleep(duration); + } + } + // Called when a new client is connected public void handle_connection(WebsocketClientID client_id, WebsocketIncomingMessage? optional_message) { + 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 (IsNotNull(optional_message)) { + handle_client_message(optional_message); + } } // Called when a client is disconnected public void handle_disconnection(WebsocketClientID client_id) { + 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 + // Called when a client sends data sends the same message back public void handle_client_message(WebsocketIncomingMessage message) { var log_txt = $"WebsocketDataSync.handle_client_message message: {message}"; bts.Log.Gen.info(log_txt); System.Console.WriteLine(log_txt); + //example to show how to send messages to clients + GetGateway().send_to_clients(new List() { new WebsocketOutgoingMessage(message.sender_client_id, message.message_body + " RESPONSE") }); } + } } \ No newline at end of file diff --git a/src/client/spark_websocket/Core/WebsocketServerParams.cs b/src/client/spark_websocket/Core/WebsocketServerParams.cs index d6a18ba80..1d9134033 100644 --- a/src/client/spark_websocket/Core/WebsocketServerParams.cs +++ b/src/client/spark_websocket/Core/WebsocketServerParams.cs @@ -2,9 +2,32 @@ using System; using System.Collections.Generic; using System.Reflection; +using Microsoft.AspNetCore.Builder; + namespace bts.websocket_core { // Placeholder for whatever is needed for the server to start // listening to clients (port, security etc) public class WebsocketServerParams { + public string Port { get; } + public string Host { get; } + public string Scheme { get; } + public string Default_end_point { get; } + public double Keep_alive_interval_seconds { get; } + public string Uri { get; } + + public WebsocketServerParams() { + //will later do this from conf file + this.Default_end_point = "/"; + this.Host = "localhost"; + this.Scheme = "http"; + this.Port = "6666"; + this.Keep_alive_interval_seconds = 0.1; + this.Uri = this.Scheme + "://" + this.Host + ":" + this.Port; ; + } + public WebSocketOptions get_ws_options() { + return new WebSocketOptions() { + KeepAliveInterval = TimeSpan.FromSeconds(this.Keep_alive_interval_seconds) + }; + } } } \ No newline at end of file diff --git a/src/client/spark_websocket/spark_websocket.csproj b/src/client/spark_websocket/spark_websocket.csproj index ec428b460..229210b70 100644 --- a/src/client/spark_websocket/spark_websocket.csproj +++ b/src/client/spark_websocket/spark_websocket.csproj @@ -8,9 +8,12 @@ true $(major).$(minor).$(patch) 0.0.0 + enable + +