|
|
@@ -6,47 +6,31 @@ using System.Reflection;
|
|
|
using System.Text;
|
|
|
using qmonlib.Attributes;
|
|
|
using Quadarax.Foundation.Core.Logging;
|
|
|
-using Quadarax.Foundation.Core.Object;
|
|
|
using Quadarax.Foundation.Core.Reflection.Extensions;
|
|
|
|
|
|
namespace qmonlib
|
|
|
{
|
|
|
- public class QMonClient : DisposableObject
|
|
|
+ public class QMonClient : QMonHostBase<QMonClientConfiuration>
|
|
|
{
|
|
|
#region *** Properties ***
|
|
|
- public bool IsEnabled
|
|
|
- {
|
|
|
- get => _isEnabled;
|
|
|
- set
|
|
|
- {
|
|
|
- SetEnabled(value);
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ protected override Uri UriGeneral => Configuration.TargetUriGeneral;
|
|
|
+ protected override Uri UriData => Configuration.TargetUriData;
|
|
|
+ protected override TimeSpan IntervalGeneral => Configuration.EmitGeneralInterval;
|
|
|
+ protected override TimeSpan? IntervalData => Configuration.EmitDataInterval;
|
|
|
+
|
|
|
public string InstanceIdentifier { get; }
|
|
|
#endregion
|
|
|
|
|
|
#region *** Fields ***
|
|
|
- private bool _isEnabled;
|
|
|
- private QMonConfiuration _configuration;
|
|
|
- private UdpClient? _udpClientGeneral;
|
|
|
- private UdpClient? _udpClientData;
|
|
|
-
|
|
|
- private Timer? _timerGeneral;
|
|
|
- private Timer? _timerData;
|
|
|
-
|
|
|
private IDictionary<string, MonItemGeneral> _generalCache = new Dictionary<string, MonItemGeneral>();
|
|
|
- private ILog? _log;
|
|
|
- private Quadarax.Foundation.Core.Json.Binder _binder = new Quadarax.Foundation.Core.Json.Binder(new FileSystem(),false,true,100);
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region *** Constructors ***
|
|
|
- public QMonClient(string instanceIdentifier, QMonConfiuration configuration, ILogger? externalLogger)
|
|
|
+ public QMonClient(string instanceIdentifier, QMonClientConfiuration configuration, ILogger? externalLogger) : base(configuration, externalLogger)
|
|
|
{
|
|
|
- _log = externalLogger?.GetLogger(GetType());
|
|
|
InstanceIdentifier = instanceIdentifier ?? throw new ArgumentNullException(nameof(instanceIdentifier));
|
|
|
- _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
|
- SetEnabled(_configuration.Enabled);
|
|
|
ScanAssemblies();
|
|
|
}
|
|
|
|
|
|
@@ -105,32 +89,19 @@ namespace qmonlib
|
|
|
#region *** Protected Operations ***
|
|
|
protected override void OnDisposing()
|
|
|
{
|
|
|
- Close();
|
|
|
+ base.OnDisposing();
|
|
|
_generalCache.Clear();
|
|
|
}
|
|
|
|
|
|
- private void Open()
|
|
|
- {
|
|
|
- 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.");
|
|
|
-
|
|
|
- _udpClientGeneral = CreateUdpClient(_configuration.TargetUriGeneral);
|
|
|
- _udpClientData = CreateUdpClient(_configuration.TargetUriData);
|
|
|
-
|
|
|
- _timerGeneral = new Timer(OnTimerGeneral, this, _configuration.EmitGeneralInterval, _configuration.EmitGeneralInterval);
|
|
|
- _timerData = new Timer(OnTimerData, this, _configuration.EmitDataInterval, _configuration.EmitDataInterval);
|
|
|
- }
|
|
|
-
|
|
|
- private void OnTimerData(object? state)
|
|
|
+ protected override void OnTimerData(object? state)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
- private void OnTimerGeneral(object? state)
|
|
|
+ protected override void OnTimerGeneral(object? state)
|
|
|
{
|
|
|
if (state == null) throw new ArgumentNullException(nameof(state));
|
|
|
var owner = (QMonClient)state;
|
|
|
- if (owner._udpClientGeneral == null) throw new InvalidOperationException("UDP client channel General not open.");
|
|
|
try
|
|
|
{
|
|
|
|
|
|
@@ -141,59 +112,36 @@ namespace qmonlib
|
|
|
};
|
|
|
data.Items.AddRange(owner._generalCache.Values);
|
|
|
var dataOut = Serialize(data);
|
|
|
- _udpClientGeneral?.Send(dataOut);
|
|
|
- _log?.Log(LogSeverityEnum.Trace, $"[{InstanceIdentifier}] Sent General packet size = {dataOut.Length} bytes");
|
|
|
+ UdpClientGeneral.Send(dataOut);
|
|
|
+ Log(LogSeverityEnum.Trace, $"[{InstanceIdentifier}] Sent General packet size = {dataOut.Length} bytes");
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
- _log?.Log(LogSeverityEnum.Error, $"[{InstanceIdentifier}] Error sending general data", ex);
|
|
|
+ Log(LogSeverityEnum.Error, $"[{InstanceIdentifier}] Error sending general data", ex);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private byte[] Serialize(MonGeneral data)
|
|
|
{
|
|
|
if (data == null) throw new ArgumentNullException(nameof(data));
|
|
|
- var json = _binder.SaveToString(data);
|
|
|
+ var json = Binder.SaveToString(data);
|
|
|
return Encoding.UTF8.GetBytes(json);
|
|
|
}
|
|
|
-
|
|
|
- private void Close()
|
|
|
- {
|
|
|
- if (_udpClientGeneral != null)
|
|
|
- {
|
|
|
- _timerGeneral?.Dispose();
|
|
|
- _udpClientGeneral.Close();
|
|
|
- _udpClientGeneral.Dispose();
|
|
|
- _udpClientGeneral = null;
|
|
|
- _timerGeneral = null;
|
|
|
- }
|
|
|
-
|
|
|
- if (_udpClientData != null)
|
|
|
- {
|
|
|
- _timerData?.Dispose();
|
|
|
- _udpClientData.Close();
|
|
|
- _udpClientData.Dispose();
|
|
|
- _udpClientData = null;
|
|
|
- _timerData = null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private UdpClient CreateUdpClient(Uri targetUri)
|
|
|
+ protected override UdpClient CreateUdpClient(Uri targetUri)
|
|
|
{
|
|
|
var ep = new IPEndPoint(IPAddress.Parse(targetUri.DnsSafeHost), targetUri.Port);
|
|
|
var udpClient = new UdpClient();
|
|
|
udpClient.Connect(ep);
|
|
|
return udpClient;
|
|
|
}
|
|
|
- private void SetEnabled(bool value)
|
|
|
- {
|
|
|
- if (value == _isEnabled)
|
|
|
- return;
|
|
|
- _isEnabled = value;
|
|
|
- if (_isEnabled)
|
|
|
- Open();
|
|
|
- else
|
|
|
- Close();
|
|
|
+
|
|
|
+ protected override void OnOpen()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnClose()
|
|
|
+ {
|
|
|
+
|
|
|
}
|
|
|
#endregion
|
|
|
|