Projekt

Obecné

Profil

Stáhnout (11.5 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

    
42
        #region Logger
43

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

    
76
        #region Api
77
        
78
        /// <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
        public string ApiBaseAddress { get; private set; } = null!;
82
        
83
        /// <summary>
84
        /// Path to the API (e.g. /api/v1/ld-logs).
85
        /// </summary>
86
        public string ApiUsbEndPoint { get; private set; } = null!;
87
        
88
        /// <summary>
89
        /// Number of the port that the API runs on.
90
        /// </summary>
91
        public uint ApiPort { get; private set; }
92

    
93
        #endregion
94

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

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

    
167

    
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
        /// <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
        #endregion
199

    
200
        /// <summary>
201
        /// Creates an instance of the class.
202
        /// </summary>
203
        public ConfigLoader() {
204
            // Create a new config builder to read the configuration file.
205
            var configuration = new ConfigurationBuilder()
206
                .AddJsonFile(ConfigFile)
207
                .Build();
208

    
209
            // Parse the logger section.
210
            ReadLoggerSection(configuration);
211
            
212
            // Parse the api section. 
213
            ReadApiSection(configuration);
214
            
215
            // Parse the cache section.
216
            ReadCacheSection(configuration);
217
            
218
            // Parse the detection section.
219
            ReadDebuggerSection(configuration);
220
            
221
            Console.WriteLine("Configuration successfully loaded!");
222
        }
223

    
224
        /// <summary>
225
        /// Parses the logging section of the configuration file.
226
        /// </summary>
227
        /// <param name="configuration">configuration</param>
228
        private void ReadLoggerSection(IConfiguration configuration) {
229
            try {
230
                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
            } catch (Exception e) {
238
                Console.WriteLine(e.Message);
239
                Environment.Exit(ErrorExitCode);
240
            }
241
        }
242

    
243
        /// <summary>
244
        /// Parses the api section of the configuration file.
245
        /// </summary>
246
        /// <param name="configuration">configuration</param>
247
        private void ReadApiSection(IConfiguration configuration) {
248
            try {
249
                var network = configuration.GetSection(NetworkSection);
250
                ApiBaseAddress = network["ApiBaseAddress"];
251
                ApiUsbEndPoint = network["ApiLDEndPoint"];
252
                ApiPort = uint.Parse(network["ApiPort"]);
253
            } catch (Exception e) {
254
                Console.WriteLine(e.Message);
255
                Environment.Exit(ErrorExitCode);
256
            }
257
        }
258

    
259
        /// <summary>
260
        /// Parses the cache section of the configuration file.
261
        /// </summary>
262
        /// <param name="configuration">configuration</param>
263
        private void ReadCacheSection(IConfiguration configuration) {
264
            try {
265
                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
            } catch (Exception e) {
271
                Console.WriteLine(e.Message);
272
                Environment.Exit(ErrorExitCode);
273
            }
274
        }
275

    
276
        /// <summary>
277
        /// Parses the detection section of the configuration file.
278
        /// </summary>
279
        /// <param name="configuration">configuration</param>
280
        private void ReadDebuggerSection(IConfiguration configuration) {
281
            try {
282
                var debugger = configuration.GetSection(DdSection);
283
                T32ProcessName = debugger["T32ProcessName"];
284
                T32InfoLocation = debugger["T32InfoLocation"];
285
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
286
                T32RemExecutable = debugger["T32RemExecutable"];
287
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
288
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
289
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
290
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
291
                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
                FetchInfoSuperiorMaxAttempts = uint.Parse(debugger["FetchInfoSuperiorMaxAttempts"]);
297
                FetchInfoSuperiorAttemptPeriod = uint.Parse(debugger["FetchInfoSuperiorAttemptPeriod"]);
298

    
299
            } catch (Exception e) {
300
                Console.WriteLine(e);
301
                Environment.Exit(ErrorExitCode);
302
            }
303
        }
304
    }
305
}
(1-1/3)