/* * 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 System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Configuration; using NLog; namespace FastForward.Cache { public partial class CacheService : ServiceBase { private CacheConfiguration configuration; private CacheEngine engine; private RemotingServer remotingServer; private bool isLoaded = false; private bool isDisposed = false; private object lockObject = new object(); private Logger logger = LogManager.GetCurrentClassLogger(); public CacheService() { RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false); ServiceName = "FastForward.Cache"; } public void Run(string[] args) { // Ensure we only start once lock (lockObject) { if (isLoaded) return; isLoaded = true; } // Load the configuration logger.Debug("Loading configuration"); configuration = ConfigurationManager.GetSection("FastForward.Cache") as CacheConfiguration; if (configuration == null) { var error = new Exception("Unable to load configuration"); logger.Fatal(error.Message); throw error; } // Initialise the engine try { logger.Debug("Initialising cache engine"); engine = new CacheEngine(logger); engine.Initialise(configuration); } catch (Exception error) { logger.Fatal("Unable to start engine: {0}", error.Message); throw; } // Initialise remoting if (!string.IsNullOrEmpty(configuration.RemotingAddress)) { logger.Debug("Initialising remoting"); remotingServer = new RemotingServer(logger, engine); RemotingServices.Marshal(remotingServer, configuration.RemotingAddress); foreach (var channel in ChannelServices.RegisteredChannels) { logger.Info("Remoting channel {0} initialised", channel.ChannelName); var receiver = channel as IChannelReceiver; if (receiver != null) { foreach (string url in receiver.GetUrlsForUri(configuration.RemotingAddress)) { logger.Info("Listening on url: {0}", url); } } } } isDisposed = false; } protected override void Dispose(bool disposing) { base.Dispose(disposing); // Ensure we only dispose once lock (lockObject) { if (isDisposed) return; isDisposed = true; } // Clean up remoting if (!string.IsNullOrEmpty(configuration.RemotingAddress)) { logger.Debug("Terminating remoting"); RemotingServices.Disconnect(remotingServer); foreach (var channel in ChannelServices.RegisteredChannels) { ChannelServices.UnregisterChannel(channel); } remotingServer = null; } // Clean up everything else logger.Debug("Terminating cache engine"); engine.Dispose(); engine = null; configuration = null; isLoaded = false; } protected override void OnStart(string[] args) { logger.Info("Start received"); Run(args); } protected override void OnStop() { logger.Info("Stop received"); Dispose(); } } }