Revize e51590be
Přidáno uživatelem Jakub Šilhavý před asi 2 roky(ů)
ld_client/LDClient/detection/ProcessDetection.cs | ||
---|---|---|
1 |
using System.Diagnostics; |
|
2 |
using LDClient.network; |
|
3 |
using LDClient.network.data; |
|
4 |
|
|
5 |
namespace LDClient.detection { |
|
6 |
|
|
7 |
public class ProcessProcessDetection : IProcessDetection { |
|
8 |
|
|
9 |
private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss"; |
|
10 |
|
|
11 |
private readonly string _processName; |
|
12 |
private readonly uint _detectionPeriodMs; |
|
13 |
private bool _processIsActive; |
|
14 |
private bool _failedToRetrieveData; |
|
15 |
private Payload? _lastConnectedPayload; |
|
16 |
|
|
17 |
private readonly InfoFetcher _infoFetcher; |
|
18 |
private readonly IApiClient _apiClient; |
|
19 |
|
|
20 |
public ProcessProcessDetection(string processName, uint detectionPeriodMs, InfoFetcher infoFetcher, IApiClient apiClient) { |
|
21 |
_processName = processName; |
|
22 |
_detectionPeriodMs = detectionPeriodMs; |
|
23 |
_infoFetcher = infoFetcher; |
|
24 |
_apiClient = apiClient; |
|
25 |
_failedToRetrieveData = false; |
|
26 |
} |
|
27 |
|
|
28 |
private async Task<bool> RetrieveDataFromDebugger() { |
|
29 |
var success = await _infoFetcher.FetchDataAsync(); |
|
30 |
if (success) { |
|
31 |
_lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat); |
|
32 |
} |
|
33 |
return success; |
|
34 |
} |
|
35 |
|
|
36 |
private async Task DebuggerDisconnected() { |
|
37 |
if (_lastConnectedPayload is not null) { |
|
38 |
_lastConnectedPayload.Status = ConnectionStatus.Disconnected; |
|
39 |
_lastConnectedPayload.TimeStamp = DateTime.Now.ToString(DatetimeFormat); |
|
40 |
await _apiClient.SendPayloadAsync(_lastConnectedPayload); |
|
41 |
_lastConnectedPayload = null; |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
private async Task DetectProcessAsync() { |
|
46 |
var processExists = Process.GetProcessesByName(_processName).Length > 0; |
|
47 |
|
|
48 |
if (processExists && !_processIsActive) { |
|
49 |
Program.DefaultLogger.Info($"Process started: {_processName}"); |
|
50 |
if (!_failedToRetrieveData) { |
|
51 |
_failedToRetrieveData = !await RetrieveDataFromDebugger(); |
|
52 |
} |
|
53 |
} else if (!processExists && _processIsActive) { |
|
54 |
Program.DefaultLogger.Info($"Process stopped: {_processName}"); |
|
55 |
_failedToRetrieveData = false; |
|
56 |
await DebuggerDisconnected(); |
|
57 |
} |
|
58 |
_processIsActive = processExists; |
|
59 |
} |
|
60 |
|
|
61 |
private async Task<Payload> SendDataToServerAsync(string headSerialNumber, string bodySerialNumber, string datetimeFormat) { |
|
62 |
Payload payload = new() { |
|
63 |
UserName = Environment.UserName, |
|
64 |
HostName = Environment.MachineName, |
|
65 |
TimeStamp = DateTime.Now.ToString(datetimeFormat), |
|
66 |
HeadDevice = new DebuggerInfo { |
|
67 |
SerialNumber = headSerialNumber |
|
68 |
}, |
|
69 |
BodyDevice = new DebuggerInfo { |
|
70 |
SerialNumber = bodySerialNumber |
|
71 |
}, |
|
72 |
Status = ConnectionStatus.Connected |
|
73 |
}; |
|
74 |
await _apiClient.SendPayloadAsync(payload); |
|
75 |
return payload; |
|
76 |
} |
|
77 |
|
|
78 |
public async void RunPeriodicDetection() { |
|
79 |
Program.DefaultLogger.Info("Process periodic detector has started"); |
|
80 |
while (true) { |
|
81 |
await DetectProcessAsync(); |
|
82 |
Thread.Sleep((int)_detectionPeriodMs); |
|
83 |
} |
|
84 |
// ReSharper disable once FunctionNeverReturns |
|
85 |
} |
|
86 |
} |
|
87 |
} |
ld_client/LDClient/detection/ProcessProcessDetection.cs | ||
---|---|---|
1 |
using System.Diagnostics; |
|
2 |
using LDClient.network; |
|
3 |
using LDClient.network.data; |
|
4 |
|
|
5 |
namespace LDClient.detection { |
|
6 |
|
|
7 |
public class ProcessProcessDetection : IProcessDetection { |
|
8 |
|
|
9 |
private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss"; |
|
10 |
|
|
11 |
private readonly string _processName; |
|
12 |
private readonly uint _detectionPeriodMs; |
|
13 |
private bool _processIsActive; |
|
14 |
private bool _failedToRetrieveData; |
|
15 |
private Payload? _lastConnectedPayload; |
|
16 |
|
|
17 |
private readonly InfoFetcher _infoFetcher; |
|
18 |
private readonly IApiClient _apiClient; |
|
19 |
|
|
20 |
public ProcessProcessDetection(string processName, uint detectionPeriodMs, InfoFetcher infoFetcher, IApiClient apiClient) { |
|
21 |
_processName = processName; |
|
22 |
_detectionPeriodMs = detectionPeriodMs; |
|
23 |
_infoFetcher = infoFetcher; |
|
24 |
_apiClient = apiClient; |
|
25 |
_failedToRetrieveData = false; |
|
26 |
} |
|
27 |
|
|
28 |
private async Task<bool> RetrieveDataFromDebugger() { |
|
29 |
var success = await _infoFetcher.FetchDataAsync(); |
|
30 |
if (success) { |
|
31 |
_lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat); |
|
32 |
} |
|
33 |
return success; |
|
34 |
} |
|
35 |
|
|
36 |
private async Task DebuggerDisconnected() { |
|
37 |
if (_lastConnectedPayload is not null) { |
|
38 |
_lastConnectedPayload.Status = ConnectionStatus.Disconnected; |
|
39 |
_lastConnectedPayload.TimeStamp = DateTime.Now.ToString(DatetimeFormat); |
|
40 |
await _apiClient.SendPayloadAsync(_lastConnectedPayload); |
|
41 |
_lastConnectedPayload = null; |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
private async Task DetectProcessAsync() { |
|
46 |
var processExists = Process.GetProcessesByName(_processName).Length > 0; |
|
47 |
|
|
48 |
if (processExists && !_processIsActive) { |
|
49 |
Program.DefaultLogger.Info($"Process started: {_processName}"); |
|
50 |
if (!_failedToRetrieveData) { |
|
51 |
_failedToRetrieveData = !await RetrieveDataFromDebugger(); |
|
52 |
} |
|
53 |
} else if (!processExists && _processIsActive) { |
|
54 |
Program.DefaultLogger.Info($"Process stopped: {_processName}"); |
|
55 |
_failedToRetrieveData = false; |
|
56 |
await DebuggerDisconnected(); |
|
57 |
} |
|
58 |
_processIsActive = processExists; |
|
59 |
} |
|
60 |
|
|
61 |
private async Task<Payload> SendDataToServerAsync(string headSerialNumber, string bodySerialNumber, string datetimeFormat) { |
|
62 |
Payload payload = new() { |
|
63 |
UserName = Environment.UserName, |
|
64 |
HostName = Environment.MachineName, |
|
65 |
TimeStamp = DateTime.Now.ToString(datetimeFormat), |
|
66 |
HeadDevice = new DebuggerInfo { |
|
67 |
SerialNumber = headSerialNumber |
|
68 |
}, |
|
69 |
BodyDevice = new DebuggerInfo { |
|
70 |
SerialNumber = bodySerialNumber |
|
71 |
}, |
|
72 |
Status = ConnectionStatus.Connected |
|
73 |
}; |
|
74 |
await _apiClient.SendPayloadAsync(payload); |
|
75 |
return payload; |
|
76 |
} |
|
77 |
|
|
78 |
public async void RunPeriodicDetection() { |
|
79 |
Program.DefaultLogger.Info("Process periodic detector has started"); |
|
80 |
while (true) { |
|
81 |
await DetectProcessAsync(); |
|
82 |
Thread.Sleep((int)_detectionPeriodMs); |
|
83 |
} |
|
84 |
// ReSharper disable once FunctionNeverReturns |
|
85 |
} |
|
86 |
} |
|
87 |
} |
Také k dispozici: Unified diff
re #9568 Fixed a typo in the filename.