From 46fdc366b062282d7e4d71a40fa5e1459dfd409e Mon Sep 17 00:00:00 2001 From: cxxpxr <60411087+cxxpxr@users.noreply.github.com> Date: Tue, 6 Apr 2021 21:43:45 -0400 Subject: [PATCH] server id change --- .../LRM_LoadBalancer/DataContainer.cs | 5 +- .../LRM_LoadBalancer/Endpoint.cs | 6 +++ .../LRM_LoadBalancer/Program.cs | 8 +++- .../LRM_LoadBalancer/ProgramExtra.cs | 46 ++++++++++++++++++- .../LRM/RelayHandler/RelayHandler.cs | 8 +++- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs index e126ebb..06cd016 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/DataContainer.cs @@ -12,6 +12,9 @@ namespace LightReflectiveMirror.LoadBalancing public int RoomCount; public int PublicRoomCount; public TimeSpan Uptime; + + [JsonIgnore] + public List serversConnectedToRelay; } [Serializable] @@ -37,7 +40,7 @@ namespace LightReflectiveMirror.LoadBalancing [Serializable] public struct Room { - public int serverId; + public string serverId; public int hostId; public string serverName; public string serverData; diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs index 1d8b538..32c3dae 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Endpoint.cs @@ -156,6 +156,12 @@ namespace LightReflectiveMirror.LoadBalancing { await context.Response.SendResponseAsync(JsonConvert.SerializeObject(_stats)); } + + [RestRoute("Get", "/api/get/id")] + public async Task GetServerID(IHttpContext context) + { + await context.Response.SendResponseAsync(Program.instance.GenerateServerID()); + } } #region Startup diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs index 0f7d0ec..3861ca3 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/Program.cs @@ -135,14 +135,18 @@ namespace LightReflectiveMirror.LoadBalancing var serverStats = wc.DownloadString(url); var deserializedData = JsonConvert.DeserializeObject(serverStats); - Logger.ForceLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); + Logger.WriteLogMessage("Server " + keys[i].Address + " still exists, keeping in collection."); + + // get current server list + deserializedData.serversConnectedToRelay = await GetServerListFromIndividualRelay(keys[i].Address, keys[i].Port); if (availableRelayServers.ContainsKey(keys[i])) availableRelayServers[keys[i]] = deserializedData; else availableRelayServers.Add(keys[i], deserializedData); + } - catch (Exception ex) + catch { // server doesnt exist anymore probably Logger.WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); diff --git a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs index d5f6423..18242eb 100644 --- a/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs +++ b/LoadBalancerProject-DONT-IMPORT-INTO-UNITY/LRM_LoadBalancer/ProgramExtra.cs @@ -1,7 +1,11 @@ -namespace LightReflectiveMirror.LoadBalancing +using System.Collections.Generic; +using System.Linq; + +namespace LightReflectiveMirror.LoadBalancing { partial class Program - { + { + public long GetTotalCCU() { long temp = 0; @@ -21,5 +25,43 @@ return temp; } + + public string GenerateServerID() + { + const int LENGTH = 5; + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + var randomID = ""; + + do + { + var random = new System.Random(); + randomID = new string(Enumerable.Repeat(chars, LENGTH) + .Select(s => s[random.Next(s.Length)]).ToArray()); + } + while (DoesServerIdExist(randomID)); + + return randomID; + } + + /// + /// Checks if a server id already is in use. + /// + /// The ID to check for + /// + bool DoesServerIdExist(string id) + { + var infos = new List(availableRelayServers.Values.ToList()); + + foreach (var info in infos) + { + foreach (var server in info.serversConnectedToRelay) + { + if (server.serverId == id) + return true; + } + } + + return false; + } } } diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs index 2184e84..6c887f3 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/LRM/RelayHandler/RelayHandler.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Linq; +using System.Net; namespace LightReflectiveMirror { @@ -100,8 +101,13 @@ namespace LightReflectiveMirror else { // ping load balancer here + using (WebClient wc = new()) + { + var uri = new Uri($"http://{Program.conf.LoadBalancerAddress}:{Program.conf.LoadBalancerPort}/api/get/id"); + string randomID = wc.DownloadString(uri).Replace("\\r", "").Replace("\\n", "").Trim(); - return ""; + return randomID; + } } }