Projekt

Obecné

Profil

Stáhnout (4.25 KB) Statistiky
| Větev: | Tag: | Revize:
1 f281acac silhavyj
using System.Diagnostics;
2 19c2a5c4 Pultak
using LDClient.utils.loggers;
3 f281acac silhavyj
4
namespace LDClient.detection {
5
6
    public class InfoFetcher {
7 6dab0250 silhavyj
        
8 f281acac silhavyj
        private const string UndefinedSerialNumber = "number";
9
10 6dab0250 silhavyj
        private readonly string _f32RemExecutable;
11 4dcc6c07 silhavyj
        private readonly string[] _f32RemArguments;
12
        private readonly int _f32SuccessExitCode;
13
        private readonly int _f32WaitTimeoutMs;
14 6dab0250 silhavyj
        private readonly uint _maxAttempts;
15
        private readonly uint _waitPeriodMs;
16 f281acac silhavyj
        private readonly string _infoFilePath;
17
18
        public string HeadSerialNumber { get; private set; } = UndefinedSerialNumber;
19
        public string BodySerialNumber { get; private set; } = UndefinedSerialNumber;
20
21 4dcc6c07 silhavyj
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) {
22 f281acac silhavyj
            _maxAttempts = maxAttempts;
23
            _waitPeriodMs = waitPeriodMs;
24
            _infoFilePath = infoFilePath;
25 6dab0250 silhavyj
            _f32RemExecutable = f32RemExecutable;
26
            _f32RemArguments = f32RemArguments;
27 4dcc6c07 silhavyj
            _f32SuccessExitCode = f32SuccessExitCode;
28
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
29 f281acac silhavyj
        }
30
31
        public async Task<bool> FetchDataAsync() {
32
            Program.DefaultLogger.Info("Fetching data from the debugger.");
33 4dcc6c07 silhavyj
            var success = SendRetrieveInfoCommands(_f32RemExecutable, _f32RemArguments, _f32SuccessExitCode, _f32WaitTimeoutMs);
34 f281acac silhavyj
            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 6dab0250 silhavyj
                await Task.Delay((int)_waitPeriodMs);
45 f281acac silhavyj
            }
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 08616eff silhavyj
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
53 f281acac silhavyj
                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 4dcc6c07 silhavyj
        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 f281acac silhavyj
                return false;
68
            }
69 4dcc6c07 silhavyj
            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 19c2a5c4 Pultak
                    if (!t32RemProcess.WaitForExit(waitTimeoutMs)) {
76
                        Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {waitTimeoutMs} ms");
77
                        return false;
78
                    }
79 4dcc6c07 silhavyj
                    if (t32RemProcess.ExitCode != successExitCode) {
80 19c2a5c4 Pultak
                        Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}");
81 4dcc6c07 silhavyj
                        return false;
82
                    }
83
                } catch (Exception exception) {
84
                    Program.DefaultLogger.Error($"Failed to run {executableFile} {argument}. {exception.Message}");
85
                    return false;
86
                }
87
            }
88 f281acac silhavyj
            return true;
89
        }
90
    }
91
}