Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.3/SlaveCommunicationService.cs @ 6945

Last change on this file since 6945 was 6945, checked in by ascheibe, 11 years ago

#1233 slave: catch more errors and log them to the windows event log

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.ServiceModel;
25using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28  public class SlaveCommunicationService : ISlaveCommunication {
29    private static List<ISlaveCommunicationCallbacks> subscribers = new List<ISlaveCommunicationCallbacks>();
30
31    public StatusCommons Subscribe() {
32      try {
33        ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
34        if (!subscribers.Contains(callback)) {
35          subscribers.Add(callback);
36        }
37        return ConfigManager.Instance.GetStatusForClientConsole();
38      }
39      catch {
40        return null;
41      }
42    }
43
44    public bool Unsubscribe() {
45      try {
46        ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
47        if (subscribers.Contains(callback))
48          subscribers.Remove(callback);
49        return true;
50      }
51      catch {
52        return false;
53      }
54    }
55
56    public void LogMessage(string message) {
57      try {
58        subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
59          if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
60            callback.OnMessageLogged(message);
61          } else {
62            subscribers.Remove(callback);
63          }
64        });
65      }
66      catch (Exception ex) {
67        EventLogManager.LogException(ex);
68      }
69    }
70
71    public void StatusChanged(StatusCommons status) {
72      try {
73        subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
74          if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
75            callback.OnStatusChanged(status);
76          } else {
77            subscribers.Remove(callback);
78          }
79        });
80      }
81      catch (Exception ex) {
82        EventLogManager.LogException(ex);
83      }
84    }
85
86    public void Shutdown() {
87      subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
88        if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
89          callback.OnShutdown();
90        } else {
91          subscribers.Remove(callback);
92        }
93      });
94    }
95
96    public void Restart() {
97      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Restart);
98      MessageQueue.GetInstance().AddMessage(mc);
99    }
100
101    public void PauseAll() {
102      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.PauseAll);
103      MessageQueue.GetInstance().AddMessage(mc);
104    }
105
106    public void StopAll() {
107      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.StopAll);
108      MessageQueue.GetInstance().AddMessage(mc);
109    }
110
111    public void AbortAll() {
112      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.AbortAll);
113      MessageQueue.GetInstance().AddMessage(mc);
114    }
115
116    public void Sleep() {
117      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Sleep);
118      MessageQueue.GetInstance().AddMessage(mc);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.