1
|
using LDClient.network;
|
2
|
using LDClient.network.data;
|
3
|
|
4
|
namespace LDClient.detection;
|
5
|
|
6
|
/// <summary>
|
7
|
/// This class takes care of process detection. When t32mtc (process)
|
8
|
/// is detected, it means that the debugger is currently being used.
|
9
|
/// The class keeps track of the current state of a debugger.
|
10
|
/// </summary>
|
11
|
public sealed class ProcessDetection : IProcessDetection {
|
12
|
|
13
|
/// <summary>
|
14
|
/// Datetime format used when sending payloads to the server.
|
15
|
/// </summary>
|
16
|
private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
|
17
|
|
18
|
/// <summary>
|
19
|
/// Name of the process the application detects.
|
20
|
/// </summary>
|
21
|
private readonly string _processName;
|
22
|
|
23
|
/// <summary>
|
24
|
/// How often the application check the current status of the process (cunning / not running).
|
25
|
/// </summary>
|
26
|
private readonly uint _detectionPeriodMs;
|
27
|
|
28
|
/// <summary>
|
29
|
/// Instance of InfoFetcher used to fetch information from the debugger
|
30
|
/// (when the process is detected).
|
31
|
/// </summary>
|
32
|
private readonly IInfoFetcher _infoFetcher;
|
33
|
|
34
|
/// <summary>
|
35
|
/// Instance of API clients used for sending data off to the server.
|
36
|
/// </summary>
|
37
|
private readonly IApiClient _apiClient;
|
38
|
|
39
|
/// <summary>
|
40
|
/// Instance of ProcessUtils which encapsulates common functionality
|
41
|
/// when it comes to dealing with processes (limited by the needs of this application).
|
42
|
/// </summary>
|
43
|
private readonly IProcessUtils _processUtils;
|
44
|
|
45
|
/// <summary>
|
46
|
/// Flag indicating whether the process is currently running or not.
|
47
|
/// </summary>
|
48
|
private bool _processIsActive;
|
49
|
|
50
|
/// <summary>
|
51
|
/// Flag if the application failed to retrieve data when the process was detected.
|
52
|
/// </summary>
|
53
|
private bool _failedToRetrieveData;
|
54
|
|
55
|
/// <summary>
|
56
|
/// Last payload that was sent to the server.
|
57
|
/// </summary>
|
58
|
private Payload? _lastConnectedPayload;
|
59
|
|
60
|
/// <summary>
|
61
|
/// Flag used to stop the thread (process detection).
|
62
|
/// </summary>
|
63
|
public bool DetectionRunning = false;
|
64
|
|
65
|
/// <summary>
|
66
|
/// Creates an instance of this class.
|
67
|
/// </summary>
|
68
|
/// <param name="processName">Name of the process the application detects</param>
|
69
|
/// <param name="detectionPeriodMs">How often the application check the current status of the process (cunning / not running)</param>
|
70
|
/// <param name="infoFetcher">Instance of InfoFetcher used to fetch information from the debugger</param>
|
71
|
/// <param name="apiClient">Instance of API clients used for sending data off to the server</param>
|
72
|
/// <param name="processUtils">Instance of ProcessUtils which encapsulates common functionality when it comes to dealing with processes (limited by the needs of this application)</param>
|
73
|
public ProcessDetection(string processName, uint detectionPeriodMs, IInfoFetcher infoFetcher,
|
74
|
IApiClient apiClient, IProcessUtils processUtils) {
|
75
|
_processName = processName;
|
76
|
_detectionPeriodMs = detectionPeriodMs;
|
77
|
_infoFetcher = infoFetcher;
|
78
|
_apiClient = apiClient;
|
79
|
_failedToRetrieveData = false;
|
80
|
_processUtils = processUtils;
|
81
|
}
|
82
|
|
83
|
/// <summary>
|
84
|
/// Retrieves data from the debugger.
|
85
|
/// </summary>
|
86
|
/// <returns>True, if the data was fetched successfully. False, otherwise.</returns>
|
87
|
private async Task<bool> RetrieveDataFromDebugger() {
|
88
|
// Try to fetch data from the debugger.
|
89
|
var success = await _infoFetcher.FetchDataAsync();
|
90
|
|
91
|
// If the data was fetched successfully, send a payload off to the server.
|
92
|
if (success) {
|
93
|
_lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber,
|
94
|
_infoFetcher.BodySerialNumber, DatetimeFormat);
|
95
|
}
|
96
|
return success;
|
97
|
}
|
98
|
|
99
|
/// <summary>
|
100
|
/// Sends a payload to the server when a debugger gets disconnected.
|
101
|
/// </summary>
|
102
|
private async Task DebuggerDisconnected() {
|
103
|
// Make sure the debugger was connected in the first place.
|
104
|
if (_lastConnectedPayload is not null) {
|
105
|
// Update the status and timestamp of the last payload
|
106
|
// (the serial numbers remain the same).
|
107
|
_lastConnectedPayload.Status = ConnectionStatus.Disconnected;
|
108
|
_lastConnectedPayload.TimeStamp = DateTime.Now.ToString(DatetimeFormat);
|
109
|
|
110
|
// Send the data to the server.
|
111
|
await _apiClient.SendPayloadAsync(_lastConnectedPayload);
|
112
|
|
113
|
// Clear the last payload.
|
114
|
_lastConnectedPayload = null;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
/// <summary>
|
119
|
/// Checks if the t32mtc process is running or not.
|
120
|
/// </summary>
|
121
|
private async Task DetectProcessAsync() {
|
122
|
// Check if the process is running.
|
123
|
var processExists = _processUtils.IsProcessRunning(_processName);
|
124
|
|
125
|
// Check if the process was not running but now it is (flip flop ON).
|
126
|
if (processExists && !_processIsActive) {
|
127
|
Program.DefaultLogger.Info($"Process started: {_processName}");
|
128
|
if (!_failedToRetrieveData) {
|
129
|
_failedToRetrieveData = !await RetrieveDataFromDebugger();
|
130
|
}
|
131
|
}
|
132
|
// Check if the process was running but now it is not (fli flop OFF).
|
133
|
else if (!processExists && _processIsActive) {
|
134
|
Program.DefaultLogger.Info($"Process stopped: {_processName}");
|
135
|
_failedToRetrieveData = false;
|
136
|
await DebuggerDisconnected();
|
137
|
}
|
138
|
|
139
|
// Keep track of the current state of the debugger.
|
140
|
_processIsActive = processExists;
|
141
|
}
|
142
|
|
143
|
/// <summary>
|
144
|
/// Creates a payload and sends it to the server.
|
145
|
/// </summary>
|
146
|
/// <param name="headSerialNumber">serial number of the head of the debugger</param>
|
147
|
/// <param name="bodySerialNumber">serial number of the body of the debugger</param>
|
148
|
/// <param name="datetimeFormat">datetime format (timestamp)</param>
|
149
|
/// <returns>the newly-created payload</returns>
|
150
|
private async Task<Payload> SendDataToServerAsync(string headSerialNumber, string bodySerialNumber, string datetimeFormat) {
|
151
|
// Create a new payload.
|
152
|
Payload payload = new() {
|
153
|
UserName = Environment.UserName,
|
154
|
HostName = Environment.MachineName,
|
155
|
TimeStamp = DateTime.Now.ToString(datetimeFormat),
|
156
|
HeadDevice = new DebuggerInfo {
|
157
|
SerialNumber = headSerialNumber
|
158
|
},
|
159
|
BodyDevice = new DebuggerInfo {
|
160
|
SerialNumber = bodySerialNumber
|
161
|
},
|
162
|
Status = ConnectionStatus.Connected
|
163
|
};
|
164
|
|
165
|
// Send it to the server and return it.
|
166
|
await _apiClient.SendPayloadAsync(payload);
|
167
|
return payload;
|
168
|
}
|
169
|
|
170
|
/// <summary>
|
171
|
/// Periodically runs process detection. This method is instantiated
|
172
|
/// as a thread from the main class (Program.cs).
|
173
|
/// </summary>
|
174
|
public async void RunPeriodicDetection() {
|
175
|
Program.DefaultLogger.Info("Process periodic detector has started");
|
176
|
DetectionRunning = true;
|
177
|
|
178
|
while (DetectionRunning) {
|
179
|
await DetectProcessAsync();
|
180
|
Thread.Sleep((int) _detectionPeriodMs);
|
181
|
}
|
182
|
}
|
183
|
}
|