Skip to content

Commit

Permalink
- Deprecated server side includes and other {codes}
Browse files Browse the repository at this point in the history
- Added MessagePack configuration option to WebSocketGateway for enabling automatic message packing into compressed bytes
- Added MigService.Pack and MigService.Unpack static methods for packing/unpacking bytes messages
  • Loading branch information
genemars committed Dec 22, 2022
1 parent c301b90 commit d4ad774
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 111 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright (2012-2023) G-Labs (https://github.com/genielabs)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions MIG/Gateways/Defs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class WebSocketGatewayOptions
public const string Authentication = "Authentication";
public const string AuthenticationRealm = "AuthenticationRealm";
public const string IgnoreExtensions = "IgnoreExtensions";
public const string MessagePack = "MessagePack";
}

public static class TcpSocketGatewayOptions
Expand Down
60 changes: 0 additions & 60 deletions MIG/Gateways/WebServiceGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,71 +516,11 @@ private void Worker(object state)
WebFile webFile = GetWebFile(requestedFile);
response.ContentEncoding = webFile.Encoding;
response.ContentType += "; charset=" + webFile.Encoding.BodyName;
// We don't need to parse the content again if it's coming from the cache
if (!webFile.IsCached)
{
string body = webFile.Content;
if (requestedFile.EndsWith(".md"))
{
// Built-in Markdown files support
body = CommonMarkConverter.Convert(body);
// TODO: add a way to include HTML header and footer template to be appended to the
// TODO: translated markdown text
}
else
{
// HTML file
// replace prepocessor directives with values
bool tagFound;
do
{
tagFound = false;
int ts = body.IndexOf("{include ");
if (ts >= 0)
{
int te = body.IndexOf("}", ts);
if (te > ts)
{
string rs = body.Substring(ts + (te - ts) + 1);
string cs = body.Substring(ts, te - ts + 1);
string ls = body.Substring(0, ts);
//
try
{
if (cs.StartsWith("{include "))
{
string fileName = cs.Substring(9).TrimEnd('}').Trim();
fileName = GetWebFilePath(fileName);
//
Encoding fileEncoding = DetectWebFileEncoding(fileName);
if (fileEncoding == null)
fileEncoding = defaultWebFileEncoding;
var incFile = File.ReadAllText(fileName, fileEncoding) + rs;
body = ls + incFile;
}
}
catch
{
body = ls + "<h5 style=\"color:red\">Error processing '" + cs.Replace("{", "[").Replace("}", "]") + "'</h5>" + rs;
}
tagFound = true;
}
}
} while (tagFound); // continue if a pre processor tag was found
// {hostos}
body = body.Replace("{hostos}", Environment.OSVersion.Platform.ToString());
// {filebase}
body = body.Replace("{filebase}", Path.GetFileNameWithoutExtension(requestedFile));
}
// update the cache content with parsing results
webFile.Content = body;
}
// Store the cache item if the file cache is enabled
if (enableFileCache)
{
UpdateWebFileCache(requestedFile, webFile.Content, response.ContentEncoding);
}
//
WebServiceUtility.WriteStringToContext(context, webFile.Content);
}
catch (Exception ex)
Expand Down
18 changes: 15 additions & 3 deletions MIG/Gateways/WebSocketGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using MIG.Config;
using MIG.Gateways.Authentication;

using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
Expand Down Expand Up @@ -77,6 +75,7 @@ public class WebSocketGateway : IMigGateway
private string authenticationSchema = WebAuthenticationSchema.None;
private string authenticationRealm = "MIG Secure Zone";
private bool ignoreExtensions = false;
private bool messagePack = false;

public WebSocketGateway()
{
Expand All @@ -103,6 +102,9 @@ public void OnSetOption(Option option)
case WebSocketGatewayOptions.IgnoreExtensions:
bool.TryParse(option.Value, out ignoreExtensions);
break;
case WebSocketGatewayOptions.MessagePack:
bool.TryParse(option.Value, out messagePack);
break;
}
}

Expand All @@ -113,7 +115,17 @@ public void OnInterfacePropertyChanged(object sender, InterfacePropertyChangedEv
WebSocketServiceHost host;
webSocketServer.WebSocketServices.TryGetServiceHost("/events", out host);
if (host == null) return;
host.Sessions.BroadcastAsync(MigService.JsonSerialize(args.EventData), () => {});

if (messagePack)
{
//var lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
//byte[] eventBytes = MessagePackSerializer.Serialize(args.EventData, lz4Options);
host.Sessions.BroadcastAsync(MigService.Pack(args.EventData), () => {});
}
else
{
host.Sessions.BroadcastAsync(MigService.JsonSerialize(args.EventData), () => {});
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion MIG/MIG.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
</PropertyGroup>

<ItemGroup>
<None Include="packages.config" />
<None Include="..\README.md">
<Link>README.md</Link>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommonMark.NET" Version="0.15.1" />
<PackageReference Include="MessagePack" Version="2.4.59" />
<PackageReference Include="MessagePack.Annotations" Version="2.4.59" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" />
<PackageReference Include="Ude.NetStandard" Version="1.2.0" />
Expand Down
28 changes: 0 additions & 28 deletions MIG/MIG.nuspec

This file was deleted.

26 changes: 18 additions & 8 deletions MIG/MigEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ limitations under the License.
*/

using System;
using MessagePack;

namespace MIG
{

[Serializable()]
[Serializable, MessagePackObject]
public class MigEvent
{
[Key(0)]
public DateTime Timestamp { get; set; }

[Key(1)]
public double UnixTimestamp
{
get
Expand All @@ -40,12 +43,20 @@ public double UnixTimestamp
}
}

public string Domain { get; }
public string Source { get; }
public string Description { get; }
public string Property { get; }
[Key(2)]
public string Domain { get; init; }
[Key(3)]
public string Source { get; init; }
[Key(4)]
public string Description { get; init; }
[Key(5)]
public string Property { get; init; }
[Key(6)]
public object Value { get; set; }

public MigEvent()
{
}
public MigEvent(string domain, string source, string description, string propertyPath, object propertyValue)
{
Timestamp = DateTime.UtcNow;
Expand All @@ -60,10 +71,9 @@ public override string ToString()
{
//string date = this.Timestamp.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
//string logentrytxt = date + "\t" + this.Domain + "\t" + this.Source + "\t" + (this.Description == "" ? "-" : this.Description) + "\t" + this.Property + "\t" + this.Value;
string logentrytxt = this.Domain + "\t" + this.Source + "\t" + (this.Description == "" ? "-" : this.Description) + "\t" + this.Property + "\t" + this.Value;
return logentrytxt;
string logEntryTxt = Domain + "\t" + Source + "\t" + (Description == "" ? "-" : Description) + "\t" + Property + "\t" + Value;
return logEntryTxt;
}
}

}

11 changes: 11 additions & 0 deletions MIG/MigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
using System.Linq;
using NLog;
using System.Reflection;
using MessagePack;
using MIG.Config;
using Gateway = MIG.Config.Gateway;

Expand Down Expand Up @@ -446,6 +447,16 @@ public static string JsonSerialize(object data, bool indent = false)
return Utility.Serialization.JsonSerialize(data, indent);
}

public static byte[] Pack(MigEvent e)
{
return MessagePackSerializer.Serialize(e);
}

public static MigEvent Unpack(byte[] data)
{
return MessagePackSerializer.Deserialize<MigEvent>(data, MessagePackSerializerOptions.Standard);
}

public static void ShellCommand(string command, string args)
{
var processInfo = new System.Diagnostics.ProcessStartInfo(command, args);
Expand Down
8 changes: 0 additions & 8 deletions MIG/packages.config

This file was deleted.

2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ test:
after_test:
- ps: .\MIG\nuget_pack.ps1
artifacts:
- path: .\MIG\bin\Debug\MIG.dll
name: MIG
- path: '*.nupkg'
name: MIG nupkg
deploy:
Expand Down

0 comments on commit d4ad774

Please sign in to comment.