Projekt

Obecné

Profil

Stáhnout (3.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 f281acac silhavyj
using System.Diagnostics;
2
3
namespace LDClient.detection {
4
5
    public class InfoFetcher {
6 6dab0250 silhavyj
        
7 f281acac silhavyj
        private const string UndefinedSerialNumber = "number";
8
9 6dab0250 silhavyj
        private readonly string _f32RemExecutable;
10 4dcc6c07 silhavyj
        private readonly string[] _f32RemArguments;
11
        private readonly int _f32SuccessExitCode;
12
        private readonly int _f32WaitTimeoutMs;
13 6dab0250 silhavyj
        private readonly uint _maxAttempts;
14
        private readonly uint _waitPeriodMs;
15 f281acac silhavyj
        private readonly string _infoFilePath;
16
17
        public string HeadSerialNumber { get; private set; } = UndefinedSerialNumber;
18
        public string BodySerialNumber { get; private set; } = UndefinedSerialNumber;
19
20 4dcc6c07 silhavyj
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) {
21 f281acac silhavyj
            _maxAttempts = maxAttempts;
22
            _waitPeriodMs = waitPeriodMs;
23
            _infoFilePath = infoFilePath;
24 6dab0250 silhavyj
            _f32RemExecutable = f32RemExecutable;
25
            _f32RemArguments = f32RemArguments;
26 4dcc6c07 silhavyj
            _f32SuccessExitCode = f32SuccessExitCode;
27
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
28 f281acac silhavyj
        }
29
30
        public async Task<bool> FetchDataAsync() {
31
            Program.DefaultLogger.Info("Fetching data from the debugger.");
32 4dcc6c07 silhavyj
            var success = SendRetrieveInfoCommands(_f32RemExecutable, _f32RemArguments, _f32SuccessExitCode, _f32WaitTimeoutMs);
33 f281acac silhavyj
            if (!success) {
34
                Program.DefaultLogger.Error("Failed to fetch data from the debugger.");
35
                return false;
36
            }
37
            for (var i = 0; i < _maxAttempts; i++) {
38
                Program.DefaultLogger.Info($"{i}. attempt to parse the info file.");
39
                if (RetrieveDebuggerInfo(_infoFilePath)) {
40
                    Program.DefaultLogger.Info($"Info file has been parsed successfully.");
41
                    return true;
42
                }
43 6dab0250 silhavyj
                await Task.Delay((int)_waitPeriodMs);
44 f281acac silhavyj
            }
45
            Program.DefaultLogger.Error("Failed to parse the into file. It may have not been created.");
46
            return false;
47
        }
48
49
        private bool RetrieveDebuggerInfo(string filePath) {
50
            try {
51 08616eff silhavyj
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
52 f281acac silhavyj
                var (headSerialNumber, bodySerialNumber) = DebuggerInfoParser.Parse(fileContent);
53
                HeadSerialNumber = headSerialNumber;
54
                BodySerialNumber = bodySerialNumber;
55
                File.Delete(filePath);
56
            } catch (Exception exception) {
57
                Program.DefaultLogger.Error($"Failed to retrieve debugger info. File {filePath} may not exist or it does not have the right format. {exception.Message}");
58
                return false;
59
            }
60
            return true;
61
        }
62
63 4dcc6c07 silhavyj
        private static bool SendRetrieveInfoCommands(string executableFile, IReadOnlyList<string>? arguments, int successExitCode, int waitTimeoutMs) {
64
            if (arguments == null) {
65
                Program.DefaultLogger.Error($"Failed to run {executableFile} - no parameters were given");
66 f281acac silhavyj
                return false;
67
            }
68 4dcc6c07 silhavyj
            foreach (var argument in arguments) {
69
                var t32RemProcess = new Process();
70
                t32RemProcess.StartInfo.FileName = executableFile;
71
                t32RemProcess.StartInfo.Arguments = argument;
72
                try {
73
                    t32RemProcess.Start();
74
                    t32RemProcess.WaitForExit(waitTimeoutMs);
75
                    if (t32RemProcess.ExitCode != successExitCode) {
76
                        return false;
77
                    }
78
                } catch (Exception exception) {
79
                    Program.DefaultLogger.Error($"Failed to run {executableFile} {argument}. {exception.Message}");
80
                    return false;
81
                }
82
            }
83 f281acac silhavyj
            return true;
84
        }
85
    }
86
}