Represents all device data and capabilities.
Inheritance: FiftyOne.Foundation.Mobile.Detection.BaseProvider
コード例 #1
0
        // Snippet Start
        public static void Run(string fileName)
        {
            // DataSet is the object used to interact with the data file.
            // StreamFactory creates Dataset with pool of binary readers to
            // perform device lookup using file on disk. The type if
            // disposable and is therefore contained in using block to
            // ensure file handles and resources are freed.
            using (DataSet dataSet = StreamFactory.Create(fileName, false))
            {
                // Provides access to device detection functions.
                Provider provider = new Provider(dataSet);

                // Used to store and access detection results.
                Match match;

                // Contains detection result for the IsMobile property.
                string IsMobile;

                // User-Agent string of an iPhone mobile device.
                string mobileUserAgent = ("Mozilla/5.0 (iPhone; CPU iPhone " +
                    "OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like " +
                    "Gecko) 'Version/7.0 Mobile/11D167 Safari/9537.53");

                // User-Agent string of Firefox Web browser version 41 on desktop.
                string desktopUserAgent = ("Mozilla/5.0 (Windows NT 6.3; " +
                    "WOW64; rv:41.0) Gecko/20100101 Firefox/41.0");

                // User-Agent string of a MediaHub device.
                string mediaHubUserAgent = ("Mozilla/5.0 (Linux; Android " +
                    "4.4.2; X7 Quad Core Build/KOT49H) AppleWebKit/537.36 " +
                    "(KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 " +
                    "Safari/537.36");

                Console.WriteLine("Staring Getting Started Example.");

                // Carries out a match for a mobile User-Agent.
                Console.WriteLine("\nMobile User-Agent: " + mobileUserAgent);
                match = provider.Match(mobileUserAgent);
                IsMobile = match["IsMobile"].ToString();
                Console.WriteLine("   IsMobile: " + IsMobile);

                // Carries out a match for a desktop User-Agent.
                Console.WriteLine("\nDesktop User-Agent: " + desktopUserAgent);
                match = provider.Match(desktopUserAgent);
                IsMobile = match["IsMobile"].ToString();
                Console.WriteLine("   IsMobile: " + IsMobile);

                // Carries out a match for a MediaHub User-Agent.
                Console.WriteLine("\nMediaHub User-Agent: " + mediaHubUserAgent);
                match = provider.Match(mediaHubUserAgent);
                IsMobile = match["IsMobile"].ToString();
                Console.WriteLine("   IsMobile: " + IsMobile);

                // Returns the number of profiles that are Mobile.
                Console.WriteLine("\nNumber of mobile profiles: {0}",
                    dataSet.FindProfiles("IsMobile", "True").Length);
            }
        }
コード例 #2
0
ファイル: Reader.cs プロジェクト: davelondon/dontstayin
 /// <summary>
 /// Creates a new provider from the binary stream supplied.
 /// </summary>
 /// <param name="stream">Binary stream to use to create the provider.</param>
 /// <returns>A new provider initialised with data from the stream provided.</returns>
 public static Provider Create(Stream stream)
 {
     var provider = new Provider();
     using (var reader = new BinaryReader(new GZipStream(stream, CompressionMode.Decompress)))
     {
         Add(provider, reader);
     }
     return provider;
 }
コード例 #3
0
 private void UserAgentsSingle(IEnumerable<string> userAgents, int cacheSize, double minMisses, double maxMisses)
 {
     var provider = new Provider(_dataSet, cacheSize);
     var results = Utils.DetectLoopSingleThreaded(
         provider,
         userAgents,
         Utils.RetrievePropertyValues,
         _dataSet.Properties);
     Assert.IsTrue(provider.PercentageCacheMisses >= minMisses &&
         provider.PercentageCacheMisses <= maxMisses, String.Format(
         "Cache misses of '{0:P2}' outside expected range of '{1:P2}' to '{2:P2}'.",
         provider.PercentageCacheMisses,
         minMisses,
         maxMisses));
 }
コード例 #4
0
        // Snippet Start
        public static void Run(string fileName)
        {
            // DataSet is the object used to interact with the data file.
            // StreamFactory creates Dataset with pool of binary readers to
            // perform device lookup using file on disk.
            DataSet dataSet = StreamFactory.Create(fileName, false);

            // Provides access to device detection functions.
            Provider provider = new Provider(dataSet);

            // Used to store and access detection results.
            Match match;

            // Device id string of an iPhone mobile device.
            string mobileDeviceId = "12280-48866-24305-18092";

            // Device id string of Firefox Web browser version 41 on desktop.
            string desktopDeviceId = "15364-21460-53251-18092";

            // Device id string of a MediaHub device.
            string mediaHubDeviceId = "41231-46303-24154-18092";

            Console.WriteLine("Starting Match For Device Id Example.");

            //Carries out a match for a mobile device id.
            match = provider.MatchForDeviceId(mobileDeviceId);
            Console.WriteLine("\nMobile Device Id: " + mobileDeviceId);
            Console.WriteLine("   IsMobile: " + match["IsMobile"]);

            // Carries out a match for a desktop device id.
            match = provider.MatchForDeviceId(desktopDeviceId);
            Console.WriteLine("\nDesktop Device Id: " + desktopDeviceId);
            Console.WriteLine("   IsMobile: " + match["IsMobile"]);

            // Carries out a match for a MediaHub device id.
            match = provider.MatchForDeviceId(mediaHubDeviceId);
            Console.WriteLine("\nMediaHub Device Id: " + mediaHubDeviceId);
            Console.WriteLine("   IsMobile: " + match["IsMobile"]);

            // Finally close the dataset, releasing resources and file locks.
            dataSet.Dispose();
        }
コード例 #5
0
        // Snippet Start
        public static void Run(string fileName)
        {
            // DataSet is the object used to interact with the data file.
            // StreamFactory creates Dataset with pool of binary readers to
            // perform device lookup using file on disk.
            DataSet dataSet = StreamFactory.Create(fileName, false);

            // Provides access to device detection functions.
            Provider provider = new Provider(dataSet);

            // Used to store and access detection results.
            Match match;

            // Contains boolean detection result for the IsMobile property.
            bool IsMobileBool;

            // User-Agent string of an iPhone mobile device.
            string mobileUserAgent = ("Mozilla/5.0 (iPhone; CPU iPhone " +
                "OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like " +
                "Gecko) 'Version/7.0 Mobile/11D167 Safari/9537.53");

            // User-Agent string of Firefox Web browser version 41 on desktop.
            string desktopUserAgent = ("Mozilla/5.0 (Windows NT 6.3; " +
                "WOW64; rv:41.0) Gecko/20100101 Firefox/41.0");

            // User-Agent string of a MediaHub device.
            string mediaHubUserAgent = ("Mozilla/5.0 (Linux; Android " +
                "4.4.2; X7 Quad Core Build/KOT49H) AppleWebKit/537.36 " +
                "(KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 " +
                "Safari/537.36");

            Console.WriteLine("Starting Strongly Typed Example.");

            // Carries out a match for a mobile User-Agent.
            Console.WriteLine("\nMobile User-Agent: " + mobileUserAgent);
            match = provider.Match(mobileUserAgent);
            IsMobileBool = getIsMobileBool(match);
            if (IsMobileBool)
            {
                Console.WriteLine("   Mobile");
            }
            else
            {
                Console.WriteLine("   Non-Mobile");
            }

            // Carries out a match for a desktop User-Agent.
            Console.WriteLine("\nDesktop User-Agent: " + desktopUserAgent);
            match = provider.Match(desktopUserAgent);
            IsMobileBool = getIsMobileBool(match);
            if (IsMobileBool)
            {
                Console.WriteLine("   Mobile");
            }
            else
            {
                Console.WriteLine("   Non-Mobile");
            }

            // Carries out a match for a MediaHub User-Agent.
            Console.WriteLine("\nMediaHub User-Agent: " + mediaHubUserAgent);
            match = provider.Match(mediaHubUserAgent);
            IsMobileBool = getIsMobileBool(match);
            if (IsMobileBool) {
                Console.WriteLine("   Mobile");
            }
            else
            {
                Console.WriteLine("   Non-Mobile");
            }

            // Finally close the dataset, releasing resources and file locks.
            dataSet.Dispose();
        }
コード例 #6
0
ファイル: Base.cs プロジェクト: axle-h/.NET-Device-Detection
 public void CreateDataSet()
 {
     Utils.CheckFileExists(DataFile);
     _dataSet = StreamFactory.Create(DataFile, false);
     _provider = new Provider(_dataSet);
 }
コード例 #7
0
ファイル: Reader.cs プロジェクト: davelondon/dontstayin
        /// <summary>
        /// Adds the data from the binary reader to the provider.
        /// </summary>
        /// <param name="provider">The provider to have data added to/</param>
        /// <param name="reader">Reader connected to the input stream.</param>
        public static void Add(Provider provider, BinaryReader reader)
        {
            // Read and ignore the copyright notice. This is ignored.
            string copyright = reader.ReadString();

            // Ignore any additional information at the moment.
            reader.ReadString();

            // Check the versions are correct.
            Version streamFormat = ReadVersion(reader);
            if ((streamFormat.Major == BinaryConstants.FormatVersion.Major &&
                streamFormat.Minor == BinaryConstants.FormatVersion.Minor) == false)
                throw new BinaryException(String.Format(
                    "The data provided is not supported by this version of Foundation. " +
                    "The version provided is '{0}' and the version expected is '{1}'.",
                    streamFormat,
                    BinaryConstants.FormatVersion));

            // Load the data now that validation is completed.
            ReadStrings(reader, provider.Strings);
            ReadHandlers(reader, provider);
            ReadDevices(reader, provider, null);
        }
コード例 #8
0
 /// <summary>
 /// Constructs a new instance of <see cref="MobileCapabilities"/>.
 /// </summary>
 /// <param name="provider">Data provider to use with this capabilities provider.</param>
 internal MobileCapabilities(Provider provider)
 {
     _provider = provider;
     True[0] = _provider.Strings.Add("True");
     True[1] = _provider.Strings.Add("true");
     False[0] = _provider.Strings.Add("False");
     False[1] = _provider.Strings.Add("false");
     AjaxRequestType = _provider.Strings.Add("AjaxRequestType");
     AjaxRequestTypeNotSupported = _provider.Strings.Add("AjaxRequestTypeNotSupported");
     Javascript = _provider.Strings.Add("Javascript");
     JavascriptVersion = _provider.Strings.Add("JavascriptVersion");
     CookiesCapable = _provider.Strings.Add("CookiesCapable");
     BrowserVersion = _provider.Strings.Add("BrowserVersion");
     PlatformName = _provider.Strings.Add("PlatformName");
     Adapters = _provider.Strings.Add("Adapters");
     ScreenPixelsHeight = _provider.Strings.Add("ScreenPixelsHeight");
     ScreenPixelsWidth = _provider.Strings.Add("ScreenPixelsWidth");
     BitsPerPixel = _provider.Strings.Add("BitsPerPixel");
     HardwareName = _provider.Strings.Add("HardwareName");
     HardwareModel = _provider.Strings.Add("HardwareModel");
     HardwareVendor = _provider.Strings.Add("HardwareVendor");
     HtmlVersion = _provider.Strings.Add("HtmlVersion");
     XHtmlVersion = _provider.Strings.Add("XHtmlVersion");
     IsMobile = _provider.Strings.Add("IsMobile");
     IsCrawler = _provider.Strings.Add("IsCrawler");
     TablesCapable = _provider.Strings.Add("TablesCapable");
     CcppAccept = _provider.Strings.Add("CcppAccept");
     ImageGif = new[] { 
         _provider.Strings.Add("image/gif") };
     ImagePng = new[] { 
         _provider.Strings.Add("image/png") };
     ImageJpeg = new[] { 
         _provider.Strings.Add("image/jpeg"),
         _provider.Strings.Add("image/jpg") };
 }
コード例 #9
0
 /// <summary>
 /// In a single thread loops through the useragents in the file
 /// provided perform a match with the data set provided passing
 /// control back to the method provided for further processing.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="userAgents"></param>
 /// <param name="method"></param>
 /// <returns>Counts for each of the methods used</returns>
 internal static Results DetectLoopSingleThreaded(Provider provider, IEnumerable<string> userAgents, ProcessMatch method, object state)
 {
     provider.DataSet.ResetCache();
     var match = provider.CreateMatch();
     var results = new Results();
     foreach (var line in userAgents)
     {
         provider.Match(line.Trim(), match);
         method(results, match, state);
         results.Count++;
         results.Methods[match.Method]++;
     }
     AssertPool(provider);
     ReportMethods(results.Methods);
     ReportProvider(provider);
     ReportTime(results);
     return results;
 }
コード例 #10
0
ファイル: Reader.cs プロジェクト: davelondon/dontstayin
        /// <summary>
        /// Reads the devices and any children.
        /// </summary>
        /// <param name="reader">BinaryReader of the input stream.</param>
        /// <param name="provider">The provider the device will be added to.</param>
        /// <param name="parent">The parent of the device, or null if a root device.</param>
        private static void ReadDevices(BinaryReader reader, Provider provider, DeviceInfo parent)
        {
            short count = reader.ReadInt16();
            for (short i = 0; i < count; i++)
            {
                // Get the device id string.
                int uniqueDeviceIDStringIndex = reader.ReadInt32();
                string uniqueDeviceID =
                    uniqueDeviceIDStringIndex >= 0 ?
                    provider.Strings.Get(uniqueDeviceIDStringIndex) :
                    String.Empty;

                // Create the new device.
                var device = new DeviceInfo(
                    provider,
                    uniqueDeviceID,
                    parent);

                // Read the number of useragents to associate with this
                // device.
                short userAgentCount = reader.ReadInt16();
                for (int u = 0; u < userAgentCount; u++)
                {
                    // Get the user agent string index and create a new
                    // device.
                    int userAgentStringIndex = reader.ReadInt32();
                    var uaDevice = new DeviceInfo(
                        provider,
                        uniqueDeviceID,
                        userAgentStringIndex,
                        device);

                    // Add the device to the handlers.
                    foreach (short index in ReadDeviceHandlers(reader))
                        provider.Handlers[index].Set(uaDevice);
                }

                
                // Add the device to the list of all devices.
                int hashCode = device.DeviceId.GetHashCode();
                if (provider.AllDevices.ContainsKey(hashCode))
                {
                    // Yes. Add this device to the list.
                    provider.AllDevices[hashCode].Add(device);
                }
                else
                {
                    // No. So add the new device.
                    provider.AllDevices.Add(hashCode, new List<BaseDeviceInfo>(new[] { device }));
                }

                // Get the remaining properties and values to the device.
                ReadCollection(reader, device.Properties);

                // Read the child devices.
                ReadDevices(reader, provider, device);
            }
        }
コード例 #11
0
 /// <summary>
 /// Create the two providers for managed and unmanaged code.
 /// </summary>
 public virtual void Initialise()
 {
     _unmanagedProvider = new PatternWrapper(DataFile, String.Empty, 5000);
     _managedProvider = new Provider(StreamFactory.Create(DataFile, false));
 }
コード例 #12
0
 /// <summary>
 /// Disposes of the providers if not already done so.
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Disposing(bool disposing)
 {
     if (_managedProvider != null)
     {
         _managedProvider.DataSet.Dispose();
         _managedProvider = null;
     }
     if (_unmanagedProvider != null)
     {
         _unmanagedProvider.Dispose();
         _unmanagedProvider = null;
     }
 }
コード例 #13
0
 protected virtual void UserAgentsSingle(IEnumerable<string> userAgents, double maxAllowedMemory)
 {
     Console.WriteLine("Expected Max Memory: {0:0.0} MB", maxAllowedMemory);
     using (var provider = new Provider(_dataSet))
     {
         Utils.DetectLoopSingleThreaded(
             provider,
             userAgents,
             Utils.MonitorMemory,
             _memory);
     }
     Console.WriteLine("Average Memory Used: {0:0.0} MB", _memory.AverageMemoryUsed);
     if (_memory.AverageMemoryUsed > maxAllowedMemory)
     {
         Assert.Inconclusive(String.Format(
             "Average memory use was '{0:0.0}MB' but max allowed '{1:0.0}MB'",
             _memory.AverageMemoryUsed,
             maxAllowedMemory));
     }
 }
コード例 #14
0
 public void CreateDataSet()
 {
     Utils.CheckFileExists(DataFile);
     _dataSet = StreamFactory.Create(File.ReadAllBytes(DataFile));
     _provider = new Provider(_dataSet);
     Console.WriteLine("Dataset: {0}", _dataSet.Name);
     Console.WriteLine("Format: {0}", _dataSet.Format);
 }
コード例 #15
0
        // Snippet Start
        public static void Run(string fileName, string inputFile)
        {
            // DataSet is the object used to interact with the data file.
            // StreamFactory creates Dataset with pool of binary readers to
            // perform device lookup using file on disk.
            DataSet dataSet = StreamFactory.Create(fileName, false);

            // Provides access to device detection functions.
            Provider provider = new Provider(dataSet);

            // Used to store and access detection results.
            // Same match will be reused multiple times to avoid unnecessary
            // object creation.
            Match match = provider.CreateMatch();;

            // Name of the output file results will be saved to.
            string outputFile = "OfflineProcessingOutput.csv";

            // How many lines of the input CSV file to read.
            const int linesToRead = 20;

            // Line currently being read.
            int currentLine = 0;

            // HTTP User-Agent string to match, taken from input the CSV file.
            string userAgent;

            // 51Degrees properties to append the CSV file with.
            // For the full list of properties see:
            // https://51degrees.com/resources/property-dictionary
            string[] properties = { "IsMobile", "PlatformName", "PlatformVersion" };

            // Opens input and output files.
            StreamReader fin = new StreamReader(inputFile);
            StreamWriter fout = new StreamWriter(outputFile);

            Console.WriteLine("Starting Offline Processing Example.");

            // Print CSV headers to output file.
            fout.Write("User-Agent");
            for (int i = 0; i < properties.Count(); i++)
            {
                fout.Write("|" + properties[i]);
            }
            fout.Write("\n");

            while ((userAgent = fin.ReadLine()) != null &&
                     currentLine < linesToRead)
            {
                // Match current User-Agent. Match object reused.
                provider.Match(userAgent, match);
                // Write the User-Agent.
                fout.Write(userAgent);
                // Append properties.
                foreach (var property in properties)
                {
                    fout.Write("|" + match[property]);
                }
                fout.Write("\n");
                currentLine++;
            }

            fin.Close();
            fout.Close();

            Console.WriteLine("Output Written to " + outputFile);

            // Finally close the dataset, releasing resources and file locks.
            dataSet.Dispose();
        }
コード例 #16
0
 internal static void AssertPool(Provider provider)
 {
     if (provider.DataSet is FiftyOne.Foundation.Mobile.Detection.Entities.Stream.DataSet)
     {
         var dataSet = (FiftyOne.Foundation.Mobile.Detection.Entities.Stream.DataSet)provider.DataSet;
         Assert.IsTrue(dataSet.ReadersCreated == dataSet.ReadersQueued,
             String.Format(
                 "DataSet pooled readers mismatched. '{0}' created and '{1}' queued.",
                 dataSet.ReadersCreated,
                 dataSet.ReadersQueued));
     }
 }
コード例 #17
0
 /// <summary>
 /// Using multiple threads loops through the useragents in the file
 /// provided perform a match with the data set provided passing
 /// control back to the method provided for further processing.
 /// </summary>
 /// <param name="provider"></param>
 /// <param name="userAgents"></param>
 /// <param name="method"></param>
 /// <returns>Counts for each of the methods used</returns>
 internal static Results DetectLoopMultiThreaded(Provider provider, IEnumerable<string> userAgents, ProcessMatch method, object state)
 {
     provider.DataSet.ResetCache();
     var results = new Results();
     Parallel.ForEach(userAgents, line =>
     {
         var match = provider.Match(line.Trim());
         method(results, match, state);
         Interlocked.Increment(ref results.Count);
         lock (results.Methods)
         {
             results.Methods[match.Method] = results.Methods[match.Method] + 1;
         }
     });
     AssertPool(provider);
     ReportMethods(results.Methods);
     ReportProvider(provider);
     ReportTime(results);
     return results;
 }
コード例 #18
0
ファイル: Reader.cs プロジェクト: davelondon/dontstayin
 /// <summary>
 /// Reads the handler from the binary reader.
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="provider"></param>
 private static void ReadHandlers(BinaryReader reader, Provider provider)
 {
     int count = reader.ReadInt32();
     for (int i = 0; i < count; i++)
     {
         Handler handler = CreateHandler(reader, provider);
         ReadHandlerRegexes(reader, handler.CanHandleRegex);
         ReadHandlerRegexes(reader, handler.CantHandleRegex);
         provider.Handlers.Add(handler);
     }
 }
コード例 #19
0
 internal static void ReportProvider(Provider provider)
 {
     Console.WriteLine("User-Agent cache misses '{0:P2}'.",
         provider.PercentageCacheMisses);
     if (provider.DataSet is FiftyOne.Foundation.Mobile.Detection.Entities.Stream.DataSet)
     {
         ReportCache(provider.DataSet);
         ReportPool((FiftyOne.Foundation.Mobile.Detection.Entities.Stream.DataSet)provider.DataSet);
     }
 }
コード例 #20
0
ファイル: Reader.cs プロジェクト: davelondon/dontstayin
        /// <summary>
        /// Creates the handler for the current provider.
        /// </summary>
        /// <param name="reader">Data source being processed.</param>
        /// <param name="provider">Provider the new handler should be assigned to.</param>
        /// <returns>An instance of the new handler.</returns>
        private static Handler CreateHandler(BinaryReader reader, Provider provider)
        {
            HandlerTypes type = (HandlerTypes)reader.ReadByte();
            byte confidence = reader.ReadByte();
            string name = reader.ReadString();
            bool checkUAProfs = reader.ReadBoolean();
            string defaultDeviceId = String.Empty;

            switch (type)
            {
                case HandlerTypes.EditDistance:
                    return new Handlers.EditDistanceHandler(
                        provider, name, defaultDeviceId, confidence, checkUAProfs);
                case HandlerTypes.ReducedInitialString:
                    return new Handlers.ReducedInitialStringHandler(
                        provider, name, defaultDeviceId, confidence, checkUAProfs, reader.ReadString());
                case HandlerTypes.RegexSegment:
                    var handler = new Handlers.RegexSegmentHandler(
                        provider, name, defaultDeviceId, confidence, checkUAProfs);
                    ReadRegexSegmentHandler(reader, handler);
                    return handler;
            }

            throw new BinaryException(String.Format("Type '{0}' is not a recognised handler.", type));
        }