#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.ServiceModel; using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts; using HeuristicLab.Common; using HeuristicLab.Core; namespace HeuristicLab.Clients.Hive.SlaveCore.Views { [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] [Item("SlaveItem", "Represents a slave which receives messages from the core")] public class SlaveItem : Item, ISlaveCommunicationCallbacks, IDisposable { ISlaveCommunication pipeProxy; DuplexChannelFactory pipeFactory; public SlaveItem() { } private void RegisterEvents() { pipeFactory.Faulted += new EventHandler(pipeFactory_Faulted); pipeFactory.Closed += new EventHandler(pipeFactory_Closed); pipeFactory.Opened += new EventHandler(pipeFactory_Opened); } private void DeregisterEvents() { pipeFactory.Faulted -= new EventHandler(pipeFactory_Faulted); pipeFactory.Closed -= new EventHandler(pipeFactory_Closed); pipeFactory.Opened -= new EventHandler(pipeFactory_Opened); } void pipeFactory_Opened(object sender, EventArgs e) { OnMessageLogged("Connection to Slave core established"); } void pipeFactory_Closed(object sender, EventArgs e) { OnMessageLogged("Connection to Slave core closed"); } void pipeFactory_Faulted(object sender, EventArgs e) { OnMessageLogged("Connection to Slave core faulted"); } public void Open() { //TODO: read info from app.config pipeFactory = new DuplexChannelFactory(this, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/HeuristicLabSlaveCom")); pipeProxy = pipeFactory.CreateChannel(); RegisterEvents(); try { pipeProxy.Subscribe(); } catch (Exception e) { OnMessageLogged("Couldn't connect to Slave core. Is it possible that the Slave Core isn't running?\nException is: " + e.ToString()); } } public void ReconnectToSlaveCore() { try { pipeProxy = pipeFactory.CreateChannel(); pipeProxy.Subscribe(); } catch (Exception e) { OnMessageLogged("Couldn't connect to Slave core. Is it possible that the Slave Core isn't running?\nException is: " + e.ToString()); } } public bool isClosed() { if (pipeFactory == null) return true; return pipeFactory.State == CommunicationState.Closed; } public void PauseAll() { try { if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) pipeProxy.PauseAll(); } catch (Exception e) { OnMessageLogged("Error soft pausening core: " + e.ToString()); } } public void StopAll() { try { if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) pipeProxy.StopAll(); } catch (Exception e) { OnMessageLogged("Error hard pausening core: " + e.ToString()); } } public void RestartCore() { try { if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) pipeProxy.Restart(); } catch (Exception e) { OnMessageLogged("Error restarting core: " + e.ToString()); } } public void Sleep() { try { if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed) pipeProxy.Sleep(); } catch (Exception e) { OnMessageLogged("Error sending core to sleep: " + e.ToString()); } } public void Close() { if (pipeFactory.State != CommunicationState.Closed) { pipeProxy.Unsubscribe(); pipeFactory.Close(); } } public event EventHandler> SlaveStatusChanged; public void OnStatusChanged(StatusCommons status) { var handler = SlaveStatusChanged; if (handler != null) handler(this, new EventArgs(status)); } public event EventHandler> SlaveMessageLogged; public void OnMessageLogged(string message) { var handler = SlaveMessageLogged; if (handler != null) handler(this, new EventArgs(message)); } public event EventHandler SlaveShutdown; public void OnShutdown() { var handler = SlaveShutdown; if (handler != null) handler(this, EventArgs.Empty); } public void Dispose() { DeregisterEvents(); Close(); } public override Common.IDeepCloneable Clone(Common.Cloner cloner) { throw new NotImplementedException("It's not allowed to clone a SlaveItem!"); } } }