Exemplo n.º 1
0
        private const int MaxUploadSize = 4; // 400 KB

        #region ISpeedTestClient

        /// <inheritdoc />
        /// <exception cref="InvalidOperationException"></exception>
        public Settings GetSettings()
        {
            using (var client = new SpeedTestHttpClient())
            {
                var settings = client.GetConfig <Settings>(ConfigUrl).GetAwaiter().GetResult();

                var serversConfig = new ServersList();
                foreach (var serversUrl in ServersUrls)
                {
                    try
                    {
                        serversConfig = client.GetConfig <ServersList>(serversUrl).GetAwaiter().GetResult();
                        if (serversConfig.Servers.Count > 0)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        //
                    }
                }

                if (serversConfig.Servers.Count <= 0)
                {
                    throw new InvalidOperationException("SpeedTest does not return any server");
                }

                var ignoredIds =
                    settings.ServerConfig.IgnoreIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                serversConfig.CalculateDistances(settings.Client.GeoCoordinate);
                settings.Servers = serversConfig.Servers
                                   .Where(s => !ignoredIds.Contains(s.Id.ToString()))
                                   .OrderBy(s => s.Distance)
                                   .ToList();

                return(settings);
            }
        }
Exemplo n.º 2
0
        internal async Task <Server> GetServer(double latitude, double longitude)
        {
            try
            {
                return(await Task.Factory.StartNew(() =>
                {
                    var _server = new Server()
                    {
                        Latitude = latitude, Longitude = longitude
                    };

                    ServersConfig.CalculateDistances(_server.GeoCoordinate);

                    return ServersConfig.Servers
                    .Where(s => !ServersConfig.IgnoreIds.Contains(s.Id))
                    .OrderBy(s => s.Distance)
                    .FirstOrDefault();
                }));
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to get Server based on the co-ordinates {latitude},{longitude}", ex);
            }
        }