Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Core/3.3/Heartbeat.cs @ 4264

Last change on this file since 4264 was 4264, checked in by cneumuel, 14 years ago

Split up "State" to "JobState" and "SlaveState" (#1159)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Linq;
25using System.Text;
26using System.Timers;
27using HeuristicLab.Hive.Slave.Common;
28using HeuristicLab.Hive.Slave.Communication;
29using System.Diagnostics;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using HeuristicLab.Hive.Contracts;
32using HeuristicLab.Hive.Slave.Core.ConfigurationManager;
33using HeuristicLab.Hive.Slave.Communication.ServerService;
34using HeuristicLab.Tracing;
35using System.Threading;
36//using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
37
38namespace HeuristicLab.Hive.Slave.Core {
39  /// <summary>
40  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
41  /// </summary>
42  public class Heartbeat {
43
44    private bool offline;
45
46    public TimeSpan Interval { get; set; }
47
48    private Thread heartBeatThread;
49
50    private static object locker = new object();
51
52    public Heartbeat() {
53      Interval = new TimeSpan(0,0,10);
54    }
55
56    public Heartbeat(TimeSpan interval) {
57      Interval = interval;
58    }
59
60    private WcfService wcfService;
61
62    private bool abortThreadPending;
63
64    /// <summary>
65    /// Starts the Heartbeat signal.
66    /// </summary>
67    public void StartHeartbeat() {
68      wcfService = WcfService.Instance;
69      wcfService.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(wcfService_ProcessHeartBeatCompleted);
70      abortThreadPending = false;
71      heartBeatThread = GetHeartBeatThread();
72      heartBeatThread.Start();
73    }
74
75    private Thread GetHeartBeatThread() {
76      return new Thread(() => {
77        while (!abortThreadPending) {
78          lock (locker) {
79            // check if cwfService is disconnected for any reason (should happen at first heartbeat)
80            // [chn] TODO: Client should always send heartbeats. when calendar disallows he should tell the server he does not want to compute anything
81            if (wcfService.ConnState == NetworkEnum.WcfConnState.Disconnected) {
82              wcfService.Connect();
83            }
84
85            if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) {
86
87              // client is allowed to calculate stuff
88              ClientDto info = ConfigManager.Instance.GetClientInfo();
89             
90              HeartBeatData heartBeatData = new HeartBeatData {
91                SlaveId = info.Id,
92                FreeCores = info.NrOfCores - ConfigManager.Instance.GetUsedCores(),
93                FreeMemory = GetFreeMemory(),
94                JobProgress = ConfigManager.Instance.GetProgressOfAllJobs(),
95                IsAllowedToCalculate = UptimeManager.Instance.IsOnline() && UptimeManager.Instance.CalendarAvailable
96              };
97
98              if (!heartBeatData.IsAllowedToCalculate) {
99                // stop all running jobs and send snapshots to server
100                MessageQueue.GetInstance().AddMessage(MessageContainer.MessageType.UptimeLimitDisconnect);
101              }
102
103              Logger.Debug("Sending Heartbeat: " + heartBeatData);
104              wcfService.ProcessHeartBeatAsync(heartBeatData);
105            }
106
107          } // lock
108          Thread.Sleep(Interval);
109        } // while
110        abortThreadPending = false;
111      });
112    }
113
114    private int GetFreeMemory() {
115      PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true);
116      int mb = (int)(counter.NextValue() / 1024 / 1024);
117      return mb;
118    }
119
120    void wcfService_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
121      Logger.Debug("Heartbeat Response received");
122      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
123    }
124
125    public void StopHeartBeat() {
126      abortThreadPending = true;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.