Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs @ 11359

Last change on this file since 11359 was 11204, checked in by ascheibe, 10 years ago

merged only Hive changes from trunk into branch

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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      }
57    }
58
59    #region Events
60    public event EventHandler Refreshing;
61    private void OnRefreshing() {
62      EventHandler handler = Refreshing;
63      if (handler != null) handler(this, EventArgs.Empty);
64    }
65    public event EventHandler Refreshed;
66    private void OnRefreshed() {
67      var handler = Refreshed;
68      if (handler != null) handler(this, EventArgs.Empty);
69    }
70    #endregion
71
72    private HiveAdminClient() { }
73
74    #region Refresh
75    public void Refresh() {
76      OnRefreshing();
77
78      try {
79        resources = new ItemList<Resource>();
80
81        HiveServiceLocator.Instance.CallHiveService(service => {
82          service.GetSlaveGroups().ForEach(g => resources.Add(g));
83          service.GetSlaves().ForEach(s => resources.Add(s));
84        });
85      }
86      catch {
87        throw;
88      }
89      finally {
90        OnRefreshed();
91      }
92    }
93    #endregion
94
95    #region Refresh downtime calendar
96    public void RefreshCalendar() {
97      if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty) {
98        OnRefreshing();
99
100        try {
101          downtimes = new ItemList<Downtime>();
102
103          HiveServiceLocator.Instance.CallHiveService(service => {
104            service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
105          });
106        }
107        catch {
108          throw;
109        }
110        finally {
111          OnRefreshed();
112        }
113      }
114    }
115    #endregion
116
117    #region Store
118    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
119      if (item.Id == Guid.Empty) {
120        if (item is SlaveGroup) {
121          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
122        }
123        if (item is Slave) {
124          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlave((Slave)item));
125        }
126        if (item is Downtime) {
127          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddDowntime((Downtime)item));
128        }
129      } else {
130        if (item is SlaveGroup) {
131          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
132        }
133        if (item is Slave) {
134          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlave((Slave)item));
135        }
136        if (item is Downtime) {
137          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
138        }
139      }
140    }
141    #endregion
142
143    #region Delete
144    public static void Delete(IHiveItem item) {
145      if (item is SlaveGroup) {
146        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
147      } else if (item is Slave) {
148        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlave(item.Id));
149      } else if (item is Downtime) {
150        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteDowntime(item.Id));
151      }
152    }
153    #endregion
154
155    public void ResetDowntime() {
156      downtimeForResourceId = Guid.Empty;
157      if (downtimes != null) {
158        downtimes.Clear();
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.