#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Threading; using HeuristicLab.Clients.Hive.Jobs; using HeuristicLab.Common; namespace HeuristicLab.Clients.Hive { public class TaskDownloader : IDisposable { private IEnumerable taskIds; private ConcurrentTaskDownloader taskDownloader; private IDictionary results; private bool exceptionOccured = false; private Exception currentException; private ReaderWriterLockSlim resultsLock = new ReaderWriterLockSlim(); public bool IsFinished { get { try { resultsLock.EnterReadLock(); return results.Count == taskIds.Count(); } finally { resultsLock.ExitReadLock(); } } } public bool IsFaulted { get { return exceptionOccured; } } public Exception Exception { get { return currentException; } } public int FinishedCount { get { try { resultsLock.EnterReadLock(); return results.Count; } finally { resultsLock.ExitReadLock(); } } } public IDictionary Results { get { try { resultsLock.EnterReadLock(); return results; } finally { resultsLock.ExitReadLock(); } } } public TaskDownloader(IEnumerable jobIds) { this.taskIds = jobIds; this.taskDownloader = new ConcurrentTaskDownloader(Settings.Default.MaxParallelDownloads, Settings.Default.MaxParallelDownloads); this.taskDownloader.ExceptionOccured += new EventHandler>(taskDownloader_ExceptionOccured); this.results = new Dictionary(); } public void StartAsync() { foreach (Guid taskId in taskIds) { taskDownloader.DownloadTaskDataAndTask(taskId, (localJob, itemJob) => { if (localJob != null && itemJob != null) { HiveTask hiveTask; if (itemJob is OptimizerTask) { hiveTask = new OptimizerHiveTask((OptimizerTask)itemJob); } else { hiveTask = new HiveTask(itemJob, true); } hiveTask.Task = localJob; try { resultsLock.EnterWriteLock(); this.results.Add(localJob.Id, hiveTask); } finally { resultsLock.ExitWriteLock(); } } }); } } private void taskDownloader_ExceptionOccured(object sender, EventArgs e) { OnExceptionOccured(e.Value); } public event EventHandler> ExceptionOccured; private void OnExceptionOccured(Exception exception) { this.exceptionOccured = true; this.currentException = exception; var handler = ExceptionOccured; if (handler != null) handler(this, new EventArgs(exception)); } #region IDisposable Members public void Dispose() { taskDownloader.ExceptionOccured -= new EventHandler>(taskDownloader_ExceptionOccured); resultsLock.Dispose(); taskDownloader.Dispose(); } #endregion } }