Beispiel #1
0
            /// <summary>
            /// Tries to read the text from a file.
            /// </summary>
            /// <param name="filePath">The path to the file to read.</param>
            /// <returns>The text from the <paramref name="filePath"/>, or null if it failed to read.</returns>
            string TryReadAllText(string filePath)
            {
                try
                {
                    return(File.ReadAllText(filePath));
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to read temp file `{0}`. Exception: {1}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, filePath, ex);
                    }
                    _masterReadInfo.AppendError(string.Format(errmsg, filePath, ex));
                }

                return(null);
            }
Beispiel #2
0
        /// <summary>
        /// The worker method for the reader threads.
        /// </summary>
        /// <param name="o">The arguments.</param>
        void ReadThreadWorker(object o)
        {
            var stateObj = (ThreadWorkerArgs)o;

            var callback = stateObj.Callback;
            var userState = stateObj.UserState;
            var readVersion = stateObj.Version;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Running MasterServerReader worker. UserState: {0}. ReadVersion: {1}. Callback: {2}", userState,
                    readVersion.HasValue ? readVersion.Value.ToString() : "[NULL]", callback);
            }

            var info = new MasterServerReadInfo();
            if (readVersion.HasValue)
                info.AddVersion(readVersion.Value);

            // Create the master server readers from the file
            IEnumerable<DownloadSourceDescriptor> descriptors;
            lock (_ioSync)
            {
                descriptors = DownloadSourceDescriptor.FromDescriptorFile(LocalMasterServerListPath);
            }

            // Ensure we have descriptors
            if (descriptors.Count() == 0)
            {
                const string errmsg = "No DownloadSourceDescriptors could be found.";
                if (log.IsErrorEnabled)
                    log.Error(errmsg);
                info.AppendError(errmsg);
                callback(this, info, userState);
                return;
            }

            // Create the source instances
            var sources = new List<IDownloadSource>();
            foreach (var desc in descriptors)
            {
                try
                {
                    var src = desc.Instantiate();
                    sources.Add(src);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to instantiate DownloadSourceDescriptor `{0}`: {1}";
                    if (log.IsErrorEnabled)
                        log.ErrorFormat(errmsg, desc, ex);
                    info.AppendError(string.Format(errmsg, desc, ex));
                }
            }

            // Ensure we have at least one source
            if (sources.Count == 0)
            {
                const string errmsg = "All DownloadSourceDescriptors failed to be instantiated - no servers available to use.";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg);
                info.AppendError(errmsg);
                callback(this, info, userState);
                return;
            }

            // Start the downloader
            using (var msd = new MasterServerDownloader(info, sources, readVersion) { DisposeSources = true })
            {
                // This will block until its complete
                msd.Execute();
            }

            // Save the new information to the files
            WriteMasterServersFile(info, LocalMasterServerListPath);
            WriteDownloadSourcesFile(info, LocalDownloadSourceListPath);

            // Invoke the callback
            if (callback != null)
                callback(this, info, userState);
        }
Beispiel #3
0
        /// <summary>
        /// The worker method for the reader threads.
        /// </summary>
        /// <param name="o">The arguments.</param>
        void ReadThreadWorker(object o)
        {
            var stateObj = (ThreadWorkerArgs)o;

            var callback    = stateObj.Callback;
            var userState   = stateObj.UserState;
            var readVersion = stateObj.Version;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Running MasterServerReader worker. UserState: {0}. ReadVersion: {1}. Callback: {2}", userState,
                                readVersion.HasValue ? readVersion.Value.ToString() : "[NULL]", callback);
            }

            var info = new MasterServerReadInfo();

            if (readVersion.HasValue)
            {
                info.AddVersion(readVersion.Value);
            }

            // Create the master server readers from the file
            IEnumerable <DownloadSourceDescriptor> descriptors;

            lock (_ioSync)
            {
                descriptors = DownloadSourceDescriptor.FromDescriptorFile(LocalMasterServerListPath);
            }

            // Ensure we have descriptors
            if (descriptors.Count() == 0)
            {
                const string errmsg = "No DownloadSourceDescriptors could be found.";
                if (log.IsErrorEnabled)
                {
                    log.Error(errmsg);
                }
                info.AppendError(errmsg);
                callback(this, info, userState);
                return;
            }

            // Create the source instances
            var sources = new List <IDownloadSource>();

            foreach (var desc in descriptors)
            {
                try
                {
                    var src = desc.Instantiate();
                    sources.Add(src);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Failed to instantiate DownloadSourceDescriptor `{0}`: {1}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, desc, ex);
                    }
                    info.AppendError(string.Format(errmsg, desc, ex));
                }
            }

            // Ensure we have at least one source
            if (sources.Count == 0)
            {
                const string errmsg = "All DownloadSourceDescriptors failed to be instantiated - no servers available to use.";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg);
                }
                info.AppendError(errmsg);
                callback(this, info, userState);
                return;
            }

            // Start the downloader
            using (var msd = new MasterServerDownloader(info, sources, readVersion)
            {
                DisposeSources = true
            })
            {
                // This will block until its complete
                msd.Execute();
            }

            // Save the new information to the files
            WriteMasterServersFile(info, LocalMasterServerListPath);
            WriteDownloadSourcesFile(info, LocalDownloadSourceListPath);

            // Invoke the callback
            if (callback != null)
            {
                callback(this, info, userState);
            }
        }