#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; using HeuristicLab.Hive.Client.Common; using HeuristicLab.Hive.Client.Communication; using System.Diagnostics; using HeuristicLab.Hive.Contracts.BusinessObjects; using HeuristicLab.Hive.Contracts; using HeuristicLab.Hive.Client.Core.ConfigurationManager; using HeuristicLab.Hive.Client.Communication.ServerService; //using BO = HeuristicLab.Hive.Contracts.BusinessObjects; namespace HeuristicLab.Hive.Client.Core { /// /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message /// public class Heartbeat { public double Interval { get; set; } private Timer heartbeatTimer = null; public Heartbeat() { Interval = 100; } public Heartbeat(double interval) { Interval = interval; } private WcfService wcfService; /// /// Starts the Heartbeat signal. /// public void StartHeartbeat() { heartbeatTimer = new System.Timers.Timer(); heartbeatTimer.Interval = this.Interval; heartbeatTimer.AutoReset = true; heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed); wcfService = WcfService.Instance; wcfService.SendHeartBeatCompleted += new EventHandler(wcfService_SendHeartBeatCompleted); heartbeatTimer.Start(); } /// /// This Method is called every time the timer ticks /// /// /// void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("tick"); ClientInfo info = ConfigManager.Instance.GetClientInfo(); // Todo: remove tempfix for free cores. HeartBeatData heartBeatData = new HeartBeatData { ClientId = info.ClientId, FreeCores = info.NrOfCores - (ClientStatusInfo.JobsFetched - ClientStatusInfo.JobsProcessed), FreeMemory = 1000, JobProgress = ConfigManager.Instance.GetProgressOfAllJobs() }; if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) { wcfService.Connect(); } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) { wcfService.SendHeartBeatAsync(heartBeatData); } } void wcfService_SendHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) { System.Diagnostics.Debug.WriteLine("Heartbeat received! "); e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc)); } public void StopHeartBeat() { heartbeatTimer.Dispose(); } } }