[5156] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.ServiceModel;
|
---|
[5599] | 24 | using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
|
---|
[5156] | 25 |
|
---|
| 26 |
|
---|
[5599] | 27 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
| 28 |
|
---|
[5156] | 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 |
|
---|
[5599] | 66 | public void StatusChanged(SlaveCore.StatusCommons status) {
|
---|
[5156] | 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 |
|
---|
[5314] | 86 | public void Restart() {
|
---|
[5450] | 87 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Restart);
|
---|
| 88 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 89 | }
|
---|
[5450] | 90 |
|
---|
| 91 | public void PauseAll() {
|
---|
| 92 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.PauseAll);
|
---|
| 93 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 94 | }
|
---|
| 95 |
|
---|
[5450] | 96 | public void StopAll() {
|
---|
| 97 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.StopAll);
|
---|
| 98 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
[5314] | 99 | }
|
---|
| 100 |
|
---|
| 101 | public void ShutdownSlave() {
|
---|
[5450] | 102 | Core.TheCore.Shutdown();
|
---|
[5314] | 103 | }
|
---|
[5450] | 104 |
|
---|
| 105 | public void AbortAll() {
|
---|
| 106 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.AbortAll);
|
---|
| 107 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
| 108 | }
|
---|
[5451] | 109 |
|
---|
| 110 | public void Sleep() {
|
---|
| 111 | MessageContainer mc = new MessageContainer(MessageContainer.MessageType.Sleep);
|
---|
| 112 | MessageQueue.GetInstance().AddMessage(mc);
|
---|
| 113 | }
|
---|
[5156] | 114 | }
|
---|
| 115 | }
|
---|