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

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

#1233 don't log every message 2 times

File size: 5.3 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.Slave.ServiceContracts;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Clients.Hive.Slave.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 SoftPauseCore() {
95      try {
96        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
97          pipeProxy.SoftPause();
98      }
99      catch (Exception e) {
100        OnMessageLogged("Error soft pausening core: " + e.ToString());
101      }
102    }
103
104    public void HardPauseCore() {
105      try {
106        if (pipeFactory.State != CommunicationState.Faulted && pipeFactory.State != CommunicationState.Closed)
107          pipeProxy.HardPause();
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 Close() {
125      if (pipeFactory.State != CommunicationState.Closed) {
126        pipeProxy.Unsubscribe();
127        pipeFactory.Close();
128      }
129    }
130
131    public event EventHandler<EventArgs<StatusCommons>> SlaveStatusChanged;
132    public void OnStatusChanged(StatusCommons status) {     
133      var handler = SlaveStatusChanged;
134      if (handler != null) handler(this, new EventArgs<StatusCommons>(status));
135    }
136
137    public event EventHandler<EventArgs<string>> SlaveMessageLogged;
138    public void OnMessageLogged(string message) {     
139      var handler = SlaveMessageLogged;
140      if (handler != null) handler(this, new EventArgs<string>(message));
141    }
142
143    public event EventHandler SlaveShutdown;
144    public void OnShutdown() {     
145      var handler = SlaveShutdown;
146      if (handler != null) handler(this, EventArgs.Empty);
147    }
148
149    public void Dispose() {
150      DeregisterEvents();
151      Close();
152    }
153
154    public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
155      throw new NotImplementedException("It's not allowed to clone a SlaveItem!");
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.