Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3578 was 3578, checked in by kgrading, 14 years ago

Removed References to HiveLogging and updated the default logging mechanism (#991)

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