NTP Analyzer  0.8.2
Analyze the operation of time servers
HostSyntaxNode.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.Collections.Generic;
24 using System.Linq;
25 using Ntp.Analyzer.Config.Node;
28 using Ntp.Analyzer.Config.Table;
29 
30 namespace Ntp.Analyzer.Config.Syntax
31 {
32  public sealed class HostSyntaxNode : SyntaxNode<HostConfiguration>
33  {
34  public HostSyntaxNode(string name, int line)
35  : base(Symbol.KeywordHost, name, line)
36  {
37  }
38 
39  private Uri location;
40 
41  protected override HostConfiguration InternalCompile()
42  {
43  var hostId = Nodes.Single(n => n.Symbol == Symbol.KeywordHostId) as IntegerSettingNode;
44  var hostName = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordIp) as StringSettingNode ??
45  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordAddress) as StringSettingNode ??
46  Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostAddress) as StringSettingNode;
47  var hostType = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostType) as HostTypeNode;
48  var filePath = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordFilePath) as StringSettingNode;
49  var menu = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordMenu) as MenuSyntaxNode;
50  var aboutPage = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordAboutPage) as AboutPageSyntaxNode;
51  var page1 = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostGraphPage) as HostGraphPageSyntaxNode;
52  var page2 = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPeerGraphPage) as PeerGraphPageSyntaxNode;
53  var hostStats = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostStats) as StatSyntaxNode;
54  var hostIoStats = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordHostIoStats) as StatSyntaxNode;
55  var peerStats = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordPeerStats) as StatSyntaxNode;
56  var driftStats = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordDriftStats) as StatSyntaxNode;
57  var hostGraphs = Nodes.Where(n => n.Symbol == Symbol.KeywordHostGraph).Cast<HostGraphSyntaxNode>();
58  var hostPages = Nodes.Where(n => n.Symbol == Symbol.KeywordHostPage).Cast<HostPageSyntaxNode>();
59  var peerGraphs = Nodes.Where(n => n.Symbol == Symbol.KeywordPeerGraph).Cast<PeerGraphSyntaxNode>();
60  var peerPages = Nodes.Where(n => n.Symbol == Symbol.KeywordPeerPage).Cast<PeerPageSyntaxNode>();
61  var trafficGraphs = Nodes.Where(n => n.Symbol == Symbol.KeywordTrafficGraph).Cast<TrafficGraphSyntaxNode>();
62  var summery = Nodes.Where(n => n.Symbol == Symbol.KeywordPeerSummaryPage).Cast<PeerSummaryPageSyntaxNode>();
63 
64  if (hostId == null)
65  {
66  throw new InvalidOperationException(@"Internal compiler error: HostSyntaxNode");
67  }
68 
69  return new HostConfiguration(
70  Name,
71  hostId.Value,
72  hostName?.Value,
73  hostType?.HostType,
74  filePath?.Value,
75  location,
76  aboutPage?.Compile(),
77  page1?.Compile(),
78  hostIoStats?.Compile(),
79  hostStats?.Compile(),
80  menu?.Compile(),
81  page2?.Compile(),
82  peerStats?.Compile(),
83  (DriftStatConfiguration) driftStats?.Compile(),
84  hostGraphs.Select(c => c.Compile()),
85  hostPages.Select(c => c.Compile()),
86  peerGraphs.Select(c => c.Compile()),
87  peerPages.Select(c => c.Compile()),
88  summery.Select(c => c.Compile()),
89  trafficGraphs.Select(c => c.Compile())
90  );
91  }
92 
93  protected override void ValidateMandatories()
94  {
95  CheckIsUnique(new List<Symbol>
96  {
97  Symbol.KeywordHostId,
98  Symbol.KeywordFilePath,
99  Symbol.KeywordWebPath,
100  Symbol.KeywordHostType,
101  Symbol.KeywordTimeStamp,
102  Symbol.KeywordAboutPage,
103  Symbol.KeywordHostGraphPage,
104  Symbol.KeywordPeerGraphPage,
105  Symbol.KeywordMenu,
106  Symbol.KeywordHostStats,
107  Symbol.KeywordHostIoStats,
108  Symbol.KeywordPeerStats,
109  Symbol.KeywordDriftStats
110  });
111 
112  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordHostId});
113 
114  if (Nodes.Count(n => n.RequirePath) != 0)
115  CheckAllIsPresent(new List<Symbol> {Symbol.KeywordFilePath});
116  }
117 
118  protected override void ValidateReferences(SymbolTable table)
119  {
120  var webPath = Nodes.SingleOrDefault(n => n.Symbol == Symbol.KeywordWebPath) as StringSettingNode;
121  if (webPath == null)
122  return;
123 
124  var keyword = Keyword.Find(Symbol.KeywordLink).Name;
125  location = CheckLink(webPath.Value, keyword);
126  }
127 
128  protected override void ValidateTypes()
129  {
130  CheckTypeIs<IntegerSettingNode>(Symbol.KeywordHostId);
131  CheckTypeIs<StringSettingNode>(Symbol.KeywordFilePath);
132  CheckTypeIs<StringSettingNode>(Symbol.KeywordWebPath);
133  CheckTypeIs<HostTypeNode>(Symbol.KeywordHostType);
134  CheckTypeIs<TimeStampNode>(Symbol.KeywordTimeStamp);
135  }
136  }
137 }
override void ValidateReferences(SymbolTable table)
static Keyword Find(Symbol symbol)
Definition: Keyword.cs:216
override void ValidateTypes()
Override to validates the types in this syntax node.
override HostConfiguration InternalCompile()
var c
Definition: bootstrap.min.js:6
override void ValidateMandatories()
Override to validates the mandatory types in this syntax node.