Ejemplo n.º 1
0
 public CallInformation(GeoInformation geoInformation, DateTime callStartTime, PhoneNumber sourcePhoneNumber, PhoneNumber destinationPhoneNumber)
 {
     Check.AgainstNull(geoInformation, "Call information should include geographical information.");
     Check.AgainstNull(sourcePhoneNumber, "Call information should include a source phone number.");
     Check.AgainstNull(destinationPhoneNumber, "Call information should include a destination phone number.");
     Check.That(()=>!sourcePhoneNumber.Equals(destinationPhoneNumber), "Calling yourself is not allowed.");
     GeoInformation = geoInformation;
     CallStartTime = callStartTime;
     SourcePhoneNumber = sourcePhoneNumber;
     DestinationPhoneNumber = destinationPhoneNumber;
 }
        private static void TryLocateFallback()
        {
            GeoInfo = new GeoInformation();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:48.0) Gecko/20100101 Firefox/48.0";
                request.Proxy     = null;
                request.Timeout   = 5000;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(responseString);

                            GeoInfo.Ip          = doc.SelectSingleNode("Response//IP").InnerXml;
                            GeoInfo.Country     = doc.SelectSingleNode("Response//CountryName").InnerXml;
                            GeoInfo.CountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
                            GeoInfo.Region      = doc.SelectSingleNode("Response//RegionName").InnerXml;
                            GeoInfo.City        = doc.SelectSingleNode("Response//City").InnerXml;
                            GeoInfo.Timezone    = doc.SelectSingleNode("Response//TimeZone").InnerXml;
                        }
                    }
                }

                LastLocated       = DateTime.UtcNow;
                LocationCompleted = true;
            }
            catch
            {
                LocationCompleted = false;
            }

            if (string.IsNullOrEmpty(GeoInfo.Ip))
            {
                TryGetWanIp();
            }
        }
Ejemplo n.º 3
0
        private static void TryLocateFallback()
        {
            GeoInfo = new GeoInformation();

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://freegeoip.net/xml/");
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0";
                request.Proxy = null;
                request.Timeout = 5000;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseString = reader.ReadToEnd();

                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(responseString);

                            string xmlIp = doc.SelectSingleNode("Response//IP").InnerXml;
                            string xmlCountry = doc.SelectSingleNode("Response//CountryName").InnerXml;
                            string xmlCountryCode = doc.SelectSingleNode("Response//CountryCode").InnerXml;
                            string xmlRegion = doc.SelectSingleNode("Response//RegionName").InnerXml;
                            string xmlCity = doc.SelectSingleNode("Response//City").InnerXml;

                            GeoInfo.ip = (!string.IsNullOrEmpty(xmlIp))
                                ? xmlIp
                                : "-";
                            GeoInfo.country = (!string.IsNullOrEmpty(xmlCountry))
                                ? xmlCountry
                                : "Unknown";
                            GeoInfo.country_code = (!string.IsNullOrEmpty(xmlCountryCode))
                                ? xmlCountryCode
                                : "-";
                            GeoInfo.region = (!string.IsNullOrEmpty(xmlRegion))
                                ? xmlRegion
                                : "Unknown";
                            GeoInfo.city = (!string.IsNullOrEmpty(xmlCity))
                                ? xmlCity
                                : "Unknown";
                        }
                    }
                }

                LastLocated = DateTime.UtcNow;
                LocationCompleted = true;
            }
            catch
            {
                GeoInfo.country = "Unknown";
                GeoInfo.country_code = "-";
                GeoInfo.region = "Unknown";
                GeoInfo.city = "Unknown";
                LocationCompleted = false;
            }

            if (string.IsNullOrEmpty(GeoInfo.ip))
                TryGetWanIp();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parse and set coordinates.
        /// </summary>
        /// <param name="type">Geographical information type.</param>
        /// <param name="target">Target geographical information.</param>
        /// <param name="reader">Current xml feed reader.</param>
        /// <param name="feed">Current feed.</param>
        /// <param name="nodeInfo">Current node information.</param>
        /// <returns>List of geographical coordinates.</returns>
        private async Task SetCoordinates(GeoType type, GeoInformation target, XmlReader reader, Feed feed, NodeInformation nodeInfo)
        {
            //Init
            target.coordinates ??= new List <GeoCoordinate>();

            if (!reader.IsEmptyElement)
            {
                var content = await reader.ReadStartElementAndContentAsStringAsync().ConfigureAwait(false);

                if (!string.IsNullOrWhiteSpace(content))
                {
                    //Attempt to parse coordinates
                    var index = 0;
                    content = content.Replace(",", ".").Trim();
                    var coords = content.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(part => part.Trim()).ToList();
                    if (coords.Count % 2 == 0)
                    {
                        //Set target type
                        target.Type = type;

                        //Get all coordinates
                        while (index < coords.Count)
                        {
                            if (double.TryParse(coords[index], NumberStyles.Float, CultureInfo.InvariantCulture, out var latitude))
                            {
                                if (double.TryParse(coords[index + 1], NumberStyles.Float, CultureInfo.InvariantCulture, out var longitude))
                                {
                                    try
                                    {
                                        //Add coordinate to target coordinates
                                        target.coordinates.Add(new GeoCoordinate(latitude, longitude));
                                    }
                                    catch (ArgumentOutOfRangeException ex)
                                    {
                                        //Unknown coordinates format
                                        SetParseError(ParseErrorType.UnknownNodeFormat, nodeInfo, feed, $"Latitude: {latitude}, Longitude: {longitude}", ex.Message);
                                    }

                                    //Next coordinates
                                    index += 2;
                                }
                                else
                                {
                                    //Parse longitude failed!
                                    SetParseError(ParseErrorType.UnknownNodeFormat, nodeInfo, feed, coords[index + 1], $"Parse longitude failed! (Index: {index + 1})");
                                }
                            }
                            else
                            {
                                //Parse latitude failed!
                                SetParseError(ParseErrorType.UnknownNodeFormat, nodeInfo, feed, coords[index], $"Parse latitude failed! (Index: {index})");
                            }
                        }
                    }
                    else
                    {
                        //Coordinates must have both latitude and longitude!
                        SetParseError(ParseErrorType.UnknownNodeFormat, nodeInfo, feed, content, "Coordinates must have both latitude and longitude!");
                    }
                }
            }
        }