Example #1
0
 protected override void DoWaitForCompleteStop()
 {
     m_SubnetBST = null;
     m_Locations = null;
 }
Example #2
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)
                        {
                            WriteLog(MessageType.Error, "DoStart('{0}')".Args(blocksFn), "Line: {0} {1}".Args(0 /*line*/, error.ToMessageWithType()), error);
                            errors++;
                            if (errors > MAX_PARSE_ERRORS)
                            {
                                WriteLog(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)
                    {
                        WriteLog(MessageType.Error, "DoStart('{0}')".Args(fnLocations), "Line: {0} Column: {1} {2}".Args(error.Line, error.Column, error.ToMessageWithType()), error);
                    }
                    catch (Exception error)
                    {
                        WriteLog(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);
            }
        }