Ejemplo n.º 1
0
        private async Task <CurrencyPeerInfoModel> GetOrCachePeerInfo(int currencyId)
        {
            var cacheResult = await CacheService.GetOrSetHybridAsync(CacheKey.CurrencyPeerInfo(currencyId), TimeSpan.FromMinutes(10), async() =>
            {
                var currency = await GetCurrency(currencyId).ConfigureAwait(false);
                var result   = new CurrencyPeerInfoModel
                {
                    Name     = currency.Name,
                    Symbol   = currency.Symbol,
                    PeerInfo = new List <PeerInfoModel>()
                };

                var peerData = await DepositService.GetPeerInfo(currencyId).ConfigureAwait(false);
#if DEBUG
                peerData.Add(new PeerInfoModel {
                    Address = "116.74.107.102:9229"
                });
                peerData.Add(new PeerInfoModel {
                    Address = "98.115.147.74:9229"
                });
                peerData.Add(new PeerInfoModel {
                    Address = "45.35.64.39:9229"
                });
                peerData.Add(new PeerInfoModel {
                    Address = "188.166.182.57:9229"
                });
#endif
                foreach (var peer in peerData)
                {
                    try
                    {
                        var ipaddress = peer.Address;
                        if (ipaddress.Contains(":"))
                        {
                            ipaddress = ipaddress.Split(':')[0];
                        }

                        using (var client = new HttpClient())
                        {
                            client.Timeout   = TimeSpan.FromSeconds(5);
                            var peerLocation = JObject.Parse(await client.GetStringAsync("http://gd.geobytes.com/GetCityDetails?fqcn=" + ipaddress).ConfigureAwait(false)).ToObject <PeerData>();
                            if (peerLocation != null && peerLocation.geobyteslatitude.HasValue && peerLocation.geobyteslongitude.HasValue)
                            {
                                peer.geobyteslatitude  = peerLocation.geobyteslatitude;
                                peer.geobyteslongitude = peerLocation.geobyteslongitude;
                            }
                        }
                    }
                    catch { }
                }
                result.PeerInfo = peerData;
                return(result);
            }).ConfigureAwait(false);

            return(cacheResult);
        }