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 | public Core SlaveCore { get; set; }
|
---|
31 |
|
---|
32 |
|
---|
33 | public bool Subscribe() {
|
---|
34 | try {
|
---|
35 | ISlaveCommunicationCallbacks callback = OperationContext.Current.GetCallbackChannel<ISlaveCommunicationCallbacks>();
|
---|
36 | if (!subscribers.Contains(callback))
|
---|
37 | subscribers.Add(callback);
|
---|
38 | return true;
|
---|
39 | }
|
---|
40 | catch {
|
---|
41 | return false;
|
---|
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 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
59 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
60 | callback.OnMessageLogged(message);
|
---|
61 | } else {
|
---|
62 | subscribers.Remove(callback);
|
---|
63 | }
|
---|
64 | });
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void StatusChanged(Slave.StatusCommons status) {
|
---|
68 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
69 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
70 | callback.OnStatusChanged(status);
|
---|
71 | } else {
|
---|
72 | subscribers.Remove(callback);
|
---|
73 | }
|
---|
74 | });
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void Shutdown() {
|
---|
78 | subscribers.ForEach(delegate(ISlaveCommunicationCallbacks callback) {
|
---|
79 | if (((ICommunicationObject)callback).State == CommunicationState.Opened) {
|
---|
80 | callback.OnShutdown();
|
---|
81 | } else {
|
---|
82 | subscribers.Remove(callback);
|
---|
83 | }
|
---|
84 | });
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void Restart() {
|
---|
88 | SlaveCore.Restart();
|
---|
89 | }
|
---|
90 | public void HardPause() {
|
---|
91 | SlaveCore.HardPause();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void SoftPause() {
|
---|
95 | SlaveCore.SoftPause();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void ShutdownSlave() {
|
---|
99 | SlaveCore.Shutdown();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|