Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 12899c8b

Přidáno uživatelem Jakub Šilhavý před více než 2 roky(ů)

re #9570 - Commented IProcessDetection.cs, IProcessUtils.cs, ProcessDetection.cs, and ProcessUtils.cs

Zobrazit rozdíly:

ld_client/LDClient/detection/IProcessDetection.cs
1 1
namespace LDClient.detection {
2 2
    
3
    /// <summary>
4
    /// This interface defines the functionality of a process detector.
5
    /// A process detector is used to determine whether a user is currently
6
    /// using a debugger or not.
7
    /// </summary>
3 8
    internal interface IProcessDetection {
4 9
        
10
        /// <summary>
11
        /// Periodically runs process detection. This method is instantiated
12
        /// as a thread from the main class (Program.cs).
13
        /// </summary>
5 14
        public void RunPeriodicDetection();
6 15
    }
7 16
}
ld_client/LDClient/detection/IProcessUtils.cs
5 5
using System.Threading.Tasks;
6 6

  
7 7
namespace LDClient.detection {
8
    
9
    /// <summary>
10
    /// This interface defines the functionality of all methods that
11
    /// are used to work with processes (within this project).
12
    /// </summary>
8 13
    public interface IProcessUtils {
14
        
15
        /// <summary>
16
        /// Checks if a process is running or not.
17
        /// </summary>
18
        /// <param name="name">Name of the process</param>
19
        /// <returns>True, if the process is running. False otherwise.</returns>
9 20
        public bool IsProcessRunning(string name);
10 21

  
22
        /// <summary>
23
        /// Executes a new process (t32rem.exe) with arguments which are passed in
24
        /// as a parameter of the method.
25
        /// </summary>
26
        /// <param name="fileName">Path to the .exe file</param>
27
        /// <param name="argument">Arguments passed into the .exe file</param>
28
        /// <param name="timeout">Timeout used when waiting for the process to terminate</param>
29
        /// <param name="desiredExitCode">Status code indicating a successful termination of the process.</param>
30
        /// <returns>True, if the command was executed successfully. False otherwise.</returns>
11 31
        public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode);
12 32
    }
13 33
}
ld_client/LDClient/detection/ProcessDetection.cs
3 3
using LDClient.network.data;
4 4

  
5 5
namespace LDClient.detection {
6
   
7
	 public sealed class ProcessDetection : IProcessDetection {
8
        
6

  
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>
9 17
        private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
10 18

  
19
        /// <summary>
20
        /// Name of the process the application detects.
21
        /// </summary>
11 22
        private readonly string _processName;
23
        
24
        /// <summary>
25
        /// How often the application check the current status of the process (cunning / not running).
26
        /// </summary>
12 27
        private readonly uint _detectionPeriodMs;
28
        
29
        /// <summary>
30
        /// Instance of InfoFetcher used to fetch information from the debugger
31
        /// (when the process is detected).
32
        /// </summary>
13 33
        private readonly IInfoFetcher _infoFetcher;
34
        
35
        /// <summary>
36
        /// Instance of API clients used for sending data off to the server.
37
        /// </summary>
14 38
        private readonly IApiClient _apiClient;
39
        
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>
15 44
        private readonly IProcessUtils _processUtils;
16 45

  
46
        /// <summary>
47
        /// Flag indicating whether the process is currently running or not.
48
        /// </summary>
17 49
        private bool _processIsActive;
50
        
51
        /// <summary>
52
        /// Flag if the application failed to retrieve data when the process was detected.
53
        /// </summary>
18 54
        private bool _failedToRetrieveData;
55
        
56
        /// <summary>
57
        /// Last payload that was sent to the server.
58
        /// </summary>
19 59
        private Payload? _lastConnectedPayload;
20 60

  
61
        /// <summary>
62
        /// Flag used to stop the thread (process detection).
63
        /// </summary>
21 64
        public bool DetectionRunning = false;
22

  
23
        public ProcessDetection(string processName, uint detectionPeriodMs, IInfoFetcher infoFetcher, IApiClient apiClient, IProcessUtils processUtils) {
65
        
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) {
24 76
            _processName = processName;
25 77
            _detectionPeriodMs = detectionPeriodMs;
26 78
            _infoFetcher = infoFetcher;
......
29 81
            _processUtils = processUtils;
30 82
        }
31 83

  
84
        /// <summary>
85
        /// Retrieves data from the debugger.
86
        /// </summary>
87
        /// <returns>True, if the data was fetched successfully. False, otherwise.</returns>
32 88
        private async Task<bool> RetrieveDataFromDebugger() {
89
            // Try to fetch data from the debugger.
33 90
            var success = await _infoFetcher.FetchDataAsync();
91
            
92
            // If the data was fetched successfully, send a payload off to the server.
34 93
            if (success) {
35
                _lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber, _infoFetcher.BodySerialNumber, DatetimeFormat);
94
                _lastConnectedPayload = await SendDataToServerAsync(_infoFetcher.HeadSerialNumber,
95
                    _infoFetcher.BodySerialNumber, DatetimeFormat);
36 96
            }
37 97
            return success;
38 98
        }
39 99

  
100
        /// <summary>
101
        /// Sends a payload to the server when a debugger gets disconnected.
102
        /// </summary>
40 103
        private async Task DebuggerDisconnected() {
104
            // Make sure the debugger was connected in the first place.
41 105
            if (_lastConnectedPayload is not null) {
106
                // Update the status and timestamp of the last payload
107
                // (the serial numbers remain the same).
42 108
                _lastConnectedPayload.Status = ConnectionStatus.Disconnected;
43 109
                _lastConnectedPayload.TimeStamp = DateTime.Now.ToString(DatetimeFormat);
110
                
111
                // Send the data to the server.
44 112
                await _apiClient.SendPayloadAsync(_lastConnectedPayload);
113
                
114
                // Clear the last payload.
45 115
                _lastConnectedPayload = null;
46 116
            }
47 117
        }
48 118

  
119
        /// <summary>
120
        /// Checks if the t32mtc process is running or not. 
121
        /// </summary>
49 122
        private async Task DetectProcessAsync() {
123
            // Check if the process is running.
50 124
            var processExists = _processUtils.IsProcessRunning(_processName);
51 125

  
126
            // Check if the process was not running but now it is (flip flop ON). 
52 127
            if (processExists && !_processIsActive) {
53 128
                Program.DefaultLogger.Info($"Process started: {_processName}");
54 129
                if (!_failedToRetrieveData) {
55 130
                    _failedToRetrieveData = !await RetrieveDataFromDebugger();
56 131
                }
57
            } else if (!processExists && _processIsActive) {
132
            }
133
            // Check if the process was running but now it is not (fli flop OFF).
134
            else if (!processExists && _processIsActive) {
58 135
                Program.DefaultLogger.Info($"Process stopped: {_processName}");
59 136
                _failedToRetrieveData = false;
60 137
                await DebuggerDisconnected();
61 138
            }
139

  
140
            // Keep track of the current state of the debugger.
62 141
            _processIsActive = processExists;
63 142
        }
64
        
143

  
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>
65 151
        private async Task<Payload> SendDataToServerAsync(string headSerialNumber, string bodySerialNumber, string datetimeFormat) {
152
            // Create a new payload. 
66 153
            Payload payload = new() {
67 154
                UserName = Environment.UserName,
68 155
                HostName = Environment.MachineName,
......
75 162
                },
76 163
                Status = ConnectionStatus.Connected
77 164
            };
165
            
166
            // Send it to the server and return it.
78 167
            await _apiClient.SendPayloadAsync(payload);
79 168
            return payload;
80 169
        }
81
        
170

  
171
        /// <summary>
172
        /// Periodically runs process detection. This method is instantiated
173
        /// as a thread from the main class (Program.cs).
174
        /// </summary>
82 175
        public async void RunPeriodicDetection() {
83 176
            Program.DefaultLogger.Info("Process periodic detector has started");
84 177
            DetectionRunning = true;
178
            
85 179
            while (DetectionRunning) {
86 180
                await DetectProcessAsync();
87
                Thread.Sleep((int)_detectionPeriodMs);
181
                Thread.Sleep((int) _detectionPeriodMs);
88 182
            }
89 183
        }
90 184
    }
ld_client/LDClient/detection/ProcessUtils.cs
6 6
using System.Threading.Tasks;
7 7

  
8 8
namespace LDClient.detection {
9
    public class ProcessUtils : IProcessUtils{
9
    
10
    /// <summary>
11
    /// This class implements the IProcessUtils interface.
12
    /// It implements methods that are used when dealing with processes.
13
    /// </summary>
14
    public class ProcessUtils : IProcessUtils {
10 15

  
16
        /// <summary>
17
        /// Checks if a process is running or not.
18
        /// </summary>
19
        /// <param name="name">Name of the process</param>
20
        /// <returns>True, if the process is running. False otherwise.</returns>
11 21
        public bool IsProcessRunning(string name) {
12 22
            return Process.GetProcessesByName(name).Length > 0;
13 23
        }
14

  
15

  
24
        
25
        /// <summary>
26
        /// Executes a new process (t32rem.exe) with arguments which are passed in
27
        /// as a parameter of the method.
28
        /// </summary>
29
        /// <param name="fileName">Path to the .exe file</param>
30
        /// <param name="argument">Arguments passed into the .exe file</param>
31
        /// <param name="timeout">Timeout used when waiting for the process to terminate</param>
32
        /// <param name="desiredExitCode">Status code indicating a successful termination of the process.</param>
33
        /// <returns>True, if the command was executed successfully. False otherwise.</returns>
16 34
        public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode) {
17

  
35
            // Create a new process.
18 36
            var t32RemProcess = new Process();
19 37
            t32RemProcess.StartInfo.FileName = fileName;
20 38
            t32RemProcess.StartInfo.Arguments = argument;
39
            
21 40
            try {
41
                // Execute the process and wait until it terminates or until the timeout is up.
22 42
                t32RemProcess.Start();
23 43
                if (!t32RemProcess.WaitForExit(timeout)) {
24 44
                    Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {timeout} ms");
25 45
                    return false;
26 46
                }
47
                
48
                // Check if the process terminated successfully.
27 49
                if (t32RemProcess.ExitCode != desiredExitCode) {
28 50
                    Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}");
29 51
                    return false;
......
32 54
                Program.DefaultLogger.Error($"Failed to run {fileName} {argument}. {exception.Message}");
33 55
                return false;
34 56
            }
35

  
36 57
            return true;
37 58
        }
38

  
39 59
    }
40 60
}

Také k dispozici: Unified diff