예제 #1
0
        /// <summary>
        /// Does all the setup of a DownloaderManager, including using Factories to construct Validators and Downloaders that it will use.
        /// Builds all the stuff up, constructs the DownloaderManager.  Packages the Mgr with its Thread in a DnldMgrHolder struct and returns it.
        /// **NOTE:  this follows a variant of the Builder Pattern, where we delegate constuction of a complex object.
        /// could also have been in a separate Builder or Factory class but small enough to put here.
        /// </summary>
        /// <param name="application">the name of the Updater target application</param>
        /// <returns>DnldMgrHolder struct containing Built DownloaderManager and the Thread it will run on</returns>
        private DnldMgrHolder SetupDownloadManager(ApplicationConfiguration application)
        {
            DownloaderManager dnldMgr = null;

            //  create an instance of a Downloader for DownloaderManager to use:
            IDownloader downloader = DownloaderFactory.Create(UpdaterConfiguration.Instance);

            //  create instance of Validator for DownloaderManager to use:
            IValidator validator = ValidatorFactory.Create(UpdaterConfiguration.Instance);

            //  populate application jobentry for shared
            DownloadJobStatusEntry dnldJob = new DownloadJobStatusEntry(application.Name, Guid.Empty, JobStatus.Ready);

            //  create the ManualResetEvent "mustStop" which we will use later if Stopping to signal "you must stop" to dnld mgr
            ManualResetEvent mustStop = new ManualResetEvent(false);

            //  create the callback delegate pointed at our RestartUpdater method so we can restart if something fails badly:
            BadExitCallback badExitCbk = new BadExitCallback(this.RestartUpdater);

            //  create the download manager, feed it our instance of actual downloader
            dnldMgr = new DownloaderManager(downloader, validator, dnldJob, mustStop, badExitCbk);

            //  create the thread that this dnldMgr + its other objects will run on
            Thread dnldThread = new Thread(new ThreadStart(dnldMgr.RunDownloader));

            dnldThread.Name = "DownloadManagerThread_" + application.Name + "_" + _guidUniqueName.ToString();

            //  create new holder struct for thread + dnldmgr
            DnldMgrHolder holder = new DnldMgrHolder(dnldThread, dnldMgr);

            #region    Event Registrations

            //  ***  NOTE:  we facade all these underlying events through this class to
            //  ***			a)  avoid exposing dnldMgr class
            //  ***			b)  so later if we choose to use a better threading model--that is, NOT allow
            //  ***				our threads to "venture outside" and possibly dawdle out there (get blocked for instance)
            //  ***				then it will be easy to spawn a dedicated eventing thread in here.
            //  ***  NOTE:  IF we have more than one app we're updating, then we must hook EACH DOWNLOADMANAGER's events.
            //  ***  the sinking app will have to distinguish which app fired which event from the event args

            //  REGISTER for DownloaderManager's events--we do not want to expose downloader directly, so
            //  we shuttle its events through this class
            dnldMgr.ServerManifestDownloaded += new UpdaterActionEventHandler(OnServerManifestDownloaded);
            dnldMgr.UpdateAvailable          += new UpdaterActionEventHandler(OnUpdateAvailable);
            dnldMgr.DownloadStarted          += new UpdaterActionEventHandler(OnDownloadStarted);
            dnldMgr.DownloadCompleted        += new UpdaterActionEventHandler(OnDownloadCompleted);
            dnldMgr.ManifestValidated        += new UpdaterActionEventHandler(OnManifestValidated);
            dnldMgr.ManifestValidationFailed += new UpdaterActionEventHandler(OnManifestValidationFailed);
            dnldMgr.FilesValidated           += new UpdaterActionEventHandler(OnFilesValidated);
            dnldMgr.FilesValidationFailed    += new UpdaterActionEventHandler(OnFilesValidationFailed);

            #endregion

            //  return the packaged DnldMgr
            return(holder);
        }
예제 #2
0
        /// <summary>
        /// Internal helper, stops the specified DownloadManager by application name
        /// </summary>
        private void StopUpdaterHelper(string appName)
        {
            bool isGoodStop = false;

            //  unpackage objects from holder
            DownloaderManager dnldMgr    = ((DnldMgrHolder)_dnldHolders[appName]).dnldMgr;
            Thread            dnldThread = ((DnldMgrHolder)_dnldHolders[appName]).dnldThread;

            //  signal downloaderMgr it's time to stop, chance to exit gracefully:
            if (null != dnldMgr)
            {
                dnldMgr.MustStopUpdating.Set();
            }

            //  check if the thread is event started
            if ((null != dnldThread) && (System.Threading.ThreadState.Unstarted != dnldThread.ThreadState))
            {
                //  wait to join for reasonable timeout
                isGoodStop = dnldThread.Join(TIMEOUT_THREAD_JOIN);
            }
            else
            {
                isGoodStop = true;
            }

            //  if it's not a clean join, then interrupt thread
            if (!isGoodStop)
            {
                dnldThread.Interrupt();
                //  log problem
                ApplicationUpdateManager.TraceWrite("[ApplicationUpdateManager.StopUpdater]", "RES_StopUpdaterInterruptThread", dnldThread.Name);
            }


            //  announce we are stopping:
            ApplicationUpdateManager.TraceWrite(
                "[ApplicationUpdateManager.StopUpdater]",
                "RESX_MESSAGE_UpdaterStopped",
                dnldMgr.ApplicationName,
                DateTime.Now.ToString(Resource.ResourceManager["RESX_DateTimeToStringFormat"], CultureInfo.CurrentCulture));
        }
 public DnldMgrHolder( Thread dnldT, DownloaderManager dnldM )
 {
     dnldThread = dnldT;
     dnldMgr = dnldM;
     restartTimer = null;
 }
        /// <summary>
        /// Does all the setup of a DownloaderManager, including using Factories to construct Validators and Downloaders that it will use.
        /// Builds all the stuff up, constructs the DownloaderManager.  Packages the Mgr with its Thread in a DnldMgrHolder struct and returns it.
        /// **NOTE:  this follows a variant of the Builder Pattern, where we delegate constuction of a complex object.
        /// could also have been in a separate Builder or Factory class but small enough to put here.
        /// </summary>
        /// <param name="application">the name of the Updater target application</param>
        /// <returns>DnldMgrHolder struct containing Built DownloaderManager and the Thread it will run on</returns>
        private DnldMgrHolder SetupDownloadManager( ApplicationConfiguration application )
        {
            DownloaderManager dnldMgr = null;

            //  create an instance of a Downloader for DownloaderManager to use:
            IDownloader downloader = DownloaderFactory.Create( UpdaterConfiguration.Instance );

            //  create instance of Validator for DownloaderManager to use:
            IValidator validator = ValidatorFactory.Create( UpdaterConfiguration.Instance );

            //  populate application jobentry for shared
            DownloadJobStatusEntry dnldJob = new DownloadJobStatusEntry( application.Name, Guid.Empty, JobStatus.Ready );

            //  create the ManualResetEvent "mustStop" which we will use later if Stopping to signal "you must stop" to dnld mgr
            ManualResetEvent mustStop = new ManualResetEvent( false );

            //  create the callback delegate pointed at our RestartUpdater method so we can restart if something fails badly:
            BadExitCallback badExitCbk = new BadExitCallback( this.RestartUpdater );

            //  create the download manager, feed it our instance of actual downloader
            dnldMgr = new DownloaderManager( downloader, validator, dnldJob, mustStop, badExitCbk );

            //  create the thread that this dnldMgr + its other objects will run on
            Thread dnldThread = new Thread( new ThreadStart( dnldMgr.RunDownloader ) );
            dnldThread.Name = "DownloadManagerThread_" + application.Name + "_" + _guidUniqueName.ToString();

            //  create new holder struct for thread + dnldmgr
            DnldMgrHolder holder = new DnldMgrHolder( dnldThread, dnldMgr );

            #region    Event Registrations

            //  ***  NOTE:  we facade all these underlying events through this class to
            //  ***			a)  avoid exposing dnldMgr class
            //  ***			b)  so later if we choose to use a better threading model--that is, NOT allow
            //  ***				our threads to "venture outside" and possibly dawdle out there (get blocked for instance)
            //  ***				then it will be easy to spawn a dedicated eventing thread in here.
            //  ***  NOTE:  IF we have more than one app we're updating, then we must hook EACH DOWNLOADMANAGER's events.
            //  ***  the sinking app will have to distinguish which app fired which event from the event args

            //  REGISTER for DownloaderManager's events--we do not want to expose downloader directly, so
            //  we shuttle its events through this class
            dnldMgr.ServerManifestDownloaded += new UpdaterActionEventHandler( OnServerManifestDownloaded );
            dnldMgr.UpdateAvailable += new UpdaterActionEventHandler( OnUpdateAvailable );
            dnldMgr.DownloadStarted += new UpdaterActionEventHandler( OnDownloadStarted );
            dnldMgr.DownloadCompleted += new UpdaterActionEventHandler( OnDownloadCompleted );
            dnldMgr.ManifestValidated +=new UpdaterActionEventHandler( OnManifestValidated );
            dnldMgr.ManifestValidationFailed +=new UpdaterActionEventHandler( OnManifestValidationFailed );
            dnldMgr.FilesValidated +=new UpdaterActionEventHandler( OnFilesValidated );
            dnldMgr.FilesValidationFailed +=new UpdaterActionEventHandler( OnFilesValidationFailed );

            #endregion

            //  return the packaged DnldMgr
            return holder;
        }
예제 #5
0
 public DnldMgrHolder(Thread dnldT, DownloaderManager dnldM)
 {
     dnldThread   = dnldT;
     dnldMgr      = dnldM;
     restartTimer = null;
 }