Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TestCounter.cs
1using System.Threading.Tasks;
2using System.Xml;
7using Waher.Script;
8
10{
14 public class TestCounter : WafActions
15 {
16 private readonly StringAttribute name;
17 private readonly Int64Attribute delta;
18 private readonly Int64Attribute above;
19 private readonly Int64Attribute below;
20
24 public TestCounter()
25 : base()
26 {
27 }
28
35 public TestCounter(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
36 : base(Xml, Parent, Document)
37 {
38 this.name = new StringAttribute(Xml, "name");
39 this.delta = new Int64Attribute(Xml, "delta");
40 this.above = new Int64Attribute(Xml, "above");
41 this.below = new Int64Attribute(Xml, "below");
42 }
43
47 public override string LocalName => nameof(TestCounter);
48
56 public override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document) => new TestCounter(Xml, Parent, Document);
57
63 public override async Task<WafResult?> Review(ProcessingState State)
64 {
66 CaseInsensitiveString Name = await this.name.EvaluateAsync(Variables, string.Empty);
67 long Delta = await this.delta.EvaluateAsync(Variables, 0L);
68 long Count, Above, Below;
69
70 if (Delta == 0)
71 Count = await RuntimeCounters.GetCount(Name);
72 else
73 Count = await RuntimeCounters.IncrementCounter(Name, Delta);
74
75 if (this.above.IsEmpty)
76 {
77 if (this.below.IsEmpty)
78 return null;
79
80 Below = await this.below.EvaluateAsync(Variables, 0L);
81
82 if (Count < Below)
83 return await this.ReviewChildren(State);
84 }
85 else
86 {
87 Above = await this.above.EvaluateAsync(Variables, 0L);
88
89 if (this.below.IsEmpty)
90 {
91 if (Count > Above)
92 return await this.ReviewChildren(State);
93 }
94 else
95 {
96 Below = await this.below.EvaluateAsync(Variables, 0L);
97
98 if (Above < Below)
99 {
100 if (Count > Above && Count < Below)
101 return await this.ReviewChildren(State);
102 }
103 else if (Above > Below)
104 {
105 if (Count > Above || Count < Below)
106 return await this.ReviewChildren(State);
107 }
108 }
109 }
110
111 return null;
112 }
113 }
114}
Represents a case-insensitive string.
Static class managing persistent counters.
static Task< long > IncrementCounter(CaseInsensitiveString Key)
Increments a counter.
static async Task< long > GetCount(CaseInsensitiveString Key)
Gets the current count of a counter.
Collection of variables.
Definition: Variables.cs:25
TestCounter(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Tests a named counter.
Definition: TestCounter.cs:35
override async Task< WafResult?> Review(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: TestCounter.cs:63
override WafAction Create(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Creates a WAF action from its XML definition.
override string LocalName
XML Local Name for the XML element defining the action.
Definition: TestCounter.cs:47
Contains the current state of a review process.
Variables Variables
Set of variables.
Abstract base class for Web Application Firewall actions.
Definition: WafAction.cs:17
WebApplicationFirewall Document
Web Application Firewall document.
Definition: WafAction.cs:60
Abstract base class for Web Application Firewall actions containing other child actions.
Definition: WafActions.cs:14
async Task< WafResult?> ReviewChildren(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Definition: WafActions.cs:72
Web Application Firewall for HttpServer.