Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppActorEndpoint.cs
1using System;
2using System.IO;
3using System.Net.Http;
4using System.Net.Security;
5using System.Security.Cryptography.X509Certificates;
6using System.Text;
7using System.Threading.Tasks;
8using System.Xml;
10using Waher.Content;
13
15{
19 public abstract class XmppActorEndpoint : XmppActor
20 {
21 private string endpoint;
22
29 : base(Parent, Model)
30 {
31 }
32
42 {
43 }
44
49 public override Task FromXml(XmlElement Definition)
50 {
51 if (Definition.HasAttribute("endpoint"))
52 this.endpoint = XML.Attribute(Definition, "endpoint");
53 else
54 this.endpoint = null;
55
56 return base.FromXml(Definition);
57 }
58
62 public override async Task Initialize()
63 {
64 await base.Initialize();
65
66 if (string.IsNullOrEmpty(this.endpoint))
67 {
68 using (HttpClient HttpClient = new HttpClient(new HttpClientHandler()
69 {
70 ServerCertificateCustomValidationCallback = this.RemoteCertificateValidationCallback,
71 UseCookies = false
72 })
73 {
74 Timeout = TimeSpan.FromMilliseconds(60000)
75 })
76 {
77 try
78 {
79 HttpResponseMessage Response = await HttpClient.GetAsync("http://" + this.Host + "/.well-known/host-meta");
80 Response.EnsureSuccessStatusCode();
81
82 Stream Stream = await Response.Content.ReadAsStreamAsync(); // Regardless of status code, we check for XML content.
83 byte[] Bin = await Response.Content.ReadAsByteArrayAsync();
84 string CharSet = Response.Content.Headers.ContentType.CharSet;
85 Encoding Encoding;
86
87 if (string.IsNullOrEmpty(CharSet))
88 Encoding = Encoding.UTF8;
89 else
90 Encoding = InternetContent.GetEncoding(CharSet);
91
92 string XmlResponse = Encoding.GetString(Bin);
93 XmlDocument Doc = new XmlDocument()
94 {
95 PreserveWhitespace = true
96 };
97 Doc.LoadXml(XmlResponse);
98
99 if (Doc.DocumentElement != null && Doc.DocumentElement.LocalName == "XRD")
100 this.endpoint = this.FindEndpoint(Doc);
101 }
102 catch (Exception)
103 {
104 this.endpoint = null;
105 }
106 }
107
108 if (string.IsNullOrEmpty(this.endpoint))
109 throw new Exception("Unable to find endpoint of " + this.Host);
110 }
111 }
112
116 protected abstract string EndpointType
117 {
118 get;
119 }
120
125 protected string FindEndpoint(XmlDocument Xrd)
126 {
127 string Rel = this.EndpointType;
128
129 foreach (XmlNode N in Xrd.DocumentElement.ChildNodes)
130 {
131 if (N is XmlElement E && E.LocalName == "Link" && XML.Attribute(E, "rel") == Rel)
132 return XML.Attribute(E, "href");
133 }
134
135 return null;
136 }
137
138 private bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
139 {
140 bool Valid;
141
142 if (sslPolicyErrors == SslPolicyErrors.None)
143 Valid = true;
144 else
145 Valid = this.TrustServer;
146
147 return Valid;
148 }
149
158 public override async Task<Actor> CreateInstanceAsync(int InstanceIndex, string InstanceId)
159 {
161 Result.endpoint = this.endpoint;
162 return Result;
163 }
164
169 protected async override Task<XmppCredentials> GetInstanceCredentials()
170 {
171 XmppCredentials Result = await base.GetInstanceCredentials();
172 Result.UriEndpoint = this.endpoint;
173 return Result;
174 }
175 }
176}
Root node of a simulation model
Definition: Model.cs:49
string InstanceId
ID of actor instance.
Definition: Actor.cs:57
int InstanceIndex
Actor instance index.
Definition: Actor.cs:67
int N
Number of actors of this type specified.
Definition: Actor.cs:62
Abstract base class for XMPP actors with custom endpoint.
XmppActorEndpoint(ISimulationNode Parent, Model Model)
Abstract base class for XMPP actors with custom endpoint.
override async Task Initialize()
Initialized the node before simulation.
async override Task< XmppCredentials > GetInstanceCredentials()
Gets XMPP credentials for the instance.
abstract string EndpointType
Type of XRD link representing endpoint.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
override async Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
string FindEndpoint(XmlDocument Xrd)
Finds the endpoint from the XRD XML definition.
XmppActorEndpoint(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Abstract base class for XMPP actors with custom endpoint.
Abstract base class for XMPP actors.
Definition: XmppActor.cs:23
bool TrustServer
If server is to be trusted, regardless of state of certificate.
Definition: XmppActor.cs:95
Static class managing encoding and decoding of internet content.
static Encoding GetEncoding(string CharacterSet)
Gets a character encoding from its name.
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Class containing credentials for an XMPP client connection.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.