Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/HeartbeatManager.cs @ 5591

Last change on this file since 5591 was 5451, checked in by ascheibe, 14 years ago

#1233

  • added Sleep cmd
  • adapted gui for new cmds
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Services.Hive.Common;
27using HeuristicLab.Services.Hive.Common.DataTransfer;
28
29namespace HeuristicLab.Clients.Hive.Slave {
30  /// <summary>
31  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
32  /// </summary>
33  public class HeartbeatManager {
34    private static object locker = new object();
35    public TimeSpan Interval { get; set; }
36    private Thread heartBeatThread;
37    private AutoResetEvent waitHandle;
38
39    public HeartbeatManager() {
40      Interval = new TimeSpan(0, 0, 10);
41    }
42
43    public HeartbeatManager(TimeSpan interval) {
44      Interval = interval;
45    }
46
47    private WcfService wcfService;
48
49    private bool threadStopped;
50
51    ReaderWriterLockSlim heartBeatThreadIsSleepingLock = new ReaderWriterLockSlim();
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();
71    }
72
73    /// <summary>
74    /// use this method to singalize there is work to do (to avoid the waiting period if its clear that actions are required)
75    /// </summary>
76    public void AwakeHeartBeatThread() {
77      if (!threadStopped)
78        waitHandle.Set();
79    }
80
81    private void RunHeartBeatThread() {
82      while (!threadStopped) {
83        try {
84          lock (locker) {
85            if (wcfService.ConnState != NetworkEnum.WcfConnState.Connected) {
86              wcfService.Connect(ConfigManager.Instance.GetClientInfo()); // Login happens automatically upon successfull connection
87            }
88            if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
89              HeuristicLab.Services.Hive.Common.DataTransfer.Slave info = ConfigManager.Instance.GetClientInfo();
90
91              Heartbeat heartBeatData = new Heartbeat {
92                SlaveId = info.Id, /*Settings.Default.Guid*/
93                FreeCores = info.Cores.HasValue ? info.Cores.Value - ConfigManager.Instance.GetUsedCores() : 0,
94                FreeMemory = ConfigManager.GetFreeMemory(),
95                JobProgress = ConfigManager.Instance.GetExecutionTimeOfAllJobs(),
96                AssignJob = true //TODO: check if we want another job
97              };
98
99              SlaveClientCom.Instance.ClientCom.LogMessage("Sending Heartbeat: " + heartBeatData);
100              List<MessageContainer> msgs = wcfService.SendHeartbeat(heartBeatData);
101
102              if (msgs == null) {
103                SlaveClientCom.Instance.ClientCom.LogMessage("Error getting response from Heartbeat");
104                OnExceptionOccured(new Exception("Error getting response from Heartbeat"));
105              }
106
107              SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Response received: ");
108              msgs.ForEach(mc => SlaveClientCom.Instance.ClientCom.LogMessage(mc.Message.ToString()));
109              msgs.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
110            }
111          }
112        }
113        catch (Exception e) {
114          SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat Thread failed badly: " + e.Message);
115          OnExceptionOccured(e);
116        }
117        waitHandle.WaitOne(this.Interval);
118      }
119      waitHandle.Close();
120      SlaveClientCom.Instance.ClientCom.LogMessage("Heartbeat thread stopped");
121    }
122
123
124    #region Eventhandler
125    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
126    private void OnExceptionOccured(Exception e) {
127      var handler = ExceptionOccured;
128      if (handler != null) handler(this, new EventArgs<Exception>(e));
129    }
130    #endregion
131  }
132}
Note: See TracBrowser for help on using the repository browser.