Skip to content

Commit

Permalink
Adds event factory (#10)
Browse files Browse the repository at this point in the history
Let EventFactory create all events
  • Loading branch information
dennis committed Jan 2, 2021
1 parent 8b2d157 commit 3c0619d
Show file tree
Hide file tree
Showing 22 changed files with 908 additions and 277 deletions.
49 changes: 24 additions & 25 deletions Backend/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using Slipstream.Backend.Plugins;
using Slipstream.Backend.Services;
using Slipstream.Shared;
using Slipstream.Shared.Events;
using Slipstream.Shared.Events.Internal;
using Slipstream.Shared.Events.Setting;
using System;
using static Slipstream.Shared.IEventFactory;

#nullable enable

namespace Slipstream.Backend
{
class Engine : Worker, IEngine, IDisposable
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly PluginManager PluginManager;
private readonly IEventBusSubscription Subscription;
private readonly IStateService StateService;
private readonly IApplicationConfiguration ApplicationConfiguration;
private readonly Shared.EventHandler EventHandler = new Shared.EventHandler();

public Engine(IEventBus eventBus, IStateService stateService, IApplicationConfiguration applicationConfiguration) : base("engine")
public Engine(IEventFactory eventFactory, IEventBus eventBus, IStateService stateService, IApplicationConfiguration applicationConfiguration) : base("engine")
{
EventFactory = eventFactory;
EventBus = eventBus;
StateService = stateService;
ApplicationConfiguration = applicationConfiguration;
PluginManager = new PluginManager(this, eventBus);
PluginManager = new PluginManager(this, eventFactory, eventBus);

Subscription = EventBus.RegisterListener();

Expand All @@ -34,13 +38,13 @@ public Engine(IEventBus eventBus, IStateService stateService, IApplicationConfig
EventHandler.OnInternalCommandPluginStates += (s, e) => OnCommandPluginStates(e.Event);

// Plugins..
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "FileMonitorPlugin", PluginName = "FileMonitorPlugin", Settings = ApplicationConfiguration.GetFileMonitorSettingsEvent() });
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "FileTriggerPlugin", PluginName = "FileTriggerPlugin" });
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "AudioPlugin", PluginName = "AudioPlugin", Settings = ApplicationConfiguration.GetAudioSettingsEvent() });
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "IRacingPlugin", PluginName = "IRacingPlugin" });
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "TwitchPlugin", PluginName = "TwitchPlugin", Settings = ApplicationConfiguration.GetTwitchSettingsEvent() });
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "TransmitterPlugin", PluginName = "TransmitterPlugin", Settings = ApplicationConfiguration.GetTxrxSettingsEvent() }, false);
RegisterPlugin(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = "ReceiverPlugin", PluginName = "ReceiverPlugin", Settings = ApplicationConfiguration.GetTxrxSettingsEvent() }, false);
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("FileMonitorPlugin", "FileMonitorPlugin", ApplicationConfiguration.GetFileMonitorSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("FileTriggerPlugin", "FileTriggerPlugin"));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("AudioPlugin", "AudioPlugin", ApplicationConfiguration.GetAudioSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("IRacingPlugin", "IRacingPlugin"));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("TwitchPlugin", "TwitchPlugin", ApplicationConfiguration.GetTwitchSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("TransmitterPlugin", "TransmitterPlugin", ApplicationConfiguration.GetTxrxSettingsEvent()), false);
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("ReceiverPlugin", "ReceiverPlugin", ApplicationConfiguration.GetTxrxSettingsEvent()), false);

// Tell Plugins that we're live - this will make eventbus distribute events
EventBus.Enabled = true;
Expand All @@ -50,21 +54,16 @@ private void OnCommandPluginStates(InternalCommandPluginStates _)
{
PluginManager.ForAllPluginsExecute(
(a) => EventBus.PublishEvent(
new Shared.Events.Internal.InternalPluginState
{
Id = a.Id,
DisplayName = a.DisplayName,
PluginName = a.Name,
PluginStatus = a.Enabled ? "Enabled" : "Disabled"
}));
EventFactory.CreateInternalPluginState(a.Id, a.Name, a.DisplayName, a.Enabled ? PluginStatusEnum.Enabled : PluginStatusEnum.Disabled)
));
}

private void RegisterPlugin(InternalCommandPluginRegister e, bool enable = true)
{
OnCommandPluginRegister(e);
if (enable)
{
EventBus.PublishEvent(new Shared.Events.Internal.InternalCommandPluginEnable() { Id = e.Id });
EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginEnable(e.Id));
}
}

Expand Down Expand Up @@ -95,12 +94,12 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new FileMonitorPlugin(ev.Id, EventBus, settings));
PluginManager.RegisterPlugin(new FileMonitorPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "FileTriggerPlugin":
PluginManager.RegisterPlugin(new FileTriggerPlugin(ev.Id, EventBus));
PluginManager.RegisterPlugin(new FileTriggerPlugin(ev.Id, EventFactory, EventBus));
break;
case "LuaPlugin":
{
Expand All @@ -110,7 +109,7 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new LuaPlugin(ev.Id, EventBus, StateService, settings));
PluginManager.RegisterPlugin(new LuaPlugin(ev.Id, EventFactory, EventBus, StateService, settings));
}
}
break;
Expand All @@ -122,12 +121,12 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new AudioPlugin(ev.Id, EventBus, settings));
PluginManager.RegisterPlugin(new AudioPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "IRacingPlugin":
PluginManager.RegisterPlugin(new IRacingPlugin(ev.Id, EventBus));
PluginManager.RegisterPlugin(new IRacingPlugin(ev.Id, EventFactory, EventBus));
break;
case "TwitchPlugin":
{
Expand All @@ -137,7 +136,7 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new TwitchPlugin(ev.Id, EventBus, settings));
PluginManager.RegisterPlugin(new TwitchPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
Expand All @@ -149,7 +148,7 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new TransmitterPlugin(ev.Id, EventBus, settings));
PluginManager.RegisterPlugin(new TransmitterPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
Expand All @@ -161,7 +160,7 @@ private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPlugi
}
else
{
PluginManager.RegisterPlugin(new ReceiverPlugin(ev.Id, EventBus, settings));
PluginManager.RegisterPlugin(new ReceiverPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
Expand Down
15 changes: 9 additions & 6 deletions Backend/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
using Slipstream.Shared;
using System;
using System.Collections.Generic;
using static Slipstream.Shared.IEventFactory;

namespace Slipstream.Backend
{
class PluginManager
{
private readonly IEngine Engine;
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly IDictionary<string, IPlugin> Plugins = new Dictionary<string, IPlugin>();
private readonly IDictionary<string, PluginWorker> PluginWorkers = new Dictionary<string, PluginWorker>();

public PluginManager(IEngine engine, IEventBus eventBus)
public PluginManager(IEngine engine, IEventFactory eventFactory, IEventBus eventBus)
{
Engine = engine;
EventFactory = eventFactory;
EventBus = eventBus;
}

Expand All @@ -34,12 +37,12 @@ public void UnregisterPlugins()
private void UnregisterPlugin(IPlugin p)
{
PluginWorkers[p.WorkerName].RemovePlugin(p);
EmitPluginStateChanged(p, "Unregistered");
EmitPluginStateChanged(p, PluginStatusEnum.Unregistered);
}

private void EmitPluginStateChanged(IPlugin plugin, string pluginStatus)
private void EmitPluginStateChanged(IPlugin plugin, PluginStatusEnum pluginStatus)
{
EmitEvent(new Shared.Events.Internal.InternalPluginState() { Id = plugin.Id, PluginName = plugin.Name, PluginStatus = pluginStatus, DisplayName = plugin.DisplayName });
EmitEvent(EventFactory.CreateInternalPluginState(plugin.Id, plugin.Name, plugin.DisplayName, pluginStatus));
}

public void RegisterPlugin(IPlugin plugin)
Expand All @@ -54,7 +57,7 @@ public void RegisterPlugin(IPlugin plugin)
}
else
{
worker = new PluginWorker(plugin.WorkerName, Engine.RegisterListener(), EventBus);
worker = new PluginWorker(plugin.WorkerName, Engine.RegisterListener(), EventFactory, EventBus);
worker.Start();
PluginWorkers.Add(worker.Name, worker);
}
Expand All @@ -63,7 +66,7 @@ public void RegisterPlugin(IPlugin plugin)

Plugins.Add(plugin.Id, plugin);

EmitPluginStateChanged(plugin, "Registered");
EmitPluginStateChanged(plugin, PluginStatusEnum.Registered);
}
}

Expand Down
8 changes: 4 additions & 4 deletions Backend/PluginWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class PluginWorker : Worker
private readonly IList<IPlugin> Plugins = new List<IPlugin>();
private readonly IEventBusSubscription Subscription;
private readonly IEventBus EventBus;
private readonly IEventFactory EventFactory;

public PluginWorker(string name, IEventBusSubscription subscription, IEventBus eventBus) : base(name)
public PluginWorker(string name, IEventBusSubscription subscription, IEventFactory eventFactory, IEventBus eventBus) : base(name)
{
Subscription = subscription;
EventBus = eventBus;
EventFactory = eventFactory;
}

public void AddPlugin(IPlugin plugin)
Expand All @@ -30,9 +32,7 @@ public void AddPlugin(IPlugin plugin)

private void Plugin_OnStateChanged(IPlugin plugin, IPlugin.EventHandlerArgs<IPlugin> e)
{
string pluginStatus = plugin.Enabled ? "Enabled" : "Disabled";

EventBus.PublishEvent(new InternalPluginState() { Id = plugin.Id, PluginName = plugin.Name, PluginStatus = pluginStatus, DisplayName = plugin.DisplayName });
EventBus.PublishEvent(EventFactory.CreateInternalPluginState(plugin.Id, plugin.Name, plugin.DisplayName, plugin.Enabled ? IEventFactory.PluginStatusEnum.Enabled : IEventFactory.PluginStatusEnum.Disabled));
}

public void RemovePlugin(IPlugin plugin)
Expand Down
6 changes: 4 additions & 2 deletions Backend/Plugins/AudioPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace Slipstream.Backend.Plugins
{
class AudioPlugin : BasePlugin
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private string? Path;
private readonly SpeechSynthesizer Synthesizer;

public AudioPlugin(string id, IEventBus eventBus, AudioSettings settings) : base(id, "AudioPlugin", "AudioPlugin", "Audio")
public AudioPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, AudioSettings settings) : base(id, "AudioPlugin", "AudioPlugin", "Audio")
{
EventFactory = eventFactory;
EventBus = eventBus;

EventHandler_OnSettingAudioSettings(settings);
Expand Down Expand Up @@ -60,7 +62,7 @@ private void EventHandler_OnAudioCommandPlay(Shared.EventHandler source, Shared.
}
catch (Exception ex)
{
EventBus.PublishEvent(new UICommandWriteToConsole() { Message = "Playing audio file failed: " + ex.Message });
EventBus.PublishEvent(EventFactory.CreateUICommandWriteToConsole("Playing audio file failed: " + ex.Message));
}
}

Expand Down
17 changes: 8 additions & 9 deletions Backend/Plugins/FileMonitorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace Slipstream.Backend.Plugins
{
class FileMonitorPlugin : BasePlugin
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly IList<FileSystemWatcher> fileSystemWatchers = new List<FileSystemWatcher>();
private readonly bool InitialScan = false;

public FileMonitorPlugin(string id, IEventBus eventBus, FileMonitorSettings settings) : base(id, "FileMonitorPlugin", "FileMonitorPlugin", "Core")
public FileMonitorPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, FileMonitorSettings settings) : base(id, "FileMonitorPlugin", "FileMonitorPlugin", "Core")
{
EventFactory = eventFactory;
EventBus = eventBus;

OnFileMonitorSettings(settings);
Expand Down Expand Up @@ -47,10 +49,7 @@ private void RescanExistingFiles()
{
foreach (var path in Directory.GetFiles(watcher.Path, "*.*"))
{
EventBus.PublishEvent(new FileMonitorFileCreated
{
FilePath = path
});
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileCreated(path));
}
}

Expand Down Expand Up @@ -85,22 +84,22 @@ private void OnFileMonitorSettings(FileMonitorSettings ev)

private void WatcherOnRenamed(object sender, RenamedEventArgs e)
{
EventBus.PublishEvent(new FileMonitorFileRenamed { FilePath = e.FullPath, OldFilePath = e.OldFullPath });
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileRenamed(e.FullPath, e.OldFullPath));
}

private void WatcherOnDeleted(object sender, FileSystemEventArgs e)
{
EventBus.PublishEvent(new FileMonitorFileDeleted { FilePath = e.FullPath });
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileDeleted(e.FullPath));
}

private void WatcherOnChanged(object sender, FileSystemEventArgs e)
{
EventBus.PublishEvent(new FileMonitorFileChanged { FilePath = e.FullPath });
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileChanged(e.FullPath));
}

private void WatcherOnCreated(object sender, FileSystemEventArgs e)
{
EventBus.PublishEvent(new FileMonitorFileCreated { FilePath = e.FullPath });
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileCreated(e.FullPath));
}
}
}
19 changes: 8 additions & 11 deletions Backend/Plugins/FileTriggerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Slipstream.Backend.Plugins
{
class FileTriggerPlugin : BasePlugin
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly IDictionary<string, string> Scripts = new Dictionary<string, string>();

Expand All @@ -19,9 +20,10 @@ class FileTriggerPlugin : BasePlugin
private bool BootUp = true;
private readonly List<string> WaitingForLuaScripts = new List<string>();

public FileTriggerPlugin(string id, IEventBus eventBus) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core")
public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core")
{
this.EventBus = eventBus;
EventFactory = eventFactory;
EventBus = eventBus;

EventHandler.OnFileMonitorFileCreated += EventHandler_OnFileMonitorFileCreated;
EventHandler.OnFileMonitorFileDeleted += EventHandler_OnFileMonitorFileDeleted;
Expand All @@ -43,6 +45,7 @@ private void EventHandler_OnInternalPluginState(EventHandler source, EventHandle
EventHandler.OnInternalPluginState -= EventHandler_OnInternalPluginState;

EventBus.PublishEvent(new Shared.Events.Internal.InternalInitialized());
EventBus.PublishEvent(EventFactory.CreateInternalInitialized());
}
}
}
Expand All @@ -69,21 +72,15 @@ private void NewFile(string filePath)
WaitingForLuaScripts.Add(pluginId);
}


EventBus.PublishEvent(new Shared.Events.Internal.InternalCommandPluginRegister() { Id = pluginId, PluginName = pluginName, Settings = GetLuaSettings(pluginId, filePath) });
EventBus.PublishEvent(new Shared.Events.Internal.InternalCommandPluginEnable() { Id = pluginId });
EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginRegister(pluginId, pluginName, EventFactory.CreateLuaSettings(pluginId, filePath)));
EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginEnable(pluginId));

Scripts.Add(filePath, pluginId);
}

private IEvent GetLuaSettings(string pluginId, string filePath)
{
return new Slipstream.Shared.Events.Setting.LuaSettings() { PluginId = pluginId, FilePath = filePath };
}

private void DeletedFile(string filePath)
{
var ev = new Shared.Events.Internal.InternalCommandPluginUnregister() { Id = Scripts[filePath] };
var ev = EventFactory.CreateInternalCommandPluginUnregister(Scripts[filePath]);

EventBus.PublishEvent(ev);

Expand Down
Loading

0 comments on commit 3c0619d

Please sign in to comment.