Skip to content

Commit c7e7107

Browse files
author
Jonathan McGee
committed
* Added centralized network error tracking methods to IntelReporter
1 parent 629c505 commit c7e7107

2 files changed

Lines changed: 86 additions & 56 deletions

File tree

PleaseIgnore.IntelMap/IntelChannelCollection.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ IEnumerator IEnumerable.GetEnumerator() {
124124
internal void Tick() {
125125
var now = DateTime.UtcNow;
126126
var period = parent.ChannelDownloadPeriod;
127-
if (!lastDownload.HasValue || now > lastDownload + period) {
127+
if ((!lastDownload.HasValue || now > lastDownload + period)
128+
&& parent.CanSend(false)) {
128129
// Get the updated channel list
129-
// TODO: Dial it back on network failures
130130
string[] channels = null;
131131
try {
132132
channels = IntelSession.GetIntelChannels();
133-
} catch (WebException) {
134-
} catch (IntelException) {
133+
} catch (WebException e) {
134+
parent.ReportFailure(e);
135+
} catch (IntelException e) {
136+
parent.ReportFailure(e);
135137
}
136138

137139
// Remove stale channels
@@ -154,9 +156,9 @@ internal void Tick() {
154156
channel.Rescan();
155157
list.Add(channel);
156158
}
157-
}
158-
}
159-
}
159+
} //lock (this.syncRoot) {
160+
} //if ((channels != null) && (channels.Length > 0)) {
161+
} //if ((!lastDownload.HasValue || now > lastDownload + period)
160162

161163
list.ForEach(x => x.Tick());
162164
}

PleaseIgnore.IntelMap/IntelReporter.cs

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -205,25 +205,42 @@ private void ThreadMain() {
205205
/// Gets or sets the TEST Alliance AUTH username.
206206
/// </summary>
207207
/// <remarks>
208-
/// Changing the username or password will force a reauthentication
209-
/// against the intel reporting server if the monitoring service is
210-
/// running. This means that changing both may force two separate
211-
/// authentications. Use <see cref="Authenticate"/> to change the
212-
/// username and password once the service has already started.
208+
/// Changes to the <see cref="Username"/>, <see cref="Password"/>,
209+
/// or <see cref="PasswordHash"/> will not be used until the next
210+
/// time the client tries to reauthenticate. To verify that the
211+
/// authentication information is correct, use the
212+
/// <see cref="Authenticate"/> or <see cref="BeginAuthenticate"/>
213+
/// methods.
213214
/// </remarks>
214215
[DefaultValue((String)null), Category("Behavior")]
215216
public string Username { get; set; }
216217

217218
/// <summary>
218219
/// Gets or sets the <em>hashed</em> services password for the user.
219220
/// </summary>
221+
/// <remarks>
222+
/// Changes to the <see cref="Username"/>, <see cref="Password"/>,
223+
/// or <see cref="PasswordHash"/> will not be used until the next
224+
/// time the client tries to reauthenticate. To verify that the
225+
/// authentication information is correct, use the
226+
/// <see cref="Authenticate"/> or <see cref="BeginAuthenticate"/>
227+
/// methods.
228+
/// </remarks>
220229
[DefaultValue((String)null), Category("Behavior")]
221230
public string PasswordHash { get; set; }
222231

223232
/// <summary>
224233
/// Sets the hashed password by automatically hashing and storing the
225234
/// plaintext password.
226235
/// </summary>
236+
/// <remarks>
237+
/// Changes to the <see cref="Username"/>, <see cref="Password"/>,
238+
/// or <see cref="PasswordHash"/> will not be used until the next
239+
/// time the client tries to reauthenticate. To verify that the
240+
/// authentication information is correct, use the
241+
/// <see cref="Authenticate"/> or <see cref="BeginAuthenticate"/>
242+
/// methods.
243+
/// </remarks>
227244
[Browsable(false)]
228245
public string Password {
229246
set {
@@ -233,27 +250,6 @@ public string Password {
233250
}
234251
}
235252

236-
/// <summary>
237-
/// Provides the asynchronous implementation of Authenticate()
238-
/// </summary>
239-
private class AuthenticateAsyncResult : IntelAsyncResult<IntelReporter, bool> {
240-
private readonly string username;
241-
private readonly string password;
242-
243-
public AuthenticateAsyncResult(IntelReporter owner, AsyncCallback callback,
244-
object state, string username, string password)
245-
: base(owner, callback, state) {
246-
Contract.Requires(username != null);
247-
Contract.Requires(password != null);
248-
this.username = username;
249-
this.password = IntelSession.HashPassword(password);
250-
}
251-
252-
public void Execute() {
253-
// TODO: Implement
254-
}
255-
}
256-
257253
/// <summary>
258254
/// Requests that we authenticate with the server under new
259255
/// credentials, reporting the results asynchronously.
@@ -336,6 +332,31 @@ public bool EndAuthenticate(IAsyncResult asyncResult) {
336332
public bool Authenticate(string username, string password) {
337333
return EndAuthenticate(BeginAuthenticate(username, password, null, null));
338334
}
335+
336+
/// <summary>
337+
/// Provides the asynchronous implementation of Authenticate()
338+
/// </summary>
339+
private class AuthenticateAsyncResult : IntelAsyncResult<IntelReporter, bool> {
340+
private readonly string username;
341+
private readonly string password;
342+
343+
public AuthenticateAsyncResult(IntelReporter owner, AsyncCallback callback,
344+
object state, string username, string password)
345+
: base(owner, callback, state) {
346+
Contract.Requires(username != null);
347+
Contract.Requires(password != null);
348+
this.username = username;
349+
this.password = IntelSession.HashPassword(password);
350+
}
351+
352+
public void Cancel() {
353+
// TODO: Implement
354+
}
355+
356+
public void Execute() {
357+
// TODO: Implement
358+
}
359+
}
339360
#endregion
340361

341362
#region File Monitoring
@@ -506,33 +527,30 @@ public IntelChannelCollection Channels {
506527
/// <see langword="true"/> if the intel was sucessfully sent to the
507528
/// reporting server; otherwise, <see langword="false"/>.
508529
/// </returns>
509-
internal bool OnIntelReported(IntelEventArgs e) {
510-
Contract.Requires(e != null);
530+
internal bool OnIntelReported(IntelEventArgs args) {
531+
Contract.Requires(args != null);
511532

512533
// Send into the ThreadPool so not to interfere with our processing
513-
ThreadPool.QueueUserWorkItem(this.IntelReportedWorkItem, e);
534+
ThreadPool.QueueUserWorkItem(this.IntelReportedWorkItem, args);
514535

515536
if (this.CanSend(false)) {
516537
// Report the intelligence
517538
try {
518539
bool success = GetSession()
519-
.Report(e.Channel.Name, e.Timestamp, e.Message);
540+
.Report(args.Channel.Name, args.Timestamp, args.Message);
520541
if (success) {
521542
++this.IntelSent;
522543
return true;
523544
} else {
524545
++this.IntelDropped;
525546
return false;
526547
}
527-
} catch (AuthenticationException) {
528-
// TODO: Record the type of failure
529-
this.lastFailure = DateTime.UtcNow;
530-
} catch (IntelException) {
531-
// TODO: Record the type of failure
532-
this.lastFailure = DateTime.UtcNow;
533-
} catch (WebException) {
534-
// TODO: Record the type of failure
535-
this.lastFailure = DateTime.UtcNow;
548+
} catch (AuthenticationException e) {
549+
this.ReportFailure(e);
550+
} catch (IntelException e) {
551+
this.ReportFailure(e);
552+
} catch (WebException e) {
553+
this.ReportFailure(e);
536554
}
537555
}
538556

@@ -612,7 +630,7 @@ public int Users {
612630
/// <see langword="true"/> if we should try to contact the server
613631
/// again; otherwise, <see langword="false"/>.
614632
/// </returns>
615-
private bool CanSend(bool throwError) {
633+
internal bool CanSend(bool throwError) {
616634
Contract.EnsuresOnThrow<IntelException>(throwError);
617635

618636
if (!lastFailure.HasValue) {
@@ -631,6 +649,19 @@ private bool CanSend(bool throwError) {
631649
}
632650
}
633651

652+
/// <summary>
653+
/// Reports a communications error with the server, setting the
654+
/// retry timer and notify the client (if required).
655+
/// </summary>
656+
/// <param name="e">
657+
/// The exception describing the failure.
658+
/// </param>
659+
internal void ReportFailure(Exception e) {
660+
Contract.Requires(e != null);
661+
// TODO: Record manner of failure
662+
this.lastFailure = DateTime.UtcNow;
663+
}
664+
634665
/// <summary>
635666
/// Returns the current session, initiating one if it does not already
636667
/// exist. Can throw any exception normally thrown by
@@ -656,9 +687,8 @@ private IntelSession GetSession() {
656687
try {
657688
this.session = new IntelSession(this.Username, this.PasswordHash);
658689
this.lastKeepAlive = DateTime.UtcNow;
659-
} catch {
660-
// TODO: Record the type of failure
661-
this.lastFailure = DateTime.UtcNow;
690+
} catch(Exception e) {
691+
this.ReportFailure(e);
662692
throw;
663693
}
664694
}
@@ -683,12 +713,10 @@ private void KeepAlive() {
683713
if (this.session.KeepAlive()) {
684714
this.lastKeepAlive = now;
685715
}
686-
} catch (IntelException) {
687-
// TODO: Record the type of failure
688-
this.lastFailure = now;
689-
} catch (WebException) {
690-
// TODO: Record the type of failure
691-
this.lastFailure = now;
716+
} catch (IntelException e) {
717+
this.ReportFailure(e);
718+
} catch (WebException e) {
719+
this.ReportFailure(e);
692720
}
693721
}
694722
}

0 commit comments

Comments
 (0)