aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.PluginBase/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-01-02 02:33:05 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-01-02 02:33:05 -0500
commit4ef0142747c4a0dd0c4cb71d8e7359c03b3a2942 (patch)
tree2991941aec6a0b981411a4f4bb83d8d2ad900aba /lib/Plugins.PluginBase/src
parenta6b628c8653485a803bdbe322e2ecd2a69fc8e5a (diff)
breaking changes: plugin service pools & move plugin api away from web related
Diffstat (limited to 'lib/Plugins.PluginBase/src')
-rw-r--r--lib/Plugins.PluginBase/src/PluginBase.cs61
-rw-r--r--lib/Plugins.PluginBase/src/ServiceExport.cs37
2 files changed, 70 insertions, 28 deletions
diff --git a/lib/Plugins.PluginBase/src/PluginBase.cs b/lib/Plugins.PluginBase/src/PluginBase.cs
index a667a8f..d8b0973 100644
--- a/lib/Plugins.PluginBase/src/PluginBase.cs
+++ b/lib/Plugins.PluginBase/src/PluginBase.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.PluginBase
@@ -44,7 +44,7 @@ namespace VNLib.Plugins
/// Provides a concrete base class for <see cref="IPlugin"/> instances using the Serilog logging provider.
/// Accepts the standard plugin <see cref="JsonDocument"/> configuration constructors
/// </summary>
- public abstract class PluginBase : MarshalByRefObject, IWebPlugin, IPluginTaskObserver
+ public abstract class PluginBase : MarshalByRefObject, IPluginTaskObserver, IPlugin
{
/*
* CTS exists for the life of the plugin, its resources are never disposed
@@ -53,6 +53,7 @@ namespace VNLib.Plugins
private readonly CancellationTokenSource Cts = new();
private readonly LinkedList<Task> DeferredTasks = new();
+ private readonly LinkedList<ServiceExport> _services = new();
/// <summary>
/// A cancellation token that is cancelled when the plugin has been unloaded
@@ -72,13 +73,6 @@ namespace VNLib.Plugins
protected virtual string PluginConfigDomPropertyName => "plugin";
/// <summary>
- /// A list of all currently prepared <see cref="IEndpoint"/> endpoints.
- /// Endpoints must be added to this list before <see cref="IWebPlugin.GetEndpoints"/> is called
- /// by the host app
- /// </summary>
- public ICollection<IEndpoint> Endpoints { get; } = new List<IEndpoint>();
-
- /// <summary>
/// The logging instance
/// </summary>
public ILogProvider Log { get; private set; }
@@ -98,6 +92,12 @@ namespace VNLib.Plugins
/// </summary>
public JsonElement PluginConfig => Configuration.RootElement.GetProperty(PluginConfigDomPropertyName);
+ /// <summary>
+ /// The collection of exported services that will be published to the host
+ /// application
+ /// </summary>
+ public ICollection<ServiceExport> Services => _services;
+
/// <inheritdoc/>
public abstract string PluginName { get; }
@@ -257,18 +257,17 @@ namespace VNLib.Plugins
Log.Error(ex);
}
}
+
/// <summary>
/// Invoked when the host process has a command message to send
/// </summary>
/// <param name="cmd">The command message</param>
protected abstract void ProcessHostCommand(string cmd);
- IEnumerable<IEndpoint> IWebPlugin.GetEndpoints()
- {
- OnGetEndpoints();
- return Endpoints;
- }
-
+ ///<inheritdoc/>
+ void IPlugin.PublishServices(IPluginServicePool pool) => OnPublishServices(pool);
+
+ ///<inheritdoc/>
void IPlugin.Load()
{
//Setup empty log if not specified
@@ -291,7 +290,8 @@ namespace VNLib.Plugins
throw;
}
}
-
+
+ ///<inheritdoc/>
void IPlugin.Unload()
{
try
@@ -317,8 +317,8 @@ namespace VNLib.Plugins
Configuration?.Dispose();
//dispose the log
(Log as IDisposable)?.Dispose();
- //Clear endpoints list
- Endpoints.Clear();
+ //Remove any services
+ _services.Clear();
//empty deffered array
DeferredTasks.Clear();
}
@@ -372,12 +372,6 @@ namespace VNLib.Plugins
}
/// <summary>
- /// Adds the specified endpoint to be routed when loading is complete
- /// </summary>
- /// <param name="endpoint">The <see cref="IEndpoint"/> to present to the application when loaded</param>
- public void Route(IEndpoint endpoint) => Endpoints.Add(endpoint);
-
- /// <summary>
/// <para>
/// Invoked when the host loads the plugin instance
/// </para>
@@ -391,11 +385,22 @@ namespace VNLib.Plugins
/// Invoked when all endpoints have been removed from service. All managed and unmanged resources should be released.
/// </summary>
protected abstract void OnUnLoad();
-
+
/// <summary>
- /// Invoked before <see cref="IWebPlugin.GetEndpoints"/> called by the host app to get all endpoints
- /// for the current plugin
+ /// Invoked when the host requests the plugin to publish services
+ /// <para>
+ /// If overriden, the base implementation must be called to
+ /// publish all services in the internal service collection
+ /// </para>
/// </summary>
- protected virtual void OnGetEndpoints() { }
+ /// <param name="pool">The pool to publish services to</param>
+ protected virtual void OnPublishServices(IPluginServicePool pool)
+ {
+ //Publish all services then cleanup
+ foreach (ServiceExport export in _services)
+ {
+ pool.ExportService(export.ServiceType, export.Service, export.Flags);
+ }
+ }
}
} \ No newline at end of file
diff --git a/lib/Plugins.PluginBase/src/ServiceExport.cs b/lib/Plugins.PluginBase/src/ServiceExport.cs
new file mode 100644
index 0000000..960f286
--- /dev/null
+++ b/lib/Plugins.PluginBase/src/ServiceExport.cs
@@ -0,0 +1,37 @@
+/*
+* Copyright (c) 2024 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.PluginBase
+* File: ServiceExport.cs
+*
+* ServiceExport.cs is part of VNLib.Plugins.PluginBase which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Plugins.PluginBase is free software: you can redistribute it and/or modify
+* it under the terms of the GNU 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.PluginBase 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
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.Plugins.PluginBase. If not, see http://www.gnu.org/licenses/.
+*/
+
+using System;
+
+namespace VNLib.Plugins
+{
+ /// <summary>
+ /// A service export that will be published to the
+ /// host application after the plugin has been loaded
+ /// </summary>
+ /// <param name="Service"> The exported service instance </param>
+ /// <param name="ServiceType"> The exported service type </param>
+ /// <param name="Flags"> The name of the service </param>
+ public sealed record ServiceExport(Type ServiceType, object Service, ExportFlags Flags);
+} \ No newline at end of file