Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 929 was 923, checked in by kgrading, 16 years ago

refactoring of the WCF Service for the Server (#418)

File size: 3.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;
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 WcfService wcfService;
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      wcfService = WcfService.Instance;
61      wcfService.SendHeartBeatCompleted += new EventHandler<SendHeartBeatCompletedEventArgs>(wcfService_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      if (wcfService.ConnState == WcfService.ConnectionState.failed) {
77        wcfService.Connect();
78      } else if (wcfService.ConnState == WcfService.ConnectionState.connected) {
79        wcfService.SendHeartBeatAsync(heartBeatData);
80      }
81    }
82
83    void wcfService_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
84      System.Diagnostics.Debug.WriteLine("Heartbeat received! ");
85      e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.