Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.3/TaskDownloader.cs @ 6984

Last change on this file since 6984 was 6743, checked in by ascheibe, 13 years ago

#1233

  • fixed a bug in the Slave UI
  • finished renaming Webservice and Dao methods to be consistent with Job/Task naming
  • some cosmetic changes and project dependencies cleanups
File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Clients.Hive.Jobs;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Clients.Hive {
29  public class TaskDownloader {
30    private IEnumerable<Guid> taskIds;
31    private ConcurrentTaskDownloader<ItemTask> taskDownloader;
32    private IDictionary<Guid, HiveTask> results;
33    private bool exceptionOccured = false;
34    private Exception currentException;
35
36    public bool IsFinished {
37      get {
38        return results.Count == taskIds.Count();
39      }
40    }
41
42    public bool IsFaulted {
43      get {
44        return exceptionOccured;
45      }
46    }
47
48    public Exception Exception {
49      get {
50        return currentException;
51      }
52    }
53   
54    public int FinishedCount {
55      get {
56        return results.Count;
57      }
58    }
59
60    public IDictionary<Guid, HiveTask> Results {
61      get {
62        return results;
63      }
64    }
65
66    public TaskDownloader(IEnumerable<Guid> jobIds) {
67      this.taskIds = jobIds;
68      this.taskDownloader = new ConcurrentTaskDownloader<ItemTask>(2, 2);
69      this.taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
70      this.results = new Dictionary<Guid, HiveTask>();
71    }
72   
73    public void StartAsync() {
74      foreach (Guid taskId in taskIds) {
75        Task task = ServiceLocator.Instance.CallHiveService(s => s.GetTask(taskId));
76
77        taskDownloader.DownloadTask(task,
78          (localJob, itemJob) => {
79            if (localJob != null && itemJob != null) {
80              HiveTask hiveTask;
81              if (itemJob is OptimizerTask) {
82                hiveTask = new OptimizerHiveTask((OptimizerTask)itemJob);
83              } else {
84                hiveTask = new HiveTask(itemJob, true);
85              }
86              hiveTask.Task = localJob;
87              this.results.Add(localJob.Id, hiveTask);
88            }
89          });
90      }     
91    }
92
93    private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
94      OnExceptionOccured(e.Value);
95    }
96
97    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
98    private void OnExceptionOccured(Exception exception) {
99      this.exceptionOccured = true;
100      this.currentException = exception;
101      var handler = ExceptionOccured;
102      if (handler != null) handler(this, new EventArgs<Exception>(exception));
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.