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] [RestResource]
public partial class Endpoint public partial class Endpoint
{ {
/// <summary> /// <summary>
/// Sent from an LRM server node /// Sent from an LRM server node
/// adds it to the list if authenticated. /// adds it to the list if authenticated.
@ -81,30 +80,8 @@ namespace LightReflectiveMirror.LoadBalancing
for (int i = 0; i < relays.Count; i++) for (int i = 0; i < relays.Count; i++)
{ {
requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort); requestedRooms = await Program.instance.RequestServerListFromNode(relays[i].Key.address, relays[i].Key.endpointPort);
_allServers.AddRange(requestedRooms); _regionRooms[LRMRegions.Any].AddRange(requestedRooms);
_regionRooms[relays[i].Key.serverRegion].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;
}
} }
CacheAllServers(); CacheAllServers();
@ -156,36 +133,12 @@ namespace LightReflectiveMirror.LoadBalancing
if(int.TryParse(region, out int regionID)) if(int.TryParse(region, out int regionID))
{ {
switch ((LRMRegions)regionID) await context.Response.SendResponseAsync(_cachedRegionRooms[(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;
}
return; return;
} }
// They didnt submit a region header, just give them all servers as they probably are viewing in browser. // 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> /// <summary>
@ -204,6 +157,15 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
await context.Response.SendResponseAsync(Program.instance.GenerateServerID()); 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 #region Startup
@ -214,6 +176,9 @@ namespace LightReflectiveMirror.LoadBalancing
{ {
try try
{ {
// Initialize the region variables
Endpoint.Initialize();
var config = new ConfigurationBuilder() var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory()) .SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

View file

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

View file

@ -11,21 +11,8 @@ namespace LightReflectiveMirror.LoadBalancing
private static readonly KeyValuePair<RelayAddress, RelayServerInfo> lowest = private static readonly KeyValuePair<RelayAddress, RelayServerInfo> lowest =
new(new() { address = "Dummy" }, new() { connectedClients = int.MaxValue }); new(new() { address = "Dummy" }, new() { connectedClients = int.MaxValue });
public static string allCachedServers = "[]"; private static Dictionary<LRMRegions, List<Room>> _regionRooms = new();
public static string NorthAmericaCachedServers = "[]"; private static Dictionary<LRMRegions, string> _cachedRegionRooms = new();
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 LoadBalancerStats _stats private LoadBalancerStats _stats
{ {