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

Last change on this file since 5826 was 5826, checked in by ascheibe, 9 years ago

#1233 slave ui now receives status information and displays it in doughnut chart

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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  [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
31  [Item("SlaveItem", "Represents a slave which receives messages from the core")]
32  public class SlaveItem : Item, ISlaveCommunicationCallbacks, IDisposable {
33    private ISlaveCommunication pipeProxy;
34    private DuplexChannelFactory<ISlaveCommunication> pipeFactory;
35
36    public SlaveItem() {
37    }
38
39    private void RegisterEvents() {
40      pipeFactory.Faulted += new EventHandler(pipeFactory_Faulted);
41      pipeFactory.Closed += new EventHandler(pipeFactory_Closed);
42      pipeFactory.Opened += new EventHandler(pipeFactory_Opened);
43    }
44
45    private void DeregisterEvents() {
46      pipeFactory.Faulted -= new EventHandler(pipeFactory_Faulted);
47      pipeFactory.Closed -= new EventHandler(pipeFactory_Closed);
48      pipeFactory.Opened -= new EventHandler(pipeFactory_Opened);
49    }
50
51    void pipeFactory_Opened(object sender, EventArgs e) {
52      OnMessageLogged("Connection to Slave core established");
53    }
54
55    void pipeFactory_Closed(object sender, EventArgs e) {
56      OnMessageLogged("Connection to Slave core closed");
57    }
58
59    void pipeFactory_Faulted(object sender, EventArgs e) {
60      OnMessageLogged("Connection to Slave core faulted");
61    }
62
63    public void Open() {
64      try {
65        pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this, "SlaveCommunicationServiceEndpoint");
66        RegisterEvents();
67      }
68      catch (Exception ex) {
69        OnMessageLogged("Error establishing connection to Core. Are you missing a configuration file?\n" + ex.ToString());
70      }
71    }
72
73    public bool ReconnectToSlaveCore() {
74      try {
75        pipeProxy = pipeFactory.CreateChannel();
76        pipeProxy.Subscribe();
77        return true;
78      }
79      catch (Exception) {
80        OnMessageLogged("Couldn't connect to Slave core. Is it possible that the Slave Core isn't running?\n");
81        return false;
82      }
83    }
84
85    public bool IsClosed() {
86      if (pipeFactory == null) return true;
87      return pipeFactory.State == CommunicationState.Closed || pipeFactory.State == CommunicationState.Faulted;
88    }
89
90    public void PauseAll() {
91      try {
92        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
93          pipeProxy.PauseAll();
94      }
95      catch (Exception e) {
96        OnMessageLogged("Error soft pausening core: " + e.ToString());
97      }
98    }
99
100    public void StopAll() {
101      try {
102        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
103          pipeProxy.StopAll();
104      }
105      catch (Exception e) {
106        OnMessageLogged("Error hard pausening core: " + e.ToString());
107      }
108    }
109
110    public void RestartCore() {
111      try {
112        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
113          pipeProxy.Restart();
114      }
115      catch (Exception e) {
116        OnMessageLogged("Error restarting core: " + e.ToString());
117      }
118    }
119
120    public void Sleep() {
121      try {
122        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
123          pipeProxy.Sleep();
124      }
125      catch (Exception e) {
126        OnMessageLogged("Error sending core to sleep: " + e.ToString());
127      }
128    }
129
130    public void Close() {
131      if (pipeFactory.State != CommunicationState.Closed) {
132        pipeProxy.Unsubscribe();
133        pipeFactory.Close();
134      }
135    }
136
137    public event EventHandler<EventArgs<StatusCommons>> SlaveStatusChanged;
138    public void OnStatusChanged(StatusCommons status) {
139      var handler = SlaveStatusChanged;
140      if (handler != null) handler(this, new EventArgs<StatusCommons>(status));
141    }
142
143    public event EventHandler<EventArgs<string>> SlaveMessageLogged;
144    public void OnMessageLogged(string message) {
145      var handler = SlaveMessageLogged;
146      if (handler != null) handler(this, new EventArgs<string>(message));
147    }
148
149    public event EventHandler SlaveShutdown;
150    public void OnShutdown() {
151      var handler = SlaveShutdown;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154
155    public void Dispose() {
156      DeregisterEvents();
157      Close();
158    }
159
160    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
161      throw new NotImplementedException("It's not allowed to clone a SlaveItem!");
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.