Projekt

Obecné

Profil

Stáhnout (3.9 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System.Diagnostics;
2

    
3
namespace LDClient.detection {
4

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

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

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

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

    
30
        public async Task<bool> FetchDataAsync() {
31
            Program.DefaultLogger.Info("Fetching data from the debugger.");
32
            var success = SendRetrieveInfoCommands(_f32RemExecutable, _f32RemArguments, _f32SuccessExitCode, _f32WaitTimeoutMs);
33
            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
                await Task.Delay((int)_waitPeriodMs);
44
            }
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
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
52
                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
        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
                return false;
67
            }
68
            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
            return true;
84
        }
85
    }
86
}
(3-3/4)