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 _plugins = new List(); private IServiceContext _serviceContext; /// 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)); } /// public void Initialize(IServiceContext serviceContext) { _serviceContext = serviceContext; } /// 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); /// public void Start(EndPointId endPointId = default(EndPointId)) { EndPointId = endPointId; foreach (var plugin in _plugins) { plugin.Start(endPointId); } } /// public void Stop() { foreach (var plugin in _plugins.Where(p=>p.IsConnected)) { plugin.Stop(); } } /// public event NotifyCollectionChangedEventHandler CollectionChanged; private void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { CollectionChanged?.Invoke(this, e); } /// public IEnumerator GetEnumerator() { return _plugins.GetEnumerator(); } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }