QMonHostBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 qmonlib
  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 Quadarax.Foundation.Core.Json.Binder Binder { get; } = new Quadarax.Foundation.Core.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. if (IntervalData.HasValue)
  52. _timerData = new Timer(OnTimerData, this, IntervalData.Value, IntervalData.Value);
  53. OnOpen();
  54. }
  55. protected void Close()
  56. {
  57. if (!_isEnabled) return;
  58. if (_udpClientGeneral != null)
  59. {
  60. _timerGeneral?.Dispose();
  61. _udpClientGeneral.Close();
  62. _udpClientGeneral.Dispose();
  63. _udpClientGeneral = null;
  64. _timerGeneral = null;
  65. }
  66. if (_udpClientData != null)
  67. {
  68. _timerData?.Dispose();
  69. _udpClientData.Close();
  70. _udpClientData.Dispose();
  71. _udpClientData = null;
  72. _timerData = null;
  73. }
  74. OnClose();
  75. }
  76. protected abstract void OnOpen();
  77. protected abstract void OnClose();
  78. protected abstract void OnTimerData(object? state);
  79. protected abstract void OnTimerGeneral(object? state);
  80. #endregion
  81. protected void SetEnabled(bool value)
  82. {
  83. if (value == _isEnabled)
  84. return;
  85. _isEnabled = value;
  86. if (_isEnabled)
  87. Open();
  88. else
  89. Close();
  90. }
  91. protected override void OnDisposing()
  92. {
  93. Close();
  94. }
  95. protected void Log(LogSeverityEnum type, string message, Exception? e = null)
  96. {
  97. _log?.Log(type, message, e);
  98. }
  99. protected abstract UdpClient CreateUdpClient(Uri targetUri);
  100. }
  101. }