Пример #1
0
            /// <summary>
            /// Initializes a new instance of the <see cref="MasterServerDownloader"/> class.
            /// </summary>
            /// <param name="masterReadInfo">The <see cref="MasterServerReadInfo"/> to add to.</param>
            /// <param name="sources">The <see cref="IDownloadSource"/> to use to download.</param>
            /// <param name="readVersion">The version of the <see cref="VersionFileList"/> to read, or null if reading
            /// the current version number.</param>
            public MasterServerDownloader(MasterServerReadInfo masterReadInfo, IEnumerable <IDownloadSource> sources,
                                          int?readVersion)
            {
                _readVersion    = readVersion;
                _masterReadInfo = masterReadInfo;
                _sources        = new List <IDownloadSource>(sources);

                // Cache the remote file path
                if (_readVersion.HasValue)
                {
                    _remoteFileToDownload = PathHelper.GetVersionString(_readVersion.Value) + ".txt";
                }
                else
                {
                    _remoteFileToDownload = CurrentVersionFilePath;
                }
            }
Пример #2
0
            void source_DownloadFailed(IDownloadSource sender, string remoteFile, string localFilePath)
            {
                // Handle based on what remote file it was
                if (remoteFile == CurrentVersionFilePath)
                {
                    Interlocked.Decrement(ref _numBusyVersionFile);
                }
                else if (_readVersion.HasValue &&
                         StringComparer.OrdinalIgnoreCase.Equals(PathHelper.GetVersionString(_readVersion.Value) + ".txt",
                                                                 remoteFile))
                {
                    Interlocked.Decrement(ref _numBusyVersionFile);
                }
                else if (remoteFile == CurrentMasterServersFilePath)
                {
                    Interlocked.Decrement(ref _numBusyMasterServersFile);
                }
                else if (remoteFile == CurrentDownloadSourcesFilePath)
                {
                    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();
            }
Пример #3
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();
            }
Пример #4
0
        /// <summary>
        /// Starts downloading a file.
        /// </summary>
        /// <param name="remoteFile">The file to download.</param>
        /// <param name="localFilePath">The complete file path that will be used to store the downloaded file.</param>
        /// <param name="version">The file version to download.</param>
        /// <returns>True if the download was successfully started; otherwise false.</returns>
        public bool Download(string remoteFile, string localFilePath, int?version)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Attempting Download on `{0}`. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                                remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
            }

            var uriPath = RootPath;

            // Add the version directory if needed
            if (version.HasValue)
            {
                uriPath = PathHelper.CombineDifferentPaths(uriPath, PathHelper.GetVersionString(version.Value));
            }

            // Add the file to get to the path
            uriPath = PathHelper.CombineDifferentPaths(uriPath, remoteFile);

            var uri = new Uri(uriPath);

            // Ensure the directory exists
            var dir = Path.GetDirectoryName(localFilePath);

            if (dir != null)
            {
                if (!Directory.Exists(dir))
                {
                    if (log.IsDebugEnabled)
                    {
                        log.DebugFormat("Creating directory: {0}", dir);
                    }
                    Directory.CreateDirectory(dir);
                }
            }

            // Get the WebClient to use
            lock (_webClientsSync)
            {
                // Check for a free WebClient
                if (_webClients.Count == 0)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.InfoFormat(
                            "Could not start download on `{0}` since no WebClients are available. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}",
                            this, remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]");
                    }
                    return(false);
                }

                // Grab the free WebClient and start the download
                var wc = _webClients.Pop();
                wc.DownloadFileAsync(uri, localFilePath, new AsyncDownloadInfo(remoteFile, localFilePath));

                if (log.IsInfoEnabled)
                {
                    log.InfoFormat(
                        "Starting download on `{0}` using WebClient {4}. RemoteFile: {1}. LocalFilePath: {2}. Version: {3}", this,
                        remoteFile, localFilePath, version.HasValue ? version.Value.ToString() : "[NULL]", wc);
                }
            }

            return(true);
        }