server id change

This commit is contained in:
cxxpxr 2021-04-06 21:43:45 -04:00
parent e71ae4f81d
commit 46fdc366b0
5 changed files with 67 additions and 6 deletions

View file

@ -12,6 +12,9 @@ namespace LightReflectiveMirror.LoadBalancing
public int RoomCount;
public int PublicRoomCount;
public TimeSpan Uptime;
[JsonIgnore]
public List<Room> 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;

View file

@ -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

View file

@ -135,14 +135,18 @@ namespace LightReflectiveMirror.LoadBalancing
var serverStats = wc.DownloadString(url);
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]))
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);

View file

@ -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;
}
/// <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.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;
}
}
}