Exemplo n.º 1
0
            void source_DownloadFinished(IDownloadSource sender, string remoteFile, string localFilePath)
            {
                // Handle based on what remote file it was
                if (remoteFile == CurrentVersionFilePath)
                {
                    // Version file
                    try
                    {
                        var txt = TryReadAllText(localFilePath);
                        if (txt != null)
                        {
                            // Try to parse and add the version
                            int version;
                            if (!int.TryParse(txt, out version))
                            {
                                const string errmsg =
                                    "Failed to parse version file to integer (remote path: {0}, local path: {1}). Contents: `{2}`";
                                if (log.IsErrorEnabled)
                                {
                                    log.ErrorFormat(errmsg, remoteFile, localFilePath, txt);
                                }
                                _masterReadInfo.AppendError(string.Format(errmsg, remoteFile, localFilePath, txt));
                            }
                            else
                            {
                                _masterReadInfo.AddVersion(version);
                                _hasReadVersion = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Unexpected error while handling version file (remote path: {0}, local path: {1}). Exception: {2}";
                        if (log.IsErrorEnabled)
                        {
                            log.ErrorFormat(errmsg, remoteFile, localFilePath, ex);
                        }
                        _masterReadInfo.AppendError(string.Format(errmsg, remoteFile, localFilePath, ex));
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _numBusyVersionFile);
                    }
                }
                else if (_readVersion.HasValue &&
                         StringComparer.OrdinalIgnoreCase.Equals(PathHelper.GetVersionString(_readVersion.Value) + ".txt",
                                                                 remoteFile))
                {
                    // VersionFileList file
                    try
                    {
                        var txt = TryReadAllText(localFilePath);
                        _masterReadInfo.AddVersionFileListText(txt);
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Unexpected error while handling version file (remote path: {0}, local path: {1}). Exception: {2}";
                        if (log.IsErrorEnabled)
                        {
                            log.ErrorFormat(errmsg, remoteFile, localFilePath, ex);
                        }
                        _masterReadInfo.AppendError(string.Format(errmsg, remoteFile, localFilePath, ex));
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _numBusyVersionFile);
                    }
                }
                else if (remoteFile == CurrentMasterServersFilePath)
                {
                    try
                    {
                        // Add the list of master servers
                        var descriptors = DownloadSourceDescriptor.FromDescriptorFile(localFilePath);
                        foreach (var desc in descriptors)
                        {
                            _masterReadInfo.AddMasterServer(desc);
                        }

                        // If any of the added master servers are not in our list, grab from it, too
                        lock (_sourcesSync)
                        {
                            foreach (var desc in descriptors)
                            {
                                var d = desc;
                                if (!_sources.Any(x => x.IsIdenticalTo(d)))
                                {
                                    // None of our existing sources match the descriptor, so add it to our list and start
                                    // grabbing from that new source
                                    try
                                    {
                                        var newSource = desc.Instantiate();
                                        _sources.Add(newSource);
                                        ExecuteSource(newSource);
                                    }
                                    catch (Exception ex)
                                    {
                                        const string errmsg =
                                            "Failed to instantiate and/or execute downoaded master server using DownloadSourceDescriptor `{0}`. Exception: {1}";
                                        if (log.IsWarnEnabled)
                                        {
                                            log.WarnFormat(errmsg, desc, ex);
                                        }
                                        Debug.Fail(string.Format(errmsg, desc, ex));
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Unexpected error while handling master servers file (remote path: {0}, local path: {1}). Exception: {2}";
                        if (log.IsErrorEnabled)
                        {
                            log.ErrorFormat(errmsg, remoteFile, localFilePath, ex);
                        }
                        _masterReadInfo.AppendError(string.Format(errmsg, remoteFile, localFilePath, ex));
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _numBusyMasterServersFile);
                    }
                }
                else if (remoteFile == CurrentDownloadSourcesFilePath)
                {
                    try
                    {
                        // Add the list of download sources
                        var descriptors = DownloadSourceDescriptor.FromDescriptorFile(localFilePath);
                        foreach (var desc in descriptors)
                        {
                            _masterReadInfo.AddDownloadSource(desc);
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg =
                            "Unexpected error while handling download sources file (remote path: {0}, local path: {1}). Exception: {2}";
                        if (log.IsErrorEnabled)
                        {
                            log.ErrorFormat(errmsg, remoteFile, localFilePath, ex);
                        }
                        _masterReadInfo.AppendError(string.Format(errmsg, remoteFile, localFilePath, ex));
                    }
                    finally
                    {
                        Interlocked.Decrement(ref _numBusyDownloadSourcesFile);
                    }
                }
                else
                {
                    const string errmsg = "Unexpected remote file `{0}` downloaded by `{1}`.";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, remoteFile, sender);
                    }
                    Debug.Fail(string.Format(errmsg, remoteFile, sender));
                    return;
                }

                // Delete the temp file
                PathHelper.SafeDeleteTempFile(localFilePath);

                CheckIfComplete();
            }