NTP Analyzer  0.8.2
Analyze the operation of time servers
Cluster.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.Collections.Generic;
24 using System.Linq;
25 using System.Threading;
26 using Ntp.Common.App;
27 using Ntp.Common.Log;
28 
29 namespace Ntp.Common.Process
30 {
31  public sealed class Cluster : IDisposable
32  {
33  public Cluster(
34  Scheduler scheduler,
35  IApplicationController controller,
36  IEnumerable<IRequest> peers,
37  LogBase log)
38  {
39  this.scheduler = scheduler;
40  this.peers = peers;
41  this.log = log;
42 
43  waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
44  controller.ExitApplication += (sender, e) =>
45  {
46  run = false;
47  waitHandle.Set();
48  scheduler.WaitHandle.Set();
49  };
50  }
51 
52  private readonly LogBase log;
53  private readonly IEnumerable<IRequest> peers;
54  private readonly Scheduler scheduler;
55  private readonly EventWaitHandle waitHandle;
56  private bool run;
57 
58  public void Activate()
59  {
60  bool activated = true;
61  if (peers.Count() != 0)
62  {
63  log.ClusterStart();
64  }
65 
66  run = true;
67  while (run)
68  {
69  bool otherActive = false;
70  foreach (var request in peers)
71  {
72  try
73  {
74  string answer = request.Send("ping");
75 
76  if (answer != null && answer == "*" && activated)
77  {
78  log.ClusterNodeAlive(request);
79  }
80  else if (answer != null && answer == "active" && activated)
81  {
82  log.ClusterNodeActive(request);
83  }
84  else if (activated)
85  {
86  log.ClusterNodeDead(request);
87  }
88 
89  if (answer != null && answer == "active")
90  {
91  otherActive = true;
92  }
93  }
94  catch (Exception e)
95  {
96  log.ClusterNodeError(request, e);
97  }
98  }
99 
100  if (!otherActive)
101  {
102  try
103  {
104  scheduler.RunOneCycle();
105  }
106  catch (Exception e)
107  {
108  log.ClusterError(e);
109  return;
110  }
111  }
112  else
113  {
114  waitHandle.WaitOne(10000);
115  }
116 
117  // This node is now active and no longer "activated"
118  activated = false;
119  }
120 
121  scheduler.Stop();
122  }
123 
124  #region IDisposable Support
125 
126  private bool disposedValue;
127 
128  private void Dispose(bool disposing)
129  {
130  if (disposedValue)
131  return;
132 
133  if (disposing)
134  {
135  waitHandle.Dispose();
136  }
137 
138  disposedValue = true;
139  }
140 
142  {
143  Dispose(false);
144  }
145 
146  public void Dispose()
147  {
148  Dispose(true);
149  GC.SuppressFinalize(this);
150  }
151 
152  #endregion
153  }
154 }
void Dispose(bool disposing)
Definition: Cluster.cs:128
A scheduler performs scheduling of jobs according to job schedule descriptions.
Definition: Scheduler.cs:35
readonly LogBase log
Definition: Cluster.cs:52
var e
Definition: bootstrap.min.js:6
EventWaitHandle WaitHandle
Gets the wait handle of this Scheduler.
Definition: Scheduler.cs:70
readonly EventWaitHandle waitHandle
Definition: Cluster.cs:55
readonly Scheduler scheduler
Definition: Cluster.cs:54
Cluster(Scheduler scheduler, IApplicationController controller, IEnumerable< IRequest > peers, LogBase log)
Definition: Cluster.cs:33
readonly IEnumerable< IRequest > peers
Definition: Cluster.cs:53