96 lines
2.6 KiB
C#
96 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using JD.Interfaces.Remoting;
|
|
using Plume.Core.Logging;
|
|
using Plume.Interfaces.Services;
|
|
|
|
namespace JD.Remoting.Services
|
|
{
|
|
public sealed class PluginService : IPluginService
|
|
{
|
|
private readonly List<IPlugin> _plugins = new List<IPlugin>();
|
|
private IServiceContext _serviceContext;
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
if (_plugins.Any())
|
|
{
|
|
foreach (IPlugin plugin in _plugins.ToArray())
|
|
{
|
|
plugin.Dispose();
|
|
RemovePlugin(plugin);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RemovePlugin(IPlugin plugin)
|
|
{
|
|
_plugins.Remove(plugin);
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, plugin));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Initialize(IServiceContext serviceContext)
|
|
{
|
|
_serviceContext = serviceContext;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Register(IPlugin plugin)
|
|
{
|
|
if (_plugins.Any() && _plugins.All(x => x.IsConnected))
|
|
{
|
|
plugin.Start(EndPointId);
|
|
}
|
|
_plugins.Add(plugin);
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, plugin));
|
|
}
|
|
|
|
|
|
public EndPointId EndPointId { get; private set; } = default(EndPointId);
|
|
|
|
/// <inheritdoc />
|
|
public void Start(EndPointId endPointId = default(EndPointId))
|
|
{
|
|
EndPointId = endPointId;
|
|
foreach (var plugin in _plugins)
|
|
{
|
|
plugin.Start(endPointId);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Stop()
|
|
{
|
|
foreach (var plugin in _plugins.Where(p=>p.IsConnected))
|
|
{
|
|
plugin.Stop();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
|
|
private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
|
{
|
|
CollectionChanged?.Invoke(this, e);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerator<IPlugin> GetEnumerator()
|
|
{
|
|
return _plugins.GetEnumerator();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
}
|
|
}
|