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