/// <summary> /// FileAgent OnComplete /// </summary> /// <param name="fileId"></param> void fileAgent_OnComplete(int fileId, string fileType) { // Notify the player thread using another event (chained events? bad idea?) Trace.WriteLine(new LogMessage("RequiredFilesAgent - fileAgent_OnComplete", "FileId finished downloading" + fileId.ToString())); // Get the required file associated with this ID RequiredFile rf = _requiredFiles.GetRequiredFile(fileId, fileType); // Set the status on the client info screen if (_requiredFiles.FilesDownloading == 0) { _clientInfoForm.RequiredFilesStatus = "Sleeping"; } else { _clientInfoForm.RequiredFilesStatus = string.Format("{0} files to download", _requiredFiles.FilesDownloading.ToString()); } // Update the RequiredFiles TextBox _clientInfoForm.UpdateRequiredFiles(RequiredFilesString()); // Write the Cache Manager to Disk _cacheManager.WriteCacheManager(); if (rf.FileType == "layout") { // Raise an event to say it is completed OnComplete(rf.Path); } }
/// <summary> /// Run Thread /// </summary> public void Run() { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Started"), LogType.Info.ToString()); while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); // Only do something if enabled if (!ApplicationSettings.Default.EnableExpiredFileDeletion) { return; } // Get required files from disk _requiredFiles = RequiredFiles.LoadFromDisk(); // Build a list of files in the library DirectoryInfo directory = new DirectoryInfo(ApplicationSettings.Default.LibraryPath); // Check each one and see if it is in required files foreach (FileInfo fileInfo in directory.GetFiles()) { // Delete files that were accessed over N days ago try { RequiredFile file = _requiredFiles.GetRequiredFile(fileInfo.Name); } catch { // Not a required file if (fileInfo.LastAccessTime < DateTime.Now.AddDays(ApplicationSettings.Default.LibraryAgentInterval * -1)) { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Deleting old file: " + fileInfo.Name), LogType.Info.ToString()); File.Delete(fileInfo.FullName); } } } } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); } } // Sleep this thread for 5 minutes _manualReset.WaitOne(900 * 1000); } Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }
/// <summary> /// FileAgent OnComplete /// </summary> /// <param name="fileId"></param> void fileAgent_OnComplete(int fileId, string fileType) { // Notify the player thread using another event (chained events? bad idea?) Trace.WriteLine(new LogMessage("RequiredFilesAgent - fileAgent_OnComplete", "FileId finished downloading" + fileId.ToString())); // Get the required file associated with this ID RequiredFile rf = _requiredFiles.GetRequiredFile(fileId, fileType); // Set the status on the client info screen if (_requiredFiles.FilesDownloading == 0) { _clientInfoForm.RequiredFilesStatus = "Sleeping"; // If we are the last download thread to complete, then we should report media inventory and raise an event to say we've got everything _requiredFiles.ReportInventory(); // Raise an event to say we've completed if (OnFullyProvisioned != null) { OnFullyProvisioned(); } } else { _clientInfoForm.RequiredFilesStatus = string.Format("{0} files to download", _requiredFiles.FilesDownloading.ToString()); } // Update the RequiredFiles TextBox _clientInfoForm.UpdateRequiredFiles(RequiredFilesString()); // Write the Cache Manager to Disk _cacheManager.WriteCacheManager(); if (rf.FileType == "layout") { // Raise an event to say it is completed if (OnComplete != null) { OnComplete(rf.SaveAs); } } }
/// <summary> /// Run Thread /// </summary> public void Run() { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Started"), LogType.Info.ToString()); while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); // Only do something if enabled if (!ApplicationSettings.Default.EnableExpiredFileDeletion) { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Expired File Deletion Disabled"), LogType.Audit.ToString()); return; } // Test Date DateTime testDate = DateTime.Now.AddDays(ApplicationSettings.Default.LibraryAgentInterval * -1); // Get required files from disk _requiredFiles = RequiredFiles.LoadFromDisk(); Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Number of required files = " + _requiredFiles.RequiredFileList.Count), LogType.Audit.ToString()); // Build a list of files in the library DirectoryInfo directory = new DirectoryInfo(ApplicationSettings.Default.LibraryPath); // Check each one and see if it is in required files foreach (FileInfo fileInfo in directory.GetFiles()) { // Never delete certain system files // Also do not delete log/stat files as they are managed by their respective agents if (_persistentFiles.Contains(fileInfo.Name) || fileInfo.Name.Contains(ApplicationSettings.Default.LogLocation) || fileInfo.Name.Contains(ApplicationSettings.Default.StatsLogFile) ) { continue; } // Delete files that were accessed over N days ago try { RequiredFile file = _requiredFiles.GetRequiredFile(fileInfo.Name); } catch { // It is a bad idea to log in here - it can cause a build up of log files. //Debug.WriteLine(new LogMessage("LibraryAgent - Run", fileInfo.Name + " is not in Required Files, testing last accessed date [" + fileInfo.LastAccessTime + "] is earlier than " + testDate), LogType.Audit.ToString()); // Not a required file if (fileInfo.LastAccessTime < testDate) { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Deleting old file: " + fileInfo.Name), LogType.Info.ToString()); File.Delete(fileInfo.FullName); // Is this a HTZ file? if (fileInfo.Extension.ToLower() == ".htz") { // Also delete the extracted version of this file string pathToPackageFolder = Path.Combine(ApplicationSettings.Default.LibraryPath, "package_" + fileInfo.Name.Replace(fileInfo.Extension, "")); if (Directory.Exists(pathToPackageFolder)) { Directory.Delete(pathToPackageFolder, true); } } } } } } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); } } // Sleep this thread for 15 minutes _manualReset.WaitOne(2700 * 1000); } Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }
/// <summary> /// Run Thread /// </summary> public void Run() { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Started"), LogType.Info.ToString()); while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); // Only do something if enabled if (!ApplicationSettings.Default.EnableExpiredFileDeletion) return; // Get required files from disk _requiredFiles = RequiredFiles.LoadFromDisk(); // Build a list of files in the library DirectoryInfo directory = new DirectoryInfo(ApplicationSettings.Default.LibraryPath); // Check each one and see if it is in required files foreach (FileInfo fileInfo in directory.GetFiles()) { // Never delete certain system files if (_persistentFiles.Contains(fileInfo.Name)) continue; // Delete files that were accessed over N days ago try { RequiredFile file = _requiredFiles.GetRequiredFile(fileInfo.Name); } catch { // Not a required file if (fileInfo.LastAccessTime < DateTime.Now.AddDays(ApplicationSettings.Default.LibraryAgentInterval * -1)) { Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Deleting old file: " + fileInfo.Name), LogType.Info.ToString()); File.Delete(fileInfo.FullName); } } } } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); } } // Sleep this thread for 5 minutes _manualReset.WaitOne(900 * 1000); } Trace.WriteLine(new LogMessage("LibraryAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }
/// <summary> /// Runs the agent /// </summary> public void Run() { Trace.WriteLine(new LogMessage("FileAgent - Run", "Thread Started"), LogType.Audit.ToString()); // Get the required file id from the list of required files. RequiredFile file = _requiredFiles.GetRequiredFile(_requiredFileId, _requiredFileType); // Set downloading to be true file.Downloading = true; // Wait for the Semaphore lock to become available _fileDownloadLimit.WaitOne(); try { Trace.WriteLine(new LogMessage("FileAgent - Run", "Thread alive and Lock Obtained"), LogType.Audit.ToString()); if (file.FileType == "resource") { // Download using XMDS GetResource using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds; xmds.UseDefaultCredentials = true; string result = xmds.GetResource(ApplicationSettings.Default.ServerKey, ApplicationSettings.Default.HardwareKey, file.LayoutId, file.RegionId, file.MediaId); // Write the result to disk using (FileStream fileStream = File.Open(ApplicationSettings.Default.LibraryPath + @"\" + file.SaveAs, FileMode.Create, FileAccess.Write, FileShare.Read)) { using (StreamWriter sw = new StreamWriter(fileStream)) { sw.Write(result); sw.Close(); } } // File completed file.Downloading = false; file.Complete = true; } } else if (file.FileType == "media" && file.Http) { // Download using HTTP and the rf.Path using (WebClient wc = new WebClient()) { wc.DownloadFile(file.Path, ApplicationSettings.Default.LibraryPath + @"\" + file.SaveAs); } // File completed file.Downloading = false; // Check MD5 string md5 = _requiredFiles.CurrentCacheManager.GetMD5(file.SaveAs); if (file.Md5 == md5) { // Mark it as complete _requiredFiles.MarkComplete(_requiredFileId, file.Md5); // Add it to the cache manager _requiredFiles.CurrentCacheManager.Add(file.SaveAs, file.Md5); Trace.WriteLine(new LogMessage("FileAgent - Run", "File Downloaded Successfully. " + file.SaveAs), LogType.Info.ToString()); } else { // Just error - we will pick it up again the next time we download Trace.WriteLine(new LogMessage("FileAgent - Run", "Downloaded file failed MD5 check. Calculated [" + md5 + "] & XMDS [ " + file.Md5 + "] . " + file.SaveAs), LogType.Info.ToString()); } } else { // Download using XMDS GetFile while (!file.Complete) { byte[] getFileReturn; // Call XMDS GetFile using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds; xmds.UseDefaultCredentials = false; getFileReturn = xmds.GetFile(ApplicationSettings.Default.ServerKey, _hardwareKey, file.Id, file.FileType, file.ChunkOffset, file.ChunkSize); } // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; if (file.FileType == "layout") { // Decode this byte[] into a string and stick it in the file. string layoutXml = Encoding.UTF8.GetString(getFileReturn); // Full file is downloaded using (FileStream fileStream = File.Open(ApplicationSettings.Default.LibraryPath + @"\" + file.SaveAs, FileMode.Create, FileAccess.Write, FileShare.Read)) { using (StreamWriter sw = new StreamWriter(fileStream)) { sw.Write(layoutXml); sw.Close(); } } file.Complete = true; } else { // Media file // Need to write to the file - in append mode using (FileStream fs = new FileStream(ApplicationSettings.Default.LibraryPath + @"\" + file.Path, FileMode.Append, FileAccess.Write)) { fs.Write(getFileReturn, 0, getFileReturn.Length); fs.Close(); } // Increment the offset by the amount we just asked for file.ChunkOffset = file.ChunkOffset + file.ChunkSize; // Has the offset reached the total size? if (file.Size > file.ChunkOffset) { double remaining = file.Size - file.ChunkOffset; // There is still more to come if (remaining < file.ChunkSize) { // Get the remaining file.ChunkSize = remaining; } // Part is complete OnPartComplete(file.Id); } else { // File complete file.Complete = true; } } getFileReturn = null; } // File completed file.Downloading = false; // Check MD5 string md5 = _requiredFiles.CurrentCacheManager.GetMD5(file.SaveAs); if (file.Md5 == md5) { // Mark it as complete _requiredFiles.MarkComplete(_requiredFileId, file.Md5); // Add it to the cache manager _requiredFiles.CurrentCacheManager.Add(file.SaveAs, file.Md5); Trace.WriteLine(new LogMessage("FileAgent - Run", "File Downloaded Successfully. " + file.SaveAs), LogType.Info.ToString()); } else { // Just error - we will pick it up again the next time we download Trace.WriteLine(new LogMessage("FileAgent - Run", "Downloaded file failed MD5 check. Calculated [" + md5 + "] & XMDS [ " + file.Md5 + "] . " + file.SaveAs), LogType.Info.ToString()); } } // Inform the Player thread that a file has been modified. OnComplete(file.Id, file.FileType); } catch (WebException webEx) { // Remove from the cache manager _requiredFiles.CurrentCacheManager.Remove(file.SaveAs); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("FileAgent - Run", "Web Exception in Run: " + webEx.Message), LogType.Info.ToString()); // Mark as not downloading file.Downloading = false; } catch (Exception ex) { // Remove from the cache manager _requiredFiles.CurrentCacheManager.Remove(file.SaveAs); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("FileAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString()); // Mark as not downloading file.Downloading = false; } // Release the Semaphore Trace.WriteLine(new LogMessage("FileAgent - Run", "Releasing Lock"), LogType.Audit.ToString()); _fileDownloadLimit.Release(); }