forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleComponentConnection.cs
More file actions
468 lines (397 loc) · 13.6 KB
/
SimpleComponentConnection.cs
File metadata and controls
468 lines (397 loc) · 13.6 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using Waher.Content;
using Waher.Content.Xml;
using System.Threading.Tasks;
#if !WINDOWS_UWP
using Waher.Content.Xsl;
#endif
using Waher.Networking.XMPP;
namespace Waher.Mock
{
/// <summary>
/// Class containing information about a simple XMPP component connection.
/// </summary>
public class SimpleComponentConfiguration
{
#if !WINDOWS_UWP
private static readonly XmlSchema schema = XSL.LoadSchema("Waher.Mock.Schema.SimpleComponentConfiguration.xsd");
private const string expectedRootElement = "SimpleComponentConfiguration";
private const string expectedNamespace = "http://waher.se/Schema/SimpleComponentConfiguration.xsd";
#endif
/// <summary>
/// Default XMPP component port number.
/// </summary>
public const int DefaultPort = 5275;
private string host = string.Empty;
private string component = string.Empty;
private string secret = string.Empty;
private bool sniffer = false;
private int port = DefaultPort;
/// <summary>
/// Class containing information about a simple XMPP component connection.
/// </summary>
public SimpleComponentConfiguration()
{
}
/// <summary>
/// Class containing information about a simple XMPP component connection.
/// </summary>
/// <param name="FileName">Name of file containing configuration.</param>
public SimpleComponentConfiguration(string FileName)
{
XmlDocument Xml = new XmlDocument();
using (StreamReader r = File.OpenText(FileName))
{
Xml.Load(r);
}
#if !WINDOWS_UWP
XSL.Validate(FileName, Xml, expectedRootElement, expectedNamespace, schema);
#endif
this.Init(Xml.DocumentElement);
}
/// <summary>
/// Class containing information about a simple XMPP component connection.
/// </summary>
/// <param name="Xml">XML Document containing information.</param>
public SimpleComponentConfiguration(XmlDocument Xml)
{
#if !WINDOWS_UWP
XSL.Validate(string.Empty, Xml, expectedRootElement, expectedNamespace, schema);
#endif
this.Init(Xml.DocumentElement);
}
/// <summary>
/// Class containing information about a simple XMPP connection.
/// </summary>
/// <param name="Xml">XML element containing information.</param>
public SimpleComponentConfiguration(XmlElement Xml)
{
this.Init(Xml);
}
private string AssertNotEnter(string s)
{
if (s.StartsWith("ENTER ", StringComparison.CurrentCultureIgnoreCase))
throw new Exception("XMPP component configuration not correctly provided.");
return s;
}
private void Init(XmlElement E)
{
foreach (XmlNode N in E.ChildNodes)
{
switch (N.LocalName)
{
case "Host":
this.host = this.AssertNotEnter(N.InnerText);
break;
case "Port":
this.port = int.Parse(N.InnerText);
break;
case "Component":
this.component = this.AssertNotEnter(N.InnerText);
break;
case "Secret":
this.secret = this.AssertNotEnter(N.InnerText);
break;
case "Sniffer":
if (!CommonTypes.TryParse(N.InnerText, out this.sniffer))
this.sniffer = false;
break;
}
}
}
/// <summary>
/// Host name of XMPP server.
/// </summary>
public string Host
{
get { return this.host; }
set { this.host = value; }
}
/// <summary>
/// Name of component subdomain.
/// </summary>
public string Component
{
get { return this.component; }
set { this.component = value; }
}
/// <summary>
/// Secret of component.
/// </summary>
public string Secret
{
get { return this.secret; }
set { this.secret = value; }
}
/// <summary>
/// If a sniffer is to be used ('true' or 'false'). If 'true', network communication will be output to the console.
/// </summary>
public bool Sniffer
{
get { return this.sniffer; }
set { this.sniffer = value; }
}
/// <summary>
/// Port number to use when connecting to XMPP server.
/// </summary>
public int Port
{
get { return this.port; }
set { this.port = value; }
}
/// <summary>
/// Loads simple XMPP component configuration from an XML file. If file does not exist, or is not valid, a console dialog with the
/// user is performed, to get the relevant information, test it, and create the corresponding XML file for later use.
/// </summary>
/// <param name="FileName">Name of configuration file.</param>
/// <returns>Simple XMPP component configuration.</returns>
public static SimpleComponentConfiguration GetConfigUsingSimpleConsoleDialog(string FileName)
{
try
{
return new SimpleComponentConfiguration(FileName);
}
catch (Exception)
{
SimpleComponentConfiguration Config = new SimpleComponentConfiguration();
bool Ok;
Config.host = string.Empty;
Config.port = DefaultPort;
Config.component = string.Empty;
Config.secret = string.Empty;
#if WINDOWS_UWP
using (XmppComponent Component = new XmppComponent(Config.host, Config.port, Config.component, Config.secret,
"collaboration", "test", "Test Component"))
{
ManualResetEvent Connected = new ManualResetEvent(false);
ManualResetEvent Failure = new ManualResetEvent(false);
Component.OnStateChanged += (sender, NewState) =>
{
Console.Out.WriteLine(NewState.ToString());
switch (NewState)
{
case XmppState.Connected:
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine("Connection successful.");
Connected.Set();
break;
case XmppState.Error:
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine("Connection failed. Please update connection information.");
Failure.Set();
break;
}
};
switch (WaitHandle.WaitAny(new WaitHandle[] { Connected, Failure }, 20000))
{
case 0:
Ok = true;
break;
case 1:
default:
Ok = false;
break;
}
if (!Ok)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
}
#else
ConsoleColor FgBak = Console.ForegroundColor;
string s;
string Default;
do
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine();
Console.Out.WriteLine("To setup a connection with the XMPP component, please answer the following");
Console.Out.WriteLine("questions:");
Default = Config.host;
Console.Out.WriteLine();
Console.Out.WriteLine("What XMPP server do you want to use? Press ENTER to use " + Default);
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.Out.Write("XMPP Server: ");
Config.host = Console.In.ReadLine();
if (string.IsNullOrEmpty(Config.host))
Config.host = Default;
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine();
Console.Out.WriteLine("You've selected to use '" + Config.host + "'. Is this correct? [y/n]");
s = Console.In.ReadLine();
Console.Out.WriteLine();
}
while (!s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase));
Default = Config.port.ToString();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine("What port do you want to connect to? Press ENTER to use " + Default);
do
{
Console.ForegroundColor = ConsoleColor.White;
do
{
Console.Out.Write("Port Number: ");
s = Console.In.ReadLine();
if (string.IsNullOrEmpty(s))
s = Default;
}
while (!int.TryParse(s, out Config.port) || Config.port < 1 || Config.port > 65535);
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine();
Console.Out.WriteLine("You've selected to use '" + Config.port.ToString() + "'. Is this correct? [y/n]");
s = Console.In.ReadLine();
Console.Out.WriteLine();
}
while (!s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase));
Default = Config.component;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine("What component to you want to connect to?");
if (!string.IsNullOrEmpty(Default))
Console.Out.WriteLine("Press ENTER to use " + Default);
do
{
Console.ForegroundColor = ConsoleColor.White;
do
{
Console.Out.Write("Component: ");
Config.component = Console.In.ReadLine();
if (string.IsNullOrEmpty(Config.component))
Config.component = Default;
}
while (string.IsNullOrEmpty(Config.component));
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine();
Console.Out.WriteLine("You've selected to use '" + Config.component + "'. Is this correct? [y/n]");
s = Console.In.ReadLine();
Console.Out.WriteLine();
}
while (!s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase));
Default = Config.secret;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine("What secret goes with the component? Remember that the configuration will,");
Console.Out.WriteLine("be stored in a simple text file along with the application.");
if (!string.IsNullOrEmpty(Default))
Console.Out.WriteLine("Press ENTER to use " + Default);
do
{
Console.ForegroundColor = ConsoleColor.White;
do
{
Console.Out.Write("Secret: ");
Config.secret = Console.In.ReadLine();
if (string.IsNullOrEmpty(Config.secret))
Config.secret = Default;
}
while (string.IsNullOrEmpty(Config.secret));
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine();
Console.Out.WriteLine("You've selected to use '" + Config.secret + "'. Is this correct? [y/n]");
s = Console.In.ReadLine();
Console.Out.WriteLine();
}
while (!s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase));
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine("I will now try to connect to the server to see if the information");
Console.Out.WriteLine("provided is correct.");
using (XmppComponent Component = new XmppComponent(Config.host, Config.port, Config.component, Config.secret,
"collaboration", "test", "Test Component"))
{
ManualResetEvent Connected = new ManualResetEvent(false);
ManualResetEvent Failure = new ManualResetEvent(false);
Component.OnStateChanged += (sender, NewState) =>
{
Console.Out.WriteLine(NewState.ToString());
switch (NewState)
{
case XmppState.Connected:
Console.ForegroundColor = ConsoleColor.Green;
Console.Out.WriteLine("Connection successful.");
Connected.Set();
break;
case XmppState.Error:
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine("Connection failed. Please update connection information.");
Failure.Set();
break;
}
return Task.CompletedTask;
};
switch (WaitHandle.WaitAny(new WaitHandle[] { Connected, Failure }, 20000))
{
case 0:
Ok = true;
break;
case 1:
default:
Ok = false;
break;
}
}
}
while (!Ok);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Out.WriteLine("Do you want to use a sniffer? If you use a sniffer, XMPP network");
Console.Out.WriteLine("communication will be Output to the console in real-time. This can");
Console.Out.WriteLine("come in handy when debugging network communication.");
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.Out.Write("Sniffer [y/n]? ");
s = Console.In.ReadLine();
Config.sniffer = s.StartsWith("y", StringComparison.InvariantCultureIgnoreCase);
}
while (!Config.sniffer && !s.StartsWith("n", StringComparison.InvariantCultureIgnoreCase));
Console.Out.WriteLine();
Console.ForegroundColor = FgBak;
#endif
Config.Save(FileName);
return Config;
}
}
/// <summary>
/// Saves the configuration to a file.
/// </summary>
/// <param name="FileName">File Name.</param>
public void Save(string FileName)
{
StringBuilder Xml = new StringBuilder();
Xml.AppendLine("<?xml version='1.0' encoding='utf-8'?>");
Xml.AppendLine("<SimpleComponentConfiguration xmlns='http://waher.se/Schema/SimpleComponentConfiguration.xsd'>");
Xml.Append("\t<Host>");
Xml.Append(XML.Encode(this.host));
Xml.AppendLine("</Host>");
Xml.Append("\t<Port>");
Xml.Append(this.port.ToString());
Xml.AppendLine("</Port>");
Xml.Append("\t<Component>");
Xml.Append(XML.Encode(this.component));
Xml.AppendLine("</Component>");
Xml.Append("\t<Secret>");
Xml.Append(XML.Encode(this.secret));
Xml.AppendLine("</Secret>");
Xml.Append("\t<Sniffer>");
Xml.Append(CommonTypes.Encode(this.sniffer));
Xml.AppendLine("</Sniffer>");
Xml.AppendLine("</SimpleComponentConfiguration>");
File.WriteAllText(FileName, Xml.ToString(), Encoding.UTF8);
}
/// <summary>
/// Gets a new XMPP component connection using the settings provided in the current object.
/// </summary>
/// <returns>XMPP Component connection object.</returns>
/// <param name="IdentityCategory">Identity category, as defined in XEP-0030.</param>
/// <param name="IdentityType">Identity type, as defined in XEP-0030.</param>
/// <param name="IdentityName">Identity name, as defined in XEP-0030.</param>
public XmppComponent GetComponent(string IdentityCategory, string IdentityType, string IdentityName)
{
return new XmppComponent(this.host, this.port, this.component, this.secret, IdentityCategory, IdentityType, IdentityName);
}
}
}