Skip to content

Commit e218a50

Browse files
Ticket #110 : Add EventMesh CLI
1 parent ff4f5e4 commit e218a50

26 files changed

Lines changed: 601 additions & 129 deletions

File tree

FaasNet.EventMesh.sln

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.31624.102
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02. Core Layer", "02. Core Layer", "{6E495E0A-0DC8-4E42-8C58-3C48506D3D24}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04. Testing Layer", "04. Testing Layer", "{2A14F697-FFC8-402F-82B4-8128FE897DC9}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06. Testing Layer", "06. Testing Layer", "{2A14F697-FFC8-402F-82B4-8128FE897DC9}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03. Startup Layer", "03. Startup Layer", "{20A0BD99-A4F3-4FD5-A6FA-1935D7464DB8}"
1111
EndProject
@@ -65,6 +65,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FaasNet.EventMesh.Service",
6565
EndProject
6666
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04. Installer Layer", "04. Installer Layer", "{F9148CB9-8CA8-43BA-85F2-46C94AF624D7}"
6767
EndProject
68+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "05. CLI Layer", "05. CLI Layer", "{E7E305E3-DDFE-4523-8343-82C62884A323}"
69+
EndProject
70+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FaasNet.EventMeshCTL.CLI", "src\EventMesh\FaasNet.EventMeshCTL.CLI\FaasNet.EventMeshCTL.CLI.csproj", "{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D}"
71+
EndProject
6872
Global
6973
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7074
Debug|Any CPU = Debug|Any CPU
@@ -159,6 +163,10 @@ Global
159163
{21CC76FF-FA0A-4FF2-A0A1-78A8C107FCEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
160164
{21CC76FF-FA0A-4FF2-A0A1-78A8C107FCEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
161165
{21CC76FF-FA0A-4FF2-A0A1-78A8C107FCEE}.Release|Any CPU.Build.0 = Release|Any CPU
166+
{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
167+
{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D}.Debug|Any CPU.Build.0 = Debug|Any CPU
168+
{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D}.Release|Any CPU.ActiveCfg = Release|Any CPU
169+
{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D}.Release|Any CPU.Build.0 = Release|Any CPU
162170
EndGlobalSection
163171
GlobalSection(SolutionProperties) = preSolution
164172
HideSolutionNode = FALSE
@@ -189,6 +197,7 @@ Global
189197
{8B617B76-FFD2-4DBE-921E-B7DB73309530} = {7A13262A-D1E6-4210-BA95-03A0741CBFA4}
190198
{D581D830-5A97-451F-96C5-8AC513EC308D} = {8B617B76-FFD2-4DBE-921E-B7DB73309530}
191199
{21CC76FF-FA0A-4FF2-A0A1-78A8C107FCEE} = {F9148CB9-8CA8-43BA-85F2-46C94AF624D7}
200+
{294141B6-1F8D-43D9-BF3E-5A86FFD8F38D} = {E7E305E3-DDFE-4523-8343-82C62884A323}
192201
EndGlobalSection
193202
GlobalSection(ExtensibilityGlobals) = postSolution
194203
SolutionGuid = {B9BD3B8C-B2C9-468F-BF54-66BFE9B565EC}

src/Common/FaasNet.Common/ServerBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public class ServerBuilder
66
{
77
private ServiceProvider _serviceProvider;
88

9+
public ServerBuilder()
10+
{
11+
Services = new ServiceCollection();
12+
}
13+
914
public ServerBuilder(IServiceCollection services)
1015
{
1116
Services = services;

src/EventMesh/FaasNet.EventMesh.Client/Constants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class Constants
1919
{ Commands.HELLO_REQUEST, Commands.HELLO_RESPONSE },
2020
{ Commands.SUBSCRIBE_REQUEST, Commands.SUBSCRIBE_RESPONSE },
2121
{ Commands.PUBLISH_MESSAGE_REQUEST, Commands.PUBLISH_MESSAGE_RESONSE },
22-
{ Commands.GET_ALL_VPNS_REQUEST, Commands.GET_ALL_VPNS_RESPONSE }
22+
{ Commands.GET_ALL_VPNS_REQUEST, Commands.GET_ALL_VPNS_RESPONSE },
23+
{ Commands.ADD_VPN_REQUEST, Commands.ADD_VPN_RESPONSE },
24+
{ Commands.ADD_CLIENT_REQUEST, Commands.ADD_CLIENT_RESPONSE }
2325
};
2426
}
2527
}

src/EventMesh/FaasNet.EventMesh.Client/Messages/Errors.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class Errors : IEquatable<Errors>
2525
public static Errors SESSION_LIFETIME_TOOLONG = new Errors("SESSION_LIFETIME_TOOLONG");
2626
public static Errors SESSION_LIFETIME_CANNOT_BE_INFINITE = new Errors("SESSION_LIFETIME_CANNOT_BE_INFINITE");
2727
public static Errors SESSION_LIFETIME_TOOSHORT = new Errors("SESSION_LIFETIME_TOOSHORT");
28+
public static Errors VPN_ALREADY_EXISTS = new Errors("VPN_ALREADY_EXISTS");
29+
public static Errors CLIENT_ALREADY_EXISTS = new Errors("CLIENT_ALREADY_EXISTS");
2830
private Errors(string code)
2931
{
3032
Code = code;

src/EventMesh/FaasNet.EventMesh.Protocols.AMQP/FaasNet.EventMesh.Protocols.AMQP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4+
<EnableDynamicLoading>true</EnableDynamicLoading>
45
</PropertyGroup>
56
<ItemGroup>
67
<PackageReference Include="AMQPNetLite" Version="2.4.4" />

src/EventMesh/FaasNet.EventMesh.Runtime/Handlers/AddClientMessageHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace FaasNet.EventMesh.Runtime.Handlers
1010
{
1111
public class AddClientMessageHandler : IMessageHandler
1212
{
13+
private readonly IVpnStore _vpnStore;
1314
private readonly IClientStore _clientStore;
1415

15-
public AddClientMessageHandler(IClientStore clientStore)
16+
public AddClientMessageHandler(IVpnStore vpnStore, IClientStore clientStore)
1617
{
18+
_vpnStore = vpnStore;
1719
_clientStore = clientStore;
1820
}
1921

@@ -22,6 +24,10 @@ public AddClientMessageHandler(IClientStore clientStore)
2224
public async Task<EventMeshPackageResult> Run(Package package, IEnumerable<IPeerHost> peers, CancellationToken cancellationToken)
2325
{
2426
var addClient = package as AddClientRequest;
27+
var existingVpn = await _vpnStore.Get(addClient.Vpn, cancellationToken);
28+
if (existingVpn == null) return EventMeshPackageResult.SendResult(PackageResponseBuilder.Error(package.Header.Command, package.Header.Seq, Errors.UNKNOWN_VPN));
29+
var existingClient = await _clientStore.Get(addClient.Vpn, addClient.ClientId, cancellationToken);
30+
if (existingClient != null) return EventMeshPackageResult.SendResult(PackageResponseBuilder.Error(package.Header.Command, package.Header.Seq, Errors.CLIENT_ALREADY_EXISTS));
2531
var client = Models.Client.Create(addClient.Vpn, addClient.ClientId, null, addClient.Purposes.ToList());
2632
await _clientStore.Add(client, cancellationToken);
2733
return EventMeshPackageResult.SendResult(PackageResponseBuilder.Client(package.Header.Seq));

src/EventMesh/FaasNet.EventMesh.Runtime/Handlers/AddVpnMessageHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public AddVpnMessageHandler(IVpnStore vpnStore)
2121
public async Task<EventMeshPackageResult> Run(Package package, IEnumerable<IPeerHost> peers, CancellationToken cancellationToken)
2222
{
2323
var addVpn = package as AddVpnRequest;
24+
var existingVpn = await _vpnStore.Get(addVpn.Vpn, cancellationToken);
25+
if (existingVpn != null) return EventMeshPackageResult.SendResult(PackageResponseBuilder.Error(addVpn.Header.Command, addVpn.Header.Seq, Errors.VPN_ALREADY_EXISTS));
2426
var vpn = Models.Vpn.Create(addVpn.Vpn, string.Empty);
2527
await _vpnStore.Add(vpn, cancellationToken);
2628
var result = PackageResponseBuilder.AddVpn(package.Header.Seq);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Runtime.Loader;
2+
3+
namespace FaasNet.EventMesh.Service
4+
{
5+
public class PluginLoadContext : AssemblyLoadContext
6+
{
7+
private AssemblyDependencyResolver _resolver;
8+
9+
10+
}
11+
}

src/EventMesh/FaasNet.EventMesh.Service/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
})
1313
.ConfigureServices((hostContext, services) =>
1414
{
15+
var pluginsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
16+
IEnumerable<string> pluginPaths = new string[0];
17+
if(Directory.Exists(pluginsDirectory)) pluginPaths = Directory.EnumerateDirectories(pluginsDirectory);
18+
// https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support
19+
/*
20+
pluginPaths.SelectMany(p =>
21+
{
22+
23+
});
24+
*/
1525
var options = hostContext.Configuration.Get<EventMeshServerOptions>();
1626
services.AddEventMeshServer(consensusNodeCallback: o => o.Port = options.Port)
1727
.UseRocksDB(o =>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.Json;
2+
3+
namespace FaasNet.EventMeshCTL.CLI
4+
{
5+
public class EventMeshCTLConfigurationManager
6+
{
7+
private static EventMeshCTLConfiguration _cachedConfiguration;
8+
private static object _lock = new object();
9+
private const string FILE_NAME = "appsettings.json";
10+
11+
public static EventMeshCTLConfiguration Get()
12+
{
13+
lock(_lock)
14+
{
15+
if (_cachedConfiguration != null) return _cachedConfiguration;
16+
var path = GetConfigurationFilePath();
17+
var json = File.ReadAllText(path);
18+
var result = JsonSerializer.Deserialize<EventMeshCTLConfiguration>(json, new JsonSerializerOptions
19+
{
20+
PropertyNameCaseInsensitive = true
21+
});
22+
if (result == null) throw new InvalidOperationException($"The configuration file {FILE_NAME} is not correct");
23+
return result;
24+
}
25+
}
26+
27+
public void Update(EventMeshCTLConfiguration configuration)
28+
{
29+
lock(_lock)
30+
{
31+
_cachedConfiguration = configuration;
32+
var path = GetConfigurationFilePath();
33+
var json = JsonSerializer.Serialize(configuration, new JsonSerializerOptions
34+
{
35+
PropertyNameCaseInsensitive = true
36+
});
37+
File.WriteAllText(path, json);
38+
}
39+
}
40+
41+
private static string GetConfigurationFilePath()
42+
{
43+
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILE_NAME);
44+
if (!File.Exists(path)) throw new InvalidOperationException($"The configuration file {FILE_NAME} doesn't exist");
45+
return path;
46+
}
47+
}
48+
49+
public class EventMeshCTLConfiguration
50+
{
51+
public int Port { get; set; } = EventMesh.Client.Constants.DefaultPort;
52+
public string Url { get; set; } = EventMesh.Client.Constants.DefaultUrl;
53+
}
54+
}

0 commit comments

Comments
 (0)