Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.Core/3.3/Heartbeat.cs @ 4171

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

Stabilization of Hive, Improvement HiveExperiment GUI (#1115)

File size: 5.3 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    private static object locker = new object();
49
50    public Heartbeat() {
51      Interval = 100;
52    }
53
54    public Heartbeat(double interval) {
55      Interval = interval;
56    }
57
58    private WcfService wcfService;
59
60    /// <summary>
61    /// Starts the Heartbeat signal.
62    /// </summary>
63    public void StartHeartbeat() {
64      heartbeatTimer = new System.Timers.Timer();
65      heartbeatTimer.Interval = this.Interval;
66      heartbeatTimer.AutoReset = true;
67      heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);
68      wcfService = WcfService.Instance;
69      wcfService.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(wcfService_ProcessHeartBeatCompleted);
70      heartbeatTimer.Start();
71    }
72
73    /// <summary>
74    /// This Method is called every time the timer ticks
75    /// </summary>
76    /// <param name="sender"></param>
77    /// <param name="e"></param>
78    void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) {
79      lock (locker) {
80        // check if cwfService is disconnected for any reason (should happen at first heartbeat)
81        // [chn] TODO: Client should always send heartbeats. when calendar disallows he should tell the server he does not want to compute anything
82        if (wcfService.ConnState == NetworkEnum.WcfConnState.Disconnected &&
83          (!UptimeManager.Instance.CalendarAvailable || UptimeManager.Instance.IsOnline())) {
84          wcfService.Connect();
85        }
86        if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
87          wcfService.LoginSync(ConfigManager.Instance.GetClientInfo());
88        }
89
90        ClientDto info = ConfigManager.Instance.GetClientInfo();
91
92        PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true);
93        int mb = (int)(counter.NextValue() / 1024 / 1024);
94
95        HeartBeatData heartBeatData = new HeartBeatData {
96          ClientId = info.Id,
97          FreeCores = info.NrOfCores - ConfigManager.Instance.GetUsedCores(),
98          FreeMemory = mb,
99          JobProgress = ConfigManager.Instance.GetProgressOfAllJobs()
100        };
101
102        DateTime lastFullHour = DateTime.Parse(DateTime.Now.Hour.ToString() + ":00");
103        TimeSpan span = DateTime.Now - lastFullHour;
104        if (span.TotalSeconds < (Interval / 1000)) {
105          if (UptimeManager.Instance.IsOnline() && UptimeManager.Instance.CalendarAvailable) {
106            //That's quiet simple: Just reconnect and you're good for new jobs
107            if (wcfService.ConnState != NetworkEnum.WcfConnState.Loggedin) {
108              Logger.Info("Client goes online according to timetable");
109              wcfService.Connect();
110            }
111          } else {
112            //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
113            MessageQueue.GetInstance().AddMessage(MessageContainer.MessageType.UptimeLimitDisconnect);
114          }
115        }
116        if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) {
117          wcfService.Connect();
118        } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) {
119          Logger.Debug("Sending Heartbeat: " + heartBeatData);
120          wcfService.ProcessHeartBeatAsync(heartBeatData);
121        }
122      }
123    }
124
125    void wcfService_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
126      Logger.Debug("Heartbeat received");
127      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
128    }
129
130    public void StopHeartBeat() {
131      heartbeatTimer.Dispose();
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.