This commit is contained in:
Derek S 2021-04-06 20:51:15 -05:00
commit 9a31b5654d
5 changed files with 67 additions and 6 deletions

View file

@ -12,6 +12,9 @@ namespace LightReflectiveMirror.LoadBalancing
public int RoomCount; public int RoomCount;
public int PublicRoomCount; public int PublicRoomCount;
public TimeSpan Uptime; public TimeSpan Uptime;
[JsonIgnore]
public List<Room> serversConnectedToRelay;
} }
[Serializable] [Serializable]
@ -37,7 +40,7 @@ namespace LightReflectiveMirror.LoadBalancing
[Serializable] [Serializable]
public struct Room public struct Room
{ {
public int serverId; public string serverId;
public int hostId; public int hostId;
public string serverName; public string serverName;
public string serverData; public string serverData;

View file

@ -156,6 +156,12 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
await context.Response.SendResponseAsync(JsonConvert.SerializeObject(_stats)); 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 #region Startup

View file

@ -135,14 +135,18 @@ namespace LightReflectiveMirror.LoadBalancing
var serverStats = wc.DownloadString(url); var serverStats = wc.DownloadString(url);
var deserializedData = JsonConvert.DeserializeObject<RelayServerInfo>(serverStats); var deserializedData = JsonConvert.DeserializeObject<RelayServerInfo>(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])) if (availableRelayServers.ContainsKey(keys[i]))
availableRelayServers[keys[i]] = deserializedData; availableRelayServers[keys[i]] = deserializedData;
else else
availableRelayServers.Add(keys[i], deserializedData); availableRelayServers.Add(keys[i], deserializedData);
} }
catch (Exception ex) catch
{ {
// server doesnt exist anymore probably // server doesnt exist anymore probably
Logger.WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red); Logger.WriteLogMessage("Server " + keys[i] + " does not exist anymore, removing", ConsoleColor.Red);

View file

@ -1,7 +1,11 @@
namespace LightReflectiveMirror.LoadBalancing using System.Collections.Generic;
using System.Linq;
namespace LightReflectiveMirror.LoadBalancing
{ {
partial class Program partial class Program
{ {
public long GetTotalCCU() public long GetTotalCCU()
{ {
long temp = 0; long temp = 0;
@ -21,5 +25,43 @@
return temp; 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;
}
/// <summary>
/// Checks if a server id already is in use.
/// </summary>
/// <param name="id">The ID to check for</param>
/// <returns></returns>
bool DoesServerIdExist(string id)
{
var infos = new List<RelayServerInfo>(availableRelayServers.Values.ToList());
foreach (var info in infos)
{
foreach (var server in info.serversConnectedToRelay)
{
if (server.serverId == id)
return true;
}
}
return false;
}
} }
} }

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.Linq; using System.Linq;
using System.Net;
namespace LightReflectiveMirror namespace LightReflectiveMirror
{ {
@ -100,8 +101,13 @@ namespace LightReflectiveMirror
else else
{ {
// ping load balancer here // 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;
}
} }
} }