[735] | 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 |
|
---|
| 22 | using System;
|
---|
[733] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Timers;
|
---|
[734] | 27 | using HeuristicLab.Hive.Client.Common;
|
---|
[790] | 28 | using HeuristicLab.Hive.Client.Communication;
|
---|
| 29 | using System.Diagnostics;
|
---|
| 30 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[793] | 31 | using HeuristicLab.Hive.Contracts;
|
---|
[946] | 32 | using HeuristicLab.Hive.Client.Core.ConfigurationManager;
|
---|
[993] | 33 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
[790] | 34 | //using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[733] | 35 |
|
---|
| 36 | namespace HeuristicLab.Hive.Client.Core {
|
---|
[735] | 37 | /// <summary>
|
---|
| 38 | /// Heartbeat class. It sends every x ms a heartbeat to the server and receives a Message
|
---|
| 39 | /// </summary>
|
---|
[733] | 40 | public class Heartbeat {
|
---|
[735] | 41 | public double Interval { get; set; }
|
---|
[733] | 42 | private Timer heartbeatTimer = null;
|
---|
| 43 |
|
---|
| 44 | public Heartbeat() {
|
---|
| 45 | Interval = 100;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public Heartbeat(double interval) {
|
---|
| 49 | Interval = interval;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[923] | 52 | private WcfService wcfService;
|
---|
[793] | 53 |
|
---|
[735] | 54 | /// <summary>
|
---|
| 55 | /// Starts the Heartbeat signal.
|
---|
| 56 | /// </summary>
|
---|
[733] | 57 | public void StartHeartbeat() {
|
---|
| 58 | heartbeatTimer = new System.Timers.Timer();
|
---|
| 59 | heartbeatTimer.Interval = this.Interval;
|
---|
| 60 | heartbeatTimer.AutoReset = true;
|
---|
| 61 | heartbeatTimer.Elapsed += new ElapsedEventHandler(heartbeatTimer_Elapsed);
|
---|
[923] | 62 | wcfService = WcfService.Instance;
|
---|
[1379] | 63 | wcfService.SendHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(wcfService_ProcessHeartBeatCompleted);
|
---|
[793] | 64 | heartbeatTimer.Start();
|
---|
[733] | 65 | }
|
---|
| 66 |
|
---|
[735] | 67 | /// <summary>
|
---|
| 68 | /// This Method is called every time the timer ticks
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <param name="sender"></param>
|
---|
| 71 | /// <param name="e"></param>
|
---|
[733] | 72 | void heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e) {
|
---|
[1031] | 73 | Console.WriteLine("tick");
|
---|
[946] | 74 | ClientInfo info = ConfigManager.Instance.GetClientInfo();
|
---|
[1368] | 75 | // Todo: remove tempfix for free cores.
|
---|
[1119] | 76 |
|
---|
[1379] | 77 | PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true);
|
---|
| 78 | int mb = (int)(counter.NextValue() / 1024 / 1024);
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | HeartBeatData heartBeatData = new HeartBeatData {
|
---|
[1119] | 82 | ClientId = info.ClientId,
|
---|
[1368] | 83 | FreeCores = info.NrOfCores - (ClientStatusInfo.JobsFetched - ClientStatusInfo.JobsProcessed),
|
---|
[1379] | 84 | FreeMemory = mb,
|
---|
[1368] | 85 | JobProgress = ConfigManager.Instance.GetProgressOfAllJobs()
|
---|
[1119] | 86 | };
|
---|
[932] | 87 | if (wcfService.ConnState == NetworkEnum.WcfConnState.Failed) {
|
---|
[923] | 88 | wcfService.Connect();
|
---|
[1255] | 89 | } else if (wcfService.ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
[923] | 90 | wcfService.SendHeartBeatAsync(heartBeatData);
|
---|
| 91 | }
|
---|
[733] | 92 | }
|
---|
| 93 |
|
---|
[1379] | 94 | void wcfService_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
|
---|
[798] | 95 | System.Diagnostics.Debug.WriteLine("Heartbeat received! ");
|
---|
| 96 | e.Result.ActionRequest.ForEach(mc => MessageQueue.GetInstance().AddMessage(mc));
|
---|
[790] | 97 | }
|
---|
[1097] | 98 |
|
---|
| 99 | public void StopHeartBeat() {
|
---|
| 100 | heartbeatTimer.Dispose();
|
---|
| 101 | }
|
---|
[733] | 102 | }
|
---|
| 103 | }
|
---|