Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 3.8 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.Collections.Generic;
23using System.ServiceModel;
24using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
25
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28
29  public class SlaveCommunicationService : ISlaveCommunication {
30    private static List<ISlaveCommunicationCallbacks> subscribers = new List<ISlaveCommunicationCallbacks>();
31
32    public bool Subscribe() {
33      try {
34        ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
35        if (!subscribers.Contains(callback))
36          subscribers.Add(callback);
37        return true;
38      }
39      catch {
40        return false;
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      subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
58        if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
59          callback.OnMessageLogged(message);
60        } else {
61          subscribers.Remove(callback);
62        }
63      });
64    }
65
66    public void StatusChanged(SlaveCore.StatusCommons status) {
67      subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
68        if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
69          callback.OnStatusChanged(status);
70        } else {
71          subscribers.Remove(callback);
72        }
73      });
74    }
75
76    public void Shutdown() {
77      subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
78        if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
79          callback.OnShutdown();
80        } else {
81          subscribers.Remove(callback);
82        }
83      });
84    }
85
86    public void Restart() {
87      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Restart);
88      MessageQueue.GetInstance().AddMessage(mc);
89    }
90
91    public void PauseAll() {
92      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.PauseAll);
93      MessageQueue.GetInstance().AddMessage(mc);
94    }
95
96    public void StopAll() {
97      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.StopAll);
98      MessageQueue.GetInstance().AddMessage(mc);
99    }
100
101    public void ShutdownSlave() {
102      Core.TheCore.Shutdown();
103    }
104
105    public void AbortAll() {
106      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.AbortAll);
107      MessageQueue.GetInstance().AddMessage(mc);
108    }
109
110    public void Sleep() {
111      MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Sleep);
112      MessageQueue.GetInstance().AddMessage(mc);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.