[4629] | 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 |
|
---|
| 22 | using System;
|
---|
[5526] | 23 | using System.Collections.Generic;
|
---|
[4629] | 24 | using System.Threading;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Clients.Hive {
|
---|
| 28 | internal class JobResultPoller {
|
---|
| 29 | private bool stopRequested { get; set; }
|
---|
| 30 | private AutoResetEvent waitHandle;
|
---|
| 31 | private Thread thread;
|
---|
| 32 |
|
---|
| 33 | private HiveJob hiveJob;
|
---|
| 34 | public HiveJob HiveJob {
|
---|
| 35 | get { return hiveJob; }
|
---|
| 36 | set { hiveJob = value; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | private TimeSpan interval;
|
---|
| 40 | public TimeSpan Interval {
|
---|
| 41 | get { return interval; }
|
---|
| 42 | set { interval = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | private bool isPolling;
|
---|
| 46 | public bool IsPolling {
|
---|
| 47 | get { return isPolling; }
|
---|
| 48 | set {
|
---|
| 49 | if (isPolling != value) {
|
---|
| 50 | isPolling = value;
|
---|
| 51 | OnIsPollingChanged();
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public JobResultPoller(HiveJob hiveJob, TimeSpan interval) {
|
---|
| 57 | this.isPolling = false;
|
---|
| 58 | this.hiveJob = hiveJob;
|
---|
| 59 | this.interval = interval;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void Start() {
|
---|
| 63 | stopRequested = false;
|
---|
| 64 | thread = new Thread(RunPolling);
|
---|
| 65 | thread.Start();
|
---|
| 66 | IsPolling = true;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public void Stop() {
|
---|
| 70 | // use AutoResetEvent.Set instead if Thread.Interrupt because its much cleaner
|
---|
| 71 | stopRequested = true;
|
---|
| 72 | waitHandle.Set();
|
---|
| 73 | IsPolling = false;
|
---|
| 74 | thread = null;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public void RunPolling() {
|
---|
| 78 | try {
|
---|
| 79 | waitHandle = new AutoResetEvent(false);
|
---|
| 80 | while (!stopRequested) {
|
---|
| 81 | OnPollingStarted();
|
---|
| 82 | FetchJobResults();
|
---|
| 83 | OnPollingFinished();
|
---|
| 84 | waitHandle.WaitOne(Interval);
|
---|
| 85 | }
|
---|
| 86 | waitHandle.Close();
|
---|
| 87 | }
|
---|
| 88 | catch (Exception e) {
|
---|
| 89 | OnExceptionOccured(e);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | private void FetchJobResults() {
|
---|
[5526] | 94 | ServiceLocator.Instance.CallHiveService(service => {
|
---|
| 95 | IEnumerable<LightweightJob> response = service.GetLightweightChildJobs(hiveJob.Job.Id, true, true);
|
---|
[4629] | 96 | OnJobResultsReceived(response);
|
---|
[5526] | 97 | });
|
---|
[4629] | 98 | }
|
---|
| 99 |
|
---|
| 100 | public event EventHandler<EventArgs<IEnumerable<LightweightJob>>> JobResultsReceived;
|
---|
| 101 | private void OnJobResultsReceived(IEnumerable<LightweightJob> lightweightJobs) {
|
---|
| 102 | var handler = JobResultsReceived;
|
---|
| 103 | if (handler != null) handler(this, new EventArgs<IEnumerable<LightweightJob>>(lightweightJobs));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 107 | private void OnExceptionOccured(Exception e) {
|
---|
| 108 | var handler = ExceptionOccured;
|
---|
| 109 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public event EventHandler IsPollingChanged;
|
---|
| 113 | private void OnIsPollingChanged() {
|
---|
| 114 | var handler = IsPollingChanged;
|
---|
| 115 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 116 | }
|
---|
| 117 | public event EventHandler PollingStarted;
|
---|
| 118 | private void OnPollingStarted() {
|
---|
| 119 | var handler = PollingStarted;
|
---|
| 120 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 121 | }
|
---|
| 122 | public event EventHandler PollingFinished;
|
---|
| 123 | private void OnPollingFinished() {
|
---|
| 124 | var handler = PollingFinished;
|
---|
| 125 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|