Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockJob.cs @ 5234

Last change on this file since 5234 was 5156, checked in by ascheibe, 14 years ago

#1233

  • Implemented communication interface between Slave and a Slave Client using Named Pipes and callbacks
  • Added new project for testing Slave - Client communication
  • Added some copyright info headers
File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Hive;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Clients.Hive.Slave.Tests {
32  [Item("Mock Job", "Represents mocked Job which just SpinWaits.")]
33  [StorableClass]
34  public class MockJob : NamedItem, IJob {
35    public virtual bool IsParallelizable {
36      get { return false; }
37    }
38
39    [Storable]
40    protected bool computeInParallel;
41    public bool ComputeInParallel {
42      get { return computeInParallel; }
43      set {
44        if (computeInParallel != value) {
45          computeInParallel = value;
46          OnComputeInParallelChanged();
47        }
48      }
49    }
50
51    [Storable]
52    private int indexInParentOptimizerList = -1;
53    public int IndexInParentOptimizerList {
54      get { return indexInParentOptimizerList; }
55      set { this.indexInParentOptimizerList = value; }
56    }
57
58    [Storable]
59    private bool collectChildJobs;
60    public bool CollectChildJobs {
61      get { return collectChildJobs; }
62      set { collectChildJobs = value; }
63    }
64
65    private int ms = 1000; //ms
66    private bool spinWait;
67    public MockJob(int ms, bool spinWait) {
68      this.ms = ms;
69      this.spinWait = spinWait;
70    }
71    public MockJob() { }
72    [StorableConstructor]
73    protected MockJob(bool deserializing) { }
74    protected MockJob(MockJob original, Cloner cloner)
75      : base(original, cloner) {
76      this.ComputeInParallel = original.ComputeInParallel;
77      this.IndexInParentOptimizerList = original.IndexInParentOptimizerList;
78      this.CollectChildJobs = original.CollectChildJobs;
79      this.ExecutionTime = original.executionTime;
80      this.ExecutionState = original.executionState;
81      this.ms = original.ms;
82    }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new MockJob(this, cloner);
85    }
86
87    [Storable]
88    private ExecutionState executionState;
89    public virtual ExecutionState ExecutionState {
90      get { return executionState; }
91      set {
92        if (executionState != value) {
93          this.executionState = value;
94          OnExecutionStateChanged();
95        }
96      }
97    }
98
99    [Storable]
100    private TimeSpan executionTime;
101    public TimeSpan ExecutionTime {
102      get { return executionTime; }
103      set {
104        if (executionTime != value) {
105          executionTime = value;
106          OnExecutionTimeChanged();
107        }
108      }
109    }
110
111    public virtual void Prepare() {
112      this.ExecutionState = Core.ExecutionState.Prepared;
113    }
114
115    public virtual void Start() {
116      new Thread(Run).Start();
117    }
118
119    private void Run() {
120      try {
121        if (spinWait) {
122          Stopwatch watch = new Stopwatch();
123          watch.Start();
124          do {
125            Thread.SpinWait(1000);
126          } while (watch.ElapsedMilliseconds < ms);
127          watch.Stop();
128        } else {
129          Thread.Sleep(ms);
130        }
131        Stop();
132        OnJobStopped();
133      }
134      catch (ThreadAbortException) {
135        //this happens when the appdomain is killed (e.g. abort from server)
136        Stop();
137      }
138      catch (Exception e) {
139        this.ExecutionState = Core.ExecutionState.Stopped;
140        OnJobFailed(e);
141      }
142    }
143
144    public virtual void Pause() {
145      this.ExecutionState = Core.ExecutionState.Paused;
146    }
147
148    public virtual void Stop() {
149      ExecutionState = Core.ExecutionState.Stopped;
150    }
151
152    public virtual void Resume(IEnumerable<IJob> childJobs) {
153      ExecutionState = Core.ExecutionState.Stopped;
154    }
155
156    #region Events
157    public event EventHandler<EventArgs<IJob>> NewChildJob;
158    protected virtual void OnNewChildJob(IJob job) {
159      EventHandler<EventArgs<IJob>> handler = NewChildJob;
160      if (handler != null) handler(this, new EventArgs<IJob>(job));
161    }
162
163    public event EventHandler WaitForChildJobs;
164    protected virtual void OnWaitForChildJobs() {
165      EventHandler handler = WaitForChildJobs;
166      if (handler != null) handler(this, EventArgs.Empty);
167    }
168
169    public event EventHandler DeleteChildJobs;
170    protected virtual void OnDeleteChildJobs() {
171      EventHandler handler = DeleteChildJobs;
172      if (handler != null) handler(this, EventArgs.Empty);
173    }
174
175    public event EventHandler ComputeInParallelChanged;
176    protected virtual void OnComputeInParallelChanged() {
177      EventHandler handler = ComputeInParallelChanged;
178      if (handler != null) handler(this, EventArgs.Empty);
179    }
180
181    public event EventHandler ExecutionTimeChanged;
182    protected virtual void OnExecutionTimeChanged() {
183      EventHandler handler = ExecutionTimeChanged;
184      if (handler != null) handler(this, EventArgs.Empty);
185    }
186
187    public event EventHandler ExecutionStateChanged;
188    protected virtual void OnExecutionStateChanged() {
189      EventHandler handler = ExecutionStateChanged;
190      if (handler != null) handler(this, EventArgs.Empty);
191    }
192
193    public event EventHandler JobFailed;
194    protected virtual void OnJobFailed(Exception e) {
195      EventHandler handler = JobFailed;
196      EventArgs<Exception> ev = new EventArgs<Exception>(e);
197      if (handler != null) handler(this, ev);
198    }
199
200    public event EventHandler JobStopped;
201    protected virtual void OnJobStopped() {
202      EventHandler handler = JobStopped;
203      if (handler != null) handler(this, EventArgs.Empty);
204    }
205
206    #endregion
207  }
208}
Note: See TracBrowser for help on using the repository browser.