Projekt

Obecné

Profil

Stáhnout (11.5 KB) Statistiky
| Větev: | Tag: | Revize:
1 b10bb263 silhavyj
using LDClient.utils.loggers;
2 d0cf9476 Pultak
using Microsoft.Extensions.Configuration;
3
4 74bd1e40 Pultak
namespace LDClient.utils {
5 f281acac silhavyj
    
6 9657d7e0 silhavyj
    /// <summary>
7
    /// This class loads up the configuration file (appsettingss.json).
8
    /// </summary>
9 74bd1e40 Pultak
    internal class ConfigLoader {
10 b10bb263 silhavyj
11 9657d7e0 silhavyj
        /// <summary>
12
        /// Status code indicating a successful termination of an application.
13
        /// </summary>
14 b10bb263 silhavyj
        private const int ErrorExitCode = 1;
15 9657d7e0 silhavyj
        
16
        /// <summary>
17
        /// Path to the configuration file.
18
        /// </summary>
19 b10bb263 silhavyj
        private const string ConfigFile = "appsettings.json";
20 9657d7e0 silhavyj
        
21
        /// <summary>
22
        /// Name of the logging section defined in the config file.
23
        /// </summary>
24 74bd1e40 Pultak
        private const string LoggingSection = "Logging";
25 9657d7e0 silhavyj
        
26
        /// <summary>
27
        /// Name of the network section defined in the config file.
28
        /// </summary>
29 58cf6a6f Pultak
        private const string NetworkSection = "Network";
30 9657d7e0 silhavyj
        
31
        /// <summary>
32
        /// Name of the cache section defined in the config file.
33
        /// </summary>
34 c2cc7813 Pultak
        private const string CacheSection = "Cache";
35 9657d7e0 silhavyj
        
36
        /// <summary>
37
        /// Name of the detection section defined in the config file.
38
        /// </summary>
39 f281acac silhavyj
        private const string DdSection = "DebuggerDetection";
40 d0cf9476 Pultak
41 f09385a3 Pultak
42 c2cc7813 Pultak
        #region Logger
43 f09385a3 Pultak
44 9657d7e0 silhavyj
        /// <summary>
45
        /// Maximum size of the log file (it will start to rotate when this limit is reached).
46
        /// </summary>
47 f281acac silhavyj
        public int LogChunkSize { get; private set; }
48 9657d7e0 silhavyj
        
49
        /// <summary>
50
        /// Number of files to be created until there will be zipped up.
51
        /// </summary>
52 f281acac silhavyj
        public int LogChunkMaxCount { get; private set; }
53 9657d7e0 silhavyj
        
54
        /// <summary>
55
        /// Maximum number of zip files
56
        /// </summary>
57 f281acac silhavyj
        public int LogArchiveMaxCount { get; private set; }
58 9657d7e0 silhavyj
        
59
        /// <summary>
60
        /// Time after which the last logs will be cleaned up.
61
        /// </summary>
62 f281acac silhavyj
        public int LogCleanupPeriod { get; private set; }
63 9657d7e0 silhavyj
        
64
        /// <summary>
65
        /// Level of verbosity.
66
        /// </summary>
67 f281acac silhavyj
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
68 9657d7e0 silhavyj
        
69
        /// <summary>
70
        /// Logger flow type.
71
        /// </summary>
72 f281acac silhavyj
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
73
        
74 c2cc7813 Pultak
        #endregion
75 d0cf9476 Pultak
76 c2cc7813 Pultak
        #region Api
77 f281acac silhavyj
        
78 9657d7e0 silhavyj
        /// <summary>
79
        /// URL to the API (it can be an IP address or a domain name if a DNS server is being used).
80
        /// </summary>
81 b10bb263 silhavyj
        public string ApiBaseAddress { get; private set; } = null!;
82 9657d7e0 silhavyj
        
83
        /// <summary>
84
        /// Path to the API (e.g. /api/v1/ld-logs).
85
        /// </summary>
86 b10bb263 silhavyj
        public string ApiUsbEndPoint { get; private set; } = null!;
87 9657d7e0 silhavyj
        
88
        /// <summary>
89
        /// Number of the port that the API runs on.
90
        /// </summary>
91 f281acac silhavyj
        public uint ApiPort { get; private set; }
92 58cf6a6f Pultak
93 c2cc7813 Pultak
        #endregion
94
95
        #region Cache
96 f281acac silhavyj
        
97 9657d7e0 silhavyj
        /// <summary>
98
        /// Name of the cache (a directory of this name will be created).
99
        /// </summary>
100 b10bb263 silhavyj
        public string CacheFileName { get; private set; } = null!;
101 9657d7e0 silhavyj
        
102
        /// <summary>
103
        /// Maximum number of payloads (entries) to be sent to the server at a time.
104
        /// </summary>
105 f281acac silhavyj
        public uint MaxRetries { get; private set; }
106 9657d7e0 silhavyj
        
107
        /// <summary>
108
        /// Maximum number of entries that can be stored in the database.
109
        /// </summary>
110 f281acac silhavyj
        public uint MaxEntries { get; private set; }
111 9657d7e0 silhavyj
        
112
        /// <summary>
113
        /// Period (how often) a certain number of entries will be resent to the server.
114
        /// </summary>
115 f281acac silhavyj
        public uint RetryPeriod { get; private set; }
116
        
117 c2cc7813 Pultak
        #endregion
118
119
        #region Detection
120 9657d7e0 silhavyj
        
121
        /// <summary>
122
        /// Name of the process to be detected (the application programmers use to connect to the debugger).
123
        /// </summary>
124 b10bb263 silhavyj
        public string T32ProcessName { get; private set; } = null!;
125 9657d7e0 silhavyj
        
126
        /// <summary>
127
        /// How often the application checks if there is the process (T32ProcessName) running on the PC.
128
        /// </summary>
129 f281acac silhavyj
        public uint DetectionPeriod { get; private set; }
130 9657d7e0 silhavyj
        
131
        /// <summary>
132
        /// Location of the generated .txt file containing all information about a debugger.
133
        /// </summary>
134 b10bb263 silhavyj
        public string T32InfoLocation { get; private set; } = null!;
135 9657d7e0 silhavyj
        
136
        /// <summary>
137
        /// Path to the t32rem.exe which is used to send commands to a debugger.
138
        /// </summary>
139 f09385a3 Pultak
        public string T32RemExecutable { get; private set; } = null!;
140 9657d7e0 silhavyj
        
141
        /// <summary>
142
        /// How many times the application attempts to check if there
143
        /// has been a .txt file generated containing all the desired information.
144
        /// </summary>
145 6dab0250 silhavyj
        public uint FetchInfoMaxAttempts { get; private set;  }
146 9657d7e0 silhavyj
        
147
        /// <summary>
148
        /// Period in milliseconds after which the application tries to locate and parse the .txt file. 
149
        /// </summary>
150 6dab0250 silhavyj
        public uint FetchInfoAttemptPeriod { get; private set; }
151 9657d7e0 silhavyj
        
152
        /// <summary>
153
        /// Arguments (commands) sent to the t32rem.exe file.
154
        /// </summary>
155 f09385a3 Pultak
        public string[] T32RemArguments { get; private set; } = null!;
156 9657d7e0 silhavyj
        
157
        /// <summary>
158
        /// Status code indication successful execution of the t32rem.exe file.
159
        /// </summary>
160 4dcc6c07 silhavyj
        public int T32RemSuccessExitCode { get; private set; }
161 9657d7e0 silhavyj
        
162
        /// <summary>
163
        /// Timeout of the execution of t32rem.exe (when sending one command).
164
        /// </summary>
165 4dcc6c07 silhavyj
        public int T32RemWaitTimeoutMs { get; private set; }
166
167 e6a01bd8 Pultak
168
        /// <summary>
169
        /// List of commands to be executed by though the t32 api
170
        /// </summary>
171
        public string[] T32ApiCommands { get; private set; } = null!;
172
173
        /// <summary>
174
        /// Address of the listening t32 application
175
        /// </summary>
176
        public string T32ApiAddress { get; private set; } = null!;
177
178
        /// <summary>
179
        /// Port of the listening t32 application
180
        /// </summary>
181
        public string T32ApiPort { get; private set; } = null!;
182
183
        /// <summary>
184
        /// Size of the packets send/received from t32 application
185
        /// </summary>
186
        public string T32ApiPacketLen { get; private set; } = null!;
187
188 2c9a5fda silhavyj
        /// <summary>
189
        /// Superior number of attempts to fetch the information (outer loop).
190
        /// </summary>
191
        public uint FetchInfoSuperiorMaxAttempts { get; private set; }
192
        
193
        /// <summary>
194
        /// Period of the superior (outer) loop to fetch the data.
195
        /// </summary>
196
        public uint FetchInfoSuperiorAttemptPeriod { get; private set;  }
197
198 c2cc7813 Pultak
        #endregion
199 58cf6a6f Pultak
200 9657d7e0 silhavyj
        /// <summary>
201
        /// Creates an instance of the class.
202
        /// </summary>
203 74bd1e40 Pultak
        public ConfigLoader() {
204 9657d7e0 silhavyj
            // Create a new config builder to read the configuration file.
205 d0cf9476 Pultak
            var configuration = new ConfigurationBuilder()
206 b10bb263 silhavyj
                .AddJsonFile(ConfigFile)
207 d0cf9476 Pultak
                .Build();
208
209 9657d7e0 silhavyj
            // Parse the logger section.
210 b10bb263 silhavyj
            ReadLoggerSection(configuration);
211 9657d7e0 silhavyj
            
212
            // Parse the api section. 
213 b10bb263 silhavyj
            ReadApiSection(configuration);
214 9657d7e0 silhavyj
            
215
            // Parse the cache section.
216 b10bb263 silhavyj
            ReadCacheSection(configuration);
217 9657d7e0 silhavyj
            
218
            // Parse the detection section.
219 b10bb263 silhavyj
            ReadDebuggerSection(configuration);
220
            
221
            Console.WriteLine("Configuration successfully loaded!");
222
        }
223 d0cf9476 Pultak
224 9657d7e0 silhavyj
        /// <summary>
225
        /// Parses the logging section of the configuration file.
226
        /// </summary>
227
        /// <param name="configuration">configuration</param>
228 b10bb263 silhavyj
        private void ReadLoggerSection(IConfiguration configuration) {
229 d0cf9476 Pultak
            try {
230 74bd1e40 Pultak
                var logging = configuration.GetSection(LoggingSection);
231
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
232
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
233
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
234
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
235
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
236
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
237 b10bb263 silhavyj
            } catch (Exception e) {
238
                Console.WriteLine(e.Message);
239
                Environment.Exit(ErrorExitCode);
240
            }
241
        }
242 914776bd Pultak
243 9657d7e0 silhavyj
        /// <summary>
244
        /// Parses the api section of the configuration file.
245
        /// </summary>
246
        /// <param name="configuration">configuration</param>
247 b10bb263 silhavyj
        private void ReadApiSection(IConfiguration configuration) {
248
            try {
249 58cf6a6f Pultak
                var network = configuration.GetSection(NetworkSection);
250
                ApiBaseAddress = network["ApiBaseAddress"];
251
                ApiUsbEndPoint = network["ApiLDEndPoint"];
252
                ApiPort = uint.Parse(network["ApiPort"]);
253 b10bb263 silhavyj
            } catch (Exception e) {
254
                Console.WriteLine(e.Message);
255
                Environment.Exit(ErrorExitCode);
256
            }
257
        }
258
259 9657d7e0 silhavyj
        /// <summary>
260
        /// Parses the cache section of the configuration file.
261
        /// </summary>
262
        /// <param name="configuration">configuration</param>
263 b10bb263 silhavyj
        private void ReadCacheSection(IConfiguration configuration) {
264
            try {
265 c2cc7813 Pultak
                var cache = configuration.GetSection(CacheSection);
266
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
267
                MaxEntries = uint.Parse(cache["MaxEntries"]);
268
                MaxRetries = uint.Parse(cache["MaxRetries"]);
269
                CacheFileName = cache["CacheFileName"];
270 b10bb263 silhavyj
            } catch (Exception e) {
271
                Console.WriteLine(e.Message);
272
                Environment.Exit(ErrorExitCode);
273
            }
274
        }
275
276 9657d7e0 silhavyj
        /// <summary>
277
        /// Parses the detection section of the configuration file.
278
        /// </summary>
279
        /// <param name="configuration">configuration</param>
280 b10bb263 silhavyj
        private void ReadDebuggerSection(IConfiguration configuration) {
281
            try {
282 f281acac silhavyj
                var debugger = configuration.GetSection(DdSection);
283 914776bd Pultak
                T32ProcessName = debugger["T32ProcessName"];
284
                T32InfoLocation = debugger["T32InfoLocation"];
285
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
286 f09385a3 Pultak
                T32RemExecutable = debugger["T32RemExecutable"];
287 6dab0250 silhavyj
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
288
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
289 4dcc6c07 silhavyj
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
290
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
291 f09385a3 Pultak
                T32RemArguments = configuration.GetSection($"{DdSection}:T32RemArguments").GetChildren().Select(key => key.Value).ToArray();
292
                T32ApiCommands = configuration.GetSection($"{DdSection}:T32ApiCommands").GetChildren().Select(key => key.Value).ToArray();
293
                T32ApiAddress = debugger["T32ApiAddress"];
294
                T32ApiPort = debugger["T32ApiPort"];
295
                T32ApiPacketLen = debugger["T32ApiPacketLen"];
296 2c9a5fda silhavyj
                FetchInfoSuperiorMaxAttempts = uint.Parse(debugger["FetchInfoSuperiorMaxAttempts"]);
297
                FetchInfoSuperiorAttemptPeriod = uint.Parse(debugger["FetchInfoSuperiorAttemptPeriod"]);
298
299 b10bb263 silhavyj
            } catch (Exception e) {
300
                Console.WriteLine(e);
301
                Environment.Exit(ErrorExitCode);
302 d0cf9476 Pultak
            }
303
        }
304
    }
305
}