forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelReporter.cs
More file actions
725 lines (684 loc) · 29.4 KB
/
Copy pathIntelReporter.cs
File metadata and controls
725 lines (684 loc) · 29.4 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
namespace PleaseIgnore.IntelMap {
/// <summary>
/// Provides a component to monitor EVE log files and report new
/// entries to the TEST Intel Map for distribution.
/// </summary>
[DefaultEvent("IntelReported")]
public class IntelReporter : Component, INotifyPropertyChanged {
// The default value for RetryAuthenticationPeriod
private const string defaultRetryAuthenticationPeriod = "01:00:00";
// The default value for ChannelIdlePeriod
private const string defaultSessionTimeout = "00:10:00";
// The default value for KeepAlivePeriod
private const string defaultKeepAlivePeriod = "00:01:00";
// The default value for RetryPeriod
private const string defaultRetryPeriod = "00:00:30";
// Protect internal state
private readonly object syncRoot = new object();
// List of active IntelChannel objects
private readonly IntelChannelContainer channels;
// Timer to use when maintaining the session keepalive
private readonly Timer timerSession;
// The instance of ISynchronizeInvoke to use for messaging
[ContractPublicPropertyName("SynchronizingObject")]
private ISynchronizeInvoke synchronizingObject;
// The current channel processing state
[ContractPublicPropertyName("Status")]
private IntelStatus status;
// The time to wait between sending keep alives
[ContractPublicPropertyName("KeepAliveInterval")]
private TimeSpan keepAlivePeriod = TimeSpan.Parse(
defaultKeepAlivePeriod,
CultureInfo.InvariantCulture);
// Field backing the ChannelIdlePeriod property
[ContractPublicPropertyName("SessionTimeout")]
private TimeSpan sessionTimeout = TimeSpan.Parse(
defaultSessionTimeout,
CultureInfo.InvariantCulture);
// Field backing the RetryAuthenticationPeriod property
[ContractPublicPropertyName("AuthenticationRetryTimeout")]
private TimeSpan authRetryTimeout = TimeSpan.Parse(
defaultRetryAuthenticationPeriod,
CultureInfo.InvariantCulture);
// Field backing the ServiceUri property
[ContractPublicPropertyName("ServiceUri")]
private Uri serviceUri = new Uri(IntelExtensions.ReportUrl);
// The session used to contact the intel server
private IntelSession session;
// Field backing the Username property
private string username;
// Field backing the PasswordHash property
private string passwordHash;
// Date and time the last intel was reported to the server
private DateTime lastIntel;
// The last time an authentication attempt failed
private DateTime? lastAuthFailure;
/// <summary>
/// Initializes a new instance of the <see cref="IntelReporter"/>
/// class.
/// </summary>
public IntelReporter() : this(null) {
Contract.Ensures(Container == null);
Contract.Ensures(Status == IntelStatus.Stopped);
Contract.Ensures(!IsRunning);
}
/// <summary>
/// Initializes a new instance of the <see cref="IntelReporter"/>
/// class and adds it to the specified <see cref="Container"/>.
/// </summary>
public IntelReporter(IContainer container) {
Contract.Ensures(this.Container == container);
Contract.Ensures(Status == IntelStatus.Stopped);
Contract.Ensures(!IsRunning);
this.channels = new IntelChannelContainer();
this.channels.IntelReported += channels_IntelReported;
this.channels.PropertyChanged += channels_PropertyChanged;
this.timerSession = new Timer(this.timer_Callback);
if (container != null) {
container.Add(this);
}
}
/// <summary>
/// Occurs when a property value changes.
/// </summary>
/// <remarks>
/// The <see cref="PropertyChanged"/> event can indicate all properties
/// on the object have changed by using either <see langword="null"/> or
/// <see cref="String.Empty"/> as the property name in the
/// <see cref="PropertyChangedEventArgs"/>.
/// </remarks>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raised when a new intel report has been parsed from a log file.
/// </summary>
/// <remarks>
/// This call will be made from the <see cref="ThreadPool"/>. The
/// consumer will need synchronize to their local threads as
/// appropriate.
/// </remarks>
public event EventHandler<IntelEventArgs> IntelReported;
/// <summary>
/// Gets a value indicating the operating state of this
/// <see cref="IntelReporter"/>.
/// </summary>
public IntelStatus Status {
get {
return IntelExtensions.Combine(this.status, this.channels.Status);
}
private set {
if (this.status != value) {
var running = this.IsRunning;
this.status = value;
this.OnPropertyChanged("Status");
if (this.IsRunning != running) {
this.OnPropertyChanged("IsRunning");
}
}
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="IntelReporter"/>
/// is a normal operating state.
/// </summary>
public bool IsRunning {
get { return this.status.IsRunning(); }
}
/// <summary>
/// Gets an instance of <see cref="IntelChannelCollection"/>
/// </summary>
/// <remarks>
/// Calling <see cref="IntelChannel.Dispose"/> on an
/// <see cref="IntelChannel"/> will remove it from
/// <see cref="Channels"/>. It may be readded when the
/// channel list is redownloaded.
/// </remarks>
public IntelChannelCollection Channels {
get { return this.channels.Channels; }
}
/// <summary>
/// Gets or sets the object used to marshal the event handler
/// calls issued as a result of a <see cref="PropertyChanged"/>
/// or <see cref="IntelReported"/> event.
/// </summary>
[DefaultValue((object)null)]
public ISynchronizeInvoke SynchronizingObject {
get { return this.synchronizingObject; }
set {
Contract.Requires<InvalidOperationException>(!this.IsRunning);
if (this.synchronizingObject != value) {
this.synchronizingObject = value;
this.OnPropertyChanged("SynchronizingObject");
}
}
}
/// <summary>
/// Gets or sets the directory path to find EVE chat logs.
/// </summary>
/// <exception cref="ArgumentException">
/// The specified directory does not exist.
/// </exception>
[AmbientValue((string)null), Category("Behavior")]
public string Path {
get { return this.channels.Path; }
set { this.channels.Path = value; }
}
/// <summary>
/// Gets or sets the TEST Alliance AUTH username.
/// </summary>
/// <remarks>
/// Changes to the <see cref="Username"/>
/// or <see cref="PasswordHash"/> will not be used until the next
/// time the client tries to reauthenticate. To verify that the
/// authentication information is correct, call
/// <see cref="Authenticate"/> instead of setting <see cref="Username"/>
/// or <see cref="PasswordHash"/> directly.
/// </remarks>
[DefaultValue((String)null), Category("Behavior")]
public string Username {
get {
return this.username;
}
set {
if (this.username != value) {
this.username = value;
this.lastAuthFailure = null;
this.OnPropertyChanged("Username");
}
}
}
/// <summary>
/// Gets or sets the <em>hashed</em> services password for the user.
/// </summary>
/// <remarks>
/// Changes to the <see cref="Username"/>
/// or <see cref="PasswordHash"/> will not be used until the next
/// time the client tries to reauthenticate. To verify that the
/// authentication information is correct, call
/// <see cref="Authenticate"/> instead of setting <see cref="Username"/>
/// or <see cref="PasswordHash"/> directly.
/// </remarks>
[DefaultValue((String)null), Category("Behavior")]
public string PasswordHash {
get {
return this.passwordHash;
}
set {
if (this.passwordHash != value) {
this.passwordHash = value;
this.lastAuthFailure = null;
this.OnPropertyChanged("PasswordHash");
}
}
}
/// <summary>
/// Gets the number of events sent to the intel reporting server.
/// </summary>
public int IntelSent { get; private set; }
/// <summary>
/// Gets the number of events that were dropped due to network
/// or server errors.
/// </summary>
public int IntelDropped { get; private set; }
/// <summary>
/// Gets the number of users currently connected to the server.
/// </summary>
public int Users {
get {
Contract.Ensures(Contract.Result<int>() >= 0);
var session = this.session;
return (session != null) ? session.Users : 0;
}
}
/// <summary>
/// Gets or sets the time between downloads of the intel
/// channel list.
/// </summary>
[DefaultValue(typeof(TimeSpan), IntelChannelContainer.defaultUpdateInterval)]
[Category("Behavior")]
public TimeSpan ChannelUpdateInterval {
get {
Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
return this.channels.ChannelUpdateInterval;
}
set {
Contract.Requires<ArgumentOutOfRangeException>(value > TimeSpan.Zero, "value");
Contract.Requires<InvalidOperationException>(!IsRunning);
this.channels.ChannelUpdateInterval = value;
}
}
/// <summary>
/// Gets or sets the time between server keep alives sent
/// to the server while a session is open.
/// </summary>
[DefaultValue(typeof(TimeSpan), defaultKeepAlivePeriod)]
[Category("Behavior")]
public TimeSpan KeepAliveInterval {
get {
Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
return this.keepAlivePeriod;
}
set {
Contract.Requires<ArgumentOutOfRangeException>(
value > TimeSpan.Zero,
"value");
Contract.Requires<InvalidOperationException>(!IsRunning);
if (this.keepAlivePeriod != value) {
this.keepAlivePeriod = value;
this.OnPropertyChanged("KeepAliveInterval");
}
}
}
/// <summary>
/// Gets or sets the time to hold the <see cref="IntelSession"/>
/// open when not reporting fresh intel.
/// </summary>
[DefaultValue(typeof(TimeSpan), defaultSessionTimeout)]
[Category("Behavior")]
public TimeSpan SessionTimeout {
get {
Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
return this.sessionTimeout;
}
set {
Contract.Requires<ArgumentOutOfRangeException>(
value > TimeSpan.Zero,
"value");
Contract.Requires<InvalidOperationException>(!IsRunning);
if (this.sessionTimeout != value) {
this.sessionTimeout = value;
this.OnPropertyChanged("SessionTimeout");
}
}
}
/// <summary>
/// Gets or sets the time to wait after authentication fails
/// before trying again.
/// </summary>
/// <remarks>
/// In some situations, a server may reject a login due to reasons other
/// than invalid password (e.g. an EVE API screw-up, database issue,
/// etc.). Instead of permanently rejecting the credentials, it will
/// periodically try to log in.
/// </remarks>
[DefaultValue(typeof(TimeSpan), defaultRetryAuthenticationPeriod)]
[Category("Behavior")]
public TimeSpan AuthenticationRetryTimeout {
get {
Contract.Ensures(Contract.Result<TimeSpan>() > TimeSpan.Zero);
return this.authRetryTimeout;
}
set {
Contract.Requires<ArgumentOutOfRangeException>(
value > TimeSpan.Zero,
"value");
Contract.Requires<InvalidOperationException>(!IsRunning);
Contract.Ensures(this.AuthenticationRetryTimeout == value);
if (this.authRetryTimeout != value) {
this.authRetryTimeout = value;
this.OnPropertyChanged("AuthenticationRetryTimeout");
}
}
}
/// <summary>
/// Gets or sets the <see cref="Uri"/> to use when downloading
/// the channel list.
/// </summary>
[DefaultValue(typeof(Uri), IntelExtensions.ChannelsUrl)]
public Uri ChannelListUri {
get {
Contract.Ensures(Contract.Result<Uri>() != null);
return this.channels.ChannelListUri;
}
set {
Contract.Requires<ArgumentNullException>(value != null, "value");
Contract.Requires<ArgumentException>(value.IsAbsoluteUri);
Contract.Requires<InvalidOperationException>(!IsRunning);
this.channels.ChannelListUri = value;
}
}
/// <summary>
/// Gets or sets the <see cref="Uri"/> to use when accessing the
/// intel reporting service.
/// </summary>
[DefaultValue(typeof(Uri), IntelExtensions.ReportUrl)]
public Uri ServiceUri {
get {
Contract.Ensures(Contract.Result<Uri>() != null);
return this.serviceUri;
}
set {
Contract.Requires<ArgumentNullException>(value != null, "value");
Contract.Requires<ArgumentException>(value.IsAbsoluteUri);
Contract.Requires<InvalidOperationException>(!IsRunning);
if (this.serviceUri != value) {
this.serviceUri = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("ServiceUri"));
}
}
}
/// <summary>
/// Downloads the channel list and begins the acquisition of log
/// entries from the EVE chat logs. This method enables
/// <see cref="IntelReported"/> events.
/// </summary>
public void Start() {
Contract.Requires<ObjectDisposedException>(
this.Status != IntelStatus.Disposed,
null);
Contract.Requires<InvalidOperationException>(
this.Status != IntelStatus.FatalError);
Contract.Ensures(this.IsRunning);
lock (this.syncRoot) {
try {
if (this.Status == IntelStatus.Stopped) {
this.Status = IntelStatus.Starting;
this.channels.Start();
try {
this.GetSession(true);
} catch (WebException) {
this.Status = IntelStatus.NetworkError;
} catch (AuthenticationException) {
this.Status = IntelStatus.AuthenticationError;
}
}
} catch {
this.Status = IntelStatus.FatalError;
throw;
}
}
}
/// <summary>
/// Stops the <see cref="IntelReporter"/> from providing
/// location data and events. <see cref="IntelReported"/>
/// events will no longer be raised.
/// </summary>
public void Stop() {
Contract.Ensures(!this.IsRunning);
lock (this.syncRoot) {
try {
if (this.IsRunning) {
this.Status = IntelStatus.Stopping;
if (this.session != null) {
this.session.Dispose();
this.session = null;
}
this.channels.Stop();
this.Status = IntelStatus.Stopped;
}
} catch {
this.Status = IntelStatus.FatalError;
throw;
}
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) {
try {
if (disposing) {
// Clean up
lock (this.syncRoot) {
if (this.Status != IntelStatus.Disposed) {
this.Status = IntelStatus.Disposing;
if (this.session != null) {
this.session.Dispose();
this.session = null;
}
this.timerSession.Dispose();
this.channels.Dispose();
}
}
} else {
// Cannot safely clean up
}
} finally {
this.status = IntelStatus.Disposed;
base.Dispose(disposing);
}
}
/// <summary>
/// Gets the instance of <see cref="IntelSession"/> to use when
/// contacting the intel server.
/// </summary>
/// <param name="create">
/// <see langword="true"/> to create a new instance of
/// <see cref="IntelSession"/> if there is currently not an active
/// session; othwerise, return <see langword="null"/> if no session
/// is currently active.
/// </param>
protected virtual IntelSession GetSession(bool create) {
Contract.Requires(IsRunning);
Contract.Ensures((Contract.Result<IntelSession>() != null) || !create);
var session = this.session;
if ((session != null) && session.IsConnected) {
// Existing session is still valid
return session;
} else if (create && (String.IsNullOrEmpty(this.username)
|| String.IsNullOrEmpty(this.passwordHash))) {
// Can't login without a username/password
throw new AuthenticationException();
} else if (create && this.lastAuthFailure.HasValue
&& (this.lastAuthFailure + this.authRetryTimeout > DateTime.UtcNow)) {
// Wait longer before trying to log in again
throw new AuthenticationException();
} else if (create) {
// Safe to create a new instance of IntelSession
session = new IntelSession(this.username, this.passwordHash, this.serviceUri);
this.session = session;
this.timerSession.Change(this.keepAlivePeriod, this.keepAlivePeriod);
this.lastIntel = DateTime.UtcNow;
this.Status = IntelStatus.Active;
this.OnPropertyChanged("Users");
return session;
} else if (session != null) {
// Session has expired
this.session = null;
timerSession.Change(Timeout.Infinite, Timeout.Infinite);
return null;
} else {
// No session open
return null;
}
}
/// <summary>
/// Raises the <see cref="IntelReported"/> event.
/// </summary>
internal protected virtual void OnIntelReported(IntelEventArgs e) {
Contract.Requires<ArgumentNullException>(e != null, "e");
// Raise the event (as appropriate)
this.lastIntel = DateTime.UtcNow;
var handler = this.IntelReported;
if (handler != null) {
var sync = this.synchronizingObject;
WaitCallback callback = (state) => handler(this, e);
if ((sync != null) && sync.InvokeRequired) {
sync.BeginInvoke(callback, new object[] { null });
} else {
ThreadPool.QueueUserWorkItem(callback);
}
}
// Report the intel
try {
lock (this.syncRoot) {
// Race condition avoidance
if (!this.IsRunning) {
return;
}
// Report as necessary
var session = this.GetSession(true);
if (session.Report(e)) {
this.Status = IntelStatus.Active;
++this.IntelSent;
this.OnPropertyChanged("IntelSent");
} else {
++this.IntelDropped;
this.OnPropertyChanged("IntelDropped");
}
}
} catch (WebException) {
this.Status = IntelStatus.NetworkError;
++this.IntelDropped;
this.OnPropertyChanged("IntelDropped");
} catch (AuthenticationException) {
this.Status = IntelStatus.AuthenticationError;
++this.IntelDropped;
this.OnPropertyChanged("IntelDropped");
}
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
internal protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
Contract.Requires<ArgumentNullException>(e != null, "e");
Debug.Assert(String.IsNullOrEmpty(e.PropertyName)
|| (this.GetType().GetProperty(e.PropertyName) != null));
// Raise the event (as appropriate)
var handler = this.PropertyChanged;
if (handler != null) {
var sync = this.synchronizingObject;
WaitCallback callback = (state) => handler(this, e);
if ((sync != null) && sync.InvokeRequired) {
sync.BeginInvoke(callback, new object[] { null });
} else {
ThreadPool.QueueUserWorkItem(callback);
}
}
}
/// <summary>
/// Called every <see cref="KeepAliveInterval"/> while the server
/// connection is open.
/// </summary>
protected virtual void OnKeepAlive() {
var session = this.GetSession(false);
if (session != null) {
if (this.lastIntel + this.sessionTimeout < DateTime.UtcNow) {
// Session has expired
session.Dispose();
this.Status = IntelStatus.Waiting;
this.OnPropertyChanged("Users");
} else {
// Maintain the session
try {
var users = session.Users;
if (session.KeepAlive()) {
this.Status = IntelStatus.Active;
}
if (users != session.Users) {
this.OnPropertyChanged("Users");
}
} catch (WebException) {
this.Status = IntelStatus.NetworkError;
}
}
}
}
/// <summary>
/// Event handler for <see cref="IntelChannelContainer.IntelReported"/>.
/// </summary>
private void channels_IntelReported(object sender, IntelEventArgs e) {
Contract.Requires(e != null);
this.OnIntelReported(e);
}
/// <summary>
/// Event handler for <see cref="IntelChannelContainer.PropertyChanged"/>.
/// </summary>
private void channels_PropertyChanged(object sender, PropertyChangedEventArgs e) {
Contract.Requires(e != null);
if ((e.PropertyName == "ChannelUpdateInterval") || (e.PropertyName == "Channels")
|| (e.PropertyName == "Status") || (e.PropertyName == "Path")
|| (e.PropertyName == "ChannelListUri")) {
// Property we forward
this.OnPropertyChanged(e);
}
}
/// <summary>
/// Periodically sends keep-alives to the session.
/// </summary>
private void timer_Callback(object state) {
lock (this.syncRoot) {
if (this.IsRunning) {
this.OnKeepAlive();
}
if (!this.IsRunning || (this.GetSession(false) == null)) {
this.timerSession.Change(Timeout.Infinite, Timeout.Infinite);
}
}
}
/// <summary>
/// Helper function to automatically construct a <see cref="PropertyChangedEventArgs"/>
/// for <see cref="OnPropertyChanged(PropertyChangedEventArgs)"/>.
/// </summary>
private void OnPropertyChanged(string propertyName) {
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Updates the user's credentials and forces the service to
/// reauthenticate with the reporting service.
/// </summary>
/// <param name="username">
/// The TEST auth username.
/// </param>
/// <param name="password">
/// The plaintext TEST services password. It will be hashed
/// automatically.
/// </param>
/// <exception cref="AuthenticationException">
/// The credentials provided by the user are incorrect.
/// </exception>
public void Authenticate(string username, string password) {
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(username));
Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(password));
Contract.Requires<ObjectDisposedException>(this.Status != IntelStatus.Disposed);
Contract.Requires<InvalidOperationException>(this.Status != IntelStatus.FatalError);
// Verify the password is correct before saving anything
var passwordHash = IntelSession.HashPassword(password);
IntelSession testSession;
try {
testSession = new IntelSession(username, passwordHash, this.serviceUri);
} catch (WebException) {
lock (this.syncRoot) {
this.Status = IntelStatus.NetworkError;
throw;
}
}
// Update the program state
lock (this.syncRoot) {
this.username = username;
this.passwordHash = passwordHash;
if (this.session != null) {
this.session.Dispose();
}
this.session = testSession;
this.lastIntel = DateTime.UtcNow;
this.lastAuthFailure = null;
this.Status = IntelStatus.Active;
this.timerSession.Change(this.keepAlivePeriod, this.keepAlivePeriod);
this.OnPropertyChanged("Username");
this.OnPropertyChanged("PasswordHash");
this.OnPropertyChanged("Users");
}
}
[ContractInvariantMethod]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
private void ObjectInvariant() {
Contract.Invariant(this.sessionTimeout > TimeSpan.Zero);
Contract.Invariant(this.keepAlivePeriod > TimeSpan.Zero);
Contract.Invariant(this.channels != null);
Contract.Invariant(this.IntelSent >= 0);
Contract.Invariant(this.IntelDropped >= 0);
}
}
}