void XmdsFileGetFileCompleted(object sender, GetFileCompletedEventArgs e) { Debug.WriteLine("Get File Completed"); try { // Success / Failure if (e.Error != null) { // There was an error - what do we do? if (e.Cancelled) { Debug.WriteLine("The GetFile Request has been cancelled."); } else { Debug.WriteLine("The GetFile Request is still active, cancelling."); // Make sure we cancel the request // xmdsFile.CancelAsync(e.UserState); } // Log it Trace.WriteLine(String.Format("Error From WebService Get File. File=[{1}], Error=[{0}], Try No [{2}]", e.Error.Message, _currentFileList.Path, _currentFileList.Retrys)); // Retry? //TODO: What if we are disconnected from XMDS? if (_currentFileList.Retrys < 5) { // Increment the Retrys _currentFileList.Retrys++; // Try again GetFile(_currentFileList.ChunkOffset, _currentFileList.ChunkSize); } else { // Delete the file try { if (_currentFileList.FileType == "layout") File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); else File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); } catch (Exception ex) { Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", "Unable to delete the failed file: " + _currentFileList.Path + " Message: " + ex.Message)); } // Removed this blacklist code. Files that are not in the cachemanager will not be played on the client (and therefore // we wont try to play a corrupt / partial file). // If we blacklist here we will never try to get this file again, until the blacklist is cleared in XMDS // Better to just skip it for now, and retry it once we have the required files list again /*// Blacklist this file? string[] mediaPath = _currentFileList.Path.Split('.'); string mediaId = mediaPath[0]; BlackList blackList = new BlackList(); blackList.Add(mediaId, BlackListType.Single, String.Format("Max number of retrys failed. BlackListing for this display. Error: {0}", e.Error.Message)); */ // Move on _currentFileList.Complete = true; _currentFile++; } } else { // Set the flag to indicate we have a connection to XMDS Settings.Default.XmdsLastConnection = DateTime.Now; // What file type were we getting if (_currentFileList.FileType == "layout") { // Decode this byte[] into a string and stick it in the file. string layoutXml = Encoding.UTF8.GetString(e.Result); // We know it is finished and that we need to write to a file try { string fullPath = Settings.Default.LibraryPath + @"\" + _currentFileList.Path; Utilities.CreateFolder(Path.GetDirectoryName(fullPath)); StreamWriter sw = new StreamWriter(File.Open(fullPath, FileMode.Create, FileAccess.Write, FileShare.Read)); sw.Write(layoutXml); sw.Close(); // This file is complete _currentFileList.Complete = true; } catch (IOException ex) { //What do we do if we cant open the file stream? System.Diagnostics.Debug.WriteLine(ex.Message, "FileCollector - GetFileCompleted"); } // Check it String md5sum = _cacheManager.GetMD5(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.MD5)); // TODO: What if the MD5 is different? if (md5sum != _currentFileList.MD5) { // Error System.Diagnostics.Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", String.Format("Incorrect MD5 for file: {0}", _currentFileList.Path))); } else { // Add to the CacheManager _cacheManager.Add(_currentFileList.Path, md5sum); // Report this completion back to XMDS _requiredFiles.MarkComplete(_currentFileList.Path, md5sum); _requiredFiles.ReportInventory(); } // Fire a layout complete event LayoutFileChanged(_currentFileList.Path); System.Diagnostics.Trace.WriteLine(String.Format("File downloaded: {0}", _currentFileList.Path), "xmdsFile_GetFileCompleted"); _currentFile++; } else { Utilities.CreateFolder(Path.GetDirectoryName(Settings.Default.LibraryPath + @"\" + _currentFileList.Path)); // Need to write to the file - in append mode FileStream fs = new FileStream(Settings.Default.LibraryPath + @"\" + _currentFileList.Path, FileMode.Append, FileAccess.Write); fs.Write(e.Result, 0, e.Result.Length); fs.Close(); fs.Dispose(); // Increment the chunkOffset by the amount we just asked for _currentFileList.ChunkOffset = _currentFileList.ChunkOffset + _currentFileList.ChunkSize; // Has the offset reached the total size? if (_currentFileList.Size > _currentFileList.ChunkOffset) { long remaining = _currentFileList.Size - _currentFileList.ChunkOffset; // There is still more to come if (remaining < _currentFileList.ChunkSize) { // Get the remaining _currentFileList.ChunkSize = remaining; } } else { String md5sum = _cacheManager.GetMD5(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.MD5)); if (md5sum != _currentFileList.MD5) { // We need to get this file again try { File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); } catch (Exception ex) { // Unable to delete incorrect file //TODO: Should we black list this file? System.Diagnostics.Debug.WriteLine(ex.Message); } // Reset the chunk offset (otherwise we will try to get this file again - but from the beginning (no so good) _currentFileList.ChunkOffset = 0; System.Diagnostics.Trace.WriteLine(String.Format("Error getting file {0}, HASH failed. Starting again", _currentFileList.Path)); } else { // Add the MD5 to the CacheManager _cacheManager.Add(_currentFileList.Path, md5sum); // This file is complete _currentFileList.Complete = true; // Fire the File Complete event MediaFileChanged(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(string.Format("File downloaded: {0}", _currentFileList.Path)); // Report this completion back to XMDS // string[] filePart = _currentFileList.Path.Split('.'); _requiredFiles.MarkComplete(_currentFileList.Path, md5sum); _requiredFiles.ReportInventory(); // All the file has been recieved. Move on to the next file. _currentFile++; } } } // Before we get the next file render any waiting events System.Windows.Forms.Application.DoEvents(); } } catch (Exception ex) { Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", "Unhanded Exception when processing getFile response: " + ex.Message)); // TODO: Blacklist the file? // Consider this file complete because we couldn't write it.... _currentFileList.Complete = true; _currentFile++; } // Get the next part GetFile(_currentFileList.ChunkOffset, _currentFileList.ChunkSize); }
void XmdsFileGetFileCompleted(object sender, GetFileCompletedEventArgs e) { Debug.WriteLine("Get File Completed"); try { // Success / Failure if (e.Error != null) { // There was an error - what do we do? if (e.Cancelled) { Debug.WriteLine("The GetFile Request has been cancelled."); } else { Debug.WriteLine("The GetFile Request is still active, cancelling."); // Make sure we cancel the request // xmdsFile.CancelAsync(e.UserState); } // Log it Trace.WriteLine(String.Format("Error From WebService Get File. File=[{1}], Error=[{0}], Try No [{2}]", e.Error.Message, _currentFileList.Path, _currentFileList.Retrys)); // Retry? //TODO: What if we are disconnected from XMDS? if (_currentFileList.Retrys < 5) { // Increment the Retrys _currentFileList.Retrys++; // Try again GetFile(_currentFileList.ChunkOffset, _currentFileList.ChunkSize); } else { // Delete the file try { if (_currentFileList.FileType == "layout") { File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); } else { File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); } } catch (Exception ex) { Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", "Unable to delete the failed file: " + _currentFileList.Path + " Message: " + ex.Message)); } // Removed this blacklist code. Files that are not in the cachemanager will not be played on the client (and therefore // we wont try to play a corrupt / partial file). // If we blacklist here we will never try to get this file again, until the blacklist is cleared in XMDS // Better to just skip it for now, and retry it once we have the required files list again /*// Blacklist this file? * string[] mediaPath = _currentFileList.Path.Split('.'); * string mediaId = mediaPath[0]; * * BlackList blackList = new BlackList(); * blackList.Add(mediaId, BlackListType.Single, String.Format("Max number of retrys failed. BlackListing for this display. Error: {0}", e.Error.Message)); */ // Move on _currentFileList.Complete = true; _currentFile++; } } else { // Set the flag to indicate we have a connection to XMDS Settings.Default.XmdsLastConnection = DateTime.Now; // What file type were we getting if (_currentFileList.FileType == "layout") { // Decode this byte[] into a string and stick it in the file. string layoutXml = Encoding.UTF8.GetString(e.Result); // We know it is finished and that we need to write to a file try { string fullPath = Settings.Default.LibraryPath + @"\" + _currentFileList.Path; Utilities.CreateFolder(Path.GetDirectoryName(fullPath)); StreamWriter sw = new StreamWriter(File.Open(fullPath, FileMode.Create, FileAccess.Write, FileShare.Read)); sw.Write(layoutXml); sw.Close(); // This file is complete _currentFileList.Complete = true; } catch (IOException ex) { //What do we do if we cant open the file stream? System.Diagnostics.Debug.WriteLine(ex.Message, "FileCollector - GetFileCompleted"); } // Check it String md5sum = _cacheManager.GetMD5(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.MD5)); // TODO: What if the MD5 is different? if (md5sum != _currentFileList.MD5) { // Error System.Diagnostics.Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", String.Format("Incorrect MD5 for file: {0}", _currentFileList.Path))); } else { // Add to the CacheManager _cacheManager.Add(_currentFileList.Path, md5sum); // Report this completion back to XMDS _requiredFiles.MarkComplete(_currentFileList.Path, md5sum); _requiredFiles.ReportInventory(); } // Fire a layout complete event LayoutFileChanged(_currentFileList.Path); System.Diagnostics.Trace.WriteLine(String.Format("File downloaded: {0}", _currentFileList.Path), "xmdsFile_GetFileCompleted"); _currentFile++; } else { Utilities.CreateFolder(Path.GetDirectoryName(Settings.Default.LibraryPath + @"\" + _currentFileList.Path)); // Need to write to the file - in append mode FileStream fs = new FileStream(Settings.Default.LibraryPath + @"\" + _currentFileList.Path, FileMode.Append, FileAccess.Write); fs.Write(e.Result, 0, e.Result.Length); fs.Close(); fs.Dispose(); // Increment the chunkOffset by the amount we just asked for _currentFileList.ChunkOffset = _currentFileList.ChunkOffset + _currentFileList.ChunkSize; // Has the offset reached the total size? if (_currentFileList.Size > _currentFileList.ChunkOffset) { long remaining = _currentFileList.Size - _currentFileList.ChunkOffset; // There is still more to come if (remaining < _currentFileList.ChunkSize) { // Get the remaining _currentFileList.ChunkSize = remaining; } } else { String md5sum = _cacheManager.GetMD5(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.MD5)); if (md5sum != _currentFileList.MD5) { // We need to get this file again try { File.Delete(Settings.Default.LibraryPath + @"\" + _currentFileList.Path); } catch (Exception ex) { // Unable to delete incorrect file //TODO: Should we black list this file? System.Diagnostics.Debug.WriteLine(ex.Message); } // Reset the chunk offset (otherwise we will try to get this file again - but from the beginning (no so good) _currentFileList.ChunkOffset = 0; System.Diagnostics.Trace.WriteLine(String.Format("Error getting file {0}, HASH failed. Starting again", _currentFileList.Path)); } else { // Add the MD5 to the CacheManager _cacheManager.Add(_currentFileList.Path, md5sum); // This file is complete _currentFileList.Complete = true; // Fire the File Complete event MediaFileChanged(_currentFileList.Path); System.Diagnostics.Debug.WriteLine(string.Format("File downloaded: {0}", _currentFileList.Path)); // Report this completion back to XMDS // string[] filePart = _currentFileList.Path.Split('.'); _requiredFiles.MarkComplete(_currentFileList.Path, md5sum); _requiredFiles.ReportInventory(); // All the file has been recieved. Move on to the next file. _currentFile++; } } } // Before we get the next file render any waiting events System.Windows.Forms.Application.DoEvents(); } } catch (Exception ex) { Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", "Unhanded Exception when processing getFile response: " + ex.Message)); // TODO: Blacklist the file? // Consider this file complete because we couldn't write it.... _currentFileList.Complete = true; _currentFile++; } // Get the next part GetFile(_currentFileList.ChunkOffset, _currentFileList.ChunkSize); }