Projekt

Obecné

Profil

Stáhnout (3.17 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
        
12
        private readonly uint _maxAttempts;
13
        private readonly uint _waitPeriodMs;
14
        private readonly string _infoFilePath;
15

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

    
19
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string f32RemArguments) {
20
            _maxAttempts = maxAttempts;
21
            _waitPeriodMs = waitPeriodMs;
22
            _infoFilePath = infoFilePath;
23
            _f32RemExecutable = f32RemExecutable;
24
            _f32RemArguments = f32RemArguments;
25
        }
26

    
27
        public async Task<bool> FetchDataAsync() {
28
            Program.DefaultLogger.Info("Fetching data from the debugger.");
29
            var success = await SendRetrieveInfoCommandAsync(_f32RemExecutable, _f32RemArguments);
30
            if (!success) {
31
                Program.DefaultLogger.Error("Failed to fetch data from the debugger.");
32
                return false;
33
            }
34
            for (var i = 0; i < _maxAttempts; i++) {
35
                Program.DefaultLogger.Info($"{i}. attempt to parse the info file.");
36
                if (RetrieveDebuggerInfo(_infoFilePath)) {
37
                    Program.DefaultLogger.Info($"Info file has been parsed successfully.");
38
                    return true;
39
                }
40
                await Task.Delay((int)_waitPeriodMs);
41
            }
42
            Program.DefaultLogger.Error("Failed to parse the into file. It may have not been created.");
43
            return false;
44
        }
45

    
46
        private bool RetrieveDebuggerInfo(string filePath) {
47
            try {
48
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
49
                var (headSerialNumber, bodySerialNumber) = DebuggerInfoParser.Parse(fileContent);
50
                HeadSerialNumber = headSerialNumber;
51
                BodySerialNumber = bodySerialNumber;
52
                File.Delete(filePath);
53
            } catch (Exception exception) {
54
                Program.DefaultLogger.Error($"Failed to retrieve debugger info. File {filePath} may not exist or it does not have the right format. {exception.Message}");
55
                return false;
56
            }
57
            return true;
58
        }
59

    
60
        private static async Task<bool> SendRetrieveInfoCommandAsync(string executableFile, string arguments) {
61
            var t32RemProcess = new Process();
62
            t32RemProcess.StartInfo.FileName = executableFile;
63
            t32RemProcess.StartInfo.Arguments = arguments;
64
            try {
65
                t32RemProcess.Start();
66
                await t32RemProcess.WaitForExitAsync();
67
            } catch (Exception exception) {
68
                Program.DefaultLogger.Error($"Failed to run {executableFile}. {exception.Message}");
69
                return false;
70
            }
71
            return true;
72
        }
73
    }
74
}
(3-3/4)