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

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

#1233 changed year to 2011

File size: 8.1 KB
RevLine 
[5314]1#region License Information
2/* HeuristicLab
[6372]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5314]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;
[5599]24using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
[5314]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
[5599]28namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
[5314]29
[6257]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
[5314]43  [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
44  [Item("SlaveItem", "Represents a slave which receives messages from the core")]
45  public class SlaveItem : Item, ISlaveCommunicationCallbacks, IDisposable {
[5795]46    private ISlaveCommunication pipeProxy;
47    private DuplexChannelFactory<ISlaveCommunication> pipeFactory;
[6257]48    private int lastJobsFetched = 0;
[5314]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");
[6257]67      OnCoreConnectionChanged(CoreConnection.Connected);
[5314]68    }
69
70    void pipeFactory_Closed(object sender, EventArgs e) {
71      OnMessageLogged("Connection to Slave core closed");
[6257]72      OnCoreConnectionChanged(CoreConnection.Offline);
[5314]73    }
74
75    void pipeFactory_Faulted(object sender, EventArgs e) {
76      OnMessageLogged("Connection to Slave core faulted");
[6257]77      OnCoreConnectionChanged(CoreConnection.Offline);
[5314]78    }
79
80    public void Open() {
[5826]81      try {
82        pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this, "SlaveCommunicationServiceEndpoint");
83        RegisterEvents();
84      }
85      catch (Exception ex) {
[6004]86        OnMessageLogged("Error establishing connection to Core. Are you missing a configuration file?" + Environment.NewLine + ex.ToString());
[5826]87      }
[5314]88    }
89
[5795]90    public bool ReconnectToSlaveCore() {
[5314]91      try {
92        pipeProxy = pipeFactory.CreateChannel();
[6263]93        StatusCommons st = pipeProxy.Subscribe();
94        if (st != null) {
95          OnStatusChanged(st);
96          return true;
97        } else {
98          return false;
99        }
[5314]100      }
[5826]101      catch (Exception) {
[6107]102        OnMessageLogged("Couldn't connect to Slave core. Is it possible that the core isn't running?");
[5795]103        return false;
[5314]104      }
105    }
106
[5795]107    public bool IsClosed() {
[5314]108      if (pipeFactory == null) return true;
[5795]109      return pipeFactory.State == CommunicationState.Closed || pipeFactory.State == CommunicationState.Faulted;
[5314]110    }
111
[5451]112    public void PauseAll() {
[5315]113      try {
114        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
[5451]115          pipeProxy.PauseAll();
[5315]116      }
117      catch (Exception e) {
118        OnMessageLogged("Error soft pausening core: " + e.ToString());
119      }
120    }
121
[5451]122    public void StopAll() {
[5315]123      try {
124        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
[5451]125          pipeProxy.StopAll();
[5315]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
[5451]142    public void Sleep() {
143      try {
[6257]144        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) {
[5451]145          pipeProxy.Sleep();
[6257]146        }
[5451]147      }
148      catch (Exception e) {
149        OnMessageLogged("Error sending core to sleep: " + e.ToString());
150      }
151    }
152
[5314]153    public void Close() {
154      if (pipeFactory.State != CommunicationState.Closed) {
155        pipeProxy.Unsubscribe();
156        pipeFactory.Close();
157      }
158    }
159
[6257]160    public event EventHandler<EventArgs<string>> UserVisibleMessageFired;
[6225]161    public void OnUserVisibleMessageFired(string msg) {
162      var handler = UserVisibleMessageFired;
163      if (handler != null) handler(this, new EventArgs<string>(msg));
164    }
165
[6257]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;
[5451]193    public void OnStatusChanged(StatusCommons status) {
[5314]194      var handler = SlaveStatusChanged;
195      if (handler != null) handler(this, new EventArgs<StatusCommons>(status));
[6225]196
[6257]197      OnSlaveDisplayStateChanged(status);
198
[6225]199      int diff = status.JobsFetched - lastJobsFetched;
200      lastJobsFetched = status.JobsFetched;
201      if (diff > 0) {
[6257]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        }
[6225]207      }
[5314]208    }
209
210    public event EventHandler<EventArgs<string>> SlaveMessageLogged;
[5451]211    public void OnMessageLogged(string message) {
[5314]212      var handler = SlaveMessageLogged;
213      if (handler != null) handler(this, new EventArgs<string>(message));
214    }
215
216    public event EventHandler SlaveShutdown;
[5451]217    public void OnShutdown() {
[5314]218      var handler = SlaveShutdown;
219      if (handler != null) handler(this, EventArgs.Empty);
[6257]220      OnSlaveDisplayStateChanged(SlaveDisplayStat.NoService);
[5314]221    }
222
[6257]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
[5314]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.