Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 9e5ac5f

Browse files
committed
Was copied TcpServerConfigBuilder from console project, read config
1 parent 93e6066 commit 9e5ac5f

4 files changed

Lines changed: 135 additions & 50 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

GUI/MossbauerLab.TinyTcpServer.MnGUI/MossbauerLab.TinyTcpServer.MnGUI/MossbauerLab.TinyTcpServer.MnGUI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="Helpers\TcpServerConfigBuilder.cs" />
5556
<Compile Include="LogUtils\RichTextBoxAppender.cs" />
5657
<Compile Include="Factories\ServerFactory.cs" />
5758
<Compile Include="View\Helpers\ServerConfigInfoHelper.cs" />

GUI/MossbauerLab.TinyTcpServer.MnGUI/MossbauerLab.TinyTcpServer.MnGUI/View/Forms/MainForm.Designer.cs

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GUI/MossbauerLab.TinyTcpServer.MnGUI/MossbauerLab.TinyTcpServer.MnGUI/View/Forms/MainForm.cs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
using MossbauerLab.TinyTcpServer.Core.Client;
1313
using MossbauerLab.TinyTcpServer.Core.Server;
1414
using MossbauerLab.TinyTcpServer.MnGUI.Factories;
15+
using MossbauerLab.TinyTcpServer.MnGUI.Helpers;
1516
using MossbauerLab.TinyTcpServer.MnGUI.LogUtils;
16-
using MossbauerLab.TinyTcpServer.MnGUI.View.Helpers;
1717
using MossbauerLab.TinyTcpServer.MnGUI.View.Utils;
1818

1919
namespace MossbauerLab.TinyTcpServer.MnGUI.View.Forms
@@ -28,7 +28,7 @@ public MainForm()
2828
_stopButton.Click += (sender, args) => Stop();
2929
_restartButton.Click += (sender, args) => Restart();
3030
_serverScriptButton.Click += OnChooseScriptFileButtonClick;
31-
_serverSettingsButton.Click += OnChooseSettingsFileButtonClick;
31+
_serverConfigButton.Click += OnChooseConfigFileButtonClick;
3232
_logLevelComboBox.SelectedIndexChanged += (sender, args) => ApplyLogLevel();
3333
}
3434

@@ -45,16 +45,13 @@ private void FillControls()
4545
// load last IP address and TCP Port settings
4646
if (File.Exists(ConfigFile))
4747
{
48-
String[] filelines = File.ReadAllLines(ConfigFile);
49-
ApplySettings(GetOptions(filelines));
50-
}
51-
else
52-
{
53-
if (_ipAddressComboBox.Items.Count > 0)
54-
_ipAddressComboBox.SelectedIndex = 0;
55-
_portTextBox.Text = DefaultTcpPort.ToString();
48+
_configFile = ConfigFile;
49+
DisplayConfig();
5650
}
5751

52+
if (_ipAddressComboBox.Items.Count > 0)
53+
_ipAddressComboBox.SelectedIndex = 0;
54+
_portTextBox.Text = DefaultTcpPort.ToString();
5855

5956
// init logger + fill log level
6057
XmlConfigurator.Configure();
@@ -65,31 +62,26 @@ private void FillControls()
6562
_logLevelComboBox.Items.Add(level.Value);
6663
_logLevelComboBox.SelectedIndex = 5;
6764

68-
// fill server config
69-
IList<String> configStrings = ServerConfigInfoHelper.GetConfigStrings(_serverConfig);
70-
foreach (String configString in configStrings)
71-
_serverParametersView.Items.Add(configString);
72-
// update controls state
7365
UpdateControlsState();
7466
}
7567

76-
private IDictionary<String, String> GetOptions(String[] lines)
68+
private void DisplayConfig()
7769
{
78-
IDictionary<String, String> options = new Dictionary<String, String>();
79-
// todo: parse options ....
80-
return options;
70+
String[] lines = File.ReadAllLines(_configFile).Where(line => !line.Trim().StartsWith("#")).ToArray();
71+
_serverParametersView.Items.Clear();
72+
foreach (String line in lines)
73+
_serverParametersView.Items.Add(line);
74+
_serverConfigBox.Text = Path.GetFileName(_configFile);
8175
}
8276

83-
private void ApplySettings(IDictionary<String, String> options)
84-
{
85-
86-
}
8777

8878
private void Start()
8979
{
9080
UInt16 port = Convert.ToUInt16(_portTextBox.Text);
9181
if (_server == null)
9282
{
83+
if (!String.IsNullOrEmpty(_configFile))
84+
_serverConfig = TcpServerConfigBuilder.Build(_configFile);
9385
if (_ipAddressComboBox.SelectedIndex >= 0 && _portTextBox.Text != null && !String.IsNullOrEmpty(_scriptFile))
9486
_server = ServerFactory.Create(_ipAddressComboBox.Items[_ipAddressComboBox.SelectedIndex].ToString(), port, _scriptFile, _logger, _serverConfig);
9587
else
@@ -99,7 +91,11 @@ private void Start()
9991
}
10092
_server.Start();
10193
}
102-
else _server.Start(_ipAddressComboBox.Items[_ipAddressComboBox.SelectedIndex].ToString(), port);
94+
else
95+
{
96+
// todo umv: if server config changed => re-create server
97+
_server.Start(_ipAddressComboBox.Items[_ipAddressComboBox.SelectedIndex].ToString(), port);
98+
}
10399

104100
if (_timers[0] == null)
105101
{
@@ -109,7 +105,7 @@ private void Start()
109105
else _timers[0].Change(500, 500);
110106
}
111107

112-
public void Stop()
108+
private void Stop()
113109
{
114110
_server.Stop(false);
115111
if (_timers[0] != null)
@@ -119,7 +115,7 @@ public void Stop()
119115
UpdateControlsState();
120116
}
121117

122-
public void Restart()
118+
private void Restart()
123119
{
124120
Stop();
125121
Start();
@@ -136,15 +132,15 @@ private void OnChooseScriptFileButtonClick(Object sender, EventArgs args)
136132
}
137133
}
138134

139-
private void OnChooseSettingsFileButtonClick(Object sender, EventArgs args)
135+
private void OnChooseConfigFileButtonClick(Object sender, EventArgs args)
140136
{
141137
OpenFileDialog openScriptFile = new OpenFileDialog();
142-
String file = openScriptFile.Run(@" Text files (*.txt)|*.txt | Config files (*.conf) |*.conf | Config files (*.cfg) |*.cfg | Any file (*.*)|*.*",
138+
String file = openScriptFile.Run(@" Text files (*.txt)|*.txt|Config files (*.conf)|*.conf|Config files (*.cfg)|*.cfg|Any file (*.*)|*.*",
143139
Path.GetFullPath("."), @"Choose Server settings file", 0);
144140
if (file != String.Empty)
145141
{
146-
_settingsFile = file;
147-
_serverSettingsBox.Text = Path.GetFileName(_settingsFile);
142+
_configFile = file;
143+
DisplayConfig();
148144
}
149145
}
150146

@@ -215,8 +211,8 @@ private void ApplyLogLevel()
215211
private ILog _logger;
216212
private ITcpServer _server;
217213
private String _scriptFile;
218-
private String _settingsFile;
219-
private readonly TcpServerConfig _serverConfig = new TcpServerConfig();
214+
private String _configFile;
215+
private TcpServerConfig _serverConfig = new TcpServerConfig();
220216
private RichTextBoxAppender _richTextBoxAppender;
221217
private readonly System.Threading.Timer[] _timers = new System.Threading.Timer[1];
222218
}

0 commit comments

Comments
 (0)