[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;
|
---|
| 24 | using HeuristicLab.Clients.Hive.Slave.ServiceContracts;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Clients.Hive.Slave {
|
---|
| 27 |
|
---|
| 28 | public class SlaveCommunicationService : ISlaveCommunication {
|
---|
| 29 | private static List<ISlaveCommunicationCallbacks> subscribers = new List<ISlaveCommunicationCallbacks>();
|
---|
| 30 |
|
---|
| 31 | public bool Subscribe() {
|
---|
| 32 | try {
|
---|
| 33 | ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
|
---|
| 34 | if (!subscribers.Contains(callback))
|
---|
| 35 | subscribers.Add(callback);
|
---|
| 36 | return true;
|
---|
| 37 | }
|
---|
| 38 | catch {
|
---|
| 39 | return false;
|
---|
| 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(Salve.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 | }
|
---|
| 86 | }
|
---|