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