예제 #1
0
        public void Scope_Insensitive()
        {
            var scope = new SealedString.Scope();

            var s1 = scope.Seal("Lenin");
            var s2 = scope.Seal("has");
            var s3 = scope.Seal("LeNIN");
            var s4 = scope.Seal("LeNeN");

            Assert.AreEqual(s1, s3);
            Assert.AreNotEqual(s1, s2);
            Assert.AreNotEqual(s1, s4);

            Assert.AreEqual(3, scope.Count);
            Assert.AreEqual(s1, scope["LENIN"]);
            Assert.AreEqual(s2, scope["HAS"]);
            Assert.AreEqual(s4, scope["LENEN"]);
        }
예제 #2
0
        public void Scope_Sensitive()
        {
            var scope = new SealedString.Scope(StringComparer.InvariantCulture);

            var s1 = scope.Seal("Lenin");
            var s2 = scope.Seal("has");
            var s3 = scope.Seal("LeNIN");
            var s4 = scope.Seal("LeNeN");

            Assert.AreNotEqual(s1, s3);
            Assert.AreNotEqual(s1, s2);
            Assert.AreNotEqual(s1, s4);

            Assert.AreEqual(4, scope.Count);
            Assert.AreEqual(s1, scope["Lenin"]);
            Assert.AreEqual(s2, scope["has"]);
            Assert.AreEqual(s3, scope["LeNIN"]);
            Assert.AreEqual(s4, scope["LeNeN"]);

            Assert.AreEqual(SealedString.Unassigned, scope["LENIN"]);
        }
예제 #3
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);
            }
        }
예제 #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
        public void Scope_Sensitive()
        {
            var scope = new SealedString.Scope(StringComparer.InvariantCulture);

              var s1 = scope.Seal("Lenin");
              var s2 = scope.Seal("has");
              var s3 = scope.Seal("LeNIN");
              var s4 = scope.Seal("LeNeN");

              Assert.AreNotEqual(s1, s3);
              Assert.AreNotEqual(s1, s2);
              Assert.AreNotEqual(s1, s4);

              Assert.AreEqual(4, scope.Count);
              Assert.AreEqual(s1, scope["Lenin"]);
              Assert.AreEqual(s2, scope["has"]);
              Assert.AreEqual(s3, scope["LeNIN"]);
              Assert.AreEqual(s4, scope["LeNeN"]);

              Assert.AreEqual(SealedString.Unassigned, scope["LENIN"]);
        }
예제 #6
0
        public void Scope_Insensitive()
        {
            var scope = new SealedString.Scope();

              var s1 = scope.Seal("Lenin");
              var s2 = scope.Seal("has");
              var s3 = scope.Seal("LeNIN");
              var s4 = scope.Seal("LeNeN");

              Assert.AreEqual(s1, s3);
              Assert.AreNotEqual(s1, s2);
              Assert.AreNotEqual(s1, s4);

              Assert.AreEqual(3, scope.Count);
              Assert.AreEqual(s1, scope["LENIN"]);
              Assert.AreEqual(s2, scope["HAS"]);
              Assert.AreEqual(s4, scope["LENEN"]);
        }