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 @ 6422

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

#1233 changed year to 2011

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