1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* Copyright (c) 2022 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Essentials.ServiceStack
* File: ServiceDomain.cs
*
* ServiceDomain.cs is part of VNLib.Plugins.Essentials.ServiceStack which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.ServiceStack is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.ServiceStack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Net;
using System.Text.Json;
using System.Diagnostics;
using VNLib.Utils;
using VNLib.Utils.IO;
using VNLib.Utils.Extensions;
using VNLib.Utils.Logging;
using VNLib.Plugins.Runtime;
using VNLib.Plugins.Essentials.Content;
using VNLib.Plugins.Essentials.Sessions;
namespace VNLib.Plugins.Essentials.ServiceStack
{
/// <summary>
/// Represents a domain of services and thier dynamically loaded plugins
/// that will be hosted by an application service stack
/// </summary>
public sealed class ServiceDomain : VnDisposeable, IPluginController
{
private const string PLUGIN_FILE_EXTENSION = ".dll";
private const string DEFUALT_PLUGIN_DIR = "/plugins";
private const string PLUGINS_CONFIG_ELEMENT = "plugins";
private readonly LinkedList<ServiceGroup> _serviceGroups;
private readonly LinkedList<RuntimePluginLoader> _pluginLoaders;
/// <summary>
/// Enumerates all loaded plugin instances
/// </summary>
public IEnumerable<IPlugin> Plugins => _pluginLoaders.SelectMany(static s =>
s.LivePlugins.Where(static p => p.Plugin != null)
.Select(static s => s.Plugin!)
);
/// <summary>
/// Gets all service groups loaded in the service manager
/// </summary>
public IReadOnlyCollection<ServiceGroup> ServiceGroups => _serviceGroups;
/// <summary>
/// Initializes a new empty <see cref="ServiceDomain"/>
/// </summary>
public ServiceDomain()
{
_serviceGroups = new();
_pluginLoaders = new();
}
/// <summary>
/// Uses the supplied callback to get a collection of virtual hosts
/// to build the current domain with
/// </summary>
/// <param name="hostBuilder">The callback method to build virtual hosts</param>
/// <returns>A value that indicates if any virtual hosts were successfully loaded</returns>
public bool BuildDomain(Action<ICollection<IServiceHost>> hostBuilder)
{
//LL to store created hosts
LinkedList<IServiceHost> hosts = new();
//build hosts
hostBuilder.Invoke(hosts);
return FromExisting(hosts);
}
/// <summary>
/// Builds the domain from an existing enumeration of virtual hosts
/// </summary>
/// <param name="hosts">The enumeration of virtual hosts</param>
/// <returns>A value that indicates if any virtual hosts were successfully loaded</returns>
public bool FromExisting(IEnumerable<IServiceHost> hosts)
{
//Get service groups and pass service group list
CreateServiceGroups(_serviceGroups, hosts);
return _serviceGroups.Any();
}
private static void CreateServiceGroups(ICollection<ServiceGroup> groups, IEnumerable<IServiceHost> hosts)
{
//Get distinct interfaces
IPEndPoint[] interfaces = hosts.Select(static s => s.TransportInfo.TransportEndpoint).Distinct().ToArray();
//Select hosts of the same interface to create a group from
foreach (IPEndPoint iface in interfaces)
{
IEnumerable<IServiceHost> groupHosts = hosts.Where(host => host.TransportInfo.TransportEndpoint.Equals(iface));
IServiceHost[]? overlap = groupHosts.Where(vh => groupHosts.Select(static s => s.Processor.Hostname).Count(hostname => vh.Processor.Hostname == hostname) > 1).ToArray();
foreach (IServiceHost vh in overlap)
{
throw new ArgumentException($"The hostname '{vh.Processor.Hostname}' is already in use by another virtual host");
}
//init new service group around an interface and its roots
ServiceGroup group = new(iface, groupHosts);
groups.Add(group);
}
}
///<inheritdoc/>
public Task LoadPlugins(JsonDocument config, ILogProvider appLog)
{
if (!config.RootElement.TryGetProperty(PLUGINS_CONFIG_ELEMENT, out JsonElement pluginEl))
{
appLog.Information("Plugins element not defined in config, skipping plugin loading");
return Task.CompletedTask;
}
//Get the plugin directory, or set to default
string pluginDir = pluginEl.GetPropString("path") ?? Path.Combine(Directory.GetCurrentDirectory(), DEFUALT_PLUGIN_DIR);
//Get the hot reload flag
bool hotReload = pluginEl.TryGetProperty("hot_reload", out JsonElement hrel) && hrel.GetBoolean();
//Load all virtual file assemblies withing the plugin folder
DirectoryInfo dir = new(pluginDir);
if (!dir.Exists)
{
appLog.Warn("Plugin directory {dir} does not exist. No plugins were loaded", pluginDir);
return Task.CompletedTask;
}
appLog.Information("Loading plugins. Hot-reload: {en}", hotReload);
//Enumerate all dll files within this dir
IEnumerable<DirectoryInfo> dirs = dir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly);
//Select only dirs with a dll that is named after the directory name
IEnumerable<string> pluginPaths = dirs.Where(static pdir =>
{
string compined = Path.Combine(pdir.FullName, pdir.Name);
string FilePath = string.Concat(compined, PLUGIN_FILE_EXTENSION);
return FileOperations.FileExists(FilePath);
})
//Return the name of the dll file to import
.Select(static pdir =>
{
string compined = Path.Combine(pdir.FullName, pdir.Name);
return string.Concat(compined, PLUGIN_FILE_EXTENSION);
});
IEnumerable<string> pluginFileNames = pluginPaths.Select(static s => $"{Path.GetFileName(s)}\n");
appLog.Debug("Found plugin files: \n{files}", string.Concat(pluginFileNames));
LinkedList<Task> loading = new();
object listLock = new();
foreach (string pluginPath in pluginPaths)
{
async Task Load()
{
string pluginName = Path.GetFileName(pluginPath);
RuntimePluginLoader plugin = new(pluginPath, config, appLog, hotReload, hotReload);
Stopwatch sw = new();
try
{
sw.Start();
await plugin.InitLoaderAsync();
//Listen for reload events to remove and re-add endpoints
plugin.Reloaded += OnPluginReloaded;
lock (listLock)
{
//Add to list
_pluginLoaders.AddLast(plugin);
}
sw.Stop();
appLog.Verbose("Loaded {pl} in {tm} ms", pluginName, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
appLog.Error(ex, $"Exception raised during loading {pluginName}. Failed to load plugin \n{ex}");
plugin.Dispose();
}
finally
{
sw.Stop();
}
}
loading.AddLast(Load());
}
//Continuation to add all initial plugins to the service manager
void Continuation(Task t)
{
appLog.Verbose("Plugins loaded");
//Add inital endpoints for all plugins
_pluginLoaders.TryForeach(ldr => _serviceGroups.TryForeach(sg => sg.AddOrUpdateEndpointsForPlugin(ldr)));
//Init session provider
InitSessionProvider();
//Init page router
InitPageRouter();
}
//wait for loading to completed
return Task.WhenAll(loading.ToArray()).ContinueWith(Continuation, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
///<inheritdoc/>
public bool SendCommandToPlugin(string pluginName, string message, StringComparison nameComparison = StringComparison.Ordinal)
{
Check();
//Find the single plugin by its name
LivePlugin? pl = _pluginLoaders.Select(p =>
p.LivePlugins.Where(lp => pluginName.Equals(lp.PluginName, nameComparison))
)
.SelectMany(static lp => lp)
.SingleOrDefault();
//Send the command
return pl?.SendConsoleMessage(message) ?? false;
}
///<inheritdoc/>
public void ForceReloadAllPlugins()
{
Check();
_pluginLoaders.TryForeach(static pl => pl.ReloadPlugin());
}
///<inheritdoc/>
public void UnloadAll()
{
Check();
//Unload service groups before unloading plugins
_serviceGroups.TryForeach(static sg => sg.UnloadAll());
//empty service groups
_serviceGroups.Clear();
//Unload all plugins
_pluginLoaders.TryForeach(static pl => pl.UnloadAll());
}
private void OnPluginReloaded(object? plugin, EventArgs empty)
{
//Update endpoints for the loader
RuntimePluginLoader reloaded = (plugin as RuntimePluginLoader)!;
//Update all endpoints for the plugin
_serviceGroups.TryForeach(sg => sg.AddOrUpdateEndpointsForPlugin(reloaded));
}
private void InitSessionProvider()
{
//Callback to reload provider
void onSessionProviderReloaded(ISessionProvider old, ISessionProvider current)
{
_serviceGroups.TryForeach(sg => sg.UpdateSessionProvider(current));
}
try
{
//get the loader that contains the single session provider
RuntimePluginLoader? sessionLoader = _pluginLoaders
.Where(static s => s.ExposesType<ISessionProvider>())
.SingleOrDefault();
//If session provider has been supplied, load it
if (sessionLoader != null)
{
//Get the session provider from the plugin loader
ISessionProvider sp = sessionLoader.GetExposedTypeFromPlugin<ISessionProvider>()!;
//Init inital provider
onSessionProviderReloaded(null!, sp);
//Register reload event
sessionLoader.RegisterListenerForSingle<ISessionProvider>(onSessionProviderReloaded);
}
}
catch (InvalidOperationException)
{
throw new TypeLoadException("More than one session provider plugin was defined in the plugin directory, cannot continue");
}
}
private void InitPageRouter()
{
//Callback to reload provider
void onRouterReloaded(IPageRouter old, IPageRouter current)
{
_serviceGroups.TryForeach(sg => sg.UpdatePageRouter(current));
}
try
{
//get the loader that contains the single page router
RuntimePluginLoader? routerLoader = _pluginLoaders
.Where(static s => s.ExposesType<IPageRouter>())
.SingleOrDefault();
//If router has been supplied, load it
if (routerLoader != null)
{
//Get initial value
IPageRouter sp = routerLoader.GetExposedTypeFromPlugin<IPageRouter>()!;
//Init inital provider
onRouterReloaded(null!, sp);
//Register reload event
routerLoader.RegisterListenerForSingle<IPageRouter>(onRouterReloaded);
}
}
catch (InvalidOperationException)
{
throw new TypeLoadException("More than one page router plugin was defined in the plugin directory, cannot continue");
}
}
///<inheritdoc/>
protected override void Free()
{
//Dispose loaders
_pluginLoaders.TryForeach(static pl => pl.Dispose());
_pluginLoaders.Clear();
_serviceGroups.Clear();
}
}
}
|