NTP Analyzer  0.8.2
Analyze the operation of time servers
DefaultPeerInfoRender.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 System.Text;
25 using Ntp.Analyzer.Objects;
26 
27 namespace Ntp.Analyzer.Render.Peer
28 {
30  {
31  public DefaultPeerInfoRender(Uri webPath, TimeServer server, string name)
32  : base(webPath)
33  {
34  this.server = server;
35  this.name = name;
36  }
37 
38  private const string Na = "&nbsp;";
39  private readonly string name;
40  private readonly TimeServer server;
41 
42  public override string Render()
43  {
44  var builder = new StringBuilder();
45 
46  builder.AppendLine("<table>");
47  builder.AppendLine(@"<col class=""fst""><col class=""sec""><col class=""thd""><col class=""frt"">");
48  builder.AppendLine("<tbody>");
49 
50  builder.AppendLine("<tr>");
51  builder.Append(@" <td>Server location</td><td colspan=""3"">");
52  builder.Append(server != null ? server.Location : Na);
53  builder.AppendLine("</td>");
54  builder.AppendLine("</tr>");
55 
56  builder.AppendLine("<tr>");
57  builder.Append(@" <td>Synchronization</td><td colspan=""3"">");
58  builder.Append(server != null ? server.Server : Na);
59  builder.AppendLine("</td>");
60  builder.AppendLine("</tr>");
61 
62  builder.AppendLine("<tr>");
63  builder.Append(" <td>IP address</td><td>");
64  builder.Append(server?.Address?.ToString() ?? string.Empty);
65  builder.AppendLine("</td>");
66  builder.Append(" <td>Access policy</td><td>");
67  builder.Append(server != null ? GetServerAccess() : Na);
68  builder.AppendLine("</td>");
69  builder.AppendLine("</tr>");
70 
71  builder.AppendLine("<tr>");
72  builder.Append(" <td>IPv6 address</td><td>");
73  builder.Append(server != null ? server.V6Address ?? "N/A" : Na);
74  builder.AppendLine("</td>");
75  builder.Append(" <td>Service area</td><td>");
76  builder.Append(server != null ? GetServiceArea() : Na);
77  builder.AppendLine("</td>");
78  builder.AppendLine("</tr>");
79 
80  builder.AppendLine("<tr>");
81  builder.Append(@" <td>Pool member info</td><td>");
82  builder.Append(server != null ? GetPoolMemberLink() : Na);
83  builder.AppendLine("</td>");
84  builder.Append(" <td>Provider page</td><td>");
85  builder.Append(server != null ? GetProviderLink() : Na);
86  builder.AppendLine("</td>");
87  builder.AppendLine("</tr>");
88 
89  builder.AppendLine("<tr>");
90  builder.Append("<td>Description updated</td><td>");
91  builder.Append(server?.Updated.ToLongDateString() ?? Na);
92  builder.AppendLine("</td>");
93  if (server != null && server.Id <= 10000)
94  {
95  builder.Append(
96  @" <td>Additional info</td><td><a href=""http://support.ntp.org/bin/view/Servers/PublicTimeServer");
97  builder.Append(server.Id.ToString(CultureInfo.InvariantCulture).PadLeft(6, '0'));
98  builder.AppendLine(@""">support.ntp.org</a></td>");
99  }
100  else
101  {
102  builder.Append($@" <td>Additional info</td><td>{Na}</td>");
103  }
104  builder.AppendLine("</tr>");
105 
106  builder.AppendLine("</tbody>");
107  builder.AppendLine("</table>");
108 
109  return builder.ToString();
110  }
111 
112  public override string RenderFooter()
113  {
114  return string.Empty;
115  }
116 
117  public override string RenderHead()
118  {
119  var builder = new StringBuilder();
120 
121  builder.Append("<h2>");
122  builder.Append(server != null ? server.Name : name);
123  builder.Append("</h2>&nbsp;&nbsp;&nbsp;&nbsp;<h4>Stratum ");
124  builder.Append(server != null ? GetStratumText() : Na);
125  builder.AppendLine(" server</h4>");
126 
127  return builder.ToString();
128  }
129 
130  #region Translators
131 
132  private string GetStratumText()
133  {
134  switch (server.Stratum)
135  {
136  case 1:
137  return "one";
138  case 2:
139  return "two";
140  case 3:
141  return "three";
142  case 4:
143  return "four";
144  case 5:
145  return "five";
146  case 6:
147  return "six";
148  case 7:
149  return "seven";
150  default:
151  return "Unknown";
152  }
153  }
154 
155  private string GetServerAccess()
156  {
157  switch (server.AccessPolicy)
158  {
159  case "OpenAccess":
160  return "Open access";
161  default:
162  return "Unknown";
163  }
164  }
165 
166  private string GetServiceArea()
167  {
168  string description = server.ServiceArea.Trim();
169 
170  if (description == string.Empty)
171  return string.Empty;
172 
173  string firstLetter = description.Substring(0, 1);
174  string rest = description.Substring(1);
175 
176  return firstLetter.ToUpper() + rest;
177  }
178 
179  private string GetPoolMemberLink()
180  {
181  if (!server.IsPoolMember)
182  return "Server is not a pool member";
183 
184  var builder = new StringBuilder();
185  builder.Append(@"<a href=""http://www.pool.ntp.org/scores/");
186  builder.Append(server.Address);
187  builder.Append(@""">pool.ntp.org</a>");
188  return builder.ToString();
189  }
190 
191  private string GetProviderLink()
192  {
193  if (server.ProviderPage == null)
194  return "Not found";
195 
196  var builder = new StringBuilder();
197  builder.Append(@"<a href=""");
198  builder.Append(server.ProviderUrl);
199  builder.Append(@""">");
200  builder.Append(server.ProviderPage);
201  builder.Append("</a>");
202  return builder.ToString();
203  }
204 
205  #endregion
206  }
207 }
DefaultPeerInfoRender(Uri webPath, TimeServer server, string name)