[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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();
|
---|
[14927] | 42 | } finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public bool IsFaulted {
|
---|
| 47 | get {
|
---|
| 48 | return exceptionOccured;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public Exception Exception {
|
---|
| 53 | get {
|
---|
| 54 | return currentException;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
[7020] | 57 |
|
---|
[6976] | 58 | public int FinishedCount {
|
---|
| 59 | get {
|
---|
[9219] | 60 | try {
|
---|
| 61 | resultsLock.EnterReadLock();
|
---|
| 62 | return results.Count;
|
---|
[14927] | 63 | } finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public IDictionary<Guid, HiveTask> Results {
|
---|
| 68 | get {
|
---|
[9219] | 69 | try {
|
---|
| 70 | resultsLock.EnterReadLock();
|
---|
| 71 | return results;
|
---|
[14927] | 72 | } finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public TaskDownloader(IEnumerable<Guid> jobIds) {
|
---|
[10130] | 77 | taskIds = jobIds;
|
---|
| 78 | taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads);
|
---|
| 79 | taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
|
---|
| 80 | results = new Dictionary<Guid, HiveTask>();
|
---|
[6976] | 81 | }
|
---|
[7020] | 82 |
|
---|
[6976] | 83 | public void StartAsync() {
|
---|
| 84 | foreach (Guid taskId in taskIds) {
|
---|
[7115] | 85 | taskDownloader.DownloadTaskDataAndTask(taskId,
|
---|
[10130] | 86 | (localTask, itemTask) => {
|
---|
| 87 | if (localTask != null && itemTask != null) {
|
---|
| 88 | HiveTask hiveTask = itemTask.CreateHiveTask();
|
---|
| 89 | hiveTask.Task = localTask;
|
---|
[7115] | 90 | try {
|
---|
| 91 | resultsLock.EnterWriteLock();
|
---|
[10130] | 92 | results.Add(localTask.Id, hiveTask);
|
---|
[14927] | 93 | } finally { resultsLock.ExitWriteLock(); }
|
---|
[6976] | 94 | }
|
---|
| 95 | });
|
---|
[7020] | 96 | }
|
---|
[6976] | 97 | }
|
---|
| 98 |
|
---|
| 99 | private void taskDownloader_ExceptionOccured(object sender, EventArgs<Exception> e) {
|
---|
| 100 | OnExceptionOccured(e.Value);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 104 | private void OnExceptionOccured(Exception exception) {
|
---|
[10130] | 105 | exceptionOccured = true;
|
---|
| 106 | currentException = exception;
|
---|
[6976] | 107 | var handler = ExceptionOccured;
|
---|
| 108 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
| 109 | }
|
---|
[9219] | 110 |
|
---|
| 111 | #region IDisposable Members
|
---|
| 112 | public void Dispose() {
|
---|
| 113 | taskDownloader.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
|
---|
| 114 | resultsLock.Dispose();
|
---|
| 115 | taskDownloader.Dispose();
|
---|
| 116 | }
|
---|
| 117 | #endregion
|
---|
[6976] | 118 | }
|
---|
| 119 | }
|
---|