Projekt

Obecné

Profil

Stáhnout (10.9 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
        #endregion
189

    
190
        /// <summary>
191
        /// Creates an instance of the class.
192
        /// </summary>
193
        public ConfigLoader() {
194
            // Create a new config builder to read the configuration file.
195
            var configuration = new ConfigurationBuilder()
196
                .AddJsonFile(ConfigFile)
197
                .Build();
198

    
199
            // Parse the logger section.
200
            ReadLoggerSection(configuration);
201
            
202
            // Parse the api section. 
203
            ReadApiSection(configuration);
204
            
205
            // Parse the cache section.
206
            ReadCacheSection(configuration);
207
            
208
            // Parse the detection section.
209
            ReadDebuggerSection(configuration);
210
            
211
            Console.WriteLine("Configuration successfully loaded!");
212
        }
213

    
214
        /// <summary>
215
        /// Parses the logging section of the configuration file.
216
        /// </summary>
217
        /// <param name="configuration">configuration</param>
218
        private void ReadLoggerSection(IConfiguration configuration) {
219
            try {
220
                var logging = configuration.GetSection(LoggingSection);
221
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
222
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
223
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
224
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
225
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
226
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
227
            } catch (Exception e) {
228
                Console.WriteLine(e.Message);
229
                Environment.Exit(ErrorExitCode);
230
            }
231
        }
232

    
233
        /// <summary>
234
        /// Parses the api section of the configuration file.
235
        /// </summary>
236
        /// <param name="configuration">configuration</param>
237
        private void ReadApiSection(IConfiguration configuration) {
238
            try {
239
                var network = configuration.GetSection(NetworkSection);
240
                ApiBaseAddress = network["ApiBaseAddress"];
241
                ApiUsbEndPoint = network["ApiLDEndPoint"];
242
                ApiPort = uint.Parse(network["ApiPort"]);
243
            } catch (Exception e) {
244
                Console.WriteLine(e.Message);
245
                Environment.Exit(ErrorExitCode);
246
            }
247
        }
248

    
249
        /// <summary>
250
        /// Parses the cache section of the configuration file.
251
        /// </summary>
252
        /// <param name="configuration">configuration</param>
253
        private void ReadCacheSection(IConfiguration configuration) {
254
            try {
255
                var cache = configuration.GetSection(CacheSection);
256
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
257
                MaxEntries = uint.Parse(cache["MaxEntries"]);
258
                MaxRetries = uint.Parse(cache["MaxRetries"]);
259
                CacheFileName = cache["CacheFileName"];
260
            } catch (Exception e) {
261
                Console.WriteLine(e.Message);
262
                Environment.Exit(ErrorExitCode);
263
            }
264
        }
265

    
266
        /// <summary>
267
        /// Parses the detection section of the configuration file.
268
        /// </summary>
269
        /// <param name="configuration">configuration</param>
270
        private void ReadDebuggerSection(IConfiguration configuration) {
271
            try {
272
                var debugger = configuration.GetSection(DdSection);
273
                T32ProcessName = debugger["T32ProcessName"];
274
                T32InfoLocation = debugger["T32InfoLocation"];
275
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
276
                T32RemExecutable = debugger["T32RemExecutable"];
277
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
278
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
279
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
280
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
281
                T32RemArguments = configuration.GetSection($"{DdSection}:T32RemArguments").GetChildren().Select(key => key.Value).ToArray();
282
                T32ApiCommands = configuration.GetSection($"{DdSection}:T32ApiCommands").GetChildren().Select(key => key.Value).ToArray();
283
                T32ApiAddress = debugger["T32ApiAddress"];
284
                T32ApiPort = debugger["T32ApiPort"];
285
                T32ApiPacketLen = debugger["T32ApiPacketLen"];
286
                
287
            } catch (Exception e) {
288
                Console.WriteLine(e);
289
                Environment.Exit(ErrorExitCode);
290
            }
291
        }
292
    }
293
}
(1-1/3)