Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs @ 6756

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

#1233 adapted Administrator UI to be more like OKB

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace HeuristicLab.Clients.Hive {
28  [Item("Hive Administrator", "Hive Administrator")]
29  public sealed class HiveAdminClient : IContent {
30    private static HiveAdminClient instance;
31    public static HiveAdminClient Instance {
32      get {
33        if (instance == null) instance = new HiveAdminClient();
34        return instance;
35      }
36    }
37
38    private IItemList<Resource> resources;
39    public IItemList<Resource> Resources {
40      get { return resources; }
41    }
42
43    private IItemList<Downtime> downtimes;
44    public IItemList<Downtime> Downtimes {
45      get { return downtimes; }
46    }
47
48    private Guid downtimeForResourceId;
49    public Guid DowntimeForResourceId {
50      get { return downtimeForResourceId; }
51      set {
52        downtimeForResourceId = value;
53        if (downtimes != null) {
54          downtimes.Clear();
55        }
56        OnDowntimesChanged();
57      }
58    }
59
60    #region Events
61    public event EventHandler Refreshing;
62    private void OnRefreshing() {
63      EventHandler handler = Refreshing;
64      if (handler != null) handler(this, EventArgs.Empty);
65    }
66    public event EventHandler Refreshed;
67    private void OnRefreshed() {
68      var handler = Refreshed;
69      if (handler != null) handler(this, EventArgs.Empty);
70    }
71    public event EventHandler DowntimesChanged;
72    private void OnDowntimesChanged() {
73      var handler = DowntimesChanged;
74      if (handler != null) handler(this, EventArgs.Empty);
75    }
76    #endregion
77
78    private HiveAdminClient() { }
79
80    #region Refresh
81    public void Refresh() {
82      OnRefreshing();
83
84      try {
85        resources = new ItemList<Resource>();
86
87        ServiceLocator.Instance.CallHiveService(service => {
88          service.GetSlaveGroups().ForEach(g => resources.Add(g));
89          service.GetSlaves().ForEach(s => resources.Add(s));
90        });
91      }
92      catch {
93        throw;
94      }
95      finally {
96        OnRefreshed();
97      }
98    }
99    public void RefreshAsync(Action<Exception> exceptionCallback) {
100      var call = new Func<Exception>(delegate() {
101        try {
102          Refresh();
103        }
104        catch (Exception ex) {
105          return ex;
106        }
107        return null;
108      });
109      call.BeginInvoke(delegate(IAsyncResult result) {
110        Exception ex = call.EndInvoke(result);
111        if (ex != null) exceptionCallback(ex);
112      }, null);
113    }
114    #endregion
115
116    #region Refresh downtime calendar
117    public void RefreshCalendar() {
118      if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty) {
119        OnRefreshing();
120
121        try {
122          downtimes = new ItemList<Downtime>();
123
124          ServiceLocator.Instance.CallHiveService(service => {
125            service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
126          });
127        }
128        catch {
129          throw;
130        }
131        finally {
132          OnRefreshed();
133        }
134      }
135    }
136    public void RefreshCalendarAsync(Action<Exception> exceptionCallback) {
137      var call = new Func<Exception>(delegate() {
138        try {
139          RefreshCalendar();
140        }
141        catch (Exception ex) {
142          return ex;
143        }
144        return null;
145      });
146      call.BeginInvoke(delegate(IAsyncResult result) {
147        Exception ex = call.EndInvoke(result);
148        if (ex != null) exceptionCallback(ex);
149      }, null);
150    }
151    #endregion
152
153    #region Store
154    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
155      if (item.Id == Guid.Empty) {
156        if (item is SlaveGroup) {
157          item.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
158        }
159        if (item is Slave) {
160          item.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddSlave((Slave)item));
161        }
162        if (item is Downtime) {
163          item.Id = ServiceLocator.Instance.CallHiveService((s) => s.AddDowntime((Downtime)item));
164        }
165      } else {
166        if (item is SlaveGroup) {
167          ServiceLocator.Instance.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
168        }
169        if (item is Slave) {
170          ServiceLocator.Instance.CallHiveService((s) => s.UpdateSlave((Slave)item));
171        }
172        if (item is Downtime) {
173          ServiceLocator.Instance.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
174        }
175      }
176    }
177    public static void StoreAsync(Action<Exception> exceptionCallback, IHiveItem item, CancellationToken cancellationToken) {
178      var call = new Func<Exception>(delegate() {
179        try {
180          Store(item, cancellationToken);
181        }
182        catch (Exception ex) {
183          return ex;
184        }
185        return null;
186      });
187      call.BeginInvoke(delegate(IAsyncResult result) {
188        Exception ex = call.EndInvoke(result);
189        if (ex != null) exceptionCallback(ex);
190      }, null);
191    }
192    #endregion
193
194    #region Delete
195    public static void Delete(IHiveItem item) {
196      if (item is SlaveGroup) {
197        ServiceLocator.Instance.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
198      } else if (item is Slave) {
199        ServiceLocator.Instance.CallHiveService((s) => s.DeleteSlave(item.Id));
200      } else if (item is Downtime) {
201        ServiceLocator.Instance.CallHiveService((s) => s.DeleteDowntime(item.Id));
202      }
203    }
204    #endregion
205
206    public void ResetDowntime() {
207      downtimeForResourceId = Guid.Empty;
208      if (downtimes != null) {
209        downtimes.Clear();
210      }
211      OnDowntimesChanged();
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.