#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Linq; using HeuristicLab.Collections; using HeuristicLab.Core; using HeuristicLab.Hive.Contracts; using HeuristicLab.Hive.Contracts.BusinessObjects; using HeuristicLab.Hive.Contracts.Interfaces; using HeuristicLab.Hive.Contracts.ResponseObjects; namespace HeuristicLab.Hive.Experiment { [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")] [Creatable("Hive")] public class HiveClient : Item, IProgressReporter { private static object locker = new object(); private bool currentlyUpdating; private ILog log; public ILog Log { get { return log; } } private HiveExperimentList hiveExperiments; public HiveExperimentList HiveExperiments { get { return hiveExperiments; } set { if (hiveExperiments != value) { DeRegisterHiveExperimentsEvents(); hiveExperiments = value; RegisterHiveExperimentsEvent(); OnHiveExperimentsChanged(); } } } private bool isProgressing; public bool IsProgressing { get { return isProgressing; } set { if (isProgressing != value) { isProgressing = value; OnIsProgressingChanged(); } } } private IProgress progress; public IProgress Progress { get { return progress; } } private void RegisterHiveExperimentsEvent() { if (hiveExperiments != null) { hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); } } private void DeRegisterHiveExperimentsEvents() { if (hiveExperiments != null) { hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler>(hiveExperiments_ItemsRemoved); } } public HiveClient() { this.log = new Log(); } public override Common.IDeepCloneable Clone(Common.Cloner cloner) { HiveClient clone = (HiveClient)base.Clone(cloner); clone.log = (ILog)cloner.Clone(this.log); clone.hiveExperiments = (HiveExperimentList)cloner.Clone(this.hiveExperiments); return clone; } public void UpdateExperimentList() { this.progress = new Progress("Downloading HiveExperiments..."); try { IsProgressing = true; if (this.HiveExperiments == null) { this.HiveExperiments = new HiveExperimentList(); } using (Disposable service = ServiceLocator.Instance.ClientFacadePool.GetService()) { currentlyUpdating = true; ResponseObject response = service.Obj.GetHiveExperiments(); progress.Status = "Populating HiveExperiment list..."; RefreshExperimentList(response.Obj); currentlyUpdating = false; } } catch (Exception) { this.HiveExperiments = null; throw; } finally { IsProgressing = false; } } private void RefreshExperimentList(HiveExperimentDtoList hiveExperiments) { foreach (HiveExperimentDto hiveExperimentDto in hiveExperiments) { HiveExperiment hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id); if (hiveExperiment == null) { // not yet there, create new this.HiveExperiments.Add(new HiveExperiment(hiveExperimentDto)); } else { // update hiveExperiment.UpdateFromDto(hiveExperimentDto); } } } private HiveExperiment GetHiveExperiment(Guid hiveExperimentId) { return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId)); } private void LogMessage(string message) { // HeuristicLab.Log is not Thread-Safe, so lock on every call lock (locker) { log.LogMessage(message); } } public event EventHandler HiveExperimentsChanged; private void OnHiveExperimentsChanged() { var handler = HiveExperimentsChanged; if (handler != null) handler(this, EventArgs.Empty); } void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs> e) { if (!currentlyUpdating) { using (Disposable service = ServiceLocator.Instance.ClientFacadePool.GetService()) { foreach (IndexedItem item in e.Items) { if (item.Value.HiveExperimentId != Guid.Empty) { service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId); } } } } } public event EventHandler IsProgressingChanged; private void OnIsProgressingChanged() { var handler = IsProgressingChanged; if (handler != null) handler(this, EventArgs.Empty); } } }