/* * Copyright (c) 2009 Craig Sutherland * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections.Generic; using NLog; using ThoughtWorks.CruiseControl.Remote; using ThoughtWorks.CruiseControl.Remote.Messages; namespace FastForward.Cache { /// /// An engine for caching server information. /// public class CacheEngine : IDisposable { #region Private fields private Dictionary clients = new Dictionary(); private Logger logger; private DatabaseConnection database; #endregion #region Constructors /// /// Initialise the cache engine. /// /// public CacheEngine(Logger logger) { this.logger = logger; } #endregion #region Public methods #region Initialise() /// /// Initialise the cache engine. /// /// The configuration to use. public void Initialise(CacheConfiguration configuration) { // Initialise a new client factory that forces each instance to be unique logger.Debug("Starting client factory"); var factory = new CruiseServerClientFactory { UseClientCaching = false }; // Initialise the database logger.Debug("Initialising local cache database"); database = DatabaseConnection.Initialise(configuration.DatabaseType, configuration.ConnectionString); foreach (var server in configuration.Servers) { // Initialise each client logger.Info("Starting client: {0} [{1}]", server.Address, server.TransportMode); CruiseServerClientBase client = null; switch (server.TransportMode) { case ServerTransportMode.Default: client = factory.GenerateClient(server.Address, server.TargetServer, server.Settings); break; case ServerTransportMode.Http: client = factory.GenerateHttpClient(server.Address, server.TargetServer, server.Settings); break; case ServerTransportMode.Remoting: client = factory.GenerateRemotingClient(server.Address, server.TargetServer, server.Settings); break; case ServerTransportMode.Cache: client = InitialiseCacheConnection(server); break; } // Add the client to the dictionary clients.Add(server.Name, new ServerStateCache(logger, client, server.PollingInterval, database)); } } #endregion #region Dispose() /// /// Clean up. /// public void Dispose() { foreach (var client in clients.Values) { logger.Info("Disposing client: {0}", client.Client.Address); client.Dispose(); } } #endregion #region ProcessMessage() /// /// Process a message for the remote server. /// /// /// /// public Response ProcessMessage(string action, ServerRequest request) { if (clients.ContainsKey(request.ServerName)) { var client = clients[request.ServerName]; var response = client.ProcessMessage(action, request); return response; } else { throw new ApplicationException("Server not found"); } } #endregion #endregion #region Private methods #region InitialiseCacheConnection() /// /// Initialise a connection to a cache server. /// /// /// private CruiseServerClientBase InitialiseCacheConnection(ServerConfiguration server) { var connection = new CacheConnection(server.Address); var actualConnection = CruiseServerClientFactory.BuildUpConnection(connection, server.Settings); var client = new CruiseServerClient(actualConnection); client.TargetServer = server.TargetServer; return client; } #endregion #endregion } }