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
|
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Essentials.ServiceStack
* File: ServiceGroup.cs
*
* ServiceGroup.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.Net;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using VNLib.Utils.Extensions;
using VNLib.Plugins.Essentials.ServiceStack.Plugins;
namespace VNLib.Plugins.Essentials.ServiceStack
{
/// <summary>
/// Represents a collection of virtual hosts that share a
/// common transport (interface, port, and SSL status)
/// and may be loaded by a single server instance.
/// </summary>
/// <remarks>
/// Initalizes a new <see cref="ServiceGroup"/> of virtual hosts
/// with common transport
/// </remarks>
/// <param name="hosts">The hosts that share a common interface endpoint</param>
public sealed class ServiceGroup(IEnumerable<IServiceHost> hosts)
{
private readonly LinkedList<IServiceHost> _vHosts = new(hosts);
private readonly ConditionalWeakTable<IManagedPlugin, IEndpoint[]> _endpointsForPlugins = new();
/// <summary>
/// The collection of hosts that are loaded by this group
/// </summary>
public IReadOnlyCollection<IServiceHost> Hosts => _vHosts;
/// <summary>
/// Manually detatches runtime services and their loaded endpoints from all
/// endpoints.
/// </summary>
internal void UnloadAll()
{
//Remove all loaded endpoints
_vHosts.TryForeach(v => _endpointsForPlugins.ForEach(eps => v.OnRuntimeServiceDetach(eps.Key, eps.Value)));
//Clear all hosts
_vHosts.Clear();
//Clear all endpoints
_endpointsForPlugins.Clear();
}
internal void OnPluginLoaded(IManagedPlugin plugin)
{
//Get all new endpoints for plugin
IEndpoint[] newEndpoints = plugin.GetEndpoints()
.ToArray();
//Add endpoints to dict
_endpointsForPlugins.AddOrUpdate(plugin, newEndpoints);
//Add endpoints to hosts
_vHosts.TryForeach(v => v.OnRuntimeServiceAttach(plugin, newEndpoints));
}
internal void OnPluginUnloaded(IManagedPlugin plugin)
{
//Get the old endpoints from the controller referrence and remove them
if (_endpointsForPlugins.TryGetValue(plugin, out IEndpoint[]? oldEps))
{
//Remove the old endpoints
_vHosts.TryForeach(v => v.OnRuntimeServiceDetach(plugin, oldEps));
//remove controller ref
_ = _endpointsForPlugins.Remove(plugin);
}
}
}
}
|