Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Clients.Hive.Slave.MultiSlavesRunner/3.3/SlaveCommListener.cs @ 9522

Last change on this file since 9522 was 9373, checked in by pfleck, 11 years ago

#2030
Added MultiSlaveRunner for starting multiple Slave Cores on a single Machine.
Mocked GUID generation for slaves to random GUID.
Deactivated PerformanceCounter measurements from ConfigManager to avoid shared resource locks.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.IO;
24using System.ServiceModel;
25using System.Threading;
26using HeuristicLab.Clients.Hive.SlaveCore.ServiceContracts;
27
28namespace HeuristicLab.Clients.Hive.SlaveCore.MultiSlavesRunner {
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 TextWriter debugOutput;
38
39    public void Open() {
40      pipeFactory = new DuplexChannelFactory<ISlaveCommunication>(this, "SlaveCommunicationServiceEndpoint");
41      /*debugOutput = new StreamWriter("slave-debug.txt") {
42        AutoFlush = true
43      };*/
44      debugOutput = Console.Out;
45
46      while (!ReconnectToSlaveCore()) {
47        Thread.Sleep(700);
48      }
49    }
50
51    public bool ReconnectToSlaveCore() {
52      try {
53        pipeProxy = pipeFactory.CreateChannel();
54        pipeProxy.Subscribe();
55        return true;
56      }
57      catch (Exception) {
58        OnMessageLogged("Couldn't connect to Slave Core. Waiting for the Core to start.");
59        return false;
60      }
61    }
62
63    public void Close() {
64      if (pipeFactory.State != CommunicationState.Closed &&
65          pipeFactory.State != CommunicationState.Closing &&
66          pipeFactory.State != CommunicationState.Faulted) {
67        pipeProxy.Unsubscribe();
68      }
69      try {
70        pipeFactory.Close();
71      }
72      catch (Exception) {
73        pipeFactory.Abort();
74      }
75      debugOutput.Close();
76    }
77
78    public void OnStatusChanged(StatusCommons status) {
79      string msg = string.Format("{0}: {1}", DateTime.Now.ToString("HH:mm:ss"), status);
80      Console.WriteLine(msg);
81      debugOutput.WriteLine(msg);
82    }
83
84    public void OnMessageLogged(string message) {
85      string msg = string.Format("{0}: {1}", DateTime.Now.ToString("HH:mm:ss"), message);
86      Console.WriteLine(msg);
87      debugOutput.WriteLine(msg);
88    }
89
90    public void OnShutdown() {
91      Console.WriteLine("SlaveCommListner: Slave quit");
92    }
93
94    public void Dispose() {
95      Close();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.