Skip to content

Commit 3f2e657

Browse files
committed
Close #1
1 parent aec41df commit 3f2e657

File tree

6 files changed

+120
-72
lines changed

6 files changed

+120
-72
lines changed

src/RemotePythonExecution.Interface/IAppSettingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace RemotePythonExecution.Interface
44
{
55
public interface IAppSettingService
66
{
7+
public ServerSettings ServerSettings { get; set; }
78
public PythonPaths PythonPaths { get; set; }
89
public SourceCodeSavePaths SourceCodeSavePaths { get; set; }
910
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
2+
{
3+
public class ServerSettings
4+
{
5+
private string mIp = "0.0.0.0";
6+
public string Ip
7+
{
8+
get { return mIp; }
9+
set
10+
{
11+
if (string.IsNullOrEmpty(value))
12+
{
13+
mIp = "0.0.0.0";
14+
return;
15+
}
16+
17+
mIp = value;
18+
}
19+
}
20+
public int Port { get; set; } = 19000;
21+
}
22+
}

src/RemotePythonExecution.Interface/RemotePythonExecution.Interface.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,4 @@
2323
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
2424
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
2525
</ItemGroup>
26-
27-
<ItemGroup>
28-
<Folder Include="RemotePythonExecutionServiceDependency\" />
29-
</ItemGroup>
3026
</Project>

src/RemotePythonExecution.Services/AppSettingService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace RemotePythonExecution.Services
55
{
66
public class AppSettingService : IAppSettingService
77
{
8+
public ServerSettings ServerSettings { get; set; } = new ServerSettings();
89
public PythonPaths PythonPaths { get; set; } = new PythonPaths();
910
public SourceCodeSavePaths SourceCodeSavePaths { get; set ; } = new SourceCodeSavePaths();
11+
1012
}
1113
}

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using RemotePythonExecution.Interface;
5-
using RemotePythonExecution.Interface.RemotePythonExecutionServiceDependency.JsonModel;
65
using System;
76
using System.Collections.Generic;
87
using System.Diagnostics;
98
using System.IO;
10-
using System.Linq;
119
using System.Runtime.InteropServices;
12-
using System.Text.Json;
1310
using System.Threading;
1411
using System.Threading.Tasks;
1512
using WatsonTcp;
@@ -37,6 +34,8 @@ public class RemotePythonExecutionService : BackgroundService
3734
private string WorkingDirrectoryPath = "";
3835
private string SourceCodeSavePaths = "";
3936

37+
private bool mIsDisposed;
38+
4039
#endregion
4140

4241
#region ~
@@ -46,32 +45,22 @@ public RemotePythonExecutionService(IServiceProvider serviceProvider)
4645
mLogger = serviceProvider.GetRequiredService<ILogger<RemotePythonExecutionService>>();
4746
mAppSettingService = serviceProvider.GetRequiredService<IAppSettingService>();
4847

49-
mTcpServer = new WatsonTcpServer("0.0.0.0", 19000);
48+
string ip =mAppSettingService.ServerSettings.Ip;
49+
int port =mAppSettingService.ServerSettings.Port;
50+
51+
mTcpServer = new WatsonTcpServer(ip, port);
5052

5153
Subscribe();
5254

5355
mTcpServer.Start();
5456

55-
mLogger.LogInformation("Load RemotePythonExecutionService ~");
56-
5757
SetPath();
58-
}
5958

60-
private void SetPath()
61-
{
62-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
63-
{
64-
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Windows;
65-
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Windows;
66-
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Windows;
67-
return;
68-
}
69-
70-
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Linux;
71-
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Linux;
72-
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Linux;
59+
mLogger.LogInformation("Server runing on {ip}:{port}", ip, port);
7360
}
7461

62+
#endregion
63+
7564
#region Subscribe/Unsubscribe
7665

7766
private void Subscribe()
@@ -161,18 +150,60 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
161150
}
162151
}
163152

153+
private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
154+
{
155+
if (!string.IsNullOrEmpty(e.Data))
156+
{
157+
try
158+
{
159+
mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
160+
mIsOutputEnded = false;
161+
}
162+
catch (Exception exp)
163+
{
164+
mLogger.LogError("Catch happened {exp}", exp);
165+
mIsOutputEnded = true;
166+
}
167+
}
168+
}
169+
170+
private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
171+
{
172+
if (!string.IsNullOrEmpty(e.Data))
173+
{
174+
try
175+
{
176+
mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
177+
mLogger.LogDebug("{data}", e.Data);
178+
mIsOutputEnded = false;
179+
}
180+
catch (Exception exp)
181+
{
182+
mLogger.LogError("Catch happened {exp}", exp);
183+
mIsOutputEnded = true;
184+
}
185+
}
186+
else
187+
{
188+
mLogger.LogDebug("Output ended happened");
189+
mIsOutputEnded = true;
190+
}
191+
}
192+
193+
private void ProcessExited(object sender, EventArgs e)
194+
{
195+
mLogger.LogDebug("Process ended happened");
196+
mIsProcessEnded = true;
197+
}
198+
164199
#endregion
165200

201+
#region Public methods
202+
166203
public override void Dispose()
167204
{
168-
UnSubscribe();
169-
mProcess?.Close();
170-
mProcess?.Dispose();
171-
172-
mTcpServer.DisconnectClientsAsync();
173-
mTcpServer.Stop();
174-
mTcpServer.Dispose();
175-
mLogger.LogInformation("Service stop and dispose");
205+
Dispose(true);
206+
GC.SuppressFinalize(this);
176207

177208
base.Dispose();
178209
}
@@ -201,7 +232,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
201232
mLogger.LogError("Error when procces close {error}", exception.Message);
202233
}
203234

204-
205235
if (mTcpServer.IsClientConnected(CurrentConnectionGuid))
206236
await mTcpServer.DisconnectClientAsync(CurrentConnectionGuid, MessageStatus.Removed, true, stoppingToken);
207237

@@ -214,6 +244,21 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
214244

215245
#region Private methods
216246

247+
private void SetPath()
248+
{
249+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
250+
{
251+
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Windows;
252+
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Windows;
253+
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Windows;
254+
return;
255+
}
256+
257+
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Linux;
258+
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Linux;
259+
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Linux;
260+
}
261+
217262
private void SaveCodeAndStartProcess(string code, bool withDebug)
218263
{
219264
File.WriteAllText(SourceCodeSavePaths, code);
@@ -238,13 +283,13 @@ private void StartProcess(bool withDebug = false)
238283
RedirectStandardError = true,
239284
RedirectStandardInput = true,
240285
};
286+
241287
mProcess = new()
242288
{
243289
StartInfo = proccesInfo,
244290
EnableRaisingEvents = true,
245291
};
246292

247-
248293
mProcess.Exited += ProcessExited;
249294
mProcess.OutputDataReceived += ProcessOutputDataReceived;
250295
mProcess.ErrorDataReceived += ProcessErrorDataReceived;
@@ -254,49 +299,24 @@ private void StartProcess(bool withDebug = false)
254299
mProcess.WaitForExit();
255300
}
256301

257-
private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
302+
protected virtual void Dispose(bool disposing)
258303
{
259-
if (!string.IsNullOrEmpty(e.Data))
304+
if (!mIsDisposed)
260305
{
261-
try
262-
{
263-
mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
264-
mIsOutputEnded = false;
265-
}
266-
catch
267-
{
268-
mIsOutputEnded = true;
269-
}
270-
}
271-
}
306+
mIsDisposed = true;
272307

273-
private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
274-
{
275-
if (!string.IsNullOrEmpty(e.Data))
276-
{
277-
try
278-
{
279-
mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
280-
mLogger.LogDebug("{data}", e.Data);
281-
mIsOutputEnded = false;
282-
}
283-
catch(Exception exp)
308+
if (disposing)
284309
{
285-
mLogger.LogDebug("Catch happened {exp}", exp);
286-
mIsOutputEnded = true;
310+
UnSubscribe();
311+
mProcess?.Close();
312+
mProcess?.Dispose();
313+
314+
mTcpServer.DisconnectClientsAsync();
315+
mTcpServer.Stop();
316+
mTcpServer.Dispose();
317+
mLogger.LogInformation("Service stop and dispose");
287318
}
288319
}
289-
else
290-
{
291-
mLogger.LogDebug("Output ended happened");
292-
mIsOutputEnded = true;
293-
}
294-
}
295-
296-
private void ProcessExited(object sender, EventArgs e)
297-
{
298-
mLogger.LogDebug("Process ended happened");
299-
mIsProcessEnded = true;
300320
}
301321

302322
#endregion

src/RemotePythonExecution/appsettings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Serilog": {
33
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
44
"MinimumLevel": {
5-
"Default": "Verbose",
5+
"Default": "Information",
66
"Override": {
77
"Microsoft": "Warning",
88
"System": "Warning"
@@ -54,6 +54,13 @@
5454
"SourceCodeSavePaths": {
5555
"Windows": "C:\\Users\\Professional\\Downloads\\python-3.13.0-embed-amd64\\test2.py",
5656
"Linux": "/home/adam/adam/remote_python.py"
57+
},
58+
59+
"ServerSettings": {
60+
/* 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. */
61+
"Ip": "",
62+
/* Specify the port on which the server will be running. The default port is 19000 */
63+
"Port": 19000
5764
}
5865
}
5966
}

0 commit comments

Comments
 (0)