Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 978 was 960, checked in by kgrading, 16 years ago

fixed fetchjobcompleted and sendheartbeat method (#430)

File size: 3.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;
33//using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
34
35namespace HeuristicLab.Hive.Client.Core {
36  /// <summary>
37  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
38  /// </summary>
39  public class Heartbeat {
40    public double Interval { get; set; }   
41    private Timer heartbeatTimer = null;
42       
43    public Heartbeat() {
44      Interval = 100;
45    }
46
47    public Heartbeat(double interval) {
48      Interval = interval;     
49    }
50
51    private WcfService wcfService;
52
53    /// <summary>
54    /// Starts the Heartbeat signal.
55    /// </summary>
56    public void StartHeartbeat() {
57      heartbeatTimer = new System.Timers.Timer();
58      heartbeatTimer.Interval = this.Interval;
59      heartbeatTimer.AutoReset = true;
60      heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);
61      wcfService = WcfService.Instance;
62      wcfService.SendHeartBeatCompleted += new EventHandler<SendHeartBeatCompletedEventArgs>(wcfService_SendHeartBeatCompleted);
63      heartbeatTimer.Start();
64    }
65
66    /// <summary>
67    /// This Method is called every time the timer ticks
68    /// </summary>
69    /// <param name="sender"></param>
70    /// <param name="e"></param>
71    void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) {
72      Console.WriteLine("tick");
73      ClientInfo info = ConfigManager.Instance.GetClientInfo();
74      // Todo: remove tempfix for free cores.
75      HeartBeatData heartBeatData = new HeartBeatData { ClientId = info.ClientId,
76                                                              freeCores = info.NrOfCores - (ClientStatusInfo.JobsFetched - ClientStatusInfo.JobsProcessed),
77                                                              freeMemory = 1000,
78                                                              jobProgress = ClientStatusInfo.JobsFetched - ClientStatusInfo.JobsProcessed};
79      if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) {
80        wcfService.Connect();
81      } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Connected) {
82        wcfService.SendHeartBeatAsync(heartBeatData);
83      }
84    }
85
86    void wcfService_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
87      System.Diagnostics.Debug.WriteLine("Heartbeat received! ");
88      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.