NTP Analyzer  0.8.2
Analyze the operation of time servers
NtpctlHostImporter.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.Collections.Generic;
23 using System.Linq;
24 using Ntp.Analyzer.Data;
25 using Ntp.Analyzer.Objects;
26 using Ntp.Common.Log;
27 
28 namespace Ntp.Analyzer.Import
29 {
30  public sealed class NtpctlHostImporter : NtpctlImporter<HostReading>
31  {
32  internal NtpctlHostImporter(Host host, ReadingBulk bulk, LogBase log)
33  : base(host.Id, log)
34  {
35  this.host = host;
36  this.bulk = bulk;
37  }
38 
39  private readonly ReadingBulk bulk;
40  private readonly Host host;
41  private List<Peer> peers;
42 
43  protected override string ErrorMessage => LogMessage.ImportHostError;
44 
45  protected override void Initialize()
46  {
47  base.Initialize();
48 
49  if (host == null)
50  return;
51 
52  peers = DataFace.Instance.Peers.ToList();
53  }
54 
55  protected override void ReadFromStream()
56  {
57  // Skip header
58  Reader.ReadLine();
59  Reader.ReadLine();
60 
61  // Find SysPeer
62  while (Reader.Peek() != -1)
63  {
64  string peer = Reader.ReadLine();
65  string stat = Reader.ReadLine();
66  var entry = ParseLines(peer, stat);
67 
68  if (entry.TallyCode != TallyCode.SysPeer)
69  continue;
70 
71  CreateEntry(entry);
72  return;
73  }
74 
75  Log.OpenNtpUnsynced();
76  }
77 
78  private void CreateEntry(AssociationEntry entry)
79  {
80  IEnumerable<Peer> peerList = peers.Where(p => p.Ip == entry.Remote).ToList();
81 
82  Peer peer;
83 
84  switch (peerList.Count())
85  {
86  case 1:
87  peer = peerList.Single();
88  break;
89  case 0:
90  Log.PeerNotFound(host.Name, entry.Remote);
91  return;
92  default:
93  Log.MultiplePeersFound(host.Name, entry.Remote);
94  return;
95  }
96 
97  Log.Syncing(host.Name, peer.Name);
98 
99  var reading = new HostReading(host, peer, bulk, entry.Offset, entry.Jitter);
100  Entries.Add(reading);
101  }
102  }
103 }
double Jitter
Gets the dispersion of the peer in milliseconds.
double Offset
Gets the offset of the peer in milliseconds.
NtpctlHostImporter(Host host, ReadingBulk bulk, LogBase log)
PeerDatabaseMapper Peers
Gets the peer mapper.
Definition: DataFace.cs:63
string Remote
Gets the address of the remote peer.
Singleton facade class used to access memory persistent data.
Definition: DataFace.cs:30
void CreateEntry(AssociationEntry entry)
static DataFace Instance
Gets the Singleton instance.
Definition: DataFace.cs:51