Projekt

Obecné

Profil

Stáhnout (9.94 KB) Statistiky
| Větev: | Tag: | Revize:
1
using LDClient.utils.loggers;
2
using Microsoft.Extensions.Configuration;
3

    
4
namespace LDClient.utils {
5
    
6
    /// <summary>
7
    /// This class loads up the configuration file (appsettingss.json).
8
    /// </summary>
9
    internal class ConfigLoader {
10

    
11
        /// <summary>
12
        /// Status code indicating a successful termination of an application.
13
        /// </summary>
14
        private const int ErrorExitCode = 1;
15
        
16
        /// <summary>
17
        /// Path to the configuration file.
18
        /// </summary>
19
        private const string ConfigFile = "appsettings.json";
20
        
21
        /// <summary>
22
        /// Name of the logging section defined in the config file.
23
        /// </summary>
24
        private const string LoggingSection = "Logging";
25
        
26
        /// <summary>
27
        /// Name of the network section defined in the config file.
28
        /// </summary>
29
        private const string NetworkSection = "Network";
30
        
31
        /// <summary>
32
        /// Name of the cache section defined in the config file.
33
        /// </summary>
34
        private const string CacheSection = "Cache";
35
        
36
        /// <summary>
37
        /// Name of the detection section defined in the config file.
38
        /// </summary>
39
        private const string DdSection = "DebuggerDetection";
40

    
41
        #region Logger
42
        
43
        /// <summary>
44
        /// Maximum size of the log file (it will start to rotate when this limit is reached).
45
        /// </summary>
46
        public int LogChunkSize { get; private set; }
47
        
48
        /// <summary>
49
        /// Number of files to be created until there will be zipped up.
50
        /// </summary>
51
        public int LogChunkMaxCount { get; private set; }
52
        
53
        /// <summary>
54
        /// Maximum number of zip files
55
        /// </summary>
56
        public int LogArchiveMaxCount { get; private set; }
57
        
58
        /// <summary>
59
        /// Time after which the last logs will be cleaned up.
60
        /// </summary>
61
        public int LogCleanupPeriod { get; private set; }
62
        
63
        /// <summary>
64
        /// Level of verbosity.
65
        /// </summary>
66
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
67
        
68
        /// <summary>
69
        /// Logger flow type.
70
        /// </summary>
71
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
72
        
73
        #endregion
74

    
75
        #region Api
76
        
77
        /// <summary>
78
        /// URL to the API (it can be an IP address or a domain name if a DNS server is being used).
79
        /// </summary>
80
        public string ApiBaseAddress { get; private set; } = null!;
81
        
82
        /// <summary>
83
        /// Path to the API (e.g. /api/v1/ld-logs).
84
        /// </summary>
85
        public string ApiUsbEndPoint { get; private set; } = null!;
86
        
87
        /// <summary>
88
        /// Number of the port that the API runs on.
89
        /// </summary>
90
        public uint ApiPort { get; private set; }
91

    
92
        #endregion
93

    
94
        #region Cache
95
        
96
        /// <summary>
97
        /// Name of the cache (a directory of this name will be created).
98
        /// </summary>
99
        public string CacheFileName { get; private set; } = null!;
100
        
101
        /// <summary>
102
        /// Maximum number of payloads (entries) to be sent to the server at a time.
103
        /// </summary>
104
        public uint MaxRetries { get; private set; }
105
        
106
        /// <summary>
107
        /// Maximum number of entries that can be stored in the database.
108
        /// </summary>
109
        public uint MaxEntries { get; private set; }
110
        
111
        /// <summary>
112
        /// Period (how often) a certain number of entries will be resent to the server.
113
        /// </summary>
114
        public uint RetryPeriod { get; private set; }
115
        
116
        #endregion
117

    
118
        #region Detection
119
        
120
        /// <summary>
121
        /// Name of the process to be detected (the application programmers use to connect to the debugger).
122
        /// </summary>
123
        public string T32ProcessName { get; private set; } = null!;
124
        
125
        /// <summary>
126
        /// How often the application checks if there is the process (T32ProcessName) running on the PC.
127
        /// </summary>
128
        public uint DetectionPeriod { get; private set; }
129
        
130
        /// <summary>
131
        /// Location of the generated .txt file containing all information about a debugger.
132
        /// </summary>
133
        public string T32InfoLocation { get; private set; } = null!;
134
        
135
        /// <summary>
136
        /// Path to the t32rem.exe which is used to send commands to a debugger.
137
        /// </summary>
138
        public string F32RemExecutable { get; private set; } = null!;
139
        
140
        /// <summary>
141
        /// How many times the application attempts to check if there
142
        /// has been a .txt file generated containing all the desired information.
143
        /// </summary>
144
        public uint FetchInfoMaxAttempts { get; private set;  }
145
        
146
        /// <summary>
147
        /// Period in milliseconds after which the application tries to locate and parse the .txt file. 
148
        /// </summary>
149
        public uint FetchInfoAttemptPeriod { get; private set; }
150
        
151
        /// <summary>
152
        /// Arguments (commands) sent to the t32rem.exe file.
153
        /// </summary>
154
        public string[] F32RemArguments { get; private set; } = null!;
155
        
156
        /// <summary>
157
        /// Status code indication successful execution of the t32rem.exe file.
158
        /// </summary>
159
        public int T32RemSuccessExitCode { get; private set; }
160
        
161
        /// <summary>
162
        /// Timeout of the execution of t32rem.exe (when sending one command).
163
        /// </summary>
164
        public int T32RemWaitTimeoutMs { get; private set; }
165

    
166
        #endregion
167

    
168
        /// <summary>
169
        /// Creates an instance of the class.
170
        /// </summary>
171
        public ConfigLoader() {
172
            // Create a new config builder to read the configuration file.
173
            var configuration = new ConfigurationBuilder()
174
                .AddJsonFile(ConfigFile)
175
                .Build();
176

    
177
            // Parse the logger section.
178
            ReadLoggerSection(configuration);
179
            
180
            // Parse the api section. 
181
            ReadApiSection(configuration);
182
            
183
            // Parse the cache section.
184
            ReadCacheSection(configuration);
185
            
186
            // Parse the detection section.
187
            ReadDebuggerSection(configuration);
188
            
189
            Console.WriteLine("Configuration successfully loaded!");
190
        }
191

    
192
        /// <summary>
193
        /// Parses the logging section of the configuration file.
194
        /// </summary>
195
        /// <param name="configuration">configuration</param>
196
        private void ReadLoggerSection(IConfiguration configuration) {
197
            try {
198
                var logging = configuration.GetSection(LoggingSection);
199
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
200
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
201
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
202
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
203
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
204
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
205
            } catch (Exception e) {
206
                Console.WriteLine(e.Message);
207
                Environment.Exit(ErrorExitCode);
208
            }
209
        }
210

    
211
        /// <summary>
212
        /// Parses the api section of the configuration file.
213
        /// </summary>
214
        /// <param name="configuration">configuration</param>
215
        private void ReadApiSection(IConfiguration configuration) {
216
            try {
217
                var network = configuration.GetSection(NetworkSection);
218
                ApiBaseAddress = network["ApiBaseAddress"];
219
                ApiUsbEndPoint = network["ApiLDEndPoint"];
220
                ApiPort = uint.Parse(network["ApiPort"]);
221
            } catch (Exception e) {
222
                Console.WriteLine(e.Message);
223
                Environment.Exit(ErrorExitCode);
224
            }
225
        }
226

    
227
        /// <summary>
228
        /// Parses the cache section of the configuration file.
229
        /// </summary>
230
        /// <param name="configuration">configuration</param>
231
        private void ReadCacheSection(IConfiguration configuration) {
232
            try {
233
                var cache = configuration.GetSection(CacheSection);
234
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
235
                MaxEntries = uint.Parse(cache["MaxEntries"]);
236
                MaxRetries = uint.Parse(cache["MaxRetries"]);
237
                CacheFileName = cache["CacheFileName"];
238
            } catch (Exception e) {
239
                Console.WriteLine(e.Message);
240
                Environment.Exit(ErrorExitCode);
241
            }
242
        }
243

    
244
        /// <summary>
245
        /// Parses the detection section of the configuration file.
246
        /// </summary>
247
        /// <param name="configuration">configuration</param>
248
        private void ReadDebuggerSection(IConfiguration configuration) {
249
            try {
250
                var debugger = configuration.GetSection(DdSection);
251
                T32ProcessName = debugger["T32ProcessName"];
252
                T32InfoLocation = debugger["T32InfoLocation"];
253
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
254
                F32RemExecutable = debugger["F32RemExecutable"];
255
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
256
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
257
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
258
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
259
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemArguments").GetChildren().Select(key => key.Value).ToArray();
260
            } catch (Exception e) {
261
                Console.WriteLine(e);
262
                Environment.Exit(ErrorExitCode);
263
            }
264
        }
265
    }
266
}
(1-1/3)