Browse Source

Update python runtime for MQTTnet.Server.

release/3.x.x
Christian Kratky 4 years ago
parent
commit
c89918a890
7 changed files with 34 additions and 38 deletions
  1. +2
    -2
      Source/MQTTnet.Server/MQTTnet.Server.csproj
  2. +5
    -5
      Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs
  3. +4
    -4
      Source/MQTTnet.Server/Scripting/PythonIOStream.cs
  4. +17
    -17
      Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs
  5. +3
    -3
      Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs
  6. +2
    -2
      Source/MQTTnet.Server/Web/Startup.cs
  7. +1
    -5
      Source/MQTTnet.Server/appsettings.json

+ 2
- 2
Source/MQTTnet.Server/MQTTnet.Server.csproj View File

@@ -43,8 +43,8 @@
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
<PackageReference Include="IronPython" Version="2.7.9" />
<PackageReference Include="IronPython.StdLib" Version="2.7.9" />
<PackageReference Include="IronPython" Version="2.7.10-candidate1" />
<PackageReference Include="IronPython.StdLib" Version="2.7.10-candidate1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" /> <PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.3.1" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.3.1" />


+ 5
- 5
Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs View File

@@ -1,14 +1,14 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Logging;


namespace MQTTnet.Server.Scripting.DataSharing namespace MQTTnet.Server.Scripting.DataSharing
{ {
public class DataSharingService public class DataSharingService
{ {
private readonly Dictionary<string, object> _storage = new Dictionary<string, object>();
private readonly PythonScriptHostService _pythonScriptHostService;
private readonly ILogger<DataSharingService> _logger;
readonly Dictionary<string, object> _storage = new Dictionary<string, object>();
readonly PythonScriptHostService _pythonScriptHostService;
readonly ILogger<DataSharingService> _logger;


public DataSharingService(PythonScriptHostService pythonScriptHostService, ILogger<DataSharingService> logger) public DataSharingService(PythonScriptHostService pythonScriptHostService, ILogger<DataSharingService> logger)
{ {


+ 4
- 4
Source/MQTTnet.Server/Scripting/PythonIOStream.cs View File

@@ -1,14 +1,14 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Microsoft.Extensions.Logging;


namespace MQTTnet.Server.Scripting namespace MQTTnet.Server.Scripting
{ {
public class PythonIOStream : Stream public class PythonIOStream : Stream
{ {
private readonly ILogger _logger;
private readonly Encoding _encoder = Encoding.UTF8;
readonly ILogger _logger;
readonly Encoding _encoder = Encoding.UTF8;


public PythonIOStream(ILogger<PythonIOStream> logger) public PythonIOStream(ILogger<PythonIOStream> logger)
{ {


+ 17
- 17
Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs View File

@@ -1,4 +1,8 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using MQTTnet.Server.Configuration;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Dynamic; using System.Dynamic;
using System.IO; using System.IO;
@@ -6,22 +10,18 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using MQTTnet.Server.Configuration;


namespace MQTTnet.Server.Scripting namespace MQTTnet.Server.Scripting
{ {
public class PythonScriptHostService public class PythonScriptHostService
{ {
private readonly IDictionary<string, object> _proxyObjects = new ExpandoObject();
private readonly List<PythonScriptInstance> _scriptInstances = new List<PythonScriptInstance>();
private readonly string _scriptsPath;
private readonly ScriptingSettingsModel _scriptingSettings;
private readonly ILogger<PythonScriptHostService> _logger;
private readonly ScriptEngine _scriptEngine;
readonly IDictionary<string, object> _proxyObjects = new ExpandoObject();
readonly List<PythonScriptInstance> _scriptInstances = new List<PythonScriptInstance>();
readonly string _scriptsPath;
readonly ScriptingSettingsModel _scriptingSettings;
readonly ILogger<PythonScriptHostService> _logger;
readonly ScriptEngine _scriptEngine;
public PythonScriptHostService(ScriptingSettingsModel scriptingSettings, PythonIOStream pythonIOStream, ILogger<PythonScriptHostService> logger) public PythonScriptHostService(ScriptingSettingsModel scriptingSettings, PythonIOStream pythonIOStream, ILogger<PythonScriptHostService> logger)
{ {
_scriptingSettings = scriptingSettings ?? throw new ArgumentNullException(nameof(scriptingSettings)); _scriptingSettings = scriptingSettings ?? throw new ArgumentNullException(nameof(scriptingSettings));
@@ -139,7 +139,7 @@ namespace MQTTnet.Server.Scripting
} }
} }


private async Task TryInitializeScriptAsync(string path)
async Task TryInitializeScriptAsync(string path)
{ {
var uid = new FileInfo(path).Name.Replace(".py", string.Empty, StringComparison.OrdinalIgnoreCase); var uid = new FileInfo(path).Name.Replace(".py", string.Empty, StringComparison.OrdinalIgnoreCase);


@@ -155,7 +155,7 @@ namespace MQTTnet.Server.Scripting
{ {
_scriptInstances.Add(scriptInstance); _scriptInstances.Add(scriptInstance);
} }
_logger.LogInformation($"Initialized script '{uid}'."); _logger.LogInformation($"Initialized script '{uid}'.");
} }
catch (Exception exception) catch (Exception exception)
@@ -164,7 +164,7 @@ namespace MQTTnet.Server.Scripting
} }
} }


private PythonScriptInstance CreateScriptInstance(string uid, string path, string code)
PythonScriptInstance CreateScriptInstance(string uid, string path, string code)
{ {
var scriptScope = _scriptEngine.CreateScope(); var scriptScope = _scriptEngine.CreateScope();


@@ -173,11 +173,11 @@ namespace MQTTnet.Server.Scripting


scriptScope.SetVariable("mqtt_net_server", _proxyObjects); scriptScope.SetVariable("mqtt_net_server", _proxyObjects);
compiledCode.Execute(scriptScope); compiledCode.Execute(scriptScope);
return new PythonScriptInstance(uid, path, scriptScope); return new PythonScriptInstance(uid, path, scriptScope);
} }


private void AddSearchPaths(ScriptEngine scriptEngine)
void AddSearchPaths(ScriptEngine scriptEngine)
{ {
if (_scriptingSettings.IncludePaths?.Any() != true) if (_scriptingSettings.IncludePaths?.Any() != true)
{ {


+ 3
- 3
Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs View File

@@ -1,12 +1,12 @@
using System;
using IronPython.Runtime;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting; using Microsoft.Scripting.Hosting;
using System;


namespace MQTTnet.Server.Scripting namespace MQTTnet.Server.Scripting
{ {
public class PythonScriptInstance public class PythonScriptInstance
{ {
private readonly ScriptScope _scriptScope;
readonly ScriptScope _scriptScope;


public PythonScriptInstance(string uid, string path, ScriptScope scriptScope) public PythonScriptInstance(string uid, string path, ScriptScope scriptScope)
{ {


+ 2
- 2
Source/MQTTnet.Server/Web/Startup.cs View File

@@ -152,7 +152,7 @@ namespace MQTTnet.Server.Web
.AddCookie(); .AddCookie();
} }


private void ReadMqttSettings(IServiceCollection services)
void ReadMqttSettings(IServiceCollection services)
{ {
var mqttSettings = new MqttSettingsModel(); var mqttSettings = new MqttSettingsModel();
Configuration.Bind("MQTT", mqttSettings); Configuration.Bind("MQTT", mqttSettings);
@@ -163,7 +163,7 @@ namespace MQTTnet.Server.Web
services.AddSingleton(scriptingSettings); services.AddSingleton(scriptingSettings);
} }


private static void ConfigureWebSocketEndpoint(
static void ConfigureWebSocketEndpoint(
IApplicationBuilder application, IApplicationBuilder application,
MqttServerService mqttServerService, MqttServerService mqttServerService,
MqttSettingsModel mqttSettings) MqttSettingsModel mqttSettings)


+ 1
- 5
Source/MQTTnet.Server/appsettings.json View File

@@ -52,11 +52,7 @@
}, },
"Scripting": { "Scripting": {
"ScriptsPath": "Scripts", "ScriptsPath": "Scripts",
"IncludePaths": [
"Lib",
"/usr/lib/python2.7",
"C:\\Python27\\Lib"
]
"IncludePaths": []
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {


Loading…
Cancel
Save