Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Views/3.4/SlaveItem.cs @ 6488

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

#1233

  • fixed Admin Views plugin dependencies
  • use settings instead of magic numbers and strings
File size: 8.2 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.ServiceModel;
24using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
25using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
30
31  public enum SlaveDisplayStat {
32    Offline,  // not connected to Hive
33    Idle,     // slave has no jobs to calculate
34    Busy,     // jobs are currently running on slave
35    Asleep,   // we are not accepting jobs at the moment
36    NoService // the slave windows service is currently not running
37  }
38
39  public enum CoreConnection {
40    Connected,
41    Offline
42  }
43
44  [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
45  [Item("SlaveItem", "Represents a slave which receives messages from the core")]
46  public class SlaveItem : Item, ISlaveCommunicationCallbacks, IDisposable {
47    private ISlaveCommunication pipeProxy;
48    private DuplexChannelFactory<ISlaveCommunication> pipeFactory;
49    private int lastJobsFetched = 0;
50
51    public SlaveItem() {
52    }
53
54    private void RegisterEvents() {
55      pipeFactory.Faulted += new EventHandler(pipeFactory_Faulted);
56      pipeFactory.Closed += new EventHandler(pipeFactory_Closed);
57      pipeFactory.Opened += new EventHandler(pipeFactory_Opened);
58    }
59
60    private void DeregisterEvents() {
61      pipeFactory.Faulted -= new EventHandler(pipeFactory_Faulted);
62      pipeFactory.Closed -= new EventHandler(pipeFactory_Closed);
63      pipeFactory.Opened -= new EventHandler(pipeFactory_Opened);
64    }
65
66    void pipeFactory_Opened(object sender, EventArgs e) {
67      OnMessageLogged("Connection to Slave core established");
68      OnCoreConnectionChanged(CoreConnection.Connected);
69    }
70
71    void pipeFactory_Closed(object sender, EventArgs e) {
72      OnMessageLogged("Connection to Slave core closed");
73      OnCoreConnectionChanged(CoreConnection.Offline);
74    }
75
76    void pipeFactory_Faulted(object sender, EventArgs e) {
77      OnMessageLogged("Connection to Slave core faulted");
78      OnCoreConnectionChanged(CoreConnection.Offline);
79    }
80
81    public void Open() {
82      try {
83        pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this, Settings.Default.SlaveCommunicationServiceEndpoint);
84        RegisterEvents();
85      }
86      catch (Exception ex) {
87        OnMessageLogged("Error establishing connection to Core. Are you missing a configuration file?" + Environment.NewLine + ex.ToString());
88      }
89    }
90
91    public bool ReconnectToSlaveCore() {
92      try {
93        pipeProxy = pipeFactory.CreateChannel();
94        StatusCommons st = pipeProxy.Subscribe();
95        if (st != null) {
96          OnStatusChanged(st);
97          return true;
98        } else {
99          return false;
100        }
101      }
102      catch (Exception) {
103        OnMessageLogged("Couldn't connect to Slave core. Is it possible that the core isn't running?");
104        return false;
105      }
106    }
107
108    public bool IsClosed() {
109      if (pipeFactory == null) return true;
110      return pipeFactory.State == CommunicationState.Closed || pipeFactory.State == CommunicationState.Faulted;
111    }
112
113    public void PauseAll() {
114      try {
115        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
116          pipeProxy.PauseAll();
117      }
118      catch (Exception e) {
119        OnMessageLogged("Error soft pausening core: " + e.ToString());
120      }
121    }
122
123    public void StopAll() {
124      try {
125        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
126          pipeProxy.StopAll();
127      }
128      catch (Exception e) {
129        OnMessageLogged("Error hard pausening core: " + e.ToString());
130      }
131    }
132
133    public void RestartCore() {
134      try {
135        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
136          pipeProxy.Restart();
137      }
138      catch (Exception e) {
139        OnMessageLogged("Error restarting core: " + e.ToString());
140      }
141    }
142
143    public void Sleep() {
144      try {
145        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) {
146          pipeProxy.Sleep();
147        }
148      }
149      catch (Exception e) {
150        OnMessageLogged("Error sending core to sleep: " + e.ToString());
151      }
152    }
153
154    public void Close() {
155      if (pipeFactory.State != CommunicationState.Closed) {
156        pipeProxy.Unsubscribe();
157        pipeFactory.Close();
158      }
159    }
160
161    public event EventHandler<EventArgs<string>> UserVisibleMessageFired;
162    public void OnUserVisibleMessageFired(string msg) {
163      var handler = UserVisibleMessageFired;
164      if (handler != null) handler(this, new EventArgs<string>(msg));
165    }
166
167    public event EventHandler<EventArgs<SlaveDisplayStat>> SlaveDisplayStateChanged;
168    public void OnSlaveDisplayStateChanged(StatusCommons status) {
169      SlaveDisplayStat stat;
170
171      if (status.Jobs.Count > 0) {
172        stat = SlaveDisplayStat.Busy;
173      } else {
174        stat = SlaveDisplayStat.Idle;
175      }
176      if (status.Asleep) {
177        stat = SlaveDisplayStat.Asleep;
178      }
179      if (status.Status == NetworkEnum.WcfConnState.Disconnected || status.Status == NetworkEnum.WcfConnState.Failed) {
180        stat = SlaveDisplayStat.Offline;
181      }
182
183      var handler = SlaveDisplayStateChanged;
184      if (handler != null) handler(this, new EventArgs<SlaveDisplayStat>(stat));
185    }
186
187    public void OnSlaveDisplayStateChanged(SlaveDisplayStat stat) {
188      var handler = SlaveDisplayStateChanged;
189      if (handler != null) handler(this, new EventArgs<SlaveDisplayStat>(stat));
190
191    }
192
193    public event EventHandler<EventArgs<StatusCommons>> SlaveStatusChanged;
194    public void OnStatusChanged(StatusCommons status) {
195      var handler = SlaveStatusChanged;
196      if (handler != null) handler(this, new EventArgs<StatusCommons>(status));
197
198      OnSlaveDisplayStateChanged(status);
199
200      int diff = status.JobsFetched - lastJobsFetched;
201      lastJobsFetched = status.JobsFetched;
202      if (diff > 0) {
203        if (diff == 1) {
204          OnUserVisibleMessageFired("HeuristicLab Hive received 1 new job!");
205        } else {
206          OnUserVisibleMessageFired(string.Format("HeuristicLab Hive received {0} new jobs!", diff));
207        }
208      }
209    }
210
211    public event EventHandler<EventArgs<string>> SlaveMessageLogged;
212    public void OnMessageLogged(string message) {
213      var handler = SlaveMessageLogged;
214      if (handler != null) handler(this, new EventArgs<string>(message));
215    }
216
217    public event EventHandler SlaveShutdown;
218    public void OnShutdown() {
219      var handler = SlaveShutdown;
220      if (handler != null) handler(this, EventArgs.Empty);
221      OnSlaveDisplayStateChanged(SlaveDisplayStat.NoService);
222    }
223
224    public event EventHandler<EventArgs<CoreConnection>> CoreConnectionChanged;
225    public void OnCoreConnectionChanged(CoreConnection conn) {
226      var handler = CoreConnectionChanged;
227      if (handler != null) handler(this, new EventArgs<CoreConnection>(conn));
228    }
229
230    public void Dispose() {
231      DeregisterEvents();
232      Close();
233    }
234
235    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
236      throw new NotImplementedException("It's not allowed to clone a SlaveItem!");
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.