|
|
@@ -1,8 +1,8 @@
|
|
|
using System.IO.Abstractions;
|
|
|
-using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
using Quadarax.Foundation.Core.Logging;
|
|
|
using Quadarax.Foundation.Core.Object;
|
|
|
+using Quadarax.Foundation.Core.Thread;
|
|
|
|
|
|
namespace Quadarax.Foundation.Core.QMonitor
|
|
|
{
|
|
|
@@ -12,14 +12,8 @@ namespace Quadarax.Foundation.Core.QMonitor
|
|
|
|
|
|
#region *** Fields ***
|
|
|
private bool _isEnabled;
|
|
|
- private ILog? _log;
|
|
|
-
|
|
|
- private UdpClient? _udpClientGeneral;
|
|
|
- private UdpClient? _udpClientData;
|
|
|
-
|
|
|
- private Timer? _timerGeneral;
|
|
|
- private Timer? _timerData;
|
|
|
-
|
|
|
+ private readonly ILog? _log;
|
|
|
+ private readonly ILogger? _logger;
|
|
|
#endregion
|
|
|
|
|
|
#region *** Properties ***
|
|
|
@@ -29,8 +23,9 @@ namespace Quadarax.Foundation.Core.QMonitor
|
|
|
protected abstract TimeSpan IntervalGeneral { get; }
|
|
|
protected abstract TimeSpan? IntervalData { get; }
|
|
|
|
|
|
- protected UdpClient UdpClientGeneral => _udpClientGeneral ?? throw new InvalidOperationException("UDP client channel General not open.");
|
|
|
- protected UdpClient UdpClientData => _udpClientData ?? throw new InvalidOperationException("UDP client channel Data not open.");
|
|
|
+ protected LoopWorker? WorkerGeneral { get; private set; }
|
|
|
+ protected LoopWorker? WorkerData { get; private set; }
|
|
|
+
|
|
|
public bool IsEnabled
|
|
|
{
|
|
|
get => _isEnabled;
|
|
|
@@ -44,6 +39,7 @@ namespace Quadarax.Foundation.Core.QMonitor
|
|
|
|
|
|
protected QMonHostBase(TConfiguration configuration, ILogger? externalLogger)
|
|
|
{
|
|
|
+ _logger = externalLogger;
|
|
|
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
|
_log = externalLogger?.GetLogger(GetType());
|
|
|
SetEnabled(configuration.Enabled);
|
|
|
@@ -55,18 +51,21 @@ namespace Quadarax.Foundation.Core.QMonitor
|
|
|
{
|
|
|
if (!_isEnabled) return;
|
|
|
|
|
|
- if (_udpClientGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
|
|
|
- if (_udpClientData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
|
|
|
+ if (WorkerGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
|
|
|
+ if (WorkerData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
|
|
|
|
|
|
- _udpClientGeneral = CreateUdpClient(UriGeneral);
|
|
|
- _udpClientData = CreateUdpClient(UriData);
|
|
|
-
|
|
|
- _timerGeneral = new Timer(OnTimerGeneral, this, IntervalGeneral, IntervalGeneral);
|
|
|
- Log(LogSeverityEnum.Debug, $"General timer started with interval '{IntervalGeneral}'");
|
|
|
+ var contextGeneral = new QMonWorkerContext(CreateUdpClient(UriGeneral));
|
|
|
+ WorkerGeneral = new LoopWorker("GeneralWorker", contextGeneral,async (_)=>await OnTimerGeneral(contextGeneral), _logger);
|
|
|
+ WorkerGeneral.IterationsDelayBetween = IntervalGeneral;
|
|
|
+ WorkerGeneral.Start();
|
|
|
+ Log(LogSeverityEnum.Debug, $"General worker started with interval '{IntervalGeneral}'");
|
|
|
if (IntervalData.HasValue)
|
|
|
{
|
|
|
- _timerData = new Timer(OnTimerData, this, IntervalData.Value, IntervalData.Value);
|
|
|
- Log(LogSeverityEnum.Debug, $"Data timer started with interval '{IntervalData}'");
|
|
|
+ var contextData = new QMonWorkerContext(CreateUdpClient(UriData));
|
|
|
+ WorkerData = new LoopWorker("GeneralWorker", contextData,async (_)=>await OnTimerData(contextData), _logger);
|
|
|
+ WorkerData.IterationsDelayBetween = IntervalData.Value;
|
|
|
+ WorkerData.Start();
|
|
|
+ Log(LogSeverityEnum.Debug, $"Data worker started with interval '{IntervalData}'");
|
|
|
}
|
|
|
|
|
|
OnOpen();
|
|
|
@@ -75,30 +74,33 @@ namespace Quadarax.Foundation.Core.QMonitor
|
|
|
{
|
|
|
if (!_isEnabled) return;
|
|
|
|
|
|
- if (_udpClientGeneral != null)
|
|
|
- {
|
|
|
- _timerGeneral?.Dispose();
|
|
|
- _udpClientGeneral.Close();
|
|
|
- _udpClientGeneral.Dispose();
|
|
|
- _udpClientGeneral = null;
|
|
|
- _timerGeneral = null;
|
|
|
- }
|
|
|
+ WorkerGeneral?.Stop();
|
|
|
+ WorkerGeneral?.Dispose();
|
|
|
+ WorkerGeneral = null;
|
|
|
+
|
|
|
+ WorkerData?.Stop();
|
|
|
+ WorkerData?.Dispose();
|
|
|
+ WorkerData = null;
|
|
|
|
|
|
- if (_udpClientData != null)
|
|
|
- {
|
|
|
- _timerData?.Dispose();
|
|
|
- _udpClientData.Close();
|
|
|
- _udpClientData.Dispose();
|
|
|
- _udpClientData = null;
|
|
|
- _timerData = null;
|
|
|
- }
|
|
|
OnClose();
|
|
|
}
|
|
|
|
|
|
protected abstract void OnOpen();
|
|
|
protected abstract void OnClose();
|
|
|
- protected abstract void OnTimerData(object? state);
|
|
|
- protected abstract void OnTimerGeneral(object? state);
|
|
|
+
|
|
|
+#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
+ protected virtual async Task OnTimerData(QMonWorkerContext context)
|
|
|
+#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
+ protected virtual async Task OnTimerGeneral(QMonWorkerContext context)
|
|
|
+#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
#endregion
|
|
|
|
|
|
protected void SetEnabled(bool value)
|