/// <inheritdoc />
        public async Task <ResolvedEndpoint> RetrieveAsync(ClientRegionLocale locale, string serviceType)
        {
            //TODO: Validate string so they don't sniff out private services
            if (!Enum.IsDefined(typeof(ClientRegionLocale), locale))
            {
                throw new ArgumentOutOfRangeException(nameof(locale), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serviceType));
            }

            if (EntryCache.ContainsKey(ConvertLocaleServiceTypeToString(locale, serviceType)))
            {
                return(EntryCache[ConvertLocaleServiceTypeToString(locale, serviceType)]);
            }

            CatalogServiceNodeEntry[] result = await QueryConsul(locale, serviceType);

            if (!QueryResultIsValid(result))
            {
                ThrowQueryException(locale, serviceType);
            }

            return(ParseQueryToEndpoint(locale, serviceType, result));
        }
        /// <inheritdoc />
        public Task <bool> HasRegionStore(ClientRegionLocale region)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }

            return(Task.FromResult(File.Exists(BuildRegionEndpointFileLocation(region))));
        }
        private string BuildRegionEndpointFileLocation(ClientRegionLocale region)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }

            return($@"Endpoints/Endpoints{region.ToString()}.json");
        }
コード例 #4
0
        /// <inheritdoc />
        public Task <bool> HasDataForRegionAsync(ClientRegionLocale locale)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), locale))
            {
                throw new ArgumentOutOfRangeException(nameof(locale), "Value should be defined in the ClientRegionLocale enum.");
            }

            return(EndpointsContext.Endpoints.AnyAsync(e => e.Region == locale));
        }
コード例 #5
0
        /// <inheritdoc />
        public async Task <ResolvedEndpoint> RetrieveAsync(ClientRegionLocale locale, string serviceType)
        {
            if (!await HasEntryAsync(locale, serviceType))
            {
                throw new InvalidOperationException($"Does not contain informations for Region: {locale} Type: {serviceType}");
            }

            NameEndpointResolutionStorageModel regionInfo = await Retrieve(locale);

            return(new ResolvedEndpoint(regionInfo.ServiceEndpoints[serviceType].EndpointAddress, regionInfo.ServiceEndpoints[serviceType].EndpointPort));
        }
        /// <inheritdoc />
        public async Task <NameEndpointResolutionStorageModel> Retrieve(ClientRegionLocale region)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }

            using (StreamReader reader = File.OpenText(BuildRegionEndpointFileLocation(region)))
            {
                return(Newtonsoft.Json.JsonConvert.DeserializeObject <NameEndpointResolutionStorageModel>(await reader.ReadToEndAsync()));
            }
        }
コード例 #7
0
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace(ClientRegionLocale region, string service)
        {
            //arrange
            ResolveServiceEndpointRequestModel model = new ResolveServiceEndpointRequestModel(region, service);

            //act
            string serializedModel = JsonConvert.SerializeObject(model);

            //assert
            Assert.NotNull(serializedModel);
            Assert.IsNotEmpty(serializedModel);
        }
コード例 #8
0
        /// <inheritdoc />
        public Task <bool> HasEntryAsync(ClientRegionLocale locale, string serviceType)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), locale))
            {
                throw new ArgumentOutOfRangeException(nameof(locale), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serviceType));
            }

            return(EndpointsContext.Endpoints.AnyAsync(e => e.Region == locale && e.Service == serviceType));
        }
コード例 #9
0
        /// <inheritdoc />
        public async Task <bool> HasEntryAsync(ClientRegionLocale locale, NetworkServiceType serviceType)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), locale))
            {
                throw new ArgumentOutOfRangeException(nameof(locale), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (!Enum.IsDefined(typeof(NetworkServiceType), serviceType))
            {
                throw new ArgumentOutOfRangeException(nameof(serviceType), "Value should be defined in the NetworkServiceType enum.");
            }

            return(await EndpointsContext.Endpoints.AnyAsync(e => e.Region == locale && e.Service == serviceType));
        }
        private static ResolvedEndpoint ParseQueryToEndpoint(ClientRegionLocale locale, string serviceType, CatalogServiceNodeEntry[] result)
        {
            if (!QueryResultIsValid(result))
            {
                ThrowQueryException(locale, serviceType);
            }

            //TODO: Check health
            //TODO: Manual loadbalancing roadrobin?
            CatalogServiceNodeEntry entry = result.First();

            return(ConvertConsulEntryToEndpoint(entry));
        }
コード例 #11
0
        //We don't need a real ctor because we load a file
        public NameEndpointResolutionStorageModel(ClientRegionLocale region, Dictionary <string, ResolvedEndpoint> serviceEndpoints)
        {
            if (serviceEndpoints == null)
            {
                throw new ArgumentNullException(nameof(serviceEndpoints));
            }
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }

            Region            = region;
            _ServiceEndpoints = serviceEndpoints;
        }
コード例 #12
0
        public ResolveServiceEndpointRequest(ClientRegionLocale region, string serviceType)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serviceType));
            }

            Region      = region;
            ServiceType = serviceType;
        }
コード例 #13
0
        }                                                     //must have setter for EF

        public GameServerListEntryModel(ClientRegionLocale region, string endpointAddress, int endpointPort)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (string.IsNullOrWhiteSpace(endpointAddress))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(endpointAddress));
            }

            Region          = region;
            EndpointAddress = endpointAddress;
            EndpointPort    = endpointPort;
        }
コード例 #14
0
        /// <inheritdoc />
        public async Task <bool> HasEntryAsync(ClientRegionLocale locale, string serviceType)
        {
            if (!await HasDataForRegionAsync(locale))
            {
                return(false);
            }

            NameEndpointResolutionStorageModel regionInfo = await Retrieve(locale);

            if (regionInfo.ServiceEndpoints.ContainsKey(serviceType))
            {
                return(true);
            }

            return(false);
        }
コード例 #15
0
        public GameServerInfo(string serverName, ClientRegionLocale serverLocale, ResolvedEndpoint endpoint)
        {
            if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }
            if (string.IsNullOrWhiteSpace(serverName))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serverName));
            }
            if (!Enum.IsDefined(typeof(ClientRegionLocale), serverLocale))
            {
                throw new ArgumentOutOfRangeException(nameof(serverLocale), "Value should be defined in the ClientRegionLocale enum.");
            }

            ServerName   = serverName;
            ServerLocale = serverLocale;
            Endpoint     = endpoint;
        }
        /// <inheritdoc />
        public async Task <bool> HasEntryAsync(ClientRegionLocale locale, string serviceType)
        {
            if (EntryCache.ContainsKey(ConvertLocaleServiceTypeToString(locale, serviceType)))
            {
                return(true);
            }

            CatalogServiceNodeEntry[] result = await QueryConsul(locale, serviceType);

            bool valid = QueryResultIsValid(result);

            if (valid)
            {
                //Add to cache so we can look up from the cache in this lifetime of the service
                EntryCache.Add(ConvertLocaleServiceTypeToString(locale, serviceType), ParseQueryToEndpoint(locale, serviceType, result));
            }

            return(valid);
        }
コード例 #17
0
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace(ClientRegionLocale value)
        {
            //arrange
            Dictionary <string, ResolvedEndpoint> endpoints = new Dictionary <string, ResolvedEndpoint>()
            {
                { "AuthenticationService", new ResolvedEndpoint("127.0.0.1", 5555) }
            };
            NameEndpointResolutionStorageModel model = new NameEndpointResolutionStorageModel(value, endpoints);

            //act
            string serializedModel = JsonConvert.SerializeObject(model);

            //assert
            Assert.NotNull(serializedModel);
            Assert.IsNotEmpty(serializedModel);
            Assert.True(serializedModel.Contains(endpoints.Values.First().EndpointAddress));
            Assert.True(serializedModel.Contains(nameof(model.ServiceEndpoints)));
            Assert.True(serializedModel.Contains(endpoints.Values.First().EndpointPort.ToString()));
            Assert.True(serializedModel.Contains(value.ToString()));
        }
コード例 #18
0
        }                                                     //must have setter for EF

        public NamedResolvedEndpointEntryModel(ClientRegionLocale region, NetworkServiceType service, string endpointAddress, int endpointPort)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), region))
            {
                throw new ArgumentOutOfRangeException(nameof(region), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (!Enum.IsDefined(typeof(NetworkServiceType), service))
            {
                throw new ArgumentOutOfRangeException(nameof(service), "Value should be defined in the NetworkServiceType enum.");
            }
            if (string.IsNullOrWhiteSpace(endpointAddress))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(endpointAddress));
            }

            Region          = region;
            Service         = service;
            EndpointAddress = endpointAddress;
            EndpointPort    = endpointPort;
        }
コード例 #19
0
        /// <inheritdoc />
        public async Task <ResolvedEndpoint> RetrieveAsync(ClientRegionLocale locale, string serviceType)
        {
            if (!Enum.IsDefined(typeof(ClientRegionLocale), locale))
            {
                throw new ArgumentOutOfRangeException(nameof(locale), "Value should be defined in the ClientRegionLocale enum.");
            }
            if (string.IsNullOrWhiteSpace(serviceType))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(serviceType));
            }

            NamedResolvedEndpointEntryModel model = await EndpointsContext.Endpoints.FirstOrDefaultAsync(e => e.Region == locale && e.Service == serviceType);

            if (model == null)
            {
                throw new KeyNotFoundException($"Provided keypair {locale} and {serviceType} not found.");
            }

            return(new ResolvedEndpoint(model.EndpointAddress, model.EndpointPort));
        }
コード例 #20
0
        public async Task <ResolvedEndpoint> RetrieveAsync(ClientRegionLocale locale, string serviceType)
        {
            //This is kinda hacky but I named the authentication app service before thinking about this properly
            //so now we have to have this ugly check
            if (serviceType.ToLower() == "authentication")
            {
                serviceType = "auth";
            }

#if AZURE_DEBUG
            string deploymentName = "test";
#elif AZURE_RELEASE
            string deploymentName = "prod";
#else
            string deploymentName = null;
            throw new NotImplementedException($"Unsupported Build Configuration detected for {nameof(AzureStaticEndpointRepository)}");
#endif

            //https://test-guardians-auth.azurewebsites.net
            return(new ResolvedEndpoint($"http://{deploymentName}-Guardians-{serviceType}.azurewebsites.net", 80));
        }
 private static string ConvertLocaleServiceTypeToString(ClientRegionLocale locale, string serviceType)
 {
     return($"{serviceType}:{locale}");
 }
コード例 #22
0
 public static void Test_Throws_On_Construction_With_Invalid_Argument_Service(ClientRegionLocale region, string service)
 {
     //assert
     Assert.Throws <ArgumentException>(() => new ResolveServiceEndpointRequestModel(region, service));
 }
 private static void ThrowQueryException(ClientRegionLocale locale, string serviceType)
 {
     throw new InvalidOperationException($"Failed to query Consul for Locale:{locale} ServiceType: {serviceType}");
 }
コード例 #24
0
        public static void Test_Can_JSON_Serialize_Then_Deserialize_With_Preserved_Values(ClientRegionLocale value)
        {
            //arrange
            Dictionary <string, ResolvedEndpoint> endpoints = new Dictionary <string, ResolvedEndpoint>()
            {
                { "AuthenticationService", new ResolvedEndpoint("127.0.0.1", 5555) }
            };
            NameEndpointResolutionStorageModel model = new NameEndpointResolutionStorageModel(value, endpoints);

            //act
            NameEndpointResolutionStorageModel deserializedModel =
                JsonConvert.DeserializeObject <NameEndpointResolutionStorageModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.NotNull(deserializedModel.ServiceEndpoints);
            Assert.IsNotEmpty(deserializedModel.ServiceEndpoints);
            Assert.True(Enum.IsDefined(typeof(ClientRegionLocale), deserializedModel.Region));
            Assert.NotNull(deserializedModel.ServiceEndpoints.Keys.First());
            Assert.AreEqual(endpoints.First().Key, deserializedModel.ServiceEndpoints.First().Key);
            Assert.AreEqual(endpoints.First().Value.EndpointAddress, deserializedModel.ServiceEndpoints.First().Value.EndpointAddress);
        }
コード例 #25
0
 public static void Test_Doesnt_Throw_On_Valid_Arguments(ClientRegionLocale region, string service)
 {
     //assert
     Assert.DoesNotThrow(() => new ResolveServiceEndpointRequestModel(region, service));
 }
コード例 #26
0
        public static void Test_Can_JSON_Serialize_Then_Deserialize_With_Preserved_Values(ClientRegionLocale region, string service)
        {
            //arrange
            ResolveServiceEndpointRequestModel model = new ResolveServiceEndpointRequestModel(region, service);

            //act
            ResolveServiceEndpointRequestModel deserializedModel =
                JsonConvert.DeserializeObject <ResolveServiceEndpointRequestModel>(JsonConvert.SerializeObject(model));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.True(Enum.IsDefined(typeof(ClientRegionLocale), deserializedModel.Region));
            Assert.NotNull(deserializedModel.ServiceType);
        }
コード例 #27
0
 public static void Test_Doesnt_Throw_On_Valid_Arguments(ClientRegionLocale value)
 {
     //assert
     Assert.DoesNotThrow(() => new NameEndpointResolutionStorageModel(value, new Dictionary <string, ResolvedEndpoint>()));
 }
コード例 #28
0
 public static void Test_Throws_On_Construction_With_Invalid_Argument_Dictionary(ClientRegionLocale value)
 {
     //assert
     Assert.Throws <ArgumentNullException>(() => new NameEndpointResolutionStorageModel(value, null));
 }
コード例 #29
0
 public static void Test_Throws_On_Construction_With_Invalid_Argument_ServiceType(ClientRegionLocale value)
 {
     //assert
     Assert.Throws <ArgumentOutOfRangeException>(() => new NameEndpointResolutionStorageModel(value, new Dictionary <string, ResolvedEndpoint>()));
 }
 /// <inheritdoc />
 public Task <bool> HasDataForRegionAsync(ClientRegionLocale locale)
 {
     //TODO: How should we handle region stuff with consul?
     return(Task.FromResult(true));
 }