Projekt

Obecné

Profil

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

    
5
namespace LDClient.detection {
6

    
7
    public class InfoFetcher : IInfoFetcher {
8
        
9
        private const string UndefinedSerialNumber = "number";
10

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

    
19
        public IProcessUtils ProcessUtils;
20
        public IFileUtils FileUtils;
21

    
22
        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
            _maxAttempts = maxAttempts;
29
            _waitPeriodMs = waitPeriodMs;
30
            _infoFilePath = infoFilePath;
31
            _f32RemExecutable = f32RemExecutable;
32
            _f32RemArguments = f32RemArguments;
33
            _f32SuccessExitCode = f32SuccessExitCode;
34
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
35
            ProcessUtils = new ProcessUtils();
36
            FileUtils = new FileUtils();
37

    
38
        }
39

    
40
        public async Task<bool> FetchDataAsync() {
41
            Program.DefaultLogger.Info("Fetching data from the debugger.");
42
            var success = SendRetrieveInfoCommands(_f32RemExecutable, _f32RemArguments, _f32SuccessExitCode, _f32WaitTimeoutMs);
43
            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
                await Task.Delay((int)_waitPeriodMs);
54
            }
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
                var fileContent = FileUtils.ReadFileAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
62
                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
        private bool SendRetrieveInfoCommands(string executableFile, IReadOnlyList<string>? arguments, int desiredExitCode, int waitTimeoutMs) {
74
            if (arguments == null) {
75
                Program.DefaultLogger.Error($"Failed to run {executableFile} - no parameters were given");
76
                return false;
77
            }
78
            foreach (var argument in arguments) {
79
                if (!ProcessUtils.ExecuteNewProcess(executableFile, argument, waitTimeoutMs, desiredExitCode)) {
80
                    return false;
81
                }
82
            }
83
            return true;
84
        }
85
    }
86
}
(5-5/7)