#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.Collections.Generic; using System.Diagnostics; using System.Threading; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Hive; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Clients.Hive.Slave.Tests { [Item("Mock Job", "Represents mocked Job which just SpinWaits.")] [StorableClass] public class MockJob : NamedItem, IJob { public virtual bool IsParallelizable { get { return false; } } [Storable] protected bool computeInParallel; public bool ComputeInParallel { get { return computeInParallel; } set { if (computeInParallel != value) { computeInParallel = value; OnComputeInParallelChanged(); } } } [Storable] private int indexInParentOptimizerList = -1; public int IndexInParentOptimizerList { get { return indexInParentOptimizerList; } set { this.indexInParentOptimizerList = value; } } [Storable] private bool collectChildJobs; public bool CollectChildJobs { get { return collectChildJobs; } set { collectChildJobs = value; } } private int ms = 1000; //ms private bool spinWait; public MockJob(int ms, bool spinWait) { this.ms = ms; this.spinWait = spinWait; } public MockJob() { } [StorableConstructor] protected MockJob(bool deserializing) { } protected MockJob(MockJob original, Cloner cloner) : base(original, cloner) { this.ComputeInParallel = original.ComputeInParallel; this.IndexInParentOptimizerList = original.IndexInParentOptimizerList; this.CollectChildJobs = original.CollectChildJobs; this.ExecutionTime = original.executionTime; this.ExecutionState = original.executionState; this.ms = original.ms; } public override IDeepCloneable Clone(Cloner cloner) { return new MockJob(this, cloner); } [Storable] private ExecutionState executionState; public virtual ExecutionState ExecutionState { get { return executionState; } set { if (executionState != value) { this.executionState = value; OnExecutionStateChanged(); } } } [Storable] private TimeSpan executionTime; public TimeSpan ExecutionTime { get { return executionTime; } set { if (executionTime != value) { executionTime = value; OnExecutionTimeChanged(); } } } public virtual void Prepare() { this.ExecutionState = HeuristicLab.Core.ExecutionState.Prepared; } public virtual void Start() { new Thread(Run).Start(); } private void Run() { try { if (spinWait) { Stopwatch watch = new Stopwatch(); watch.Start(); do { Thread.SpinWait(1000); } while (watch.ElapsedMilliseconds < ms); watch.Stop(); } else { Thread.Sleep(ms); } Stop(); OnJobStopped(); } catch (ThreadAbortException) { //this happens when the appdomain is killed (e.g. abort from server) Stop(); } catch (Exception e) { this.ExecutionState = HeuristicLab.Core.ExecutionState.Stopped; OnJobFailed(e); } } public virtual void Pause() { this.ExecutionState = HeuristicLab.Core.ExecutionState.Paused; } public virtual void Stop() { ExecutionState = HeuristicLab.Core.ExecutionState.Stopped; } public virtual void Resume(IEnumerable childJobs) { ExecutionState = HeuristicLab.Core.ExecutionState.Stopped; } #region Events public event EventHandler> NewChildJob; protected virtual void OnNewChildJob(IJob job) { EventHandler> handler = NewChildJob; if (handler != null) handler(this, new EventArgs(job)); } public event EventHandler WaitForChildJobs; protected virtual void OnWaitForChildJobs() { EventHandler handler = WaitForChildJobs; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler DeleteChildJobs; protected virtual void OnDeleteChildJobs() { EventHandler handler = DeleteChildJobs; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ComputeInParallelChanged; protected virtual void OnComputeInParallelChanged() { EventHandler handler = ComputeInParallelChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ExecutionTimeChanged; protected virtual void OnExecutionTimeChanged() { EventHandler handler = ExecutionTimeChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ExecutionStateChanged; protected virtual void OnExecutionStateChanged() { EventHandler handler = ExecutionStateChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler JobFailed; protected virtual void OnJobFailed(Exception e) { EventHandler handler = JobFailed; EventArgs ev = new EventArgs(e); if (handler != null) handler(this, ev); } public event EventHandler JobStopped; protected virtual void OnJobStopped() { EventHandler handler = JobStopped; if (handler != null) handler(this, EventArgs.Empty); } #endregion } }