1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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;
|
---|
23 | using System.IO;
|
---|
24 | using System.ServiceModel;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Clients.Hive.SlaveCore.ConsoleClient {
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// Mock a client, simply print out messages
|
---|
32 | /// </summary>
|
---|
33 | [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
|
---|
34 | public class SlaveCommListener : ISlaveCommunicationCallbacks, IDisposable {
|
---|
35 | private ISlaveCommunication pipeProxy;
|
---|
36 | private DuplexChannelFactory<ISlaveCommunication> pipeFactory;
|
---|
37 | private StreamWriter debugOutput;
|
---|
38 |
|
---|
39 | public void Open() {
|
---|
40 | pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this, "SlaveCommunicationServiceEndpoint");
|
---|
41 | debugOutput = new StreamWriter("slave-debug.txt");
|
---|
42 | debugOutput.AutoFlush = true;
|
---|
43 |
|
---|
44 | while (!ReconnectToSlaveCore()) {
|
---|
45 | Thread.Sleep(700);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public bool ReconnectToSlaveCore() {
|
---|
50 | try {
|
---|
51 | pipeProxy = pipeFactory.CreateChannel();
|
---|
52 | pipeProxy.Subscribe();
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | catch (Exception) {
|
---|
56 | OnMessageLogged("Couldn't connect to Slave Core. Waiting for the Core to start.");
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void Close() {
|
---|
62 | if (pipeFactory.State != CommunicationState.Closed &&
|
---|
63 | pipeFactory.State != CommunicationState.Closing &&
|
---|
64 | pipeFactory.State != CommunicationState.Faulted) {
|
---|
65 | pipeProxy.Unsubscribe();
|
---|
66 | }
|
---|
67 | try {
|
---|
68 | pipeFactory.Close();
|
---|
69 | }
|
---|
70 | catch (Exception) {
|
---|
71 | pipeFactory.Abort();
|
---|
72 | }
|
---|
73 | debugOutput.Close();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void OnStatusChanged(StatusCommons status) {
|
---|
77 | string msg = string.Format("{0}: {1}", DateTime.Now.ToString("HH:mm:ss"), status);
|
---|
78 | Console.WriteLine(msg);
|
---|
79 | debugOutput.WriteLine(msg);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void OnMessageLogged(string message) {
|
---|
83 | string msg = string.Format("{0}: {1}", DateTime.Now.ToString("HH:mm:ss"), message);
|
---|
84 | Console.WriteLine(msg);
|
---|
85 | debugOutput.WriteLine(msg);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void OnShutdown() {
|
---|
89 | Console.WriteLine("SlaveCommListner: Slave quit");
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void Dispose() {
|
---|
93 | Close();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|