forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmppEventReceptor.cs
213 lines (183 loc) · 5.61 KB
/
XmppEventReceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Waher.Events;
using Waher.Content;
using Waher.Content.Xml;
using Waher.Networking.XMPP;
namespace Waher.Events.XMPP
{
/// <summary>
/// This class handles incoming events from the XMPP network. The default behaviour is to log incoming events to <see cref="Log"/>.
/// This behaviour can be overridden by provding an event handler for the <see cref="OnEvent"/> event.
///
/// The format is specified in XEP-0337:
/// http://xmpp.org/extensions/xep-0337.html
/// </summary>
public class XmppEventReceptor : IDisposable
{
private XmppClient client;
/// <summary>
/// This class handles incoming events from the XMPP network. The default behaviour is to log incoming events to <see cref="Log"/>.
/// This behaviour can be overridden by provding an event handler for the <see cref="OnEvent"/> event.
///
/// The format is specified in XEP-0337:
/// http://xmpp.org/extensions/xep-0337.html
/// </summary>
/// <param name="Client">Client on which to receive events from.</param>
public XmppEventReceptor(XmppClient Client)
{
this.client = Client;
this.client.RegisterMessageHandler("log", XmppEventSink.NamespaceEventLogging, this.EventMessageHandler, true);
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
this.client.UnregisterMessageHandler("log", XmppEventSink.NamespaceEventLogging, this.EventMessageHandler, true);
}
private void EventMessageHandler(object Sender, MessageEventArgs e)
{
XmlElement E = e.Content;
XmlElement E2;
List<KeyValuePair<string, object>> Tags = new List<KeyValuePair<string, object>>();
DateTime Timestamp = XML.Attribute(E, "timestamp", DateTime.MinValue);
string EventId = XML.Attribute(E, "id");
EventType Type = (EventType)XML.Attribute(E, "type", EventType.Informational);
EventLevel Level = (EventLevel)XML.Attribute(E, "level", EventLevel.Minor);
string Object = XML.Attribute(E, "object");
string Actor = XML.Attribute(E, "subject");
string Facility = XML.Attribute(E, "facility");
string Module = XML.Attribute(E, "module");
string Message = string.Empty;
string StackTrace = string.Empty;
foreach (XmlNode N in E.ChildNodes)
{
switch (N.LocalName)
{
case "message":
Message = N.InnerText;
break;
case "tag":
E2 = (XmlElement)N;
string TagName = XML.Attribute(E2, "name");
string TagValue = XML.Attribute(E2, "value");
string TagType = XML.Attribute(E2, "type");
object TagValueParsed = TagValue;
switch (TagType)
{
case "xs:anyURI":
Uri URI;
if (Uri.TryCreate(TagValue, UriKind.Absolute, out URI))
TagValueParsed = URI;
break;
case "xs:boolean":
bool b;
if (CommonTypes.TryParse(TagValue, out b))
TagValueParsed = b;
break;
case "xs:unsignedByte":
byte ui8;
if (byte.TryParse(TagValue, out ui8))
TagValueParsed = ui8;
break;
case "xs:short":
short i16;
if (short.TryParse(TagValue, out i16))
TagValueParsed = i16;
break;
case "xs:int":
int i32;
if (int.TryParse(TagValue, out i32))
TagValueParsed = i32;
break;
case "xs:long":
long i64;
if (long.TryParse(TagValue, out i64))
TagValueParsed = i64;
break;
case "xs:byte":
sbyte i8;
if (sbyte.TryParse(TagValue, out i8))
TagValueParsed = i8;
break;
case "xs:unsignedShort":
ushort ui16;
if (ushort.TryParse(TagValue, out ui16))
TagValueParsed = ui16;
break;
case "xs:unsignedInt":
uint ui32;
if (uint.TryParse(TagValue, out ui32))
TagValueParsed = ui32;
break;
case "xs:unsignedLong":
ulong ui64;
if (ulong.TryParse(TagValue, out ui64))
TagValueParsed = ui64;
break;
case "xs:decimal":
decimal d;
if (CommonTypes.TryParse(TagValue, out d))
TagValueParsed = d;
break;
case "xs:double":
double d2;
if (CommonTypes.TryParse(TagValue, out d2))
TagValueParsed = d2;
break;
case "xs:float":
float f;
if (CommonTypes.TryParse(TagValue, out f))
TagValueParsed = f;
break;
case "xs:time":
TimeSpan TS;
if (TimeSpan.TryParse(TagValue, out TS))
TagValueParsed = TS;
break;
case "xs:date":
case "xs:dateTime":
DateTime DT;
if (XML.TryParse(TagValue, out DT))
TagValueParsed = DT;
break;
case "xs:string":
case "xs:language":
default:
break;
}
Tags.Add(new KeyValuePair<string, object>(TagName, TagValueParsed));
break;
case "stackTrace":
StackTrace = N.InnerText;
break;
}
}
if (string.IsNullOrEmpty(Facility))
Facility = e.FromBareJID;
Event Event = new Event(Timestamp, Type, Message, Object, Actor, EventId, Level, Facility, Module, StackTrace, Tags.ToArray());
EventEventHandler h = this.OnEvent;
if (h == null)
Log.Event(Event);
else
{
try
{
h(this, new EventEventArgs(e, Event));
}
catch (Exception ex)
{
Log.Critical(ex);
}
}
}
/// <summary>
/// Event raised whenever an event has been received. If no event handler is defined, the default action is to log the event
/// to <see cref="Log"/>.
/// </summary>
public event EventEventHandler OnEvent = null;
}
}