Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs @ 15071

Last change on this file since 15071 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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      } catch {
86        throw;
87      } finally {
88        OnRefreshed();
89      }
90    }
91    #endregion
92
93    #region Refresh downtime calendar
94    public void RefreshCalendar() {
95      if (downtimeForResourceId != null && downtimeForResourceId != Guid.Empty) {
96        OnRefreshing();
97
98        try {
99          downtimes = new ItemList<Downtime>();
100
101          HiveServiceLocator.Instance.CallHiveService(service => {
102            service.GetDowntimesForResource(downtimeForResourceId).ForEach(d => downtimes.Add(d));
103          });
104        } catch {
105          throw;
106        } finally {
107          OnRefreshed();
108        }
109      }
110    }
111    #endregion
112
113    #region Store
114    public static void Store(IHiveItem item, CancellationToken cancellationToken) {
115      if (item.Id == Guid.Empty) {
116        if (item is SlaveGroup) {
117          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlaveGroup((SlaveGroup)item));
118        }
119        if (item is Slave) {
120          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddSlave((Slave)item));
121        }
122        if (item is Downtime) {
123          item.Id = HiveServiceLocator.Instance.CallHiveService((s) => s.AddDowntime((Downtime)item));
124        }
125      } else {
126        if (item is SlaveGroup) {
127          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlaveGroup((SlaveGroup)item));
128        }
129        if (item is Slave) {
130          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateSlave((Slave)item));
131        }
132        if (item is Downtime) {
133          HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateDowntime((Downtime)item));
134        }
135      }
136    }
137    #endregion
138
139    #region Delete
140    public static void Delete(IHiveItem item) {
141      if (item is SlaveGroup) {
142        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlaveGroup(item.Id));
143      } else if (item is Slave) {
144        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteSlave(item.Id));
145      } else if (item is Downtime) {
146        HiveServiceLocator.Instance.CallHiveService((s) => s.DeleteDowntime(item.Id));
147      }
148    }
149    #endregion
150
151    public void ResetDowntime() {
152      downtimeForResourceId = Guid.Empty;
153      if (downtimes != null) {
154        downtimes.Clear();
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.