1212using MossbauerLab . TinyTcpServer . Core . Client ;
1313using MossbauerLab . TinyTcpServer . Core . Server ;
1414using MossbauerLab . TinyTcpServer . MnGUI . Factories ;
15+ using MossbauerLab . TinyTcpServer . MnGUI . Helpers ;
1516using MossbauerLab . TinyTcpServer . MnGUI . LogUtils ;
16- using MossbauerLab . TinyTcpServer . MnGUI . View . Helpers ;
1717using MossbauerLab . TinyTcpServer . MnGUI . View . Utils ;
1818
1919namespace 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