Projekt

Obecné

Profil

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

    
4
namespace LDClient.detection {
5

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

    
10
        private readonly string _f32RemExecutable;
11
        private readonly string _f32RemArguments;
12
        
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) {
21
            _maxAttempts = maxAttempts;
22
            _waitPeriodMs = waitPeriodMs;
23
            _infoFilePath = infoFilePath;
24
            _f32RemExecutable = f32RemExecutable;
25
            _f32RemArguments = f32RemArguments;
26
        }
27

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

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

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