Skip to content

Commit 67f1bf1

Browse files
committed
Close #2 #3
1 parent e633bc3 commit 67f1bf1

File tree

9 files changed

+122
-41
lines changed

9 files changed

+122
-41
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
1+
namespace RemotePythonExecution.Interface.AppSettingsDependency
22
{
33
public class InterpreterPath
44
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
1+
namespace RemotePythonExecution.Interface.AppSettingsDependency
22
{
33
public class PythonPaths
44
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RemotePythonExecution.Interface.AppSettingsDependency
2+
{
3+
public class ServerSettings
4+
{
5+
public string Ip { get; set; } = "0.0.0.0";
6+
public int Port { get; set; } = 19000;
7+
}
8+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
1+
namespace RemotePythonExecution.Interface.AppSettingsDependency
22
{
33
public class SourceCodeSavePath
44
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
1+
namespace RemotePythonExecution.Interface.AppSettingsDependency
22
{
33
public class WorkingDirrectoryPath
44
{

src/RemotePythonExecution.Interface/IAppSettingsServiceDependency/ServerSettings.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/RemotePythonExecution.Services/AppSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using RemotePythonExecution.Interface.IAppSettingsServiceDependency;
1+
using RemotePythonExecution.Interface.AppSettingsDependency;
22

33
namespace RemotePythonExecution.Services
44
{

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.IO;
10+
using System.Net.Sockets;
1011
using System.Runtime.InteropServices;
1112
using System.Threading;
1213
using System.Threading.Tasks;
@@ -25,13 +26,17 @@ public class RemotePythonExecutionService : BackgroundService
2526

2627
#region Var
2728

28-
private readonly WatsonTcpServer mTcpServer;
29+
private WatsonTcpServer mTcpServer;
2930
private Process mProcess;
3031
private bool mIsOutputEnded = false;
3132
private bool mIsProcessEnded = false;
3233
public Guid CurrentConnectionGuid;
3334
private bool mIsDisposed;
3435

36+
private string mInterpreterPath = string.Empty;
37+
private string mWorkingDirrectoryPath = string.Empty;
38+
private string mSourceCodeSavePath = string.Empty;
39+
3540
#endregion
3641

3742
#region ~
@@ -41,20 +46,20 @@ public RemotePythonExecutionService(IServiceProvider serviceProvider)
4146
mLogger = serviceProvider.GetRequiredService<ILogger<RemotePythonExecutionService>>();
4247
mAppSettingsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<AppSettings>>();
4348

49+
Ip = mAppSettingsMonitor.CurrentValue.ServerSettings.Ip;
50+
Port = mAppSettingsMonitor.CurrentValue.ServerSettings.Port;
4451

45-
string ip = mAppSettingsMonitor.CurrentValue.ServerSettings.Ip;
46-
int port = mAppSettingsMonitor.CurrentValue.ServerSettings.Port;
4752

48-
mTcpServer = new WatsonTcpServer(ip, port);
53+
SetPath(mAppSettingsMonitor.CurrentValue);
54+
//SetServerAddress(mAppSettingsMonitor.CurrentValue);
55+
56+
mTcpServer = new WatsonTcpServer(Ip, Port);
4957
mAppSettingsMonitor.OnChange(OnChangeSettings);
5058

5159
Subscribe();
52-
5360
mTcpServer.Start();
61+
mLogger.LogInformation("Server runing on {ip}:{port}", Ip, Port);
5462

55-
SetPath(mAppSettingsMonitor.CurrentValue);
56-
57-
mLogger.LogInformation("Server runing on {ip}:{port}", ip, port);
5863
}
5964

6065
#endregion
@@ -67,22 +72,27 @@ private void Subscribe()
6772
mTcpServer.Events.ClientDisconnected += ClientDisconnected;
6873
mTcpServer.Events.MessageReceived += MessageReceived;
6974
mTcpServer.Events.ExceptionEncountered += ExceptionEncountered;
75+
//mTcpServer.Events.ServerStopped += ServerStopped;
7076
}
7177

7278
private void UnSubscribe()
7379
{
7480
mTcpServer.Events.ClientConnected -= ClientConnected;
7581
mTcpServer.Events.ClientDisconnected -= ClientDisconnected;
7682
mTcpServer.Events.MessageReceived -= MessageReceived;
77-
mTcpServer.Events.ExceptionEncountered -= ExceptionEncountered;
83+
mTcpServer.Events.ExceptionEncountered -= ExceptionEncountered;
84+
//mTcpServer.Events.ServerStopped -= ServerStopped;
85+
7886
}
7987

8088
#endregion
8189

8290
#region Events
91+
8392
private void OnChangeSettings(AppSettings settings, string arg2)
8493
{
8594
SetPath(settings);
95+
SetServerAddress(settings);
8696
}
8797

8898
private void ClientConnected(object sender, ConnectionEventArgs e)
@@ -107,9 +117,18 @@ private void ExceptionEncountered(object sender, ExceptionEventArgs e)
107117
if (e.Exception is IOException)
108118
return;
109119

120+
if (e.Exception is SocketException)
121+
return;
122+
110123
mLogger.LogError("Error happened {errorMessage}", e.Exception);
111124
}
112125

126+
private void ServerStopped(object sender, EventArgs e)
127+
{
128+
mIsOutputEnded = true;
129+
mIsProcessEnded = true;
130+
}
131+
113132
private void MessageReceived(object sender, MessageReceivedEventArgs e)
114133
{
115134
Dictionary<string, object> metadata = e.Metadata;
@@ -179,6 +198,10 @@ private async void ProcessOutputDataReceived(object sender, DataReceivedEventArg
179198
mLogger.LogDebug("{data}", e.Data);
180199
mIsOutputEnded = false;
181200
}
201+
catch (TaskCanceledException)
202+
{
203+
mLogger.LogError("Task was canceled");
204+
}
182205
catch (Exception exp)
183206
{
184207
mLogger.LogError("Catch happened {exp}", exp);
@@ -244,10 +267,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
244267

245268
#endregion
246269

247-
248270
#region Private fields
249271

250-
private string mInterpreterPath = string.Empty;
251272
private string InterpreterPath
252273
{
253274
get { return mInterpreterPath; }
@@ -264,7 +285,6 @@ private string InterpreterPath
264285
}
265286
}
266287

267-
private string mWorkingDirrectoryPath = string.Empty;
268288
private string WorkingDirrectoryPath
269289
{
270290
get { return mWorkingDirrectoryPath; }
@@ -280,8 +300,7 @@ private string WorkingDirrectoryPath
280300
mLogger.LogDebug("New path for {name} register with values {value}", nameof(WorkingDirrectoryPath), WorkingDirrectoryPath);
281301
}
282302
}
283-
284-
private string mSourceCodeSavePath = string.Empty;
303+
285304
private string SourceCodeSavePath
286305
{
287306
get { return mSourceCodeSavePath; }
@@ -299,9 +318,76 @@ private string SourceCodeSavePath
299318

300319
}
301320

321+
private string mIp = string.Empty;
322+
private string Ip
323+
{
324+
get { return mIp; }
325+
set
326+
{
327+
if (string.IsNullOrEmpty(value))
328+
{
329+
mIp = "0.0.0.0";
330+
return;
331+
}
332+
333+
if (value.Equals(mIp))
334+
return;
335+
336+
mIp = value;
337+
mLogger.LogDebug("New ip for server register with values {value}", Ip);
338+
339+
OnServerAddressChange();
340+
}
341+
}
342+
343+
344+
private int mPort;
345+
346+
private int Port
347+
{
348+
get { return mPort; }
349+
set
350+
{
351+
if (value.Equals(mPort))
352+
return;
353+
354+
mPort = value;
355+
mLogger.LogDebug("New port for server register with values {value}", Port);
356+
357+
OnServerAddressChange();
358+
}
359+
}
360+
302361
#endregion
362+
303363
#region Private methods
304364

365+
private void OnServerAddressChange()
366+
{
367+
if (mTcpServer == null)
368+
return;
369+
370+
if(mTcpServer.IsListening)
371+
{
372+
if(mTcpServer.Connections != 0)
373+
{
374+
mIsOutputEnded = true;
375+
mIsProcessEnded = true;
376+
//mTcpServer.DisconnectClientsAsync();
377+
}
378+
379+
380+
mTcpServer.Stop();
381+
UnSubscribe();
382+
//mTcpServer?.Dispose();
383+
}
384+
385+
mTcpServer = new WatsonTcpServer(Ip, Port);
386+
Subscribe();
387+
mTcpServer.Start();
388+
mLogger.LogInformation("Server runing on {ip}:{port}", Ip, Port);
389+
}
390+
305391
private void SetPath(AppSettings appSettings)
306392
{
307393
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -317,6 +403,12 @@ private void SetPath(AppSettings appSettings)
317403
WorkingDirrectoryPath = appSettings.PythonPaths.WorkingDirrectoryPath.Linux;
318404
}
319405

406+
private void SetServerAddress(AppSettings appSettings)
407+
{
408+
Ip = appSettings.ServerSettings.Ip;
409+
Port = appSettings.ServerSettings.Port;
410+
}
411+
320412
private void SaveCodeAndStartProcess(string code, bool withDebug)
321413
{
322414
File.WriteAllText(SourceCodeSavePath, code);

src/RemotePythonExecution/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
]
3939
},
4040

41+
/* All settings supported to change without restarting the application (hot reload) */
4142
"AppSettings": {
4243
"PythonPaths": {
4344
"InterpreterPath": {
@@ -58,8 +59,10 @@
5859

5960
"ServerSettings": {
6061
/* Specify the ip interface on which the server will run. To listen to all interfaces, specify 0.0.0.0 or leave the field empty. */
62+
/* A change during code execution will result in disconnection all clients*/
6163
"Ip": "",
6264
/* Specify the port on which the server will be running. The default port is 19000 */
65+
/* A change during code execution will result in disconnection all clients*/
6366
"Port": 19000
6467
}
6568
}

0 commit comments

Comments
 (0)