204
219
void xmdsFile_GetFileCompleted(object sender, XiboClient.xmds.GetFileCompletedEventArgs e)
206
// Expect new schedule XML
209
//There was an error - what do we do?
211
System.Diagnostics.Debug.WriteLine(e.Error.Message, "WS Error");
213
System.Diagnostics.Trace.WriteLine(String.Format("Error From WebService Get File. File=[{1}], Error=[{0}], Try No [{2}]", e.Error.Message, currentFileList.path, currentFileList.retrys));
216
if (currentFileList.retrys < 5)
218
// Increment the Retrys
219
currentFileList.retrys++;
226
// Blacklist this file
227
string[] mediaPath = currentFileList.path.Split('.');
228
string mediaId = mediaPath[0];
230
BlackList blackList = new BlackList();
231
blackList.Add(currentFileList.path, BlackListType.All, String.Format("Max number of retrys failed. BlackListing for all displays. Error {0}", e.Error.Message));
239
// Set the flag to indicate we have a connection to XMDS
240
Properties.Settings.Default.XmdsLastConnection = DateTime.Now;
242
// What file type were we getting
243
if (currentFileList.type == "layout")
245
// Decode this byte[] into a string and stick it in the file.
246
string layoutXml = Encoding.UTF8.GetString(e.Result);
248
// MD5 it to make sure it arrived ok
249
string md5sum = Hashes.MD5(layoutXml);
251
if (md5sum != currentFileList.md5)
253
// We need to get this file again
256
// We know it is finished and that we need to write to a file
259
string fullPath = Properties.Settings.Default.LibraryPath + @"\" + currentFileList.path + ".xlf";
261
StreamWriter sw = new StreamWriter(File.Open(fullPath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.UTF8);
265
// This file is complete
266
currentFileList.complete = true;
268
catch (IOException ex)
270
//What do we do if we cant open the file stream?
271
System.Diagnostics.Debug.WriteLine(ex.Message, "FileCollector - GetFileCompleted");
274
// Fire a layout complete event
275
LayoutFileChanged(currentFileList.path + ".xlf");
277
System.Diagnostics.Trace.WriteLine(String.Format("File downloaded: {0}", currentFileList.path), "xmdsFile_GetFileCompleted");
283
// Need to write to the file - in append mode
284
FileStream fs = new FileStream(Properties.Settings.Default.LibraryPath + @"\" + currentFileList.path, FileMode.Append, FileAccess.Write);
286
fs.Write(e.Result, 0, e.Result.Length);
290
// Increment the chunkOffset by the amount we just asked for
291
currentFileList.chunkOffset = currentFileList.chunkOffset + currentFileList.chunkSize;
293
// Has the offset reached the total size?
294
if (currentFileList.size > currentFileList.chunkOffset)
296
int remaining = currentFileList.size - currentFileList.chunkOffset;
297
// There is still more to come
298
if (remaining < currentFileList.chunkSize)
301
currentFileList.chunkSize = remaining;
306
// Do we need to do some sort of MD5 here? To make sure we got what we should have
307
fs = new FileStream(Properties.Settings.Default.LibraryPath + @"\" + currentFileList.path, FileMode.Open, FileAccess.Read);
309
string md5sum = Hashes.MD5(fs);
311
if (md5sum != currentFileList.md5)
313
// We need to get this file again
316
File.Delete(Properties.Settings.Default.LibraryPath + @"\" + currentFileList.path);
320
// Unable to delete incorrect file
321
// Hopefully we will overwrite it
322
System.Diagnostics.Debug.WriteLine(ex.Message);
325
//Reset the chunk offset (otherwise we will try to get this file again - but from the beginning (no so good)
326
currentFileList.chunkOffset = 0;
328
System.Diagnostics.Trace.WriteLine(String.Format("Error getting file {0}, HASH failed. Starting again", currentFileList.path));
223
// Expect new schedule XML
226
//There was an error - what do we do?
228
System.Diagnostics.Debug.WriteLine(e.Error.Message, "WS Error");
230
System.Diagnostics.Trace.WriteLine(String.Format("Error From WebService Get File. File=[{1}], Error=[{0}], Try No [{2}]", e.Error.Message, _currentFileList.path, _currentFileList.retrys));
233
if (_currentFileList.retrys < 5)
235
// Increment the Retrys
236
_currentFileList.retrys++;
243
// Blacklist this file
244
string[] mediaPath = _currentFileList.path.Split('.');
245
string mediaId = mediaPath[0];
247
BlackList blackList = new BlackList();
248
blackList.Add(_currentFileList.path, BlackListType.All, String.Format("Max number of retrys failed. BlackListing for all displays. Error {0}", e.Error.Message));
256
// Set the flag to indicate we have a connection to XMDS
257
Properties.Settings.Default.XmdsLastConnection = DateTime.Now;
259
// What file type were we getting
260
if (_currentFileList.type == "layout")
262
// Decode this byte[] into a string and stick it in the file.
263
string layoutXml = Encoding.UTF8.GetString(e.Result);
266
// We know it is finished and that we need to write to a file
269
string fullPath = Properties.Settings.Default.LibraryPath + @"\" + _currentFileList.path + ".xlf";
271
StreamWriter sw = new StreamWriter(File.Open(fullPath, FileMode.Create, FileAccess.Write, FileShare.Read), Encoding.Default);
275
// This file is complete
276
_currentFileList.complete = true;
278
catch (IOException ex)
280
//What do we do if we cant open the file stream?
281
System.Diagnostics.Debug.WriteLine(ex.Message, "FileCollector - GetFileCompleted");
285
String md5sum = _cacheManager.GetMD5(_currentFileList.path + ".xlf");
287
System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.md5));
289
// TODO: What if the MD5 is different?
290
if (md5sum != _currentFileList.md5)
291
System.Diagnostics.Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", String.Format("Incorrect MD5 for file: {0}", _currentFileList.path)));
293
// Fire a layout complete event
294
LayoutFileChanged(_currentFileList.path + ".xlf");
296
System.Diagnostics.Trace.WriteLine(String.Format("File downloaded: {0}", _currentFileList.path), "xmdsFile_GetFileCompleted");
302
// Need to write to the file - in append mode
303
FileStream fs = new FileStream(Properties.Settings.Default.LibraryPath + @"\" + _currentFileList.path, FileMode.Append, FileAccess.Write);
305
fs.Write(e.Result, 0, e.Result.Length);
309
// Increment the chunkOffset by the amount we just asked for
310
_currentFileList.chunkOffset = _currentFileList.chunkOffset + _currentFileList.chunkSize;
312
// Has the offset reached the total size?
313
if (_currentFileList.size > _currentFileList.chunkOffset)
315
int remaining = _currentFileList.size - _currentFileList.chunkOffset;
316
// There is still more to come
317
if (remaining < _currentFileList.chunkSize)
320
_currentFileList.chunkSize = remaining;
332
// This file is complete
333
currentFileList.complete = true;
335
// Fire the File Complete event
336
MediaFileChanged(currentFileList.path);
338
System.Diagnostics.Debug.WriteLine(string.Format("File downloaded: {0}", currentFileList.path));
340
// All the file has been recieved. Move on to the next file.
325
String md5sum = _cacheManager.GetMD5(_currentFileList.path);
327
System.Diagnostics.Debug.WriteLine(String.Format("Comparing MD5 of completed download [{0}] with given MD5 [{1}]", md5sum, _currentFileList.md5));
329
if (md5sum != _currentFileList.md5)
331
// We need to get this file again
334
File.Delete(Properties.Settings.Default.LibraryPath + @"\" + _currentFileList.path);
338
// Unable to delete incorrect file
339
// Hopefully we will overwrite it
340
System.Diagnostics.Debug.WriteLine(ex.Message);
343
// Reset the chunk offset (otherwise we will try to get this file again - but from the beginning (no so good)
344
_currentFileList.chunkOffset = 0;
346
System.Diagnostics.Trace.WriteLine(String.Format("Error getting file {0}, HASH failed. Starting again", _currentFileList.path));
350
// This file is complete
351
_currentFileList.complete = true;
353
// Fire the File Complete event
354
MediaFileChanged(_currentFileList.path);
356
System.Diagnostics.Debug.WriteLine(string.Format("File downloaded: {0}", _currentFileList.path));
358
// All the file has been recieved. Move on to the next file.
364
// Before we get the next file render any waiting events
365
System.Windows.Forms.Application.DoEvents();
346
// Before we get the next file render any waiting events
347
System.Windows.Forms.Application.DoEvents();
370
Trace.WriteLine(new LogMessage("xmdsFile_GetFileCompleted", "Unable to get the file. Exception: " + ex.Message));
372
// Consider this file complete because we couldn't write it....
373
_currentFileList.complete = true;