[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 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.Collections.Generic;
|
---|
| 24 | using System.Threading;
|
---|
| 25 | using HeuristicLab.Clients.Hive.SlaveCore.Properties;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// Heartbeat Manager sends every x ms a heartbeat to the server and receives a message.
|
---|
| 30 | /// The message is added to the MessageQueue from where the Core pulls them and decides what to do.
|
---|
| 31 | /// </summary>
|
---|
| 32 | public class HeartbeatManager {
|
---|
| 33 | private static object locker = new object();
|
---|
| 34 | private TimeSpan interval;
|
---|
| 35 |
|
---|
| 36 | public TimeSpan Interval {
|
---|
| 37 | get { return interval; }
|
---|
| 38 | set {
|
---|
| 39 | interval = value;
|
---|
| 40 | Settings.Default.HeartbeatInterval = interval;
|
---|
| 41 | Settings.Default.Save();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | private Thread heartBeatThread;
|
---|
| 45 | private AutoResetEvent waitHandle;
|
---|
| 46 | private WcfService wcfService;
|
---|
| 47 | private bool threadStopped;
|
---|
| 48 |
|
---|
| 49 | public HeartbeatManager() {
|
---|
| 50 | interval = Settings.Default.HeartbeatInterval;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Starts the Heartbeat signal.
|
---|
| 55 | /// </summary>
|
---|
| 56 | public void StartHeartbeat() {
|
---|
| 57 | this.waitHandle = new AutoResetEvent(true);
|
---|
| 58 | wcfService = WcfService.Instance;
|
---|
| 59 | threadStopped = false;
|
---|
| 60 | heartBeatThread = new Thread(RunHeartBeatThread);
|
---|
| 61 | heartBeatThread.Start();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// Stop the heartbeat
|
---|
| 66 | /// </summary>
|
---|
| 67 | public void StopHeartBeat() {
|
---|
| 68 | threadStopped = true;
|
---|
| 69 | waitHandle.Set();
|
---|
| 70 | heartBeatThread.Join();
|
---|
[9347] | 71 | waitHandle.Close();
|
---|
[6983] | 72 | }
|
---|
| 73 |
|
---|
| 74 | /// <summary>
|
---|
| 75 | /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
|
---|
| 76 | /// </summary>
|
---|
| 77 | public void AwakeHeartBeatThread() {
|
---|
| 78 | if (!threadStopped)
|
---|
| 79 | waitHandle.Set();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private void RunHeartBeatThread() {
|
---|
| 83 | while (!threadStopped) {
|
---|
[7166] | 84 | try {
|
---|
[7171] | 85 | SlaveClientCom.Instance.StatusChanged(ConfigManager.Instance.GetStatusForClientConsole());
|
---|
[7166] | 86 | }
|
---|
| 87 | catch (Exception ex) {
|
---|
| 88 | EventLogManager.LogMessage("Couldn't sent status information to client ui. Exception is: " + Environment.NewLine + ex.ToString());
|
---|
| 89 | }
|
---|
[6983] | 90 |
|
---|
| 91 | try {
|
---|
| 92 | lock (locker) {
|
---|
| 93 | if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
|
---|
| 94 | // login happens automatically upon successfull connection
|
---|
| 95 | wcfService.Connect(ConfigManager.Instance.GetClientInfo());
|
---|
| 96 | SlaveStatusInfo.LoginTime = DateTime.Now;
|
---|
| 97 | }
|
---|
| 98 | if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
|
---|
| 99 | Slave info = ConfigManager.Instance.GetClientInfo();
|
---|
| 100 |
|
---|
| 101 | Heartbeat heartBeatData = new Heartbeat {
|
---|
| 102 | SlaveId = info.Id,
|
---|
| 103 | FreeCores = info.Cores.HasValue ? info.Cores.Value - SlaveStatusInfo.UsedCores : 0,
|
---|
| 104 | FreeMemory = ConfigManager.Instance.GetFreeMemory(),
|
---|
| 105 | CpuUtilization = ConfigManager.Instance.GetCpuUtilization(),
|
---|
| 106 | JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
|
---|
| 107 | AssignJob = !ConfigManager.Instance.Asleep,
|
---|
| 108 | HbInterval = (int)interval.TotalSeconds
|
---|
| 109 | };
|
---|
| 110 |
|
---|
[7171] | 111 | SlaveClientCom.Instance.LogMessage("Send HB: " + heartBeatData);
|
---|
[6983] | 112 | List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
|
---|
| 113 |
|
---|
| 114 | if (msgs == null) {
|
---|
[7171] | 115 | SlaveClientCom.Instance.LogMessage("Error getting response from HB");
|
---|
[6983] | 116 | } else {
|
---|
[7171] | 117 | SlaveClientCom.Instance.LogMessage("HB Response received (" + msgs.Count + "): ");
|
---|
| 118 | msgs.ForEach(mc => SlaveClientCom.Instance.LogMessage(mc.Message.ToString()));
|
---|
[6983] | 119 | msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | catch (Exception e) {
|
---|
[7171] | 125 | SlaveClientCom.Instance.LogMessage("Heartbeat thread failed: " + e.ToString());
|
---|
[6983] | 126 | }
|
---|
| 127 | waitHandle.WaitOne(this.interval);
|
---|
| 128 | }
|
---|
[7171] | 129 | SlaveClientCom.Instance.LogMessage("Heartbeat thread stopped");
|
---|
[6983] | 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|