forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteroperabilityClient.cs
120 lines (107 loc) · 3.42 KB
/
InteroperabilityClient.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
using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Waher.Content.Xml;
using Waher.Events;
namespace Waher.Networking.XMPP.Interoperability
{
/// <summary>
/// Implements the client-side for Interoperability interfaces, as defined in:
///
/// https://github.com/joachimlindborg/XMPP-IoT/blob/master/xep-0000-IoT-Interoperability.html
/// http://htmlpreview.github.io/?https://github.com/joachimlindborg/XMPP-IoT/blob/master/xep-0000-IoT-Interoperability.html
/// </summary>
public class InteroperabilityClient : XmppExtension
{
/// <summary>
/// Implements the client-side for Interoperability interfaces, as defined in:
///
/// https://github.com/joachimlindborg/XMPP-IoT/blob/master/xep-0000-IoT-Interoperability.html
/// http://htmlpreview.github.io/?https://github.com/joachimlindborg/XMPP-IoT/blob/master/xep-0000-IoT-Interoperability.html
/// </summary>
/// <param name="Client">XMPP Client</param>
public InteroperabilityClient(XmppClient Client)
: base(Client)
{
}
/// <summary>
/// Implemented extensions.
/// </summary>
public override string[] Extensions => new string[0];
/// <summary>
/// Sends a request for interoperability interfaces from an entity.
/// </summary>
/// <param name="Destination">JID of entity.</param>
/// <param name="Callback">Method to call when response or error is returned.</param>
/// <param name="State">State object to pass on to callback method.</param>
public void SendGetInterfacesRequest(string Destination, InteroperabilityInterfacesClientEventHandler Callback, object State)
{
this.client.SendIqGet(Destination, "<getInterfaces xmlns='" + InteroperabilityServer.NamespaceInteroperability + "'/>",
this.GetInterfacesResult, new object[] { Callback, State });
}
private void GetInterfacesResult(object Sender, IqResultEventArgs e)
{
List<string> Interfaces = new List<string>();
object[] P = (object[])e.State;
InteroperabilityInterfacesClientEventHandler Callback = (InteroperabilityInterfacesClientEventHandler)P[0];
object State = P[1];
if (e.Ok)
{
foreach (XmlNode N in e.Response.ChildNodes)
{
if (N.LocalName == "getInterfacesResponse")
{
foreach (XmlNode N2 in N.ChildNodes)
{
if (N2.LocalName == "interface")
Interfaces.Add(XML.Attribute((XmlElement)N2, "name"));
}
}
}
}
InteroperabilityClientEventArgs e2 = new InteroperabilityClientEventArgs(Interfaces.ToArray(), e);
if (Callback != null)
{
try
{
Callback(this, e2);
}
catch (Exception ex)
{
Log.Critical(ex);
}
}
}
/// <summary>
/// Gets interoperability interfaces from an entity.
/// </summary>
/// <param name="Destination">JID of entity.</param>
/// <param name="Timeout">Timeout in milliseconds.</param>
/// <exception cref="TimeoutException">If timeout occurs.</exception>
/// <returns>Array of Interfaces.</returns>
public string[] GetInterfaces(string Destination, int Timeout)
{
ManualResetEvent Done = new ManualResetEvent(false);
InteroperabilityClientEventArgs e = null;
try
{
this.SendGetInterfacesRequest(Destination, (sender, e2) =>
{
e = e2;
Done.Set();
}, null);
if (!Done.WaitOne(Timeout))
throw new TimeoutException();
}
finally
{
Done.Dispose();
}
if (!e.Ok)
throw e.StanzaError;
return e.Interfaces;
}
}
}