QMonHostBase.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.IO.Abstractions;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using Quadarax.Foundation.Core.Logging;
  5. using Quadarax.Foundation.Core.Object;
  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 ILog? _log;
  14. private UdpClient? _udpClientGeneral;
  15. private UdpClient? _udpClientData;
  16. private Timer? _timerGeneral;
  17. private Timer? _timerData;
  18. #endregion
  19. #region *** Properties ***
  20. protected TConfiguration Configuration { get; }
  21. protected abstract Uri UriGeneral { get; }
  22. protected abstract Uri UriData { get; }
  23. protected abstract TimeSpan IntervalGeneral { get; }
  24. protected abstract TimeSpan? IntervalData { get; }
  25. protected UdpClient UdpClientGeneral => _udpClientGeneral ?? throw new InvalidOperationException("UDP client channel General not open.");
  26. protected UdpClient UdpClientData => _udpClientData ?? throw new InvalidOperationException("UDP client channel Data not open.");
  27. public bool IsEnabled
  28. {
  29. get => _isEnabled;
  30. set => SetEnabled(value);
  31. }
  32. protected Json.Binder Binder { get; } = new Json.Binder(new FileSystem(), false, true, 100);
  33. #endregion
  34. #region *** Constructors ***
  35. protected QMonHostBase(TConfiguration configuration, ILogger? externalLogger)
  36. {
  37. Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  38. _log = externalLogger?.GetLogger(GetType());
  39. SetEnabled(configuration.Enabled);
  40. }
  41. #endregion
  42. #region *** Private Operations ***
  43. protected void Open()
  44. {
  45. if (!_isEnabled) return;
  46. if (_udpClientGeneral != null) throw new InvalidOperationException("UDP client channel General already open. Close first.");
  47. if (_udpClientData != null) throw new InvalidOperationException("UDP client channel Data already open. Close first.");
  48. _udpClientGeneral = CreateUdpClient(UriGeneral);
  49. _udpClientData = CreateUdpClient(UriData);
  50. _timerGeneral = new Timer(OnTimerGeneral, this, IntervalGeneral, IntervalGeneral);
  51. Log(LogSeverityEnum.Debug, $"General timer started with interval '{IntervalGeneral}'");
  52. if (IntervalData.HasValue)
  53. {
  54. _timerData = new Timer(OnTimerData, this, IntervalData.Value, IntervalData.Value);
  55. Log(LogSeverityEnum.Debug, $"Data timer started with interval '{IntervalData}'");
  56. }
  57. OnOpen();
  58. }
  59. protected void Close()
  60. {
  61. if (!_isEnabled) return;
  62. if (_udpClientGeneral != null)
  63. {
  64. _timerGeneral?.Dispose();
  65. _udpClientGeneral.Close();
  66. _udpClientGeneral.Dispose();
  67. _udpClientGeneral = null;
  68. _timerGeneral = null;
  69. }
  70. if (_udpClientData != null)
  71. {
  72. _timerData?.Dispose();
  73. _udpClientData.Close();
  74. _udpClientData.Dispose();
  75. _udpClientData = null;
  76. _timerData = null;
  77. }
  78. OnClose();
  79. }
  80. protected abstract void OnOpen();
  81. protected abstract void OnClose();
  82. protected abstract void OnTimerData(object? state);
  83. protected abstract void OnTimerGeneral(object? state);
  84. #endregion
  85. protected void SetEnabled(bool value)
  86. {
  87. if (value == _isEnabled)
  88. return;
  89. _isEnabled = value;
  90. if (_isEnabled)
  91. Open();
  92. else
  93. Close();
  94. }
  95. protected override void OnDisposing()
  96. {
  97. Close();
  98. }
  99. protected void Log(LogSeverityEnum type, string message, Exception? e = null)
  100. {
  101. _log?.Log(type, message, e);
  102. }
  103. protected abstract UdpClient CreateUdpClient(Uri targetUri);
  104. }
  105. }