Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceRegistrationComponent.cs
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using System.Threading.Tasks;
6using Waher.Events;
10
12{
14 {
15 public const string NamespaceServiceRegistration = "http://waher.se/Schema/ServiceRegistration.xsd";
16
18 : base(Server, Subdomain, Name)
19 {
20 this.RegisterMessageHandler("register", NamespaceServiceRegistration, this.RegistrationMessageHandler, true);
21 this.RegisterIqSetHandler("register", NamespaceServiceRegistration, this.RegistrationIqSetHandler, false);
22 }
23
27 public override void Dispose()
28 {
29 this.UnregisterMessageHandler("register", NamespaceServiceRegistration, this.RegistrationMessageHandler, true);
30 }
31
32 public override bool SupportsAccounts => false;
33
34 private Task RegistrationMessageHandler(object Sender, MessageEventArgs e)
35 {
36 return this.RegistrationHandler(e.Content, e.From);
37 }
38
39 private async Task RegistrationIqSetHandler(object Sender, IqEventArgs e)
40 {
41 try
42 {
43 await this.RegistrationHandler(e.Query, e.From);
44 await e.IqResult(string.Empty, e.To);
45 }
46 catch (Exception ex)
47 {
48 await e.IqError(ex, e.To);
49 }
50 }
51
52 private async Task RegistrationHandler(XmlElement E, XmppAddress From)
53 {
54 CaseInsensitiveString BareJid = XML.Attribute(E, "jid").ToLower();
55 if (string.IsNullOrEmpty(BareJid) || BareJid != From.BareJid)
56 {
57 Log.Error("Invalid registration received:\r\r\n" + E.OuterXml, BareJid, From.BareJid);
58 return;
59 }
60
61 ServiceRegistration Registration = await Database.FindFirstDeleteRest<ServiceRegistration>(new FilterFieldEqualTo("BareJid", BareJid));
62 bool Created = Registration is null;
63
64 if (Created)
65 {
66 Registration = new ServiceRegistration()
67 {
68 BareJid = BareJid
69 };
70 }
71
72 Registration.ClientName = XML.Attribute(E, "clientName");
73 Registration.ClientVersion = XML.Attribute(E, "clientVersion");
74 Registration.ClientOS = XML.Attribute(E, "clientOS");
75 Registration.Language = XML.Attribute(E, "language");
76 Registration.Host = XML.Attribute(E, "host");
77 Registration.Domain = XML.Attribute(E, "domain");
78 Registration.Created = XML.Attribute(E, "created", DateTime.MinValue);
79 Registration.Updated = XML.Attribute(E, "updated", DateTime.MinValue);
80
81 List<string> Features = new List<string>();
82 List<string> Assemblies = new List<string>();
83 List<Annotation> Annotations = new List<Annotation>();
84
85 foreach (XmlNode N in E.ChildNodes)
86 {
87 switch (N.LocalName)
88 {
89 case "Feature":
90 Features.Add(N.InnerText);
91 break;
92
93 case "Assembly":
94 Assemblies.Add(N.InnerText);
95 break;
96
97 case "Annotation":
98 XmlElement E2 = (XmlElement)N;
99 string Tag = XML.Attribute(E2, "tag");
100 string Value = N.InnerText;
101
102 Annotations.Add(new Annotation(Tag, Value));
103 break;
104 }
105 }
106
107 Registration.Features = Features.ToArray();
108 Registration.Assemblies = Assemblies.ToArray();
109 Registration.Annotations = Annotations.ToArray();
110
111 if (Created)
112 await Database.InsertLazy(Registration);
113 else
114 await Database.UpdateLazy(Registration);
115 }
116
117 public static ServiceRegistration GetRegistration(CaseInsensitiveString BareJid)
118 {
119 return GetRegistrationAsync(BareJid).Result;
120 }
121
122 public static async Task<ServiceRegistration> GetRegistrationAsync(CaseInsensitiveString BareJid)
123 {
124 return await Database.FindFirstDeleteRest<ServiceRegistration>(new FilterFieldEqualTo("BareJid", BareJid));
125 }
126 }
127}
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
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:682
Base class for components.
Definition: Component.cs:16
bool UnregisterMessageHandler(string LocalName, string Namespace, EventHandlerAsync< MessageEventArgs > Handler, bool RemoveNamespaceAsFeature)
Unregisters a message handler.
Definition: Component.cs:297
void RegisterIqSetHandler(string LocalName, string Namespace, EventHandlerAsync< IqEventArgs > Handler, bool PublishNamespaceAsFeature)
Registers an IQ-Set handler.
Definition: Component.cs:161
CaseInsensitiveString Subdomain
Subdomain name.
Definition: Component.cs:76
XmppServer Server
XMPP Server.
Definition: Component.cs:96
void RegisterMessageHandler(string LocalName, string Namespace, EventHandlerAsync< MessageEventArgs > Handler, bool PublishNamespaceAsFeature)
Registers a message handler.
Definition: Component.cs:190
Event arguments for IQ queries.
Definition: IqEventArgs.cs:12
XmppAddress From
From address attribute
Definition: IqEventArgs.cs:93
Task IqResult(string Xml, string From)
Returns a response to the current request.
Definition: IqEventArgs.cs:113
XmlElement Query
Query element, if found, null otherwise.
Definition: IqEventArgs.cs:70
XmppAddress To
To address attribute
Definition: IqEventArgs.cs:88
async Task IqError(string ErrorType, string Xml, XmppAddress From, string ErrorText, string Language)
Returns an error response to the current request.
Definition: IqEventArgs.cs:137
XmppAddress From
From address attribute
XmlElement Content
Content element, if found, null otherwise.
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task InsertLazy(object Object)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
Definition: Database.cs:156
static async Task UpdateLazy(object Object)
Updates an object in the database, if unlocked. If locked, object will be updated at next opportunity...
Definition: Database.cs:687
This filter selects objects that have a named field equal to a given value.
override bool SupportsAccounts
If the component supports accounts (true), or if the subdomain name is the only valid address.