[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[9219] | 25 | using System.Threading;
|
---|
[6976] | 26 | using HeuristicLab.Common;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Clients.Hive {
|
---|
[9219] | 29 | public class TaskDownloader : IDisposable {
|
---|
[6976] | 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;
|
---|
[7115] | 35 | private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim();
|
---|
[6976] | 36 |
|
---|
| 37 | public bool IsFinished {
|
---|
| 38 | get {
|
---|
[9219] | 39 | try {
|
---|
| 40 | resultsLock.EnterReadLock();
|
---|
| 41 | return results.Count == taskIds.Count();
|
---|
| 42 | }
|
---|
| 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 {
|
---|
[9219] | 61 | try {
|
---|
| 62 | resultsLock.EnterReadLock();
|
---|
| 63 | return results.Count;
|
---|
| 64 | }
|
---|
| 65 | finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public IDictionary<Guid, HiveTask> Results {
|
---|
| 70 | get {
|
---|
[9219] | 71 | try {
|
---|
| 72 | resultsLock.EnterReadLock();
|
---|
| 73 | return results;
|
---|
| 74 | }
|
---|
| 75 | finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public TaskDownloader(IEnumerable<Guid> jobIds) {
|
---|
[11083] | 80 | taskIds = jobIds;
|
---|
| 81 | taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
| 82 | taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
|
---|
| 83 | results = new Dictionary<Guid, HiveTask>();
|
---|
[6976] | 84 | }
|
---|
[7020] | 85 |
|
---|
[6976] | 86 | public void StartAsync() {
|
---|
| 87 | foreach (Guid taskId in taskIds) {
|
---|
[7115] | 88 | taskDownloader.DownloadTaskDataAndTask(taskId,
|
---|
[11083] | 89 | (localTask, itemTask) => {
|
---|
| 90 | if (localTask != null && itemTask != null) {
|
---|
| 91 | HiveTask hiveTask = itemTask.CreateHiveTask();
|
---|
| 92 | hiveTask.Task = localTask;
|
---|
[7115] | 93 | try {
|
---|
| 94 | resultsLock.EnterWriteLock();
|
---|
[11083] | 95 | results.Add(localTask.Id, hiveTask);
|
---|
[9219] | 96 | }
|
---|
| 97 | finally { resultsLock.ExitWriteLock(); }
|
---|
[6976] | 98 | }
|
---|
| 99 | });
|
---|
[7020] | 100 | }
|
---|
[6976] | 101 | }
|
---|
| 102 |
|
---|
| 103 | private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
| 104 | OnExceptionOccured(e.Value);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 108 | private void OnExceptionOccured(Exception exception) {
|
---|
[11083] | 109 | exceptionOccured = true;
|
---|
| 110 | currentException = exception;
|
---|
[6976] | 111 | var handler = ExceptionOccured;
|
---|
| 112 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 113 | }
|
---|
[9219] | 114 |
|
---|
| 115 | #region IDisposable Members
|
---|
| 116 | public void Dispose() {
|
---|
| 117 | taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
|
---|
| 118 | resultsLock.Dispose();
|
---|
| 119 | taskDownloader.Dispose();
|
---|
| 120 | }
|
---|
| 121 | #endregion
|
---|
[6976] | 122 | }
|
---|
| 123 | }
|
---|