Projekt

Obecné

Profil

Stáhnout (4.25 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System.Diagnostics;
2
using LDClient.utils.loggers;
3

    
4
namespace LDClient.detection {
5

    
6
    public class InfoFetcher {
7
        
8
        private const string UndefinedSerialNumber = "number";
9

    
10
        private readonly string _f32RemExecutable;
11
        private readonly string[] _f32RemArguments;
12
        private readonly int _f32SuccessExitCode;
13
        private readonly int _f32WaitTimeoutMs;
14
        private readonly uint _maxAttempts;
15
        private readonly uint _waitPeriodMs;
16
        private readonly string _infoFilePath;
17

    
18
        public string HeadSerialNumber { get; private set; } = UndefinedSerialNumber;
19
        public string BodySerialNumber { get; private set; } = UndefinedSerialNumber;
20

    
21
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) {
22
            _maxAttempts = maxAttempts;
23
            _waitPeriodMs = waitPeriodMs;
24
            _infoFilePath = infoFilePath;
25
            _f32RemExecutable = f32RemExecutable;
26
            _f32RemArguments = f32RemArguments;
27
            _f32SuccessExitCode = f32SuccessExitCode;
28
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
29
        }
30

    
31
        public async Task<bool> FetchDataAsync() {
32
            Program.DefaultLogger.Info("Fetching data from the debugger.");
33
            var success = SendRetrieveInfoCommands(_f32RemExecutable, _f32RemArguments, _f32SuccessExitCode, _f32WaitTimeoutMs);
34
            if (!success) {
35
                Program.DefaultLogger.Error("Failed to fetch data from the debugger.");
36
                return false;
37
            }
38
            for (var i = 0; i < _maxAttempts; i++) {
39
                Program.DefaultLogger.Info($"{i}. attempt to parse the info file.");
40
                if (RetrieveDebuggerInfo(_infoFilePath)) {
41
                    Program.DefaultLogger.Info($"Info file has been parsed successfully.");
42
                    return true;
43
                }
44
                await Task.Delay((int)_waitPeriodMs);
45
            }
46
            Program.DefaultLogger.Error("Failed to parse the into file. It may have not been created.");
47
            return false;
48
        }
49

    
50
        private bool RetrieveDebuggerInfo(string filePath) {
51
            try {
52
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
53
                var (headSerialNumber, bodySerialNumber) = DebuggerInfoParser.Parse(fileContent);
54
                HeadSerialNumber = headSerialNumber;
55
                BodySerialNumber = bodySerialNumber;
56
                File.Delete(filePath);
57
            } catch (Exception exception) {
58
                Program.DefaultLogger.Error($"Failed to retrieve debugger info. File {filePath} may not exist or it does not have the right format. {exception.Message}");
59
                return false;
60
            }
61
            return true;
62
        }
63

    
64
        private static bool SendRetrieveInfoCommands(string executableFile, IReadOnlyList<string>? arguments, int successExitCode, int waitTimeoutMs) {
65
            if (arguments == null) {
66
                Program.DefaultLogger.Error($"Failed to run {executableFile} - no parameters were given");
67
                return false;
68
            }
69
            foreach (var argument in arguments) {
70
                var t32RemProcess = new Process();
71
                t32RemProcess.StartInfo.FileName = executableFile;
72
                t32RemProcess.StartInfo.Arguments = argument;
73
                try {
74
                    t32RemProcess.Start();
75
                    if (!t32RemProcess.WaitForExit(waitTimeoutMs)) {
76
                        Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {waitTimeoutMs} ms");
77
                        return false;
78
                    }
79
                    if (t32RemProcess.ExitCode != successExitCode) {
80
                        Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}");
81
                        return false;
82
                    }
83
                } catch (Exception exception) {
84
                    Program.DefaultLogger.Error($"Failed to run {executableFile} {argument}. {exception.Message}");
85
                    return false;
86
                }
87
            }
88
            return true;
89
        }
90
    }
91
}
(3-3/4)