NTP Analyzer  0.8.2
Analyze the operation of time servers
BootstrapPeerInfoRender.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.Net;
25 using System.Text;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Web;
28 
29 namespace Ntp.Analyzer.Render.Peer
30 {
32  {
34  Uri webPath,
35  TimeServer server,
36  DateTime now,
37  DateTime next,
38  string name,
39  string ip)
40  : base(webPath)
41  {
42  this.server = server;
43  this.now = now;
44  this.next = next;
45  this.name = name;
46  this.ip = ip;
47  }
48 
49  private readonly string ip;
50  private readonly string name;
51  private readonly DateTime next;
52  private readonly DateTime now;
53  private readonly TimeServer server;
54 
55  private string Name => string.IsNullOrEmpty(server?.Name) ? name : server.Name;
56 
57  private string StratumText
58  {
59  get
60  {
61  if (server == null || server.Stratum == 0)
62  return null;
63 
64  switch (server.Stratum)
65  {
66  case 1:
67  return "one";
68  case 2:
69  return "two";
70  case 3:
71  return "three";
72  case 4:
73  return "four";
74  case 5:
75  return "five";
76  case 6:
77  return "six";
78  case 7:
79  return "seven";
80  default:
81  return null;
82  }
83  }
84  }
85 
86  public string Time => string.Concat(
87  now.ToLongDateString(),
88  " ",
89  TimeZoneInfo.Local.IsDaylightSavingTime(now)
90  ? TimeZoneInfo.Local.DaylightName
91  : TimeZoneInfo.Local.StandardName,
92  " ",
93  now.ToLongTimeString()
94  );
95 
96  public string Location => string.IsNullOrEmpty(server?.Location) ? PageText.HtmlBlank : server.Location;
97 
98  private string ServerSync => string.IsNullOrEmpty(server?.Server) ? PageText.HtmlBlank : server.Server;
99 
100  private string IpV4Address
101  {
102  get
103  {
104  if (server?.Address == null || Equals(server.Address, IPAddress.None))
105  return PageText.HtmlBlank;
106 
107  return server.Address.ToString();
108  }
109  }
110 
111  private string ServerAccess
112  {
113  get
114  {
115  if (string.IsNullOrEmpty(server?.AccessPolicy))
116  return PageText.HtmlBlank;
117 
118  switch (server.AccessPolicy)
119  {
120  case "OpenAccess":
121  return "Open access";
122  default:
123  return "Unknown";
124  }
125  }
126  }
127 
128  private string IpV6Address => string.IsNullOrEmpty(server?.V6Address) ? PageText.HtmlBlank : server.V6Address;
129 
130  private string ServiceArea
131  {
132  get
133  {
134  if (string.IsNullOrEmpty(server?.ServiceArea))
135  return PageText.HtmlBlank;
136 
137  string description = server.ServiceArea.Trim();
138  string firstLetter = description.Substring(0, 1);
139  string rest = description.Substring(1);
140 
141  return firstLetter.ToUpper() + rest;
142  }
143  }
144 
145  private string PoolMemberStatus
146  {
147  get
148  {
149  if (server == null)
150  return PageText.HtmlBlank;
151 
152  return server.IsPoolMember
153  ? "Server is pool member"
154  : "Server is not a pool member";
155  }
156  }
157 
158  private string PoolMemberLink
159  {
160  get
161  {
162  if (server == null || !server.IsPoolMember)
163  return null;
164 
165  string address =
166  server.Address == null || Equals(server.Address, IPAddress.None)
167  ? ip
168  : server.Address.ToString();
169 
170  var builder = new StringBuilder();
171  builder.Append(@"<a class=""btn btn-lg btn-primary"" href=""");
172  builder.Append("http://www.pool.ntp.org/scores/");
173  builder.Append(address);
174  builder.Append(@""">View info on pool.ntp.org &raquo;</a>");
175  return builder.ToString();
176  }
177  }
178 
179  private string ProviderLink
180  {
181  get
182  {
183  if (string.IsNullOrEmpty(server?.ProviderPage))
184  return null;
185 
186  var builder = new StringBuilder();
187  builder.Append(@"<a class=""btn btn-lg btn-primary"" href=""");
188  builder.Append(server.ProviderUrl);
189  builder.Append(@""">View info on ");
190  builder.Append(server.ProviderPage);
191  builder.Append(" &raquo;</a>");
192  return builder.ToString();
193  }
194  }
195 
196  public override string Render()
197  {
198  var builder = new StringBuilder();
199 
200  builder.AppendLine(@" <div class=""row"">");
201  builder.AppendLine(@" <div class=""col-md-2""><b>Server location</b></div>");
202  builder.Append(@" <div class=""col-md-10"">");
203  builder.Append(Location);
204  builder.AppendLine("</div>");
205  builder.AppendLine(@" </div>");
206 
207  builder.AppendLine(@" <div class=""row"">");
208  builder.AppendLine(@" <div class=""col-md-2""><b>Synchronization</b></div>");
209  builder.Append(@" <div class=""col-md-10"">");
210  builder.Append(ServerSync);
211  builder.AppendLine("</div>");
212  builder.AppendLine(@" </div>");
213 
214  builder.AppendLine(@" <div class=""row"">");
215  builder.AppendLine(@" <div class=""col-md-2""><b>IP address</b></div>");
216  builder.Append(@" <div class=""col-md-4"">");
217  builder.Append(IpV4Address);
218  builder.AppendLine("</div>");
219 
220  builder.AppendLine(@" <div class=""col-md-2""><b>Access policy</b></div>");
221  builder.Append(@" <div class=""col-md-4"">");
222  builder.Append(ServerAccess);
223  builder.AppendLine("</div>");
224  builder.AppendLine(@" </div>");
225 
226  builder.AppendLine(@" <div class=""row"">");
227  builder.AppendLine(@" <div class=""col-md-2""><b>IPv6 address</b></div>");
228  builder.Append(@" <div class=""col-md-4"">");
229  builder.Append(IpV6Address);
230  builder.AppendLine(@"</div>");
231 
232  builder.AppendLine(@" <div class=""col-md-2""><b>Service area</b></div>");
233  builder.Append(@" <div class=""col-md-4"">");
234  builder.Append(ServiceArea);
235  builder.AppendLine(@"</div>");
236  builder.AppendLine(@" </div>");
237 
238  builder.AppendLine(@" <div class=""row"">");
239  builder.AppendLine(@" <div class=""col-md-2""><b>Pool member</b></div>");
240  builder.Append(@" <div class=""col-md-4"">");
241  builder.Append(PoolMemberStatus);
242  builder.AppendLine(@"</div>");
243 
244  builder.AppendLine(@" <div class=""col-md-2""><b>Description</b></div>");
245  builder.Append(@" <div class=""col-md-4"">");
246  builder.Append(server?.Updated.ToLongDateString() ?? PageText.HtmlBlank);
247  builder.AppendLine(@"</div>");
248  builder.AppendLine(@" </div>");
249 
250  builder.AppendLine(@" <p class=""pool"">");
251 
252  if (PoolMemberLink != null)
253  {
254  builder.Append(@" ");
255  builder.Append(PoolMemberLink);
256  builder.AppendLine(@"&nbsp;&nbsp;&nbsp;");
257  }
258 
259  if (ProviderLink != null)
260  {
261  builder.Append(@" ");
262  builder.Append(ProviderLink);
263  builder.AppendLine(@"&nbsp;&nbsp;&nbsp;");
264  }
265 
266  if (server != null && server.Id <= 10000)
267  {
268  builder.Append(@" ");
269  builder.Append(
270  @"<a class=""btn btn-lg btn-primary"" href=""http://support.ntp.org/bin/view/Servers/PublicTimeServer");
271  builder.Append(server.Id.ToString(CultureInfo.InvariantCulture).PadLeft(6, '0'));
272  builder.AppendLine(@""">View info on support.ntp.org &raquo;</a>");
273  }
274 
275  builder.AppendLine(@" </p>");
276 
277  return builder.ToString();
278  }
279 
280  public override string RenderFooter()
281  {
282  var builder = new StringBuilder();
283 
284  builder.AppendLine(" </div>");
285  builder.AppendLine(" </div>");
286 
287  return builder.ToString();
288  }
289 
290  public override string RenderHead()
291  {
292  var builder = new StringBuilder();
293 
294  builder.AppendLine(@" <div class=""container"">");
295  builder.AppendLine(@" <div class=""jumbotron"">");
296 
297  builder.Append(" <h1>");
298  builder.Append(Name);
299  builder.Append("</h1>");
300 
301  if (StratumText != null)
302  {
303  builder.Append("&nbsp;&nbsp;&nbsp;&nbsp;<h3>Stratum ");
304  builder.Append(StratumText);
305  builder.AppendLine(" server</h3>");
306  }
307 
308  builder.Append(" <p>");
309  builder.Append(Time);
310  builder.Append(". ");
311  builder.Append("Next refresh scheduled at ");
312  builder.Append(next.ToShortTimeString());
313  builder.AppendLine(".</p>");
314 
315  return builder.ToString();
316  }
317  }
318 }
const string HtmlBlank
Definition: PageText.cs:26
BootstrapPeerInfoRender(Uri webPath, TimeServer server, DateTime now, DateTime next, string name, string ip)