QMonHostBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public bool IsEnabled
  25. {
  26. get => _isEnabled;
  27. set => SetEnabled(value);
  28. }
  29. protected Json.Binder Binder { get; } = new Json.Binder(new FileSystem(), false, true, 100);
  30. #endregion
  31. #region *** Constructors ***
  32. protected QMonHostBase(TConfiguration configuration, ILogger? externalLogger)
  33. {
  34. _logger = externalLogger;
  35. Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  36. _log = externalLogger?.GetLogger(GetType());
  37. SetEnabled(configuration.Enabled);
  38. }
  39. #endregion
  40. #region *** Private Operations ***
  41. protected void Open()
  42. {
  43. if (!_isEnabled) return;
  44. if (WorkerGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
  45. if (WorkerData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
  46. var contextGeneral = new QMonWorkerContext(CreateUdpClient(UriGeneral));
  47. WorkerGeneral = new LoopWorker("GeneralWorker", contextGeneral,async (_)=>await OnTimerGeneral(contextGeneral), _logger);
  48. WorkerGeneral.IterationsDelayBetween = IntervalGeneral;
  49. WorkerGeneral.Start();
  50. Log(LogSeverityEnum.Debug, $"General worker started with interval '{IntervalGeneral}'");
  51. if (IntervalData.HasValue)
  52. {
  53. var contextData = new QMonWorkerContext(CreateUdpClient(UriData));
  54. WorkerData = new LoopWorker("GeneralWorker", contextData,async (_)=>await OnTimerData(contextData), _logger);
  55. WorkerData.IterationsDelayBetween = IntervalData.Value;
  56. WorkerData.Start();
  57. Log(LogSeverityEnum.Debug, $"Data worker started with interval '{IntervalData}'");
  58. }
  59. OnOpen();
  60. }
  61. protected void Close()
  62. {
  63. if (!_isEnabled) return;
  64. WorkerGeneral?.Stop();
  65. WorkerGeneral?.Dispose();
  66. WorkerGeneral = null;
  67. WorkerData?.Stop();
  68. WorkerData?.Dispose();
  69. WorkerData = null;
  70. OnClose();
  71. }
  72. protected abstract void OnOpen();
  73. protected abstract void OnClose();
  74. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  75. protected virtual async Task OnTimerData(QMonWorkerContext context)
  76. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  77. {
  78. throw new NotImplementedException();
  79. }
  80. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  81. protected virtual async Task OnTimerGeneral(QMonWorkerContext context)
  82. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  83. {
  84. throw new NotImplementedException();
  85. }
  86. #endregion
  87. protected void SetEnabled(bool value)
  88. {
  89. if (value == _isEnabled)
  90. return;
  91. _isEnabled = value;
  92. if (_isEnabled)
  93. Open();
  94. else
  95. Close();
  96. }
  97. protected override void OnDisposing()
  98. {
  99. Close();
  100. }
  101. protected void Log(LogSeverityEnum type, string message, Exception? e = null)
  102. {
  103. _log?.Log(type, message, e);
  104. }
  105. protected abstract UdpClient CreateUdpClient(Uri targetUri);
  106. }
  107. }