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