NTP Analyzer  0.8.2
Analyze the operation of time servers
TimeServerLoader.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.Globalization;
24 using Ntp.Analyzer.Data.Log;
25 using Ntp.Analyzer.Objects;
26 using Ntp.Common.Log;
27 
28 namespace Ntp.Analyzer.Data.Import
29 {
30  internal sealed class TimeServerLoader : TimeServerWebAdapter
31  {
32  internal TimeServerLoader(LogBase log)
33  : base(log)
34  {
35  }
36 
37  private const string ServerTagStart = @"<div class=""twikiAfterText""></div><div class=""twikiForm"">";
38  private const string ServerTagEnd = @"</div><!-- /twikiForm --></div><!-- /patternContent-->";
39 
40  protected override string Provider => "support.ntp.org";
41 
42  public override TimeServer Import(int orgId)
43  {
44  string table = ImportServer(orgId);
45  if (table == null)
46  return null;
47 
48  var importer = new TimeServerImporter(Log);
49  var server = importer.ParseTable(table, orgId);
50  return server;
51  }
52 
53  private string ImportServer(int orgId)
54  {
55  string orgString = orgId.ToString(CultureInfo.InvariantCulture).PadLeft(6, '0');
56  string url = $"http://support.ntp.org/bin/view/Servers/PublicTimeServer{orgString}";
57  string html = FetchHtml(url, orgId);
58 
59  if (html == null)
60  return null;
61 
62  html = html.Replace("<nos>", string.Empty);
63 
64  try
65  {
66  int serverTagPos = html.IndexOf(ServerTagStart, StringComparison.Ordinal);
67  int tableStartPos = html.IndexOf("<table", serverTagPos, StringComparison.Ordinal);
68  int tableEndPos = html.IndexOf(ServerTagEnd, serverTagPos, StringComparison.Ordinal);
69  string table = html.Substring(tableStartPos, tableEndPos - tableStartPos);
70  return table;
71  }
72  catch (Exception e)
73  {
74  Log.TimeServerParseError("HTML", e);
75  return null;
76  }
77  }
78  }
79 }
override TimeServer Import(int orgId)
var e
Definition: bootstrap.min.js:6