Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/3.2/Heartbeat.cs @ 3001

Last change on this file since 3001 was 2846, checked in by kgrading, 15 years ago

various improvements (#828) concerning the stability of the server

File size: 4.6 KB
RevLine 
[735]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;
[733]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Timers;
[734]27using HeuristicLab.Hive.Client.Common;
[790]28using HeuristicLab.Hive.Client.Communication;
29using System.Diagnostics;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
[793]31using HeuristicLab.Hive.Contracts;
[946]32using HeuristicLab.Hive.Client.Core.ConfigurationManager;
[993]33using HeuristicLab.Hive.Client.Communication.ServerService;
[790]34//using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
[733]35
36namespace HeuristicLab.Hive.Client.Core {
[735]37  /// <summary>
38  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
39  /// </summary>
[733]40  public class Heartbeat {
[2025]41
42    private bool offline;
43
[735]44    public double Interval { get; set; }   
[733]45    private Timer heartbeatTimer = null;
46       
47    public Heartbeat() {
48      Interval = 100;
49    }
50
51    public Heartbeat(double interval) {
52      Interval = interval;     
53    }
54
[923]55    private WcfService wcfService;
[793]56
[735]57    /// <summary>
58    /// Starts the Heartbeat signal.
59    /// </summary>
[733]60    public void StartHeartbeat() {
61      heartbeatTimer = new System.Timers.Timer();
62      heartbeatTimer.Interval = this.Interval;
63      heartbeatTimer.AutoReset = true;
64      heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);
[923]65      wcfService = WcfService.Instance;
[1379]66      wcfService.SendHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(wcfService_ProcessHeartBeatCompleted);
[793]67      heartbeatTimer.Start();
[733]68    }
69
[735]70    /// <summary>
71    /// This Method is called every time the timer ticks
72    /// </summary>
73    /// <param name="sender"></param>
74    /// <param name="e"></param>
[2846]75    void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) {     
[2025]76      ClientInfo info = ConfigManager.Instance.GetClientInfo();     
[1119]77
[1379]78      PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true);
79      int mb = (int)(counter.NextValue() / 1024 / 1024);
80
[1755]81      HeartBeatData heartBeatData = new HeartBeatData {
[1449]82        ClientId = info.Id,
[1755]83        FreeCores = info.NrOfCores - ConfigManager.Instance.GetUsedCores(),
[1379]84        FreeMemory = mb,
[2025]85        JobProgress = ConfigManager.Instance.GetProgressOfAllJobs()     
[1119]86      };
[2846]87       
[2025]88      DateTime lastFullHour = DateTime.Parse(DateTime.Now.Hour.ToString() + ":00");
89      TimeSpan span = DateTime.Now - lastFullHour;
90      if (span.TotalSeconds < (Interval/1000)) {
91        if (UptimeManager.Instance.isOnline()) {
92          //That's quiet simple: Just reconnect and you're good for new jobs
[2116]93          if (wcfService.ConnState != NetworkEnum.WcfConnState.Loggedin) {
[2025]94            Logging.Instance.Info(this.ToString(), "Client goes online according to timetable");
95            wcfService.Connect();
96          }
97        } else {
98          //We have quit a lot of work to do here: snapshot all jobs, submit them back, then disconnect and then pray to god that nothing goes wrong
99          MessageQueue.GetInstance().AddMessage(MessageContainer.MessageType.UptimeLimitDisconnect);                 
100        }       
101      }
[932]102      if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) {
[923]103        wcfService.Connect();
[1255]104      } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) {
[2846]105        Logging.Instance.Info(this.ToString(), "Sending Heartbeat: " + heartBeatData);       
[923]106        wcfService.SendHeartBeatAsync(heartBeatData);
107      }
[733]108    }
109
[1379]110    void wcfService_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
[2846]111      Logging.Instance.Info(this.ToString(), "Heartbeat received");
[1936]112      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));     
[790]113    }
[1097]114
115    public void StopHeartBeat() {
116      heartbeatTimer.Dispose();
117    }
[733]118  }
119}
Note: See TracBrowser for help on using the repository browser.