Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Hive.Client.Core/Heartbeat.cs @ 1226

Last change on this file since 1226 was 798, checked in by whackl, 16 years ago

implemented serveral services (#376)

File size: 3.2 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;
32//using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
33
34namespace HeuristicLab.Hive.Client.Core {
35  /// <summary>
36  /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
37  /// </summary>
38  public class Heartbeat {
39    public double Interval { get; set; }   
40    private Timer heartbeatTimer = null;
41       
42    public Heartbeat() {
43      Interval = 100;
44    }
45
46    public Heartbeat(double interval) {
47      Interval = interval;     
48    }
49
50    private ClientCommunicatorClient clientCommunicator;
51
52    /// <summary>
53    /// Starts the Heartbeat signal.
54    /// </summary>
55    public void StartHeartbeat() {
56      heartbeatTimer = new System.Timers.Timer();
57      heartbeatTimer.Interval = this.Interval;
58      heartbeatTimer.AutoReset = true;
59      heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);
60      clientCommunicator = ServiceLocator.GetClientCommunicator();
61      clientCommunicator.SendHeartBeatCompleted += new EventHandler<SendHeartBeatCompletedEventArgs>(ClientCommunicator_SendHeartBeatCompleted);
62      heartbeatTimer.Start();
63    }
64
65    /// <summary>
66    /// This Method is called every time the timer ticks
67    /// </summary>
68    /// <param name="sender"></param>
69    /// <param name="e"></param>
70    void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) {
71      Console.WriteLine("tick");
72      HeartBeatData heartBeatData = new HeartBeatData { ClientId = Guid.NewGuid(),
73                                                              freeCores = 4,
74                                                              freeMemory = 1000,
75                                                              jobProgress = 1};
76      clientCommunicator.SendHeartBeatAsync(heartBeatData);
77    }
78
79    void ClientCommunicator_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
80      System.Diagnostics.Debug.WriteLine("Heartbeat received! ");
81      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.