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