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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.ServiceModel;
|
---|
24 | using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
27 | public class SlaveCommunicationService : ISlaveCommunication {
|
---|
28 | private static List<ISlaveCommunicationCallbacks> subscribers = new List<ISlaveCommunicationCallbacks>();
|
---|
29 |
|
---|
30 | public StatusCommons Subscribe() {
|
---|
31 | try {
|
---|
32 | ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
|
---|
33 | if (!subscribers.Contains(callback)) {
|
---|
34 | subscribers.Add(callback);
|
---|
35 | }
|
---|
36 | return ConfigManager.Instance.GetStatusForClientConsole();
|
---|
37 | }
|
---|
38 | catch {
|
---|
39 | return null;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public bool Unsubscribe() {
|
---|
44 | try {
|
---|
45 | ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
|
---|
46 | if (subscribers.Contains(callback))
|
---|
47 | subscribers.Remove(callback);
|
---|
48 | return true;
|
---|
49 | }
|
---|
50 | catch {
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void LogMessage(string message) {
|
---|
56 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
57 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
58 | callback.OnMessageLogged(message);
|
---|
59 | } else {
|
---|
60 | subscribers.Remove(callback);
|
---|
61 | }
|
---|
62 | });
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void StatusChanged(StatusCommons status) {
|
---|
66 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
67 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
68 | callback.OnStatusChanged(status);
|
---|
69 | } else {
|
---|
70 | subscribers.Remove(callback);
|
---|
71 | }
|
---|
72 | });
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void Shutdown() {
|
---|
76 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
77 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
78 | callback.OnShutdown();
|
---|
79 | } else {
|
---|
80 | subscribers.Remove(callback);
|
---|
81 | }
|
---|
82 | });
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void Restart() {
|
---|
86 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Restart);
|
---|
87 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void PauseAll() {
|
---|
91 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.PauseAll);
|
---|
92 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void StopAll() {
|
---|
96 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.StopAll);
|
---|
97 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void AbortAll() {
|
---|
101 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.AbortAll);
|
---|
102 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void Sleep() {
|
---|
106 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Sleep);
|
---|
107 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|