|
| 1 | +// SPDX-FileCopyrightText: 2025 smdn <smdn@smdn.jp> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | + |
| 12 | +using Smdn.Net.MuninNode.Transport; |
| 13 | +using Smdn.Net.MuninPlugin; |
| 14 | + |
| 15 | +namespace Smdn.Net.MuninNode.DependencyInjection; |
| 16 | + |
| 17 | +internal sealed class DefaultMuninNodeBuilder : IMuninNodeBuilder { |
| 18 | + private readonly List<Func<IServiceProvider, IPlugin>> pluginFactories = new(capacity: 4); |
| 19 | + private Func<IServiceProvider, IPluginProvider>? buildPluginProvider; |
| 20 | + private Func<IServiceProvider, INodeSessionCallback>? buildSessionCallback; |
| 21 | + private Func<IServiceProvider, IMuninNodeListenerFactory>? buildListenerFactory; |
| 22 | + |
| 23 | + public IServiceCollection Services { get; } |
| 24 | + public string ServiceKey { get; } |
| 25 | + |
| 26 | + public DefaultMuninNodeBuilder(IMuninServiceBuilder serviceBuilder, string serviceKey) |
| 27 | + { |
| 28 | + Services = (serviceBuilder ?? throw new ArgumentNullException(nameof(serviceBuilder))).Services; |
| 29 | + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); |
| 30 | + } |
| 31 | + |
| 32 | + public void AddPluginFactory(Func<IServiceProvider, IPlugin> buildPlugin) |
| 33 | + { |
| 34 | + if (buildPlugin is null) |
| 35 | + throw new ArgumentNullException(nameof(buildPlugin)); |
| 36 | + |
| 37 | + pluginFactories.Add(serviceProvider => buildPlugin(serviceProvider)); |
| 38 | + } |
| 39 | + |
| 40 | + public void SetPluginProviderFactory( |
| 41 | + Func<IServiceProvider, IPluginProvider> buildPluginProvider |
| 42 | + ) |
| 43 | + { |
| 44 | + if (buildPluginProvider is null) |
| 45 | + throw new ArgumentNullException(nameof(buildPluginProvider)); |
| 46 | + |
| 47 | + this.buildPluginProvider = buildPluginProvider; |
| 48 | + } |
| 49 | + |
| 50 | + public void SetSessionCallbackFactory( |
| 51 | + Func<IServiceProvider, INodeSessionCallback> buildSessionCallback |
| 52 | + ) |
| 53 | + { |
| 54 | + if (buildSessionCallback is null) |
| 55 | + throw new ArgumentNullException(nameof(buildSessionCallback)); |
| 56 | + |
| 57 | + this.buildSessionCallback = buildSessionCallback; |
| 58 | + } |
| 59 | + |
| 60 | + public void SetListenerFactory( |
| 61 | + Func<IServiceProvider, IMuninNodeListenerFactory> buildListenerFactory |
| 62 | + ) |
| 63 | + { |
| 64 | + if (buildListenerFactory is null) |
| 65 | + throw new ArgumentNullException(nameof(buildListenerFactory)); |
| 66 | + |
| 67 | + this.buildListenerFactory = buildListenerFactory; |
| 68 | + } |
| 69 | + |
| 70 | + public IMuninNode Build(IServiceProvider serviceProvider) |
| 71 | + { |
| 72 | + if (serviceProvider is null) |
| 73 | + throw new ArgumentNullException(nameof(serviceProvider)); |
| 74 | + |
| 75 | + return new DefaultMuninNode( |
| 76 | + options: serviceProvider.GetRequiredService<IOptionsMonitor<MuninNodeOptions>>().Get(name: ServiceKey), |
| 77 | + pluginProvider: buildPluginProvider is null |
| 78 | + ? new PluginProvider( |
| 79 | + plugins: pluginFactories.Select(factory => factory(serviceProvider)).ToList(), |
| 80 | + sessionCallback: buildSessionCallback?.Invoke(serviceProvider) |
| 81 | + ) |
| 82 | + : buildPluginProvider.Invoke(serviceProvider), |
| 83 | + listenerFactory: buildListenerFactory?.Invoke(serviceProvider), |
| 84 | + logger: serviceProvider.GetService<ILoggerFactory>()?.CreateLogger<DefaultMuninNode>() |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + private sealed class PluginProvider : IPluginProvider { |
| 89 | + public IReadOnlyCollection<IPlugin> Plugins { get; } |
| 90 | + public INodeSessionCallback? SessionCallback { get; } |
| 91 | + |
| 92 | + public PluginProvider( |
| 93 | + IReadOnlyCollection<IPlugin> plugins, |
| 94 | + INodeSessionCallback? sessionCallback |
| 95 | + ) |
| 96 | + { |
| 97 | + Plugins = plugins ?? throw new ArgumentNullException(nameof(plugins)); |
| 98 | + SessionCallback = sessionCallback; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments