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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Timers;
|
---|
27 | using HeuristicLab.Hive.Client.Common;
|
---|
28 | using HeuristicLab.Hive.Client.Communication;
|
---|
29 | using System.Diagnostics;
|
---|
30 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
31 | using HeuristicLab.Hive.Contracts;
|
---|
32 | //using BO = HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
33 |
|
---|
34 | namespace 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 | }
|
---|