#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Threading; using HeuristicLab.Hive.Contracts.Interfaces; using HeuristicLab.Hive.Tracing; using System.Collections.Generic; using HeuristicLab.PluginInfrastructure.Manager; using System.IO; namespace HeuristicLab.Hive.Server.Core { public class LifecycleManager : ILifecycleManager { private readonly string pluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Plugins"); private AutoResetEvent waitHandle; private Thread thread; private bool shutdownRequested; private PluginManager pm; private TimeSpan interval = new TimeSpan(0, 0, 10); public TimeSpan Interval { get { return interval; } set { interval = value; } } private int jobsCurrentlyTransfering = 0; public int JobsCurrentlyTransferring { get { return jobsCurrentlyTransfering; } set { if (jobsCurrentlyTransfering != value) { jobsCurrentlyTransfering = value; Logger.Info("JobsCurrentlyTransfering: " + jobsCurrentlyTransfering); } } } private IEnumerable plugins; public IEnumerable Plugins { get { return pm.Plugins; } } private byte[] configurationFile; public byte[] ConfigurationFile { get { if (configurationFile == null) { configurationFile = File.ReadAllBytes(Path.Combine(pluginDir, "HeuristicLab 3.3.exe.config")); } return configurationFile; } } #region ILifecycleManager Members public void Start() { LoadPlugins(); Thread thread = new Thread(RunHeartbeatThread); thread.Start(); using (ServiceLocator.GetContextFactory().GetContext()) { OnStarted(); } } private void LoadPlugins() { pm = new PluginManager(pluginDir); pm.DiscoverAndCheckPlugins(); pm.InitializeLifetimeService(); } private void RunHeartbeatThread() { waitHandle = new AutoResetEvent(false); shutdownRequested = false; while (!shutdownRequested) { using (ServiceLocator.GetContextFactory().GetContext(false)) { OnServerHeartbeat(); } waitHandle.WaitOne(this.Interval); } waitHandle.Close(); using (ServiceLocator.GetContextFactory().GetContext()) { OnStopped(); } } public void Stop() { shutdownRequested = true; if (waitHandle != null) waitHandle.Set(); if (thread != null) thread.Join(); thread = null; } public event EventHandler ServerHeartbeat; private void OnServerHeartbeat() { var handler = ServerHeartbeat; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Started; private void OnStarted() { var handler = Started; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Stopped; private void OnStopped() { var handler = Stopped; if (handler != null) handler(this, EventArgs.Empty); } #endregion } }