Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveExperimentManagerClient.cs @ 5599

Last change on this file since 5599 was 5599, checked in by ascheibe, 13 years ago

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Clients.Hive {
29  using HeuristicLab.Common;
30
31  [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
32  [Creatable("Hive")]
33  public class HiveExperimentManagerClient : Item, IProgressReporter {
34    private static object locker = new object();
35    private bool currentlyUpdating;
36
37    private ItemList<HiveExperimentClient> hiveExperiments;
38    public ItemList<HiveExperimentClient> HiveExperiments {
39      get { return hiveExperiments; }
40      set {
41        if (hiveExperiments != value) {
42          DeRegisterHiveExperimentsEvents();
43          hiveExperiments = value;
44          RegisterHiveExperimentsEvent();
45          OnHiveExperimentsChanged();
46        }
47      }
48    }
49
50    private bool isProgressing;
51    public bool IsProgressing {
52      get { return isProgressing; }
53      set {
54        if (isProgressing != value) {
55          isProgressing = value;
56          OnIsProgressingChanged();
57        }
58      }
59    }
60
61    private IProgress progress;
62    public IProgress Progress {
63      get { return progress; }
64    }
65
66    public HiveExperimentManagerClient() { }
67    protected HiveExperimentManagerClient(HiveExperimentManagerClient original, Cloner cloner)
68      : base(original, cloner) {
69      this.hiveExperiments = cloner.Clone(original.hiveExperiments);
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new HiveExperimentManagerClient(this, cloner);
73    }
74
75    private void RegisterHiveExperimentsEvent() {
76      if (hiveExperiments != null) {
77        hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
78      }
79    }
80
81    private void DeRegisterHiveExperimentsEvents() {
82      if (hiveExperiments != null) {
83        hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperimentClient>>(hiveExperiments_ItemsRemoved);
84      }
85    }
86
87    public void UpdateExperimentList() {
88      this.progress = new Progress("Downloading HiveExperiments...");
89      try {
90        IsProgressing = true;
91        if (this.HiveExperiments == null) {
92          this.HiveExperiments = new ItemList<HiveExperimentClient>();
93        }
94        currentlyUpdating = true;
95        IEnumerable<HiveExperiment> response = ServiceLocator.Instance.CallHiveService(s => s.GetHiveExperiments());
96        progress.Status = "Populating HiveExperiment list...";
97        RefreshExperimentList(response);
98        currentlyUpdating = false;
99      }
100      catch (Exception) {
101        this.HiveExperiments = null;
102        throw;
103      }
104      finally {
105        IsProgressing = false;
106      }
107    }
108
109    private void RefreshExperimentList(IEnumerable<HiveExperiment> hiveExperiments) {
110      foreach (HiveExperiment hiveExperimentDto in hiveExperiments) {
111        HiveExperimentClient hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
112        if (hiveExperiment == null) {
113          // not yet there, create new
114          this.HiveExperiments.Add(new HiveExperimentClient(hiveExperimentDto));
115        } else {
116          // update
117          hiveExperiment.UpdateFromDto(hiveExperimentDto);
118        }
119      }
120    }
121
122    private HiveExperimentClient GetHiveExperiment(Guid hiveExperimentId) {
123      return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId));
124    }
125
126    public event EventHandler HiveExperimentsChanged;
127    private void OnHiveExperimentsChanged() {
128      var handler = HiveExperimentsChanged;
129      if (handler != null) handler(this, EventArgs.Empty);
130    }
131
132    private void hiveExperiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveExperimentClient>> e) {
133      if (!currentlyUpdating) {
134        ServiceLocator.Instance.CallHiveService(service => {
135          foreach (IndexedItem<HiveExperimentClient> item in e.Items) {
136            if (item.Value.HiveExperimentId != Guid.Empty) {
137              service.DeleteHiveExperiment(item.Value.HiveExperimentId);
138            }
139          }
140        });
141      }
142    }
143
144    public event EventHandler IsProgressingChanged;
145    private void OnIsProgressingChanged() {
146      var handler = IsProgressingChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.