[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[9363] | 25 | using System.Threading;
|
---|
[6976] | 26 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Clients.Hive {
|
---|
[9363] | 30 | public class TaskDownloader : IDisposable {
|
---|
[6976] | 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 {
|
---|
[9363] | 40 | try {
|
---|
| 41 | resultsLock.EnterReadLock();
|
---|
| 42 | return results.Count == taskIds.Count();
|
---|
| 43 | }
|
---|
| 44 | finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 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 | }
|
---|
[7020] | 59 |
|
---|
[6976] | 60 | public int FinishedCount {
|
---|
| 61 | get {
|
---|
[9363] | 62 | try {
|
---|
| 63 | resultsLock.EnterReadLock();
|
---|
| 64 | return results.Count;
|
---|
| 65 | }
|
---|
| 66 | finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public IDictionary<Guid, HiveTask> Results {
|
---|
| 71 | get {
|
---|
[9363] | 72 | try {
|
---|
| 73 | resultsLock.EnterReadLock();
|
---|
| 74 | return results;
|
---|
| 75 | }
|
---|
| 76 | finally { resultsLock.ExitReadLock(); }
|
---|
[6976] | 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[9508] | 80 | public TaskDownloader(IEnumerable<Guid> jobIds, IHiveServiceLocator locator) {
|
---|
[6976] | 81 | this.taskIds = jobIds;
|
---|
[9508] | 82 | this.taskDownloader = new ConcurrentTaskDownloader<ItemTask>(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads, locator);
|
---|
| 83 | this.taskDownloader.ExceptionOccured += new EventHandler<EventArgs<Exception>>(taskDownloader_ExceptionOccured);
|
---|
[6976] | 84 | this.results = new Dictionary<Guid, HiveTask>();
|
---|
| 85 | }
|
---|
[7020] | 86 |
|
---|
[6976] | 87 | public void StartAsync() {
|
---|
| 88 | foreach (Guid taskId in taskIds) {
|
---|
[7115] | 89 | taskDownloader.DownloadTaskDataAndTask(taskId,
|
---|
[6976] | 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;
|
---|
[7115] | 99 | try {
|
---|
| 100 | resultsLock.EnterWriteLock();
|
---|
| 101 | this.results.Add(localJob.Id, hiveTask);
|
---|
[9363] | 102 | }
|
---|
| 103 | finally { resultsLock.ExitWriteLock(); }
|
---|
[6976] | 104 | }
|
---|
| 105 | });
|
---|
[7020] | 106 | }
|
---|
[6976] | 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 | }
|
---|
[9363] | 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
|
---|
[6976] | 128 | }
|
---|
| 129 | }
|
---|