Skip to content

Commit

Permalink
Merge pull request #125 from dennis/ui-for-endpoints
Browse files Browse the repository at this point in the history
UI for endpoints
  • Loading branch information
dennis authored Aug 18, 2021
2 parents 3b295be + 7f601b8 commit fd097a4
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
end)
```
It will setup a `handle()` function for you that wires it up.
- Endpoints created, are now shown in menu under "Endpoints"

## [0.8.0](https://github.com/dennis/slipstream/releases/tag/v0.8.0) (2021-08-04)
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.7.0...v0.8.0)
Expand Down
10 changes: 10 additions & 0 deletions Components/WebWidget/EventFactory/WebWidgetEventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ public WebWidgetCommandEvent CreateWebWidgetCommandEvent(IEventEnvelope envelope
{
return new WebWidgetCommandEvent { Envelope = envelope, Data = data };
}

public WebWidgetEndpointAdded CreateWebWidgetEndpointAdded(IEventEnvelope envelope, string endpoint)
{
return new WebWidgetEndpointAdded { Envelope = envelope, Endpoint = endpoint };
}

public WebWidgetEndpointRemoved CreateWebWidgetEndpointRemoved(IEventEnvelope envelope, string endpoint)
{
return new WebWidgetEndpointRemoved { Envelope = envelope, Endpoint = endpoint };
}
}
}
7 changes: 7 additions & 0 deletions Components/WebWidget/EventHandler/WebWidgetEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Slipstream.Components.WebWidget.Events;
using Slipstream.Shared;

using System;

namespace Slipstream.Components.WebWidget.EventHandler
Expand All @@ -10,11 +11,17 @@ internal class WebWidgetEventHandler : IEventHandler
{
public event EventHandler<WebWidgetCommandEvent>? OnWebWidgetCommandEvent;

public event EventHandler<WebWidgetEndpointAdded>? OnWebWidgetEndpointAdded;

public event EventHandler<WebWidgetEndpointRemoved>? OnWebWidgetEndpointRemoved;

public IEventHandler.HandledStatus HandleEvent(IEvent @event)
{
return @event switch
{
WebWidgetCommandEvent tev => OnEvent(OnWebWidgetCommandEvent, tev),
WebWidgetEndpointAdded tev => OnEvent(OnWebWidgetEndpointAdded, tev),
WebWidgetEndpointRemoved tev => OnEvent(OnWebWidgetEndpointRemoved, tev),
_ => IEventHandler.HandledStatus.NotMine,
};
}
Expand Down
13 changes: 13 additions & 0 deletions Components/WebWidget/Events/WebWidgetEndpointAdded.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using Slipstream.Shared;

namespace Slipstream.Components.WebWidget.Events
{
public class WebWidgetEndpointAdded : IEvent
{
public string EventType => typeof(WebWidgetEndpointAdded).Name;
public IEventEnvelope Envelope { get; set; } = new EventEnvelope();
public string Endpoint { get; set; } = string.Empty;
}
}
13 changes: 13 additions & 0 deletions Components/WebWidget/Events/WebWidgetEndpointRemoved.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using Slipstream.Shared;

namespace Slipstream.Components.WebWidget.Events
{
public class WebWidgetEndpointRemoved : IEvent
{
public string EventType => typeof(WebWidgetEndpointRemoved).Name;
public IEventEnvelope Envelope { get; set; } = new EventEnvelope();
public string Endpoint { get; set; } = string.Empty;
}
}
16 changes: 13 additions & 3 deletions Components/WebWidget/HttpServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -9,6 +10,7 @@
using Serilog;

using Slipstream.Components.Internal;
using Slipstream.Components.Internal.EventFactory;
using Slipstream.Components.WebWidget.EventHandler;
using Slipstream.Components.WebWidget.Lua;
using Slipstream.Shared;
Expand All @@ -29,6 +31,7 @@ public class HttpServer : IHttpServer, IHttpServerApi
private readonly IEventBus EventBus;
private readonly IInternalEventFactory InternalEventFactory;
private readonly WebWidgetLuaLibrary LuaLibrary;
private readonly IWebWidgetEventFactory WebWidgetEventFactory;
private readonly IWebWidgetInstances Instances = new WebWidgetInstances();
private readonly IEventEnvelope BroadcastEnvelope = new EventEnvelope("webwidget");
private const string Url = "http://127.0.0.1:1919"; // Must NOT end with slash
Expand All @@ -40,6 +43,7 @@ public HttpServer(
IEventHandlerController eventHandlerController,
IEventBus eventBus,
IInternalEventFactory internalEventFactory,
IWebWidgetEventFactory webWidgetEventFactory,
WebWidgetLuaLibrary luaLibrary)
{
Logger = logger;
Expand All @@ -48,6 +52,7 @@ public HttpServer(
EventBus = eventBus;
InternalEventFactory = internalEventFactory;
LuaLibrary = luaLibrary;
WebWidgetEventFactory = webWidgetEventFactory;

EventsServerModule = new WebSocketsEventsServer(Logger, Instances);

Expand All @@ -68,20 +73,23 @@ public void AddInstance(string instanceId, string webWidgetType, string? data)
}
}

var endpoint = $"{Url}/instances/{instanceId}";

// Crude sanity check
var indexFile = WEB_WIDGET_ROOT_DIRECTORY + webWidgetType + "/index.html";
if (System.IO.File.Exists(indexFile))
{
EventBus.PublishEvent(InternalEventFactory.CreateInternalInstanceAdded(BroadcastEnvelope, "webwidget", instanceId));
EventBus.PublishEvent(WebWidgetEventFactory.CreateWebWidgetEndpointAdded(BroadcastEnvelope, endpoint));

Instances.Add(instanceId, webWidgetType, data);
Subscription.AddImpersonate(instanceId);

Logger.Information($"HttpServer: {Url}/instances/{instanceId} added");
Logger.Information($"HttpServer: {endpoint} added");
}
else
{
Logger.Error($"HttpServer: {Url}/instances/{instanceId} not added, as {indexFile} does not exist");
Logger.Error($"HttpServer: {endpoint} not added, as {indexFile} does not exist");
}
}

Expand Down Expand Up @@ -150,9 +158,11 @@ private void InactiveInstance(string instanceId)

public void RemoveInstance(string instanceId)
{
var endpoint = $"{Url}/instances/{instanceId}";
Instances.Remove(instanceId);
Subscription.DeleteImpersonation(instanceId);
Logger.Information($"HttpServer: {Url}/instances/{instanceId} removed");
Logger.Information($"HttpServer: {endpoint} removed");
EventBus.PublishEvent(WebWidgetEventFactory.CreateWebWidgetEndpointRemoved(BroadcastEnvelope, endpoint));
EventBus.PublishEvent(InternalEventFactory.CreateInternalInstanceRemoved(BroadcastEnvelope, "webwidget", instanceId));
Stopping = Instances.Count == 0;
LuaLibrary.InstanceDropped(instanceId);
Expand Down
4 changes: 4 additions & 0 deletions Components/WebWidget/IWebWidgetEventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ namespace Slipstream.Components.WebWidget
public interface IWebWidgetEventFactory
{
WebWidgetCommandEvent CreateWebWidgetCommandEvent(IEventEnvelope envelope, string data);

WebWidgetEndpointAdded CreateWebWidgetEndpointAdded(IEventEnvelope envelope, string endpoint);

WebWidgetEndpointRemoved CreateWebWidgetEndpointRemoved(IEventEnvelope envelope, string endpoint);
}
}
12 changes: 11 additions & 1 deletion Components/WinFormUI/Forms/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Components/WinFormUI/Forms/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class MainWindow : Form

private DataGridViewCellEventArgs? EventViewerMouseLocation;
private readonly TreeNode? DeepViewInstanceNode;
private readonly Dictionary<string, ToolStripMenuItem> EndpointMenuItems = new Dictionary<string, ToolStripMenuItem>();

public MainWindow(
string instanceId,
Expand Down Expand Up @@ -190,6 +191,7 @@ private void EventListenerMain()

var internalEventHandler = EventHandler.Get<Internal.EventHandler.Internal>();
var uiEventHandler = EventHandler.Get<EventHandler.WinFormUIEventHandler>();
var webWidgetEventHandler = EventHandler.Get<WebWidget.EventHandler.WebWidgetEventHandler>();

uiEventHandler.OnWinFormUICommandWriteToConsole += (_, e) => PendingMessages.Add(e);
uiEventHandler.OnWinFormUICommandCreateButton += (_, e) => EventHandler_OnWinFormUICommandCreateButton(e);
Expand All @@ -208,6 +210,9 @@ private void EventListenerMain()
internalEventHandler.OnInternaDependencyAdded += (_, e) => EventHandler_OnInternaDependencyAdded_DeepView(e.InstanceId, e.DependsOn);
internalEventHandler.OnInternalDependencyRemoved += (_, e) => EventHandler_OnInternalDependencyRemoved_DeepView(e.InstanceId, e.DependsOn);

webWidgetEventHandler.OnWebWidgetEndpointAdded += (_, e) => EventHandler_OnWebWidgetEndpointAdded(e.Endpoint);
webWidgetEventHandler.OnWebWidgetEndpointRemoved += (_, e) => EventHandler_OnWebWidgetEndpointRemoved(e.Endpoint);

var token = (CancellationToken)EventHandlerThreadCancellationToken; // We got a Assert ensuring this isn't null

while (!token.IsCancellationRequested)
Expand All @@ -221,6 +226,35 @@ private void EventListenerMain()
}
}

private void EventHandler_OnWebWidgetEndpointRemoved(string endpoint)
{
ExecuteSecure(() =>
{
EndpointsToolStripMenuItem.DropDownItems.Remove(EndpointMenuItems[endpoint]);
EndpointsToolStripMenuItem.Enabled = EndpointsToolStripMenuItem.DropDownItems.Count != 0;
EndpointMenuItems.Remove(endpoint);
});
}

private void EventHandler_OnWebWidgetEndpointAdded(string endpoint)
{
ExecuteSecure(() =>
{
EndpointsToolStripMenuItem.Enabled = true;
var openItem = new ToolStripMenuItem("Open in browser");
openItem.Click += (_, e) => { Process.Start(new ProcessStartInfo(endpoint) { UseShellExecute = true }); };
var copyItem = new ToolStripMenuItem("Copy to clipboard");
copyItem.Click += (_, e) => CopyToClipBoard(endpoint);
var item = new ToolStripMenuItem(endpoint);
item.DropDownItems.AddRange(new ToolStripItem[] { openItem, copyItem });
EndpointMenuItems.Add(endpoint, item);
EndpointsToolStripMenuItem.DropDownItems.Add(item);
});
}

private void EventHandler_OnInternalDependencyRemoved_Envelope(string instanceId, string dependsOn)
{
// something stopped begin dependant on us, so remove it from our Envelope
Expand Down

0 comments on commit fd097a4

Please sign in to comment.