Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Route.cs
1using System;
2using System.Collections.Generic;
3using System.Net.NetworkInformation;
4using System.Threading.Tasks;
12
14{
19 {
29 {
30 }
31
35 public override string FunctionName => nameof(Route);
36
41 public override bool IsAsynchronous => true;
42
50 {
51 return this.EvaluateScalarAsync(Argument, Variables).Result;
52 }
53
60 public override async Task<IElement> EvaluateScalarAsync(string Argument, Variables Variables)
61 {
62 using (System.Net.NetworkInformation.Ping Icmp = new System.Net.NetworkInformation.Ping())
63 {
64 PingReply Response;
65 PingOptions Options = new PingOptions()
66 {
67 Ttl = 1
68 };
69 LinkedList<IElement> Elements = new LinkedList<IElement>();
70 IElement E;
71
72 do
73 {
74 Response = await Icmp.SendPingAsync(Argument, 2000, Ping.data, Options);
75
76 Elements.AddLast(new DoubleNumber(Options.Ttl));
77 Elements.AddLast(new ObjectValue(Response.Address));
78
79 if (Response.Status == IPStatus.Success)
80 E = new PhysicalQuantity(Response.RoundtripTime, Unit.Parse("ms"));
81 else if (Response.Status == IPStatus.TimedOut ||
82 Response.Status == IPStatus.TimeExceeded ||
83 Response.Status == IPStatus.TtlExpired)
84 {
85 E = ObjectValue.Null;
86 }
87 else
88 E = new StringValue(Ping.GetErrorMessage(Response));
89
90 Elements.AddLast(E);
91
92 try
93 {
94 string[] Names = await DnsResolver.ReverseDns(Response.Address);
95
96 if (Names.Length == 1)
97 E = new StringValue(Names[0]);
98 else
99 E = new ObjectVector(Names);
100 }
101 catch (Exception)
102 {
103 E = ObjectValue.Null;
104 }
105
106 Elements.AddLast(E);
107 Options.Ttl++;
108 }
109 while (Response.Status != IPStatus.Success && Options.Ttl < 256);
110
111 return new ObjectMatrix(Elements.Count / 4, 4, Elements)
112 {
113 ColumnNames = new string[] { "TTL", "IP", "Roundtrip", "Name" }
114 };
115 }
116 }
117
125 {
126 return this.EvaluateScalarAsync(Argument, Variables).Result;
127 }
128
135 public override Task<IElement> EvaluateScalarAsync(IElement Argument, Variables Variables)
136 {
137 return this.EvaluateScalarAsync(Argument.AssociatedObjectValue?.ToString() ?? string.Empty, Variables);
138 }
139 }
140}
DNS resolver, as defined in:
Definition: DnsResolver.cs:32
static async Task< string[]> ReverseDns(IPAddress Address)
Performs a reverse DNS lookup of an IP address.
Definition: DnsResolver.cs:973
Class managing a script expression.
Definition: Expression.cs:39
Base class for funcions of one scalar variable.
ScriptNode Argument
Function argument.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Performs an ICMP Echo to ping a remote endpoint, and return the roundtrip time.
Definition: Ping.cs:17
Uses the ICMP Echo protocol to find the route in the IP network to a given host.
Definition: Route.cs:19
override Task< IElement > EvaluateScalarAsync(IElement Argument, Variables Variables)
Evaluates the function on a scalar argument.
Definition: Route.cs:135
override IElement EvaluateScalar(string Argument, Variables Variables)
Evaluates the function on a scalar argument.
Definition: Route.cs:49
override IElement EvaluateScalar(IElement Argument, Variables Variables)
Evaluates the function on a scalar argument.
Definition: Route.cs:124
override async Task< IElement > EvaluateScalarAsync(string Argument, Variables Variables)
Evaluates the function on a scalar argument.
Definition: Route.cs:60
override string FunctionName
Name of the function
Definition: Route.cs:35
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Route.cs:41
Route(ScriptNode Argument, int Start, int Length, Expression Expression)
Uses the ICMP Echo protocol to find the route in the IP network to a given host.
Definition: Route.cs:27
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Represents a unit.
Definition: Unit.cs:15
static Unit Parse(string UnitString)
Parses a unit string.
Definition: Unit.cs:123
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33