Made regions into a dictionary

This commit is contained in:
Derek S 2021-04-08 21:56:08 -05:00
parent 8992305d94
commit bf39a95962
3 changed files with 22 additions and 80 deletions

View file

@ -18,7 +18,6 @@ namespace LightReflectiveMirror.LoadBalancing
[RestResource]
public partial class Endpoint
{
/// <summary>
/// Sent from an LRM server node
/// adds it to the list if authenticated.
@ -81,30 +80,8 @@ namespace LightReflectiveMirror.LoadBalancing
for (int i = 0; i < relays.Count; i++)
{
requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort);
_allServers.AddRange(requestedRooms);
switch (relays[i].Key.serverRegion)
{
default:
case LRMRegions.NorthAmerica:
_northAmericaServers.AddRange(requestedRooms);
break;
case LRMRegions.SouthAmerica:
_southAmericaServers.AddRange(requestedRooms);
break;
case LRMRegions.Europe:
_europeServers.AddRange(requestedRooms);
break;
case LRMRegions.Africa:
_africaServers.AddRange(requestedRooms);
break;
case LRMRegions.Asia:
_asiaServers.AddRange(requestedRooms);
break;
case LRMRegions.Oceania:
_oceaniaServers.AddRange(requestedRooms);
break;
}
_regionRooms[LRMRegions.Any].AddRange(requestedRooms);
_regionRooms[relays[i].Key.serverRegion].AddRange(requestedRooms);
}
CacheAllServers();
@ -156,36 +133,12 @@ namespace LightReflectiveMirror.LoadBalancing
if(int.TryParse(region, out int regionID))
{
switch ((LRMRegions)regionID)
{
case LRMRegions.Any:
await context.Response.SendResponseAsync(allCachedServers);
break;
case LRMRegions.NorthAmerica:
await context.Response.SendResponseAsync(NorthAmericaCachedServers);
break;
case LRMRegions.SouthAmerica:
await context.Response.SendResponseAsync(SouthAmericaCachedServers);
break;
case LRMRegions.Europe:
await context.Response.SendResponseAsync(EuropeCachedServers);
break;
case LRMRegions.Africa:
await context.Response.SendResponseAsync(AfricaCachedServers);
break;
case LRMRegions.Asia:
await context.Response.SendResponseAsync(AsiaCachedServers);
break;
case LRMRegions.Oceania:
await context.Response.SendResponseAsync(OceaniaCachedServers);
break;
}
await context.Response.SendResponseAsync(_cachedRegionRooms[(LRMRegions)regionID]);
return;
}
// They didnt submit a region header, just give them all servers as they probably are viewing in browser.
await context.Response.SendResponseAsync(allCachedServers);
await context.Response.SendResponseAsync(_cachedRegionRooms[LRMRegions.Any]);
}
/// <summary>
@ -204,6 +157,15 @@ namespace LightReflectiveMirror.LoadBalancing
{
await context.Response.SendResponseAsync(Program.instance.GenerateServerID());
}
public static void Initialize()
{
foreach (LRMRegions region in Enum.GetValues(typeof(LRMRegions)))
{
_regionRooms.Add(region, new());
_cachedRegionRooms.Add(region, "[]");
}
}
}
#region Startup
@ -214,6 +176,9 @@ namespace LightReflectiveMirror.LoadBalancing
{
try
{
// Initialize the region variables
Endpoint.Initialize();
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

View file

@ -9,24 +9,14 @@ namespace LightReflectiveMirror.LoadBalancing
{
static void CacheAllServers()
{
allCachedServers = JsonConvert.SerializeObject(_allServers);
NorthAmericaCachedServers = JsonConvert.SerializeObject(_northAmericaServers);
SouthAmericaCachedServers = JsonConvert.SerializeObject(_southAmericaServers);
EuropeCachedServers = JsonConvert.SerializeObject(_europeServers);
AsiaCachedServers = JsonConvert.SerializeObject(_asiaServers);
AfricaCachedServers = JsonConvert.SerializeObject(_africaServers);
OceaniaCachedServers = JsonConvert.SerializeObject(_oceaniaServers);
foreach (var region in _regionRooms)
_cachedRegionRooms[region.Key] = JsonConvert.SerializeObject(region.Value);
}
static void ClearAllServersLists()
{
_northAmericaServers.Clear();
_southAmericaServers.Clear();
_europeServers.Clear();
_asiaServers.Clear();
_africaServers.Clear();
_oceaniaServers.Clear();
_allServers.Clear();
foreach (var region in _regionRooms)
region.Value.Clear();
}
}

View file

@ -11,21 +11,8 @@ namespace LightReflectiveMirror.LoadBalancing
private static readonly KeyValuePair<RelayAddress, RelayServerInfo> lowest =
new(new() { address = "Dummy" }, new() { connectedClients = int.MaxValue });
public static string allCachedServers = "[]";
public static string NorthAmericaCachedServers = "[]";
public static string SouthAmericaCachedServers = "[]";
public static string EuropeCachedServers = "[]";
public static string AsiaCachedServers = "[]";
public static string AfricaCachedServers = "[]";
public static string OceaniaCachedServers = "[]";
private static List<Room> _northAmericaServers = new();
private static List<Room> _southAmericaServers = new();
private static List<Room> _europeServers = new();
private static List<Room> _africaServers = new();
private static List<Room> _asiaServers = new();
private static List<Room> _oceaniaServers = new();
private static List<Room> _allServers = new();
private static Dictionary<LRMRegions, List<Room>> _regionRooms = new();
private static Dictionary<LRMRegions, string> _cachedRegionRooms = new();
private LoadBalancerStats _stats
{