NTP Analyzer  0.8.2
Analyze the operation of time servers
Heartbeat.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Text;
24 using System.Threading;
25 using Ntp.Common.Log;
26 
27 namespace Ntp.Common.App
28 {
29  public class Heartbeat
30  {
31  public Heartbeat(LogBase log, int interval)
32  {
33  this.log = log;
34  this.interval = interval;
35  }
36 
37  private readonly int interval;
38  private readonly LogBase log;
39  private bool alive;
40  private int beats;
41  private DateTime start;
42 
43  public void Start()
44  {
45  start = DateTime.Now;
46  alive = true;
47  beats = 0;
48 
49  var thread = new Thread(Pulse)
50  {
51  Name = "Heartbeat",
52  IsBackground = true,
53  Priority = ThreadPriority.AboveNormal
54  };
55 
56  thread.Start();
57  log.HeartbeatStarted(interval);
58  }
59 
60  public void Stop()
61  {
62  alive = false;
63  }
64 
65  private void Pulse()
66  {
67  while (alive)
68  {
69  int next = (beats + 1)*interval;
70  var sleep = start.AddMinutes(next).Subtract(DateTime.Now).TotalMilliseconds;
71  Thread.Sleep(Convert.ToInt32(sleep));
72  beats++;
73 
74  var span = TimeSpan.FromMinutes(beats*interval);
75  var builder = new StringBuilder();
76 
77  if (span.Days != 0)
78  {
79  builder.Append(span.Days);
80  builder.Append(" day");
81  }
82 
83  if (span.Days > 1)
84  {
85  builder.Append("s");
86  }
87 
88  if (span.Days >= 1)
89  {
90  if (span.Hours != 0 && span.Minutes == 0)
91  {
92  builder.Append(" and ");
93  }
94  else if (span.Hours == 0 && span.Minutes != 0)
95  {
96  builder.Append(" and ");
97  }
98  else if (span.Hours != 0 && span.Minutes != 0)
99  {
100  builder.Append(", ");
101  }
102  }
103 
104  if (span.Hours != 0)
105  {
106  builder.Append(span.Hours);
107  builder.Append(" hour");
108  }
109 
110  if (span.Hours > 1)
111  {
112  builder.Append("s");
113  }
114 
115  if (span.Hours >= 1 && span.Minutes != 0)
116  {
117  builder.Append(" ");
118  }
119 
120  if (span.Hours != 0 && span.Minutes != 0)
121  {
122  builder.Append("and ");
123  }
124 
125  if (span.Minutes != 0)
126  {
127  builder.Append(span.Minutes);
128  builder.Append(" minute");
129  }
130 
131  if (span.Minutes > 1)
132  {
133  builder.Append("s");
134  }
135 
136  log.HeartbeatUptime(builder.ToString());
137  }
138  }
139  }
140 }
readonly int interval
Definition: Heartbeat.cs:37
readonly LogBase log
Definition: Heartbeat.cs:38
Heartbeat(LogBase log, int interval)
Definition: Heartbeat.cs:31