Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs @ 9456

Last change on this file since 9456 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
25using System.Threading;
26using HeuristicLab.Clients.Hive.Jobs;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Clients.Hive {
30  public class TaskDownloader : IDisposable {
31    private IEnumerable<Guid> taskIds;
32    private ConcurrentTaskDownloader<ItemTask> taskDownloader;
33    private IDictionary<Guid, HiveTask> results;
34    private bool exceptionOccured = false;
35    private Exception currentException;
36    private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim();
37
38    public bool IsFinished {
39      get {
40        try {
41          resultsLock.EnterReadLock();
42          return results.Count == taskIds.Count();
43        }
44        finally { resultsLock.ExitReadLock(); }
45      }
46    }
47
48    public bool IsFaulted {
49      get {
50        return exceptionOccured;
51      }
52    }
53
54    public Exception Exception {
55      get {
56        return currentException;
57      }
58    }
59
60    public int FinishedCount {
61      get {
62        try {
63          resultsLock.EnterReadLock();
64          return results.Count;
65        }
66        finally { resultsLock.ExitReadLock(); }
67      }
68    }
69
70    public IDictionary<Guid, HiveTask> Results {
71      get {
72        try {
73          resultsLock.EnterReadLock();
74          return results;
75        }
76        finally { resultsLock.ExitReadLock(); }
77      }
78    }
79
80    public TaskDownloader(IEnumerable<Guid> jobIds) {
81      this.taskIds = jobIds;
82      this.taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
83      this.taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
84      this.results = new Dictionary<Guid, HiveTask>();
85    }
86
87    public void StartAsync() {
88      foreach (Guid taskId in taskIds) {
89        taskDownloader.DownloadTaskDataAndTask(taskId,
90          (localJob, itemJob) => {
91            if (localJob != null && itemJob != null) {
92              HiveTask hiveTask;
93              if (itemJob is OptimizerTask) {
94                hiveTask = new OptimizerHiveTask((OptimizerTask)itemJob);
95              } else {
96                hiveTask = new HiveTask(itemJob, true);
97              }
98              hiveTask.Task = localJob;
99              try {
100                resultsLock.EnterWriteLock();
101                this.results.Add(localJob.Id, hiveTask);
102              }
103              finally { resultsLock.ExitWriteLock(); }
104            }
105          });
106      }
107    }
108
109    private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
110      OnExceptionOccured(e.Value);
111    }
112
113    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
114    private void OnExceptionOccured(Exception exception) {
115      this.exceptionOccured = true;
116      this.currentException = exception;
117      var handler = ExceptionOccured;
118      if (handler != null) handler(this, new EventArgs<Exception>(exception));
119    }
120
121    #region IDisposable Members
122    public void Dispose() {
123      taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
124      resultsLock.Dispose();
125      taskDownloader.Dispose();
126    }
127    #endregion
128  }
129}
Note: See TracBrowser for help on using the repository browser.