Projekt

Obecné

Profil

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