Exemple #1
0
        static void Main(string[] args)
        {
            // Initialise the pattern provider with a list of 4 properties.
            using (var pattern = new PatternWrapper(
                       new FileInfo("..\\..\\..\\..\\..\\data\\51Degrees-LiteV3.2.dat").FullName,
                       new[] { "Id", "DeviceType", "IsMobile", "ScreenPixelsWidth", "ScreenPixelsHeight" }))
            {
                // Initialise the trie provider with a data file and a list of 4 properties.
                using (var trie = new TrieWrapper(
                           new FileInfo("..\\..\\..\\..\\..\\data\\51Degrees-LiteV3.2.trie").FullName,
                           new[] { "Id", "DeviceType", "IsMobile", "ScreenPixelsWidth", "ScreenPixelsHeight" }))
                {
                    // IMPORTANT: For a full list of properties see: https://51degrees.com/resources/property-dictionary

                    using (var reader = new FileInfo(args.Length > 0 ? args[0] : "..\\..\\..\\..\\..\\data\\20000 User Agents.csv").OpenText())
                    {
                        var start = DateTime.UtcNow;
                        Console.WriteLine("Started -> {0}", start);
                        var line = reader.ReadLine();
                        while (line != null)
                        {
                            using (var patternResults = pattern.Match(line.Trim()))
                            {
                                Output(pattern, patternResults);
                            }
                            using (var trieResults = trie.Match(line.Trim()))
                            {
                                Output(trie, trieResults);
                            }
                            line = reader.ReadLine();
                        }
                        Console.WriteLine("Elapsed Time -> {0} seconds", (DateTime.UtcNow - start).TotalSeconds);
                    }
                }
            }
            Console.ReadKey();
        }
        /// <summary>
        /// Represents payload for a thread. Function opens the file with
        /// User-Agent strings and runs device detection for each User-Agent.
        /// Hash is computed for each detection and the XORed with the
        /// existing hash to verify that reloading the dataset did not affect
        /// the detection.
        /// </summary>
        public void threadPayload()
        {
            // Local variables.
            long         hash             = 0L;
            int          recordsProcessed = 0;
            IMatchResult match;

            // Open file containing User-Agent strings for read.
            using (FileStream fs = File.Open(userAgentsFile,
                                             FileMode.Open,
                                             FileAccess.Read,
                                             FileShare.ReadWrite))
                using (BufferedStream bs = new BufferedStream(fs))
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        // Read next line.
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            // Performs detection. Disposes of match.
                            using (match = provider.Match(line))
                            {
                                // Compute hash for this match.
                                hash ^= getHash(match);
                                // Update count of processed User-Agent lines.
                                recordsProcessed++;
                            }
                        }
                    }
            // When thread is finished increment threadsFinished counter and
            // Report on the progress
            cb.Add(hash);
            Interlocked.Increment(ref threadsFinished);
            Console.WriteLine("Thread complete with hash code: " + hash +
                              " and records processed: " + recordsProcessed);
        }