Example #1
0
 /// <summary>
 /// Creates the device detection providers.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Application_Start(object sender, EventArgs e)
 {
     _pattern = new PatternWrapper(
         Path.Combine(
             AppDomain.CurrentDomain.BaseDirectory,
             "..\\..\\data\\51Degrees-LiteV3.2.dat"));
     _trie = new TrieWrapper(
         Path.Combine(
             AppDomain.CurrentDomain.BaseDirectory,
             "..\\..\\data\\51Degrees-LiteV3.2.trie"));
 }
Example #2
0
 static void Main(string[] args)
 {
     using (var pattern = new PatternWrapper("../../../../../data/51Degrees-LiteV3.2.dat"))
     {
         Process(pattern);
     }
     using (var trie = new TrieWrapper("../../../../../data/51Degrees-LiteV3.4.trie"))
     {
         Process(trie);
     }
 }
Example #3
0
 /// <summary>
 /// Disposes of the device detection providers.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Application_End(object sender, EventArgs e)
 {
     if (_pattern != null)
     {
         _pattern.Dispose();
         _pattern = null;
     }
     if (_trie != null)
     {
         _trie.Dispose();
         _trie = null;
     }
 }
Example #4
0
        /// <summary>
        /// Performs dataset reload tests. A number of threads run in the
        /// background constantly performing device detection while the main
        /// thread does the data file reloads.
        ///
        /// The main thread will carry on doing the dataset reloads while at
        /// least one thread has not finished.
        /// </summary>
        public void Run()
        {
            // Threads that run device detection in the background.
            int numberOfThreads = 4;

            // Contains references to background threads.
            Thread[] threads;

            Console.WriteLine("Starting the Reload Data File Example.");

            provider = new TrieWrapper(deviceDataFile, propertiesToUse);
            threads  = new Thread[numberOfThreads];
            // Start detection threads.
            for (int i = 0; i < numberOfThreads; i++)
            {
                threads[i] = new Thread(new ThreadStart(threadPayload));
                threads[i].Start();
            }

            // Reload data file until at least one thread is still not done.
            while (threadsFinished < numberOfThreads)
            {
                provider.ReloadFromMemory();
                Console.WriteLine("Provider reloaded.");
                Thread.Sleep(1000);
            }

            // Wait for all detection threads to complete.
            for (int i = 0; i < numberOfThreads; i++)
            {
                threads[i].Join();
            }

            // Release resources held by the provider.
            provider.Dispose();

            // Perform the test.
            if (!cb.IsEmpty)
            {
                long first, current;
                cb.TryTake(out first);
                while (!cb.IsEmpty)
                {
                    cb.TryTake(out current);
                    Assert.IsTrue(first == current, "Hash values are not equal.");
                }
            }
        }
Example #5
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();
        }