| @@ -21,10 +21,43 @@ · 20 unmodified lines |
| 21 | 21 | // |
| 22 | 22 | //****************************************************************************************************** |
| 23 | 23 | |
| 24 | +using System; |
| 24 | 25 | using GSF.Adapters; |
| 25 | 26 | |
| 26 | 27 | namespace GSF.ServiceProcess |
| 27 | 28 | { |
| 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 | + |
| 28 | 61 | /// <summary> |
| 29 | 62 | /// Adapter loader that loads implementations of <see cref="IServiceMonitor"/> |
| 30 | 63 | /// and delegates messages to the enabled monitors. |
| @@ -38,25 +71,27 @@ namespace GSF.ServiceProcess · 7 unmodified lines |
| 38 | 71 | /// </summary> |
| 39 | 72 | public void HandleServiceHeartbeat() |
| 40 | 73 | { |
| 41 | | − foreach (IServiceMonitor serviceMonitor in Adapters) |
| 42 | | − { |
| 43 | | − if (serviceMonitor.Enabled) |
| 44 | | − serviceMonitor.HandleServiceHeartbeat(); |
| 45 | | − } |
| 74 | + OperationQueue.Enqueue(new Action<IServiceMonitor>(serviceMonitor => serviceMonitor.HandleServiceHeartbeat())); |
| 46 | 75 | } |
| 47 | 76 | |
| 48 | 77 | /// <summary> |
| 49 | 78 | /// Handles messages received by the service |
| 50 | 79 | /// whenever the service encounters an error. |
| 51 | 80 | /// </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) |
| 54 | 83 | { |
| 55 | | − foreach (IServiceMonitor serviceMonitor in Adapters) |
| 84 | + OperationQueue.Enqueue(new Action<IServiceMonitor>(serviceMonitor => |
| 56 | 85 | { |
| 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 | + })); |
| 60 | 95 | } |
| 61 | 96 | |
| 62 | 97 | /// <summary> |
| @@ -65,11 +100,20 @@ namespace GSF.ServiceProcess · 2 unmodified lines |
| 65 | 100 | /// <param name="args">Arguments provided by the client.</param> |
| 66 | 101 | public void HandleClientMessage(string[] args) |
| 67 | 102 | { |
| 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); |
| 73 | 117 | } |
| 74 | 118 | } |
| 75 | 119 | } |