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