History

GSF.ServiceProcess: In ServiceMonitors, made use of the operation queue for asynchronous processing and exception handling.

StephenCWills committed 12 years ago

Commit fc3190c1 parent ed0ea982 files · +63 −18Browse files

Unified · Split

2 files
  1. Source/Libraries/GSF.ServiceProcess/IServiceMonitor.cs +3 −2
  2. Source/Libraries/GSF.ServiceProcess/ServiceMonitors.cs +60 −16
Source/Libraries/GSF.ServiceProcess/IServiceMonitor.cs+3−2
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 //
2222 //******************************************************************************************************
2323
24+using System;
2425 using GSF.Adapters;
2526
2627 namespace GSF.ServiceProcess
@@ -41,8 +42,8 @@ namespace GSF.ServiceProcess · 14 unmodified lines
4142 /// Handles messages received by the service
4243 /// whenever the service encounters an error.
4344 /// </summary>
44− /// <param name="message">The error message received from the service.</param>
45− void HandleServiceErrorMessage(string message);
45+ /// <param name="ex">The exception received from the service.</param>
46+ void HandleServiceError(Exception ex);
4647
4748 /// <summary>
4849 /// Handles messages sent by a client.
Source/Libraries/GSF.ServiceProcess/ServiceMonitors.cs+60−16
@@ -21,10 +21,43 @@ · 20 unmodified lines
2121 //
2222 //******************************************************************************************************
2323
24+using System;
2425 using GSF.Adapters;
2526
2627 namespace GSF.ServiceProcess
2728 {
29+ /// <summary>
30+ /// A special exception thrown by error handlers.
31+ /// </summary>
32+ public class ErrorHandlerException : Exception
33+ {
34+ /// <summary>
35+ /// Creates a new instance of the <see cref="ErrorHandlerException"/> class.
36+ /// </summary>
37+ public ErrorHandlerException()
38+ {
39+ }
40+
41+ /// <summary>
42+ /// Creates a new instance of the <see cref="ErrorHandlerException"/> class.
43+ /// </summary>
44+ /// <param name="message">The message that describes the error.</param>
45+ public ErrorHandlerException(string message)
46+ : base(message)
47+ {
48+ }
49+
50+ /// <summary>
51+ /// Creates a new instance of the <see cref="ErrorHandlerException"/> class.
52+ /// </summary>
53+ /// <param name="message">The message that describes the error.</param>
54+ /// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
55+ public ErrorHandlerException(string message, Exception innerException)
56+ : base(message, innerException)
57+ {
58+ }
59+ }
60+
2861 /// <summary>
2962 /// Adapter loader that loads implementations of <see cref="IServiceMonitor"/>
3063 /// and delegates messages to the enabled monitors.
@@ -38,25 +71,27 @@ namespace GSF.ServiceProcess · 7 unmodified lines
3871 /// </summary>
3972 public void HandleServiceHeartbeat()
4073 {
41− foreach (IServiceMonitor serviceMonitor in Adapters)
42− {
43− if (serviceMonitor.Enabled)
44− serviceMonitor.HandleServiceHeartbeat();
45− }
74+ OperationQueue.Enqueue(new Action<IServiceMonitor>(serviceMonitor => serviceMonitor.HandleServiceHeartbeat()));
4675 }
4776
4877 /// <summary>
4978 /// Handles messages received by the service
5079 /// whenever the service encounters an error.
5180 /// </summary>
52− /// <param name="message">The error message received from the service.</param>
53− public void HandleServiceErrorMessage(string message)
81+ /// <param name="ex">The exception received from the service.</param>
82+ public void HandleServiceError(Exception ex)
5483 {
55− foreach (IServiceMonitor serviceMonitor in Adapters)
84+ OperationQueue.Enqueue(new Action<IServiceMonitor>(serviceMonitor =>
5685 {
57− if (serviceMonitor.Enabled)
58− serviceMonitor.HandleServiceErrorMessage(message);
59− }
86+ try
87+ {
88+ serviceMonitor.HandleServiceError(ex);
89+ }
90+ catch (Exception errorHandlerException)
91+ {
92+ throw new ErrorHandlerException(errorHandlerException.Message, errorHandlerException);
93+ }
94+ }));
6095 }
6196
6297 /// <summary>
@@ -65,11 +100,20 @@ namespace GSF.ServiceProcess · 2 unmodified lines
65100 /// <param name="args">Arguments provided by the client.</param>
66101 public void HandleClientMessage(string[] args)
67102 {
68− foreach (IServiceMonitor serviceMonitor in Adapters)
69− {
70− if (serviceMonitor.Enabled)
71− serviceMonitor.HandleClientMessage(args);
72− }
103+ OperationQueue.Enqueue(new Action<IServiceMonitor>(serviceMonitor => serviceMonitor.HandleClientMessage(args)));
104+ }
105+
106+ /// <summary>
107+ /// Executes an operation on the <paramref name="adapter"/> with the given <paramref name="data"/>.
108+ /// </summary>
109+ /// <param name="adapter">Adapter on which an operation is to be executed.</param>
110+ /// <param name="data">The operation to be executed.</param>
111+ protected override void ExecuteAdapterOperation(IServiceMonitor adapter, object data)
112+ {
113+ Action<IServiceMonitor> operation = data as Action<IServiceMonitor>;
114+
115+ if ((object)operation != null)
116+ operation(adapter);
73117 }
74118 }
75119 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t