Projekt

Obecné

Profil

Stáhnout (9.94 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 c2cc7813 Pultak
        #region Logger
42 f281acac silhavyj
        
43 9657d7e0 silhavyj
        /// <summary>
44
        /// Maximum size of the log file (it will start to rotate when this limit is reached).
45
        /// </summary>
46 f281acac silhavyj
        public int LogChunkSize { get; private set; }
47 9657d7e0 silhavyj
        
48
        /// <summary>
49
        /// Number of files to be created until there will be zipped up.
50
        /// </summary>
51 f281acac silhavyj
        public int LogChunkMaxCount { get; private set; }
52 9657d7e0 silhavyj
        
53
        /// <summary>
54
        /// Maximum number of zip files
55
        /// </summary>
56 f281acac silhavyj
        public int LogArchiveMaxCount { get; private set; }
57 9657d7e0 silhavyj
        
58
        /// <summary>
59
        /// Time after which the last logs will be cleaned up.
60
        /// </summary>
61 f281acac silhavyj
        public int LogCleanupPeriod { get; private set; }
62 9657d7e0 silhavyj
        
63
        /// <summary>
64
        /// Level of verbosity.
65
        /// </summary>
66 f281acac silhavyj
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
67 9657d7e0 silhavyj
        
68
        /// <summary>
69
        /// Logger flow type.
70
        /// </summary>
71 f281acac silhavyj
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
72
        
73 c2cc7813 Pultak
        #endregion
74 d0cf9476 Pultak
75 c2cc7813 Pultak
        #region Api
76 f281acac silhavyj
        
77 9657d7e0 silhavyj
        /// <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 b10bb263 silhavyj
        public string ApiBaseAddress { get; private set; } = null!;
81 9657d7e0 silhavyj
        
82
        /// <summary>
83
        /// Path to the API (e.g. /api/v1/ld-logs).
84
        /// </summary>
85 b10bb263 silhavyj
        public string ApiUsbEndPoint { get; private set; } = null!;
86 9657d7e0 silhavyj
        
87
        /// <summary>
88
        /// Number of the port that the API runs on.
89
        /// </summary>
90 f281acac silhavyj
        public uint ApiPort { get; private set; }
91 58cf6a6f Pultak
92 c2cc7813 Pultak
        #endregion
93
94
        #region Cache
95 f281acac silhavyj
        
96 9657d7e0 silhavyj
        /// <summary>
97
        /// Name of the cache (a directory of this name will be created).
98
        /// </summary>
99 b10bb263 silhavyj
        public string CacheFileName { get; private set; } = null!;
100 9657d7e0 silhavyj
        
101
        /// <summary>
102
        /// Maximum number of payloads (entries) to be sent to the server at a time.
103
        /// </summary>
104 f281acac silhavyj
        public uint MaxRetries { get; private set; }
105 9657d7e0 silhavyj
        
106
        /// <summary>
107
        /// Maximum number of entries that can be stored in the database.
108
        /// </summary>
109 f281acac silhavyj
        public uint MaxEntries { get; private set; }
110 9657d7e0 silhavyj
        
111
        /// <summary>
112
        /// Period (how often) a certain number of entries will be resent to the server.
113
        /// </summary>
114 f281acac silhavyj
        public uint RetryPeriod { get; private set; }
115
        
116 c2cc7813 Pultak
        #endregion
117
118
        #region Detection
119 9657d7e0 silhavyj
        
120
        /// <summary>
121
        /// Name of the process to be detected (the application programmers use to connect to the debugger).
122
        /// </summary>
123 b10bb263 silhavyj
        public string T32ProcessName { get; private set; } = null!;
124 9657d7e0 silhavyj
        
125
        /// <summary>
126
        /// How often the application checks if there is the process (T32ProcessName) running on the PC.
127
        /// </summary>
128 f281acac silhavyj
        public uint DetectionPeriod { get; private set; }
129 9657d7e0 silhavyj
        
130
        /// <summary>
131
        /// Location of the generated .txt file containing all information about a debugger.
132
        /// </summary>
133 b10bb263 silhavyj
        public string T32InfoLocation { get; private set; } = null!;
134 9657d7e0 silhavyj
        
135
        /// <summary>
136
        /// Path to the t32rem.exe which is used to send commands to a debugger.
137
        /// </summary>
138 b10bb263 silhavyj
        public string F32RemExecutable { get; private set; } = null!;
139 9657d7e0 silhavyj
        
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 6dab0250 silhavyj
        public uint FetchInfoMaxAttempts { get; private set;  }
145 9657d7e0 silhavyj
        
146
        /// <summary>
147
        /// Period in milliseconds after which the application tries to locate and parse the .txt file. 
148
        /// </summary>
149 6dab0250 silhavyj
        public uint FetchInfoAttemptPeriod { get; private set; }
150 9657d7e0 silhavyj
        
151
        /// <summary>
152
        /// Arguments (commands) sent to the t32rem.exe file.
153
        /// </summary>
154 b10bb263 silhavyj
        public string[] F32RemArguments { get; private set; } = null!;
155 9657d7e0 silhavyj
        
156
        /// <summary>
157
        /// Status code indication successful execution of the t32rem.exe file.
158
        /// </summary>
159 4dcc6c07 silhavyj
        public int T32RemSuccessExitCode { get; private set; }
160 9657d7e0 silhavyj
        
161
        /// <summary>
162
        /// Timeout of the execution of t32rem.exe (when sending one command).
163
        /// </summary>
164 4dcc6c07 silhavyj
        public int T32RemWaitTimeoutMs { get; private set; }
165
166 c2cc7813 Pultak
        #endregion
167 58cf6a6f Pultak
168 9657d7e0 silhavyj
        /// <summary>
169
        /// Creates an instance of the class.
170
        /// </summary>
171 74bd1e40 Pultak
        public ConfigLoader() {
172 9657d7e0 silhavyj
            // Create a new config builder to read the configuration file.
173 d0cf9476 Pultak
            var configuration = new ConfigurationBuilder()
174 b10bb263 silhavyj
                .AddJsonFile(ConfigFile)
175 d0cf9476 Pultak
                .Build();
176
177 9657d7e0 silhavyj
            // Parse the logger section.
178 b10bb263 silhavyj
            ReadLoggerSection(configuration);
179 9657d7e0 silhavyj
            
180
            // Parse the api section. 
181 b10bb263 silhavyj
            ReadApiSection(configuration);
182 9657d7e0 silhavyj
            
183
            // Parse the cache section.
184 b10bb263 silhavyj
            ReadCacheSection(configuration);
185 9657d7e0 silhavyj
            
186
            // Parse the detection section.
187 b10bb263 silhavyj
            ReadDebuggerSection(configuration);
188
            
189
            Console.WriteLine("Configuration successfully loaded!");
190
        }
191 d0cf9476 Pultak
192 9657d7e0 silhavyj
        /// <summary>
193
        /// Parses the logging section of the configuration file.
194
        /// </summary>
195
        /// <param name="configuration">configuration</param>
196 b10bb263 silhavyj
        private void ReadLoggerSection(IConfiguration configuration) {
197 d0cf9476 Pultak
            try {
198 74bd1e40 Pultak
                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 b10bb263 silhavyj
            } catch (Exception e) {
206
                Console.WriteLine(e.Message);
207
                Environment.Exit(ErrorExitCode);
208
            }
209
        }
210 914776bd Pultak
211 9657d7e0 silhavyj
        /// <summary>
212
        /// Parses the api section of the configuration file.
213
        /// </summary>
214
        /// <param name="configuration">configuration</param>
215 b10bb263 silhavyj
        private void ReadApiSection(IConfiguration configuration) {
216
            try {
217 58cf6a6f Pultak
                var network = configuration.GetSection(NetworkSection);
218
                ApiBaseAddress = network["ApiBaseAddress"];
219
                ApiUsbEndPoint = network["ApiLDEndPoint"];
220
                ApiPort = uint.Parse(network["ApiPort"]);
221 b10bb263 silhavyj
            } catch (Exception e) {
222
                Console.WriteLine(e.Message);
223
                Environment.Exit(ErrorExitCode);
224
            }
225
        }
226
227 9657d7e0 silhavyj
        /// <summary>
228
        /// Parses the cache section of the configuration file.
229
        /// </summary>
230
        /// <param name="configuration">configuration</param>
231 b10bb263 silhavyj
        private void ReadCacheSection(IConfiguration configuration) {
232
            try {
233 c2cc7813 Pultak
                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 b10bb263 silhavyj
            } catch (Exception e) {
239
                Console.WriteLine(e.Message);
240
                Environment.Exit(ErrorExitCode);
241
            }
242
        }
243
244 9657d7e0 silhavyj
        /// <summary>
245
        /// Parses the detection section of the configuration file.
246
        /// </summary>
247
        /// <param name="configuration">configuration</param>
248 b10bb263 silhavyj
        private void ReadDebuggerSection(IConfiguration configuration) {
249
            try {
250 f281acac silhavyj
                var debugger = configuration.GetSection(DdSection);
251 914776bd Pultak
                T32ProcessName = debugger["T32ProcessName"];
252
                T32InfoLocation = debugger["T32InfoLocation"];
253
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
254 6dab0250 silhavyj
                F32RemExecutable = debugger["F32RemExecutable"];
255
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
256
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
257 4dcc6c07 silhavyj
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
258
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
259 19c2a5c4 Pultak
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemArguments").GetChildren().Select(key => key.Value).ToArray();
260 b10bb263 silhavyj
            } catch (Exception e) {
261
                Console.WriteLine(e);
262
                Environment.Exit(ErrorExitCode);
263 d0cf9476 Pultak
            }
264
        }
265
    }
266
}