QMonHostBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.IO.Abstractions;
  2. using System.Net.Sockets;
  3. using Quadarax.Foundation.Core.Logging;
  4. using Quadarax.Foundation.Core.Object;
  5. using Quadarax.Foundation.Core.Thread;
  6. namespace Quadarax.Foundation.Core.QMonitor
  7. {
  8. public abstract class QMonHostBase<TConfiguration> : DisposableObject
  9. where TConfiguration : QMonHostConfiguration
  10. {
  11. #region *** Fields ***
  12. private bool _isEnabled;
  13. private readonly ILog? _log;
  14. private readonly ILogger? _logger;
  15. #endregion
  16. #region *** Properties ***
  17. protected TConfiguration Configuration { get; }
  18. protected abstract Uri UriGeneral { get; }
  19. protected abstract Uri UriData { get; }
  20. protected abstract TimeSpan IntervalGeneral { get; }
  21. protected abstract TimeSpan? IntervalData { get; }
  22. protected LoopWorker? WorkerGeneral { get; private set; }
  23. protected LoopWorker? WorkerData { get; private set; }
  24. protected ILogger? Logger => _logger;
  25. public bool IsEnabled
  26. {
  27. get => _isEnabled;
  28. set => SetEnabled(value);
  29. }
  30. protected Json.Binder Binder { get; } = new Json.Binder(new FileSystem(), false, true, 100);
  31. #endregion
  32. #region *** Constructors ***
  33. protected QMonHostBase(TConfiguration configuration, ILogger? externalLogger)
  34. {
  35. _logger = externalLogger;
  36. Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  37. _log = externalLogger?.GetLogger(GetType());
  38. SetEnabled(configuration.Enabled);
  39. }
  40. #endregion
  41. #region *** Private Operations ***
  42. protected void Open()
  43. {
  44. if (!_isEnabled) return;
  45. if (WorkerGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
  46. if (WorkerData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
  47. var contextGeneral = new QMonWorkerContext(CreateUdpClient(UriGeneral));
  48. WorkerGeneral = new LoopWorker("GeneralWorker", contextGeneral,async (_)=>await OnTimerGeneral(contextGeneral), _logger);
  49. WorkerGeneral.IterationsDelayBetween = IntervalGeneral;
  50. WorkerGeneral.Start();
  51. Log(LogSeverityEnum.Debug, $"General worker started with interval '{IntervalGeneral}'");
  52. if (IntervalData.HasValue)
  53. {
  54. var contextData = new QMonWorkerContext(CreateUdpClient(UriData));
  55. WorkerData = new LoopWorker("GeneralWorker", contextData,async (_)=>await OnTimerData(contextData), _logger);
  56. WorkerData.IterationsDelayBetween = IntervalData.Value;
  57. WorkerData.Start();
  58. Log(LogSeverityEnum.Debug, $"Data worker started with interval '{IntervalData}'");
  59. }
  60. OnOpen();
  61. }
  62. protected void Close()
  63. {
  64. if (!_isEnabled) return;
  65. WorkerGeneral?.Stop();
  66. WorkerGeneral?.Dispose();
  67. WorkerGeneral = null;
  68. WorkerData?.Stop();
  69. WorkerData?.Dispose();
  70. WorkerData = null;
  71. OnClose();
  72. }
  73. protected abstract void OnOpen();
  74. protected abstract void OnClose();
  75. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  76. protected virtual async Task OnTimerData(QMonWorkerContext context)
  77. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  78. {
  79. throw new NotImplementedException();
  80. }
  81. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  82. protected virtual async Task OnTimerGeneral(QMonWorkerContext context)
  83. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  84. {
  85. throw new NotImplementedException();
  86. }
  87. #endregion
  88. protected void SetEnabled(bool value)
  89. {
  90. if (value == _isEnabled)
  91. return;
  92. _isEnabled = value;
  93. if (_isEnabled)
  94. Open();
  95. else
  96. Close();
  97. }
  98. protected override void OnDisposing()
  99. {
  100. Close();
  101. }
  102. protected void Log(LogSeverityEnum type, string message, Exception? e = null)
  103. {
  104. _log?.Log(type, message, e);
  105. }
  106. protected abstract UdpClient CreateUdpClient(Uri targetUri);
  107. }
  108. }