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.Net.NetworkInformation;
3using 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 PingReply Response;
64 PingOptions Options = new PingOptions()
65 {
66 Ttl = 1
67 };
69 IElement E;
70
71 do
72 {
73 Response = await Icmp.SendPingAsync(Argument, 2000, Ping.data, Options);
74
75 Elements.Add(new DoubleNumber(Options.Ttl));
76 Elements.Add(new ObjectValue(Response.Address));
77
78 if (Response.Status == IPStatus.Success)
79 E = new PhysicalQuantity(Response.RoundtripTime, Unit.Parse("ms"));
80 else if (Response.Status == IPStatus.TimedOut ||
81 Response.Status == IPStatus.TimeExceeded ||
82 Response.Status == IPStatus.TtlExpired)
83 {
84 E = ObjectValue.Null;
85 }
86 else
87 E = new StringValue(Ping.GetErrorMessage(Response));
88
89 Elements.Add(E);
90
91 try
92 {
93 string[] Names = await DnsResolver.TryReverseDns(Response.Address);
94
95 if (Names is null)
96 E = ObjectValue.Null;
97 else if (Names.Length == 1)
98 E = new StringValue(Names[0]);
99 else
100 E = new ObjectVector(Names);
101 }
102 catch (Exception)
103 {
104 E = ObjectValue.Null;
105 }
106
107 Elements.Add(E);
108 Options.Ttl++;
109 }
110 while (Response.Status != IPStatus.Success && Options.Ttl < 256);
111
112 return new ObjectMatrix(Elements.Count / 4, 4, Elements)
113 {
114 ColumnNames = new string[] { "TTL", "IP", "Roundtrip", "Name" }
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(ToString(Argument) ?? string.Empty, Variables);
138 }
139 }
140}
DNS resolver, as defined in:
Definition: DnsResolver.cs:32
static Task< string[]> TryReverseDns(IPAddress Address)
Tries to perform a reverse DNS lookup of an IP address.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
int Count
Number of elements in collection.
Definition: ChunkedList.cs:68
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Class managing a script expression.
Definition: Expression.cs:41
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
override string ToString()
Definition: ScriptNode.cs:359
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:88
Represents a unit.
Definition: Unit.cs:16
static Unit Parse(string UnitString)
Parses a unit string.
Definition: Unit.cs:106
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21