Skip to content

Commit e633bc3

Browse files
committed
Preparation for close #2 #3
1 parent cd84fe2 commit e633bc3

File tree

8 files changed

+102
-57
lines changed

8 files changed

+102
-57
lines changed

service_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"services": {
33
"name": "RemotePythonExecution",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"projectType": "DotnetProject",
66
"dependencies": []
77
},

src/RemotePythonExecution.Interface/IAppSettingService.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace RemotePythonExecution.Interface.IAppSettingsServiceDependency
22
{
3-
public class SourceCodeSavePaths
3+
public class SourceCodeSavePath
44
{
55
public string Windows { get; set; } = "";
66
public string Linux { get; set; } = "";

src/RemotePythonExecution.Services/AppSettingService.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using RemotePythonExecution.Interface.IAppSettingsServiceDependency;
2+
3+
namespace RemotePythonExecution.Services
4+
{
5+
public class AppSettings
6+
{
7+
public ServerSettings ServerSettings { get; set; } = new ServerSettings();
8+
public PythonPaths PythonPaths { get; set; } = new PythonPaths();
9+
public SourceCodeSavePath SourceCodeSavePath { get; set ; } = new SourceCodeSavePath();
10+
11+
}
12+
}

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Options;
45
using RemotePythonExecution.Interface;
56
using System;
67
using System.Collections.Generic;
@@ -18,7 +19,7 @@ public class RemotePythonExecutionService : BackgroundService
1819
#region Services
1920

2021
private readonly ILogger<RemotePythonExecutionService> mLogger;
21-
private readonly IAppSettingService mAppSettingService;
22+
private readonly IOptionsMonitor<AppSettings> mAppSettingsMonitor;
2223

2324
#endregion
2425

@@ -29,11 +30,6 @@ public class RemotePythonExecutionService : BackgroundService
2930
private bool mIsOutputEnded = false;
3031
private bool mIsProcessEnded = false;
3132
public Guid CurrentConnectionGuid;
32-
33-
private string InterpreterPath = "";
34-
private string WorkingDirrectoryPath = "";
35-
private string SourceCodeSavePaths = "";
36-
3733
private bool mIsDisposed;
3834

3935
#endregion
@@ -43,18 +39,20 @@ public class RemotePythonExecutionService : BackgroundService
4339
public RemotePythonExecutionService(IServiceProvider serviceProvider)
4440
{
4541
mLogger = serviceProvider.GetRequiredService<ILogger<RemotePythonExecutionService>>();
46-
mAppSettingService = serviceProvider.GetRequiredService<IAppSettingService>();
42+
mAppSettingsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<AppSettings>>();
4743

48-
string ip =mAppSettingService.ServerSettings.Ip;
49-
int port =mAppSettingService.ServerSettings.Port;
44+
45+
string ip = mAppSettingsMonitor.CurrentValue.ServerSettings.Ip;
46+
int port = mAppSettingsMonitor.CurrentValue.ServerSettings.Port;
5047

5148
mTcpServer = new WatsonTcpServer(ip, port);
49+
mAppSettingsMonitor.OnChange(OnChangeSettings);
5250

5351
Subscribe();
5452

5553
mTcpServer.Start();
5654

57-
SetPath();
55+
SetPath(mAppSettingsMonitor.CurrentValue);
5856

5957
mLogger.LogInformation("Server runing on {ip}:{port}", ip, port);
6058
}
@@ -82,6 +80,10 @@ private void UnSubscribe()
8280
#endregion
8381

8482
#region Events
83+
private void OnChangeSettings(AppSettings settings, string arg2)
84+
{
85+
SetPath(settings);
86+
}
8587

8688
private void ClientConnected(object sender, ConnectionEventArgs e)
8789
{
@@ -167,13 +169,13 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
167169
}
168170
}
169171

170-
private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
172+
private async void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
171173
{
172-
if (!string.IsNullOrEmpty(e.Data))
174+
if (!(e.Data == null))
173175
{
174176
try
175177
{
176-
mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
178+
await mTcpServer.SendAsync(CurrentConnectionGuid, e.Data, start: 0);
177179
mLogger.LogDebug("{data}", e.Data);
178180
mIsOutputEnded = false;
179181
}
@@ -242,35 +244,91 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
242244

243245
#endregion
244246

247+
248+
#region Private fields
249+
250+
private string mInterpreterPath = string.Empty;
251+
private string InterpreterPath
252+
{
253+
get { return mInterpreterPath; }
254+
set
255+
{
256+
if (string.IsNullOrEmpty(value))
257+
return;
258+
259+
if (value.Equals(mInterpreterPath))
260+
return;
261+
262+
mInterpreterPath = value;
263+
mLogger.LogDebug("New path for {name} register with values {value}", nameof(InterpreterPath), InterpreterPath);
264+
}
265+
}
266+
267+
private string mWorkingDirrectoryPath = string.Empty;
268+
private string WorkingDirrectoryPath
269+
{
270+
get { return mWorkingDirrectoryPath; }
271+
set
272+
{
273+
if (string.IsNullOrEmpty(value))
274+
return;
275+
276+
if (value.Equals(mWorkingDirrectoryPath))
277+
return;
278+
279+
mWorkingDirrectoryPath = value;
280+
mLogger.LogDebug("New path for {name} register with values {value}", nameof(WorkingDirrectoryPath), WorkingDirrectoryPath);
281+
}
282+
}
283+
284+
private string mSourceCodeSavePath = string.Empty;
285+
private string SourceCodeSavePath
286+
{
287+
get { return mSourceCodeSavePath; }
288+
set
289+
{
290+
if (string.IsNullOrEmpty(value))
291+
return;
292+
293+
if (value.Equals(mSourceCodeSavePath))
294+
return;
295+
296+
mSourceCodeSavePath = value;
297+
mLogger.LogDebug("New path for {name} register with values {value}", nameof(SourceCodeSavePath), SourceCodeSavePath);
298+
}
299+
300+
}
301+
302+
#endregion
245303
#region Private methods
246304

247-
private void SetPath()
305+
private void SetPath(AppSettings appSettings)
248306
{
249307
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
250308
{
251-
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Windows;
252-
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Windows;
253-
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Windows;
309+
SourceCodeSavePath = appSettings.SourceCodeSavePath.Windows;
310+
InterpreterPath = appSettings.PythonPaths.InterpreterPath.Windows;
311+
WorkingDirrectoryPath = appSettings.PythonPaths.WorkingDirrectoryPath.Windows;
254312
return;
255313
}
256314

257-
SourceCodeSavePaths = mAppSettingService.SourceCodeSavePaths.Linux;
258-
InterpreterPath = mAppSettingService.PythonPaths.InterpreterPath.Linux;
259-
WorkingDirrectoryPath = mAppSettingService.PythonPaths.WorkingDirrectoryPath.Linux;
315+
SourceCodeSavePath = appSettings.SourceCodeSavePath.Linux;
316+
InterpreterPath = appSettings.PythonPaths.InterpreterPath.Linux;
317+
WorkingDirrectoryPath = appSettings.PythonPaths.WorkingDirrectoryPath.Linux;
260318
}
261319

262320
private void SaveCodeAndStartProcess(string code, bool withDebug)
263321
{
264-
File.WriteAllText(SourceCodeSavePaths, code);
322+
File.WriteAllText(SourceCodeSavePath, code);
265323
Task.Run(() => StartProcess(withDebug: withDebug));
266324
}
267325

268326
private void StartProcess(bool withDebug = false)
269327
{
270-
string arg = string.Format($"-u -m {Path.GetFileNameWithoutExtension(SourceCodeSavePaths)}");
328+
string arg = string.Format($"-u -m {Path.GetFileNameWithoutExtension(SourceCodeSavePath)}");
271329

272330
if (withDebug)
273-
arg = string.Format($"-u -m pdb {SourceCodeSavePaths}");
331+
arg = string.Format($"-u -m pdb {SourceCodeSavePath}");
274332

275333
ProcessStartInfo proccesInfo = new()
276334
{

src/RemotePythonExecution/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ static async Task Main(string[] args)
2121
.ConfigureAppConfiguration((context, config) =>
2222
{
2323
config.Sources.Clear();
24-
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
24+
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
2525
})
2626

2727
.ConfigureServices((context, services) =>
2828
{
2929
services.AddAdamDefaultArgumentsParser(args);
3030

31-
AppSettingService options = new();
32-
context.Configuration.GetRequiredSection("AppSettingsOptions").Bind(options);
33-
services.AddSingleton<IAppSettingService>(options);
34-
31+
AppSettings options = new();
32+
services.Configure<AppSettings>(context.Configuration.GetRequiredSection(nameof(AppSettings)));
33+
3534
services.AddLogging(loggingBuilder =>
3635
{
3736
Logger logger = new LoggerConfiguration()

src/RemotePythonExecution/appsettings.json

Lines changed: 3 additions & 3 deletions
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": "Information",
5+
"Default": "Debug",
66
"Override": {
77
"Microsoft": "Warning",
88
"System": "Warning"
@@ -38,7 +38,7 @@
3838
]
3939
},
4040

41-
"AppSettingsOptions": {
41+
"AppSettings": {
4242
"PythonPaths": {
4343
"InterpreterPath": {
4444
"Windows": "C:\\Users\\Professional\\Downloads\\python-3.13.0-embed-amd64\\python.exe",
@@ -51,7 +51,7 @@
5151
}
5252
},
5353

54-
"SourceCodeSavePaths": {
54+
"SourceCodeSavePath": {
5555
"Windows": "C:\\Users\\Professional\\Downloads\\python-3.13.0-embed-amd64\\test2.py",
5656
"Linux": "/home/adam/adam/remote_python.py"
5757
},

0 commit comments

Comments
 (0)