Пример #1
0
 protected internal GeoEntity(string query, IPAddressBlock block, Location location)
 {
   Query = query;
   Block = block;
   Location = location;
 }
Пример #2
0
      private Location parseLocation(string data)
      {
        var row = new Location();
        if (data.IsNullOrWhiteSpace()) return row;
        
        var i = 0;
        var fc = row.Schema.FieldCount; 
        foreach(var seg in parseCSVLine(data))
        {
          if (i==fc) break;
          var v = seg;

          if (row.Schema[i].Type==typeof(Boolean)) v = v=="1"?"true":"false";
          try
          {
            row[i] = v;
          }
          catch(Exception error)
          {
            throw new GeoException("Location {0}.{1}[{2}] = '{3}' error: {4} ".Args(
                                   row.Schema.Name,
                                   row.Schema[i].Name,
                                   i,
                                   v,
                                   error.ToMessageWithType()), error); 
          }
          i++;
        }
        return row;
      }
Пример #3
0
 protected internal GeoEntity(IPAddress query, IPSubnetBlock block, Location? location)
 {
     Query = query;
       Block = block;
       Location = location;
 }
Пример #4
0
        protected override void DoStart()
        {
            if (m_Resolution!= LookupResolution.Country && m_Resolution!= LookupResolution.City)
              throw new GeoException(StringConsts.GEO_LOOKUP_SVC_RESOLUTION_ERROR.Args(m_Resolution));

            if (!Directory.Exists(m_DataPath))
              throw new GeoException(StringConsts.GEO_LOOKUP_SVC_PATH_ERROR.Args(m_DataPath ?? StringConsts.UNKNOWN));

            var fnBlocks    = Path.Combine(m_DataPath, "GeoLite2-{0}-Blocks-IPv6.csv".Args(m_Resolution));
            var fnBlocksV4 = Path.Combine(m_DataPath, "GeoLite2-{0}-Blocks-IPv4.csv".Args(m_Resolution));
            var fnLocations = Path.Combine(m_DataPath, "GeoLite2-{0}-Locations-en.csv".Args(m_Resolution));

            if (!File.Exists(fnBlocks))
            throw new GeoException(StringConsts.GEO_LOOKUP_SVC_DATA_FILE_ERROR.Args(fnBlocks));
            if (!File.Exists(fnBlocksV4))
            throw new GeoException(StringConsts.GEO_LOOKUP_SVC_DATA_FILE_ERROR.Args(fnBlocksV4));
            if (!File.Exists(fnLocations))
            throw new GeoException(StringConsts.GEO_LOOKUP_SVC_DATA_FILE_ERROR.Args(fnLocations));

            m_CancelStart = false;
            m_Locations = new Dictionary<SealedString, Location>();

            try
            {
            const int MAX_PARSE_ERRORS = 8;

            var tree = new BinaryTree<Subnet, IPSubnetBlock>();
            var scope = new SealedString.Scope();
            foreach (var blocksFn in new[] { fnBlocks, fnBlocksV4 })
            {
              using (var stream = new FileStream(blocksFn, FileMode.Open, FileAccess.Read, FileShare.Read, 4*1024*1024))
              {
                int errors = 0;
                try
                {
                  foreach (var row in stream.AsCharEnumerable().ParseCSV(skipHeader: true, columns: 10))
                  {
                    if (m_CancelStart || !App.Active) break;
                    var arr = row.ToArray();
                    var block = new IPSubnetBlock(
                      scope.Seal(arr[0]),
                      scope.Seal(arr[1]),
                      scope.Seal(arr[2]),
                      scope.Seal(arr[3]),
                      arr[4].AsBool(),
                      arr[5].AsBool(),
                      scope.Seal(arr[6]),
                      arr[7].AsFloat(),
                      arr[8].AsFloat());

                    tree[new Subnet(block.Subnet.Value, true)] = block;
                  }
                }
                catch (Exception error)
                {
                  log(MessageType.Error, "DoStart('{0}')".Args(blocksFn), "Line: {0} {1}".Args(0/*line*/, error.ToMessageWithType()), error);
                  errors++;
                  if (errors > MAX_PARSE_ERRORS)
                  {
                    log(MessageType.CatastrophicError, "DoStart('{0}')".Args(blocksFn), "Errors > {0}. Aborting file '{1}' import".Args(MAX_PARSE_ERRORS, blocksFn));
                    break;
                  }
                }
              }
            }
            m_SubnetBST = new SubnetTree<IPSubnetBlock>(tree.BuildIndex());

            using (var stream = new FileStream(fnLocations, FileMode.Open, FileAccess.Read, FileShare.Read, 4 * 1024 * 1024))
            {
              try
              {
                foreach (var row in stream.AsCharEnumerable().ParseCSV(skipHeader: true, columns: 13))
                {
                  if (m_CancelStart || !App.Active) break;
                  var arr = row.ToArray();
                  var location = new Location(
                    scope.Seal(arr[0]),
                    scope.Seal(arr[1]),
                    scope.Seal(arr[2]),
                    scope.Seal(arr[3]),
                    scope.Seal(arr[4]),
                    scope.Seal(arr[5]),
                    scope.Seal(arr[6]),
                    scope.Seal(arr[7]),
                    scope.Seal(arr[8]),
                    scope.Seal(arr[9]),
                    scope.Seal(arr[10]),
                    scope.Seal(arr[11]),
                    scope.Seal(arr[12]));
                  m_Locations.Add(location.ID, location);
                }
              }
              catch (CSVParserException error)
              {
                log(MessageType.Error, "DoStart('{0}')".Args(fnLocations), "Line: {0} Column: {1} {2}".Args(error.Line, error.Column, error.ToMessageWithType()), error);
              }
              catch (Exception error)
              {
                log(MessageType.Error, "DoStart('{0}')".Args(fnLocations), "{1}".Args(error.ToMessageWithType()), error);
              }
            }
            }
            catch
            {
              m_SubnetBST = null;
              m_Locations = null;
              m_SubnetBST = null;
              throw;
            }

            if (m_CancelStart) throw new GeoException(StringConsts.GEO_LOOKUP_SVC_CANCELED_ERROR);
        }
Пример #5
0
      private Location parseLocation(string data)
      {
        var row = new Location();
        if (data.IsNullOrWhiteSpace()) return row;
        
        var i = 0;
        var fc = row.Schema.FieldCount; 
        foreach(var seg in parseCSVLine(data))
        {
          if (i==fc) break;
          var v = seg;

          if (row.Schema[i].Type==typeof(Boolean)) v = v=="1"?"true":"false";
          row[i] = v;
          i++;
        }
        return row;
      }