|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using MossbauerLab.TinyTcpServer.Core.Server; |
| 6 | + |
| 7 | +namespace MossbauerLab.TinyTcpServer.MnGUI.Helpers |
| 8 | +{ |
| 9 | + public static class TcpServerConfigBuilder |
| 10 | + { |
| 11 | + public static TcpServerConfig Build(String serverConfig) |
| 12 | + { |
| 13 | + if(String.IsNullOrEmpty(serverConfig)) |
| 14 | + throw new ArgumentNullException("serverConfig"); |
| 15 | + if(!File.Exists(serverConfig)) |
| 16 | + throw new ApplicationException("Config file does not exists"); |
| 17 | + IList<String> content = File.ReadAllLines(serverConfig).Select(line=>line.Trim().ToLower()) |
| 18 | + .Where(line => !String.IsNullOrEmpty(line)) |
| 19 | + .Where(line => !line.StartsWith(CommentarySymbol)) |
| 20 | + .ToList(); |
| 21 | + TcpServerConfig config = new TcpServerConfig(); |
| 22 | + Int32 value = GetConfigurationValue(content, ParallelTaskKey); |
| 23 | + if (value != -1) // otherwise we using default value |
| 24 | + config.ParallelTask = value; |
| 25 | + value = GetConfigurationValue(content, ClientBufferSizeKey); |
| 26 | + if (value != -1) // otherwise we using default value |
| 27 | + config.ClientBufferSize = value; |
| 28 | + value = GetConfigurationValue(content, ChunkSizeKey); |
| 29 | + if (value != -1) // otherwise we using default value |
| 30 | + config.ChunkSize = value; |
| 31 | + value = GetConfigurationValue(content, ClientConnectAttemptsKey); |
| 32 | + if (value != -1) // otherwise we using default value |
| 33 | + config.ClientConnectAttempts = value; |
| 34 | + value = GetConfigurationValue(content, ClientConnectTimeoutKey); |
| 35 | + if (value != -1) // otherwise we using default value |
| 36 | + config.ClientConnectTimeout = value; |
| 37 | + value = GetConfigurationValue(content, ClientInactivityTimeKey); |
| 38 | + if (value != -1) // otherwise we using default value |
| 39 | + config.ClientInactivityTime = value; |
| 40 | + value = GetConfigurationValue(content, ReadTimeoutKey); |
| 41 | + if (value != -1) // otherwise we using default value |
| 42 | + config.ReadTimeout = value; |
| 43 | + value = GetConfigurationValue(content, WriteTimeoutKey); |
| 44 | + if (value != -1) // otherwise we using default value |
| 45 | + config.WriteTimeout = value; |
| 46 | + value = GetConfigurationValue(content, ClientReadAttemptsKey); |
| 47 | + if (value != -1) // otherwise we using default value |
| 48 | + config.ClientReadAttempts= value; |
| 49 | + value = GetConfigurationValue(content, ServerCloseTimeoutKey); |
| 50 | + if (value != -1) // otherwise we using default value |
| 51 | + config.ServerCloseTimeout = value; |
| 52 | + return config; |
| 53 | + } |
| 54 | + |
| 55 | + private static Int32 GetConfigurationValue(IList<String> fileContent, String key) |
| 56 | + { |
| 57 | + try |
| 58 | + { |
| 59 | + String configLine = fileContent.FirstOrDefault(line => line.ToLower().StartsWith(key.ToLower())); |
| 60 | + if (configLine == null) |
| 61 | + return -1; |
| 62 | + Int32 index = configLine.IndexOf(KeyValueSeparator, StringComparison.InvariantCulture); |
| 63 | + if (index <= 0) |
| 64 | + return -1; |
| 65 | + String value = configLine.Substring(index + 1); |
| 66 | + return Int32.Parse(value); |
| 67 | + } |
| 68 | + catch (Exception) |
| 69 | + { |
| 70 | + return -1; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private const String KeyValueSeparator = "="; |
| 75 | + private const String CommentarySymbol = "#"; |
| 76 | + |
| 77 | + private const String ParallelTaskKey = "ParallelTask"; |
| 78 | + private const String ClientBufferSizeKey = "ClientBufferSize"; |
| 79 | + private const String ChunkSizeKey = "ChunkSize"; |
| 80 | + private const String ClientConnectAttemptsKey = "ClientConnectAttempts"; |
| 81 | + private const String ClientInactivityTimeKey = "ClientInactivityTime"; |
| 82 | + private const String ClientConnectTimeoutKey = "ClientConnectTimeout"; |
| 83 | + private const String ClientReadAttemptsKey = "ClientReadAttempts"; |
| 84 | + private const String ReadTimeoutKey = "ReadTimeout"; |
| 85 | + private const String ServerCloseTimeoutKey = "ServerCloseTimeout"; |
| 86 | + private const String WriteTimeoutKey = "WriteTimeout"; |
| 87 | + } |
| 88 | +} |
0 commit comments