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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
27 | using HeuristicLab.Services.Hive.Common;
|
---|
28 | using System.Collections.Generic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Clients.Hive {
|
---|
31 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
32 | using HeuristicLab.Common;
|
---|
33 |
|
---|
34 | [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
|
---|
35 | [Creatable("Hive")]
|
---|
36 | public class HiveClient : Item, IProgressReporter {
|
---|
37 | private static object locker = new object();
|
---|
38 | private bool currentlyUpdating;
|
---|
39 |
|
---|
40 | private ILog log;
|
---|
41 | public ILog Log {
|
---|
42 | get { return log; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private HiveExperimentList hiveExperiments;
|
---|
46 | public HiveExperimentList HiveExperiments {
|
---|
47 | get { return hiveExperiments; }
|
---|
48 | set {
|
---|
49 | if (hiveExperiments != value) {
|
---|
50 | DeRegisterHiveExperimentsEvents();
|
---|
51 | hiveExperiments = value;
|
---|
52 | RegisterHiveExperimentsEvent();
|
---|
53 | OnHiveExperimentsChanged();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private bool isProgressing;
|
---|
59 | public bool IsProgressing {
|
---|
60 | get { return isProgressing; }
|
---|
61 | set {
|
---|
62 | if (isProgressing != value) {
|
---|
63 | isProgressing = value;
|
---|
64 | OnIsProgressingChanged();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private IProgress progress;
|
---|
70 | public IProgress Progress {
|
---|
71 | get { return progress; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void RegisterHiveExperimentsEvent() {
|
---|
75 | if (hiveExperiments != null) {
|
---|
76 | hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void DeRegisterHiveExperimentsEvents() {
|
---|
81 | if (hiveExperiments != null) {
|
---|
82 | hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public HiveClient() {
|
---|
87 | this.log = new Log();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | HiveClient clone = (HiveClient)base.Clone(cloner);
|
---|
92 | clone.log = (ILog)cloner.Clone(this.log);
|
---|
93 | clone.hiveExperiments = (HiveExperimentList)cloner.Clone(this.hiveExperiments);
|
---|
94 | return clone;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void UpdateExperimentList() {
|
---|
98 | this.progress = new Progress("Downloading HiveExperiments...");
|
---|
99 | try {
|
---|
100 | IsProgressing = true;
|
---|
101 | if (this.HiveExperiments == null) {
|
---|
102 | this.HiveExperiments = new HiveExperimentList();
|
---|
103 | }
|
---|
104 | using (var service = ServiceLocator.Instance.ServicePool.GetService()) {
|
---|
105 | currentlyUpdating = true;
|
---|
106 | IEnumerable<DT.HiveExperiment> response = service.Obj.GetHiveExperiments();
|
---|
107 | progress.Status = "Populating HiveExperiment list...";
|
---|
108 | RefreshExperimentList(response);
|
---|
109 | currentlyUpdating = false;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | catch (Exception) {
|
---|
113 | this.HiveExperiments = null;
|
---|
114 | throw;
|
---|
115 | }
|
---|
116 | finally {
|
---|
117 | IsProgressing = false;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void RefreshExperimentList(IEnumerable<DT.HiveExperiment> hiveExperiments) {
|
---|
122 | foreach (DT.HiveExperiment hiveExperimentDto in hiveExperiments) {
|
---|
123 | DT.HiveExperiment hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
|
---|
124 | if (hiveExperiment == null) {
|
---|
125 | // not yet there, create new
|
---|
126 | this.HiveExperiments.Add(new HiveExperiment(hiveExperimentDto));
|
---|
127 | } else {
|
---|
128 | // update
|
---|
129 | hiveExperiment.UpdateFromDto(hiveExperimentDto);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | private DT.HiveExperiment GetHiveExperiment(Guid hiveExperimentId) {
|
---|
135 | return this.HiveExperiments.SingleOrDefault(he => he.Id.Equals(hiveExperimentId));
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void LogMessage(string message) {
|
---|
139 | // HeuristicLab.Log is not Thread-Safe, so lock on every call
|
---|
140 | lock (locker) {
|
---|
141 | log.LogMessage(message);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public event EventHandler HiveExperimentsChanged;
|
---|
146 | private void OnHiveExperimentsChanged() {
|
---|
147 | var handler = HiveExperimentsChanged;
|
---|
148 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
|
---|
152 | if (!currentlyUpdating) {
|
---|
153 | using (Disposable<IHiveService> service = ServiceLocator.Instance.ServicePool.GetService()) {
|
---|
154 | foreach (IndexedItem<HiveExperiment> item in e.Items) {
|
---|
155 | if (item.Value.HiveExperimentId != Guid.Empty) {
|
---|
156 | service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | public event EventHandler IsProgressingChanged;
|
---|
164 | private void OnIsProgressingChanged() {
|
---|
165 | var handler = IsProgressingChanged;
|
---|
166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|