1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.ServiceModel;
|
---|
23 | using HeuristicLab.Clients.Hive.SlaveCore.Properties;
|
---|
24 | using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
|
---|
25 | using System;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Singleton which encapsulates the SlaveCommunicationService
|
---|
31 | /// </summary>
|
---|
32 | public class SlaveClientCom {
|
---|
33 | private static SlaveClientCom instance;
|
---|
34 | private static DuplexChannelFactory<ISlaveCommunication> pipeFactory;
|
---|
35 | public ISlaveCommunication ClientCom { get; set; }
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Getter for the Instance of the SlaveClientCom
|
---|
39 | /// </summary>
|
---|
40 | /// <returns>the Instance of the SlaveClientCom class</returns>
|
---|
41 | public static SlaveClientCom Instance {
|
---|
42 | get {
|
---|
43 | if (instance == null) {
|
---|
44 | instance = new SlaveClientCom();
|
---|
45 | }
|
---|
46 | return instance;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void LogMessage(string message) {
|
---|
51 | try {
|
---|
52 | ClientCom.LogMessage(message);
|
---|
53 | }
|
---|
54 | catch (Exception ex) {
|
---|
55 | EventLogManager.LogMessage("Exception on LogMessage: " + ex.ToString() + Environment.NewLine + "Orginal message was: " + message);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void StatusChanged(StatusCommons status) {
|
---|
60 | try {
|
---|
61 | ClientCom.StatusChanged(status);
|
---|
62 | }
|
---|
63 | catch (Exception ex) {
|
---|
64 | EventLogManager.LogMessage("Exception on StatusChanged: " + ex.ToString());
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private SlaveClientCom() {
|
---|
69 | SetupClientCom();
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void SetupClientCom() {
|
---|
73 | DummyListener dummy = new DummyListener();
|
---|
74 | try {
|
---|
75 | pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(dummy, Settings.Default.SlaveCommunicationServiceEndpoint);
|
---|
76 | }
|
---|
77 | catch {
|
---|
78 | EventLogManager.LogMessage("Couldn't create pipe for SlaveClientCom with config!");
|
---|
79 |
|
---|
80 | try {
|
---|
81 | pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(dummy, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/HeuristicLabSlaveCom"));
|
---|
82 | }
|
---|
83 | catch {
|
---|
84 | EventLogManager.LogMessage("Couldn't create pipe for SlaveClientCom with fixed addresse!");
|
---|
85 | return;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | pipeFactory.Faulted += new System.EventHandler(pipeFactory_Faulted);
|
---|
89 |
|
---|
90 | ISlaveCommunication pipeProxy = pipeFactory.CreateChannel();
|
---|
91 | ClientCom = pipeProxy;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void pipeFactory_Faulted(object sender, System.EventArgs e) {
|
---|
95 | EventLogManager.LogMessage("SlaveClientCom just faulted. Trying to repair it...");
|
---|
96 |
|
---|
97 | try {
|
---|
98 | pipeFactory.Faulted -= new System.EventHandler(pipeFactory_Faulted);
|
---|
99 | SetupClientCom();
|
---|
100 | }
|
---|
101 | catch { }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static void Close() {
|
---|
105 | if (pipeFactory.State != CommunicationState.Closed && pipeFactory.State != CommunicationState.Faulted)
|
---|
106 | pipeFactory.Close();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | } |
---|