[5156] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[6371] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5156] | 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;
|
---|
[5599] | 24 | using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
|
---|
[5156] | 25 |
|
---|
[5599] | 26 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
[5156] | 27 | public class SlaveCommunicationService : ISlaveCommunication {
|
---|
| 28 | private static List<ISlaveCommunicationCallbacks> subscribers = new List<ISlaveCommunicationCallbacks>();
|
---|
| 29 |
|
---|
[6263] | 30 | public StatusCommons Subscribe() {
|
---|
[5156] | 31 | try {
|
---|
| 32 | ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
|
---|
[5826] | 33 | if (!subscribers.Contains(callback)) {
|
---|
[5156] | 34 | subscribers.Add(callback);
|
---|
[5826] | 35 | }
|
---|
[6263] | 36 | return ConfigManager.Instance.GetStatusForClientConsole();
|
---|
[5156] | 37 | }
|
---|
| 38 | catch {
|
---|
[6263] | 39 | return null;
|
---|
[5156] | 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 |
|
---|
[6357] | 65 | public void StatusChanged(StatusCommons status) {
|
---|
[5156] | 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 |
|
---|
[5314] | 85 | public void Restart() {
|
---|
[5450] | 86 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Restart);
|
---|
| 87 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 88 | }
|
---|
[5450] | 89 |
|
---|
| 90 | public void PauseAll() {
|
---|
| 91 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.PauseAll);
|
---|
| 92 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 93 | }
|
---|
| 94 |
|
---|
[5450] | 95 | public void StopAll() {
|
---|
| 96 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.StopAll);
|
---|
| 97 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 98 | }
|
---|
| 99 |
|
---|
[5450] | 100 | public void AbortAll() {
|
---|
| 101 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.AbortAll);
|
---|
| 102 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
| 103 | }
|
---|
[5451] | 104 |
|
---|
| 105 | public void Sleep() {
|
---|
| 106 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Sleep);
|
---|
| 107 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
| 108 | }
|
---|
[5156] | 109 | }
|
---|
| 110 | }
|
---|