public void CreateDataSet() { _memory = new Utils.MemoryMonitor(); Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(File.ReadAllBytes(DataFile)); }
public void CreateDataSet() { Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(File.ReadAllBytes(DataFile)); }
/// <summary> /// Forces the provider to update current ActiveProvider with new data. /// </summary> private static WebProvider Create() { WebProvider provider = null; CleanTemporaryFiles(); try { // Does a binary file exist? if (Manager.BinaryFilePath != null) { // Log API version for diagnosis. var assembly = Assembly.GetExecutingAssembly().GetName(); EventLog.Info(String.Format( "Creating data set from '{0}' version '{1}'", assembly.Name, assembly.Version)); if (File.Exists(Manager.BinaryFilePath)) { if (Manager.MemoryMode) { EventLog.Info(String.Format( "Creating memory byte array dataset and provider from binary data file '{0}'.", Manager.BinaryFilePath)); provider = new WebProvider(StreamFactory.Create(File.ReadAllBytes(Manager.BinaryFilePath))); } else { provider = new WebProvider(GetTempFileDataSet()); } EventLog.Info(String.Format( "Created provider from version '{0}' format '{1}' data published on '{2:u}' in master file '{3}'.", provider.DataSet.Version, provider.DataSet.Name, provider.DataSet.Published, Manager.BinaryFilePath)); } else { EventLog.Info("Data file at '{0}' could not be found. Either it does not exist or " + "there is insufficient permission to read it. Check the AppPool has read permissions " + "and the application is not running in medium trust if the data file is not in the " + "application directory.", Manager.BinaryFilePath); } } } catch (Exception ex) { // Record the exception in the log file. EventLog.Fatal( new MobileException(String.Format( "Exception processing device data from binary file '{0}'. " + "Enable debug level logging and try again to help identify cause.", Manager.BinaryFilePath), ex)); } // Does the provider exist and has data been loaded? if (provider == null || provider.DataSet == null) { EventLog.Fatal("No data source available to create provider."); } return(provider); }
public void CreateDataSet() { Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(DataFile, false); _provider = new Provider(_dataSet); }
public void CreateDataSet() { Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(DataFile); }
/// <summary> /// Returns a data set initialised with a temporary data file. /// If an existing one is present that can be used then it will /// be favoured. If a new one is needed then one will be created. /// </summary> /// <returns></returns> private static DataSet GetTempFileDataSet() { DataSet dataSet = null; var masterFile = new FileInfo(Manager.BinaryFilePath); if (masterFile.Exists) { // Get the publish date of the master data file. var masterDate = GetDataFileDate(masterFile.FullName); // Make sure the temporary directory exists. var directory = new DirectoryInfo( Mobile.Configuration.Support.GetFilePath(Constants.TemporaryFilePath)); if (directory.Exists == false) { directory.Create(); } // Loop through the temporary files to see if we can use any of them. foreach (var temporaryFile in directory.GetFiles("*.tmp").Where(i => i.Exists && i.FullName.Equals(masterFile.FullName) == false && i.Name.StartsWith(masterFile.Name) && masterDate.Equals(GetDataFileDate(i.FullName)))) { // Use the existing temporary file. EventLog.Debug( "Trying to use existing temporary data file '{0}' with published date '{1}' as data source.", temporaryFile.FullName, masterDate.HasValue ? masterDate.Value.ToShortDateString() : "null"); try { // Try and create the data set from the existing temporary file. // If the file can't be used then record the exception in debug // logging. dataSet = StreamFactory.Create(temporaryFile.FullName, File.GetLastWriteTimeUtc(masterFile.FullName), true); // The data set could be created so exit the loop. break; } catch (Exception ex) { // This can happen if the data file is being used by another process in // exclusive mode and as yet hasn't been freed up. EventLog.Debug( "Could not use existing temporary data file '{0}' as data source", temporaryFile.FullName); EventLog.Debug(ex); dataSet = null; } } if (dataSet == null) { // No suitable temp file was found, create one in the // temporary file folder to enable the source file to be updated // without stopping the web site. dataSet = StreamFactory.Create(CreateNewTemporaryFile(masterFile).FullName, File.GetLastWriteTimeUtc(masterFile.FullName), true); } } return(dataSet); }
// 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(); }
// 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(); }
private void StartReading() { try { while (!cancellationToken.IsCancellationRequested) { log.LogInformation("Start message reader"); byte[] header = new byte[HeaderSize]; int headerResult = readStream.Read(header, 0, header.Length); try { if (headerResult == 0) { throw new ClientDisconnectedException(); } (int messageLength, bool isSplit, Guid messageGuid, byte confirmation) = ParseHeader(header); if (messageLength > 0) { ReadMessage(messageLength, isSplit, messageGuid, confirmation); } else { ProcessConfirmationFlag(confirmation, messageGuid); } } catch (ClientDisconnectedException) { communicationProtocol.Disconnect(); } } log.LogInformation("Shutdown reader thread."); } catch (Exception) { //Do not log anything as any log will lead to another exception } void ReadMessage(int messageLength, bool isSplit, Guid messageGuid, byte confirmation) { Stream messageStream = streamFactory.Create(messageLength); int intervals = messageLength / BufferSize; int remaining = messageLength % BufferSize; byte[] buffer = new byte[BufferSize]; IncomingMessage message = new IncomingMessage(messageGuid, messageStream, log); try { ReadIntervals(); int result = Extensions.ExecutesWithTimeout(() => readStream.Read(buffer, 0, remaining), MaxConfirmationResponseTime); if (result == 0) { throw new ClientDisconnectedException(); } if (result != remaining) { throw new PartialMessageException(messageGuid); } AppendBuffer(messageStream, buffer, remaining); messageStream.Seek(0, SeekOrigin.Begin); if (splitMessages.ContainsKey(messageGuid) && splitMessages.TryGetValue(messageGuid, out IncomingMessage splitMessage)) { MergeWithSplitMessage(splitMessage, messageStream); message.Dispose(); message = splitMessage; } if (isSplit) { EnqueueSplitMessage(messageGuid, message); } if (!isSplit) { splitMessages.TryRemove(messageGuid, out _); CompleteMessage(message, messageGuid, confirmation); } } catch (ClientDisconnectedException disconnectedException) { log.LogError($"Client disconnected during communication.{Environment.NewLine}" + $"{disconnectedException}"); messageStream?.Dispose(); message?.Dispose(); throw; } catch (TaskCanceledException c) { log.LogError($"Exception during message read.{Environment.NewLine}" + $"{new MessageTimeoutException(messageGuid.GetHashCode(), MaxConfirmationResponseTime, c)}"); } catch (OperationCanceledException oc) { log.LogError($"Exception during message read.{Environment.NewLine}" + $"{new MessageTimeoutException(messageGuid.GetHashCode(), MaxConfirmationResponseTime, oc)}"); } catch (PartialMessageException) { messageStream?.Dispose(); message?.Dispose(); outgoingMessageQueue.SendMessageConfirmation(messageGuid, false); } catch (Exception e) { log.LogError($"Exception during message read.{Environment.NewLine}" + $"{e}"); messageStream?.Dispose(); message?.Dispose(); throw new ClientDisconnectedException(e); } log.LogVerbose("Finished reading message."); void ReadIntervals() { while (intervals > 0) { intervals--; ReadInterval(); } void ReadInterval() { int result = Extensions.ExecutesWithTimeout(() => readStream.Read(buffer, 0, BufferSize), MaxConfirmationResponseTime); if (result == 0) { throw new ClientDisconnectedException(); } if (result != BufferSize) { throw new PartialMessageException(messageGuid); } AppendBuffer(messageStream, buffer, BufferSize); } } } }
public void Run() { try { using (EchoServiceClient proxy = new EchoServiceClient(_echoServiceEndpointName)) { string result = ""; result = proxy.Ping(); Console.WriteLine(string.Format("EchoService : {0}", result)); } } catch (System.ServiceModel.EndpointNotFoundException) { Trace.WriteLine(string.Format("!Note! : No endpoint listening at [{0}]", _echoServiceEndpointName)); } using (LibraryServiceClient proxy = new LibraryServiceClient(_libraryServiceEndpointName)) { Book book = null; Action prompt = new Action(() => { Console.WriteLine("LIST - Return a list of Books using the Search Pattern."); Console.WriteLine("READ - Return a Book using the Key value."); Console.WriteLine("Press ENTER to terminate."); }); prompt(); string input = ""; StringBuilder sb = new StringBuilder(); while ((input = Console.ReadLine().ToUpper()).Length > 0) { try { string[] tokens = input.Split(' '); switch ((tokens.Length) > 0 ? tokens[0] : "") { case "LIST": Console.Write("Search Pattern : "); string searchPattern = Console.ReadLine(); proxy.List(searchPattern, out books); Stream stream = new MemoryStream(); EntitySerializationHelpers.SerializeBooks(books.ToList(), stream); Console.WriteLine(stream.ContentsToString()); break; case "LOAD": Stream xstream = StreamFactory.Create(@"res://AppData.Books.xml"); Console.WriteLine(string.Format("Load = {0}", proxy.Load(xstream))); break; case "READ": Console.Write("Key : "); string key = Console.ReadLine(); if (proxy.Read(key, out book)) { stream = new MemoryStream(); EntitySerializationHelpers.SerializeBook(book, stream); Console.WriteLine(stream.ContentsToString()); } else { Console.WriteLine(" *Not Found*"); } break; default: prompt(); break; } } catch (Exception exp) { Trace.WriteLine(string.Format("Error {0} : {1}", input, exp.Message)); } } } }
public void CreateDataSet() { _memory = new Utils.Memory(); Utils.CheckFileExists(DataFile); _dataSet = StreamFactory.Create(DataFile); }
private void ReadMessage(int messageLength) { Stream messageStream = streamFactory.Create(messageLength); int intervals = messageLength / BufferSize; int remaining = messageLength % BufferSize; byte[] buffer = new byte[BufferSize]; try { ReadIntervals(); int result = Extensions.ExecutesWithTimeout(() => readStream.Read(buffer, 0, remaining), MaxPackageSendTime); if (result != remaining) { throw new ClientDisconnectedException(); } AppendBuffer(messageStream, buffer, remaining); messageStream.Seek(0, SeekOrigin.Begin); CompleteMessage(messageStream); } catch (ClientDisconnectedException disconnectedException) { communicationProtocol.LogError($"Client disconnected during communication.{Environment.NewLine}" + $"{disconnectedException}"); messageStream?.Dispose(); throw; } catch (TaskCanceledException c) { communicationProtocol.LogError($"Exception during message read.{Environment.NewLine}" + $"{new MessageTimeoutException(messageLength, MaxPackageSendTime, c)}"); } catch (OperationCanceledException oc) { communicationProtocol.LogError($"Exception during message read.{Environment.NewLine}" + $"{new MessageTimeoutException(messageLength, MaxPackageSendTime, oc)}"); } catch (Exception e) { communicationProtocol.LogError($"Exception during message read.{Environment.NewLine}" + $"{e}"); messageStream?.Dispose(); throw new ClientDisconnectedException(e); } communicationProtocol.LogVerbose("Finished reading message."); void ReadIntervals() { while (intervals > 0) { intervals--; ReadInterval(); } void ReadInterval() { int result = Extensions.ExecutesWithTimeout(() => readStream.Read(buffer, 0, BufferSize), MaxPackageSendTime); if (result != BufferSize) { throw new ClientDisconnectedException(); } AppendBuffer(messageStream, buffer, BufferSize); } } }
protected override void OnLoad(EventArgs e) { string currentWebsiteHost = ""; WebsitePage websitePageItem = null; WebsitePageSizeType websitePageSizeType = WebsitePageSizeType.Desktop; Provider fiftyOneDegreesMobileProvider = null; this.Page = (Page)this.Context.Handler; currentWebsiteHost = this.Context.Request.Url.Authority; this.Website = DataProvider.SelectSingleFull <Website>(new Website() { WebsiteHost = currentWebsiteHost }); if (this.Website == null || this.Website.WebsiteID == 0) { WebsiteAlias websiteAlias = DataProvider.SelectSingle <WebsiteAlias>(new WebsiteAlias() { WebsiteAliasHost = currentWebsiteHost }); if (websiteAlias != null && websiteAlias.WebsiteAliasID > 0) { this.Website = DataProvider.SelectSingleFull <Website>(new Website() { WebsiteID = websiteAlias.WebsiteID }); } else { return; } } this.CurrentPage = this.Page.AppRelativeVirtualPath.Replace(".aspx", "").Replace("~/", ""); fiftyOneDegreesMobileProvider = new Provider(StreamFactory.Create(this.Context.Server.MapPath("App_Data/51Degrees.dat"))); if (fiftyOneDegreesMobileProvider != null) { Match fiftyOneDegreesMatch = null; fiftyOneDegreesMatch = fiftyOneDegreesMobileProvider.Match(this.Context.Request.UserAgent); if (fiftyOneDegreesMatch != null) { Values fiftyOneDegreesValuesIsMobile = null; fiftyOneDegreesValuesIsMobile = fiftyOneDegreesMatch["IsMobile"]; if (fiftyOneDegreesValuesIsMobile != null) { bool isMobile = false; bool.TryParse(fiftyOneDegreesValuesIsMobile.ToString(), out isMobile); websitePageSizeType = (isMobile) ? WebsitePageSizeType.Mobile : WebsitePageSizeType.Desktop; } } } websitePageItem = DataProvider.SelectSingle(new WebsitePage() { PageName = this.CurrentPage, WebsiteID = this.Website.WebsiteID, WebsitePageSizeTypeID = websitePageSizeType }, this.Website.WebsiteConnection.ConnectionString); if (websitePageItem != null && websitePageItem.WebsitePageID > 0) { this.WebsitePage = DataProvider.SelectSingleOrDefaultFull(new WebsitePage() { PageName = this.CurrentPage, WebsiteID = this.Website.WebsiteID, WebsitePageContentTypeID = websitePageItem.WebsitePageContentTypeID, WebsitePageSizeTypeID = websitePageSizeType }, this.Website.WebsiteConnection.ConnectionString, "PageName"); this.LoadPageHeader(); } else { throw new Exception("No page data found"); } }
/// <summary> /// Uses the first arguement as the 51Degrees data file to generate /// the Epi visitor group criteris from the meta data. If not provided the /// default data file path is used. /// Uses the second arguement for the output project location. If not /// provided the default package file path is used. /// </summary> /// <param name="args"></param> static void Main(string[] args) { // Path to the 51Degrees device data file used to build the package. // This is the first parameter of the command. var dataFile = args.Length > 0 && File.Exists(args[0]) ? args[0] : Constants.DEFAULT_51DEGREES_PATH; // Get the path the the Visitor Group Criteria folder. var projectFolder = args.Length > 1 && Directory.Exists(args[1]) ? args[1] : Constants.DEFAULT_PROJECT_FOLDER; // Open the data set to get the data from. var dataSet = StreamFactory.Create(dataFile); // Create an XML stream to be used to output the language resource // information to. var lang = Lang.CreateXml(Path.Combine( projectFolder, Constants.LANG_FOLDER, Constants.PROJECT_NAME + "_EN.xml")); // Create an XML stream to be used to output the language resource // information to. var upgrade = Upgrade.CreateXml(Path.Combine( projectFolder, Constants.LANG_FOLDER, Constants.PROJECT_NAME + "Upgrade_EN.xml")); // Loop through the lookup properties. foreach (var property in Constants.LookupProperties.Select(i => dataSet.Properties[i]).Where(i => i != null)) { Lang.Write(lang, property); Enum.Write( Path.Combine(projectFolder, Constants.ENUMS_FOLDER), property); CriterionModel.Write( Path.Combine(projectFolder, Constants.CRITERION_MODELS_FOLDER), property); } // Loop through the normal properties. foreach (var property in Constants.TopProperties.Select(i => dataSet.Properties[i]).Where(i => i != null)) { GenerateProperty(projectFolder, upgrade, property); } // Loop through the numeric properties. foreach (var property in Constants.TopProperties.Select(i => dataSet.Properties[i]).Where(i => i != null && (i.ValueType == typeof(double) || i.ValueType == typeof(int)))) { GenerateProperty(projectFolder, upgrade, property, "Range"); } // Close the language XML file. Lang.Close(lang); Upgrade.Close(upgrade); }
protected StreamFactory GetStreamFactory() { return(StreamFactory.Create(streamFactoryType)); }