PollingServerWatcher.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Remote » Monitor » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Remote » Monitor » PollingServerWatcher.cs
using System;
using System.Threading;

namespace ThoughtWorks.CruiseControl.Remote.Monitor{
    /// <summary>
    /// Polls the remote server on a regular basis for any changes.
    /// </summary>
    public class PollingServerWatcher
        : IServerWatcher, IDisposable
    {
        #region Private fields
        private readonly CruiseServerClientBase client;
        private Thread pollingThread;
        private long interval = 5;
        private DateTime nextRefresh;
        private bool disposing;
        #endregion

        #region Constructors
        /// <summary>
        /// Initialise a new <see cref="PollingServerWatcher"/>.
        /// </summary>
        /// <param name="client">The underlying client to poll.</param>
        public PollingServerWatcher(CruiseServerClientBase client)
        {
            if (client == null) throw new ArgumentNullException("client");
            this.client = client;

            nextRefresh = DateTime.Now.AddSeconds(interval);
            pollingThread = new Thread(Poll);
            pollingThread.IsBackground = true;
            pollingThread.Start();
        }
        #endregion

        #region Public properties
        #region Interval
        /// <summary>
        /// The interval to poll (in seconds).
        /// </summary>
        public long Interval
        {
            get { return interval; }
            set
            {
                interval = value;
                nextRefresh = DateTime.Now.AddSeconds(interval);
            }
        }
        #endregion
        #endregion

        #region Methods
        #region Refresh()
        /// <summary>
        /// Checks the server for a refresh.
        /// </summary>
        public virtual void Refresh()
        {
            nextRefresh = DateTime.Now;
        }
        #endregion

        #region Dispose()
        /// <summary>
        /// Cleans up when this watcher is no longer needed.
        /// </summary>
        public void Dispose()
        {
            disposing = true;
        }
        #endregion
        #endregion

        #region Events
        #region Update
        /// <summary>
        /// An update has been received from a remote server.
        /// </summary>
        public event EventHandler<ServerUpdateArgs> Update;
        #endregion
        #endregion

        #region Private methods
        #region Poll()
        /// <summary>
        /// Checks to see if the server should be checked.
        /// </summary>
        private void Poll()
        {
            Thread.CurrentThread.Name = "Server watcher: " + client.Address;
            while (!disposing)
            {
                Thread.Sleep(500);
                if (!disposing && (DateTime.Now > nextRefresh))
                {
                    RetrieveSnapshot();
                    nextRefresh = DateTime.Now.AddSeconds(interval);
                }
            }
        }
        #endregion

        #region RetrieveSnapshot()
        /// <summary>
        /// Attempt to retrieve a snapshot from the remote server.
        /// </summary>
        private void RetrieveSnapshot()
        {
            // Retrieve the snapshot
            ServerUpdateArgs args;
            try
            {
                CruiseServerSnapshot snapshot = null;
                try
                {
                    client.ProcessSingleAction<object>(o =>
                    {
                        snapshot = client.GetCruiseServerSnapshot();
                    }, null);
                }
                catch (NotImplementedException)
                {
                    // This is an older style server, try fudging the snapshot
                    snapshot = new CruiseServerSnapshot
                    {
                        ProjectStatuses = client.GetProjectStatus()
                    };
                }
                args = new ServerUpdateArgs(snapshot);
            }
            catch (Exception error)
            {
                args = new ServerUpdateArgs(error);
            }

            // Fire the update
            if (Update != null)
            {
                Update(this, args);
            }
        }
        #endregion
        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.