Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9035 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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