NTP Analyzer  0.8.2
Analyze the operation of time servers
SignalHandler.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.Threading;
24 using Ntp.Common.App;
25 using Ntp.Common.Log;
26 
27 namespace Ntp.Common.System
28 {
29  public sealed class SignalHandler : IApplicationController
30  {
31  public SignalHandler(string name, LogBase log)
32  {
33  this.name = name;
34  this.log = log;
35  Reload = false;
36  Stopped = false;
37  }
38 
39  private readonly LogBase log;
40  private readonly string name;
41 
42  public bool Reload { get; private set; }
43 
44  public bool Stopped { get; private set; }
45 
46  public event EventHandler<EventArgs> ExitApplication;
47 
48  public void Start()
49  {
50  // Signal handler is only supported on Unix platforms
51  if (Environment.OSVersion.Platform != PlatformID.Unix)
52  return;
53 
54  var signalHandler = new Thread(Execute);
55  signalHandler.Start();
56  }
57 
58  private void Execute()
59  {
60  bool loop = true;
61 
62  while (loop)
63  {
64  try
65  {
66  switch (InterProcess.Wait(name, log))
67  {
68  case InterProcess.Signal.Exit:
69  loop = false;
70  log.SignalClosing();
71  RaiseExitApplicationEvent();
72  break;
73  case InterProcess.Signal.Reload:
74  Reload = true;
75  loop = false;
76  log.SignalReloading();
77  RaiseExitApplicationEvent();
78  break;
79  case InterProcess.Signal.Refresh:
80  var thread = new Thread(() =>
81  {
83  Thread.Sleep(30000);
85  });
86  log.SignalRefreshing();
87  thread.Start();
88  break;
89  case InterProcess.Signal.Notify:
90  break;
91  case InterProcess.Signal.Error:
92  loop = false;
93  log.SignalInterProcError();
94  RaiseExitApplicationEvent();
95  break;
96  default:
97  log.UnexpectedSignal();
98  break;
99  }
100  }
101  catch (Exception e)
102  {
103  log.SignalHandlerError(e);
104  loop = false;
105  RaiseExitApplicationEvent();
106  }
107  }
108  }
109 
111  {
112  Stopped = true;
113  ExitApplication?.Invoke(this, EventArgs.Empty);
114  }
115  }
116 }
static Signal Wait(string name, LogBase log)
Definition: InterProcess.cs:52
SignalHandler(string name, LogBase log)
EventHandler< EventArgs > ExitApplication
var e
Definition: bootstrap.min.js:6
static void Resume()
Definition: LogFactory.cs:96