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

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

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 5.6 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    ISlaveCommunication pipeProxy;
34    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      //TODO: read info from app.config
65      pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this,
66        new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/HeuristicLabSlaveCom"));
67
68      pipeProxy = pipeFactory.CreateChannel();
69      RegisterEvents();
70
71      try {
72        pipeProxy.Subscribe();
73      }
74      catch (Exception e) {
75        OnMessageLogged("Couldn't connect to Slave core. Is it possible that the Slave Core isn't running?\nException is: " + e.ToString());
76      }
77    }
78
79    public void ReconnectToSlaveCore() {
80      try {
81        pipeProxy = pipeFactory.CreateChannel();
82        pipeProxy.Subscribe();
83      }
84      catch (Exception e) {
85        OnMessageLogged("Couldn't connect to Slave core. Is it possible that the Slave Core isn't running?\nException is: " + e.ToString());
86      }
87    }
88
89    public bool isClosed() {
90      if (pipeFactory == null) return true;
91      return pipeFactory.State == CommunicationState.Closed;
92    }
93
94    public void PauseAll() {
95      try {
96        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
97          pipeProxy.PauseAll();
98      }
99      catch (Exception e) {
100        OnMessageLogged("Error soft pausening core: " + e.ToString());
101      }
102    }
103
104    public void StopAll() {
105      try {
106        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
107          pipeProxy.StopAll();
108      }
109      catch (Exception e) {
110        OnMessageLogged("Error hard pausening core: " + e.ToString());
111      }
112    }
113
114    public void RestartCore() {
115      try {
116        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
117          pipeProxy.Restart();
118      }
119      catch (Exception e) {
120        OnMessageLogged("Error restarting core: " + e.ToString());
121      }
122    }
123
124    public void Sleep() {
125      try {
126        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
127          pipeProxy.Sleep();
128      }
129      catch (Exception e) {
130        OnMessageLogged("Error sending core to sleep: " + e.ToString());
131      }
132    }
133
134    public void Close() {
135      if (pipeFactory.State != CommunicationState.Closed) {
136        pipeProxy.Unsubscribe();
137        pipeFactory.Close();
138      }
139    }
140
141    public event EventHandler<EventArgs<StatusCommons>> SlaveStatusChanged;
142    public void OnStatusChanged(StatusCommons status) {
143      var handler = SlaveStatusChanged;
144      if (handler != null) handler(this, new EventArgs<StatusCommons>(status));
145    }
146
147    public event EventHandler<EventArgs<string>> SlaveMessageLogged;
148    public void OnMessageLogged(string message) {
149      var handler = SlaveMessageLogged;
150      if (handler != null) handler(this, new EventArgs<string>(message));
151    }
152
153    public event EventHandler SlaveShutdown;
154    public void OnShutdown() {
155      var handler = SlaveShutdown;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158
159    public void Dispose() {
160      DeregisterEvents();
161      Close();
162    }
163
164    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
165      throw new NotImplementedException("It's not allowed to clone a SlaveItem!");
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.