-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate SignalR feature into WebHost (#121)
* Migrate SignalR feature into WebHost - Migrate the SignalR feature into WebHost. - Further refactor WebHost from "Api" to "WebHost. - Remove the versioning support from REST API. - Enable flags to enable/disable UI, API and WS. - Generate and include XML documentation in the REST API for all features. - Add a basic example on Web Socket listener. * Improve the Web Socket event handling - Return the EventBase directly to Web Socket consumers. - Remove unused code. - Remove references in WebHost project. * created a class to handle swagger api documentation xml file inclusion. * Fix issue with exceptions during type loading - Move the Assembly extension from Swagger scaffolding to Utilities. - Minor bug discovered in dispose when object is null (happened during an integration test). * Rename WebHost to NodeHost - Next commit will rename the physical folder. * Rename WebHost folder to NodeHost - Updates all references, etc. from WebHost to NodeHost. * Removing the dependency on a static service provider * Remove the Hash and Height - We're returning the whole object in the evernt, no longer needed to read out hash and height. * Revert "Remove the Hash and Height" This reverts commit 4f8af1b. * Move the resolving of HubContext so it only runs when WS is enabled - Minor cleanup of events, removing older properties. * Remove hardcoded URL to JS and WS endpoints - Update to use relative path when hosted on the NodeHost. - Add JsonIgnore on objects that can't serialize. This needs to be added to more. * Handle serialization of IPAddress, add ignore on structures that crash node - Certain types will crash the node when sent to the Web Socket consumer. - Beware of this issue in the future when working on the event types. - Remove the EventType and instead only have EventName. * Fix dependency issue for broadcasters when no IEventsSubscriptionService is registered - When using the node without .UseWebHost, no implementation of IEventsSubscriptionService is registered. This fixes the broadcasters by handling them as empty if one is not provided. * Update InitialBlockDownloadState.cs - Reverted the changes to check for nulls on IBD check. Co-authored-by: Mithril Man <red.angel@libero.it> Co-authored-by: dangershony <dan.gershony@gmail.com>
- Loading branch information
1 parent
9db4d40
commit 7917a2f
Showing
138 changed files
with
6,130 additions
and
1,402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 18 additions & 13 deletions
31
...s.SignalR/Broadcasters/BroadcasterBase.cs → ...Blockcore/Broadcasters/BroadcasterBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Blockcore.AsyncWork; | ||
using Blockcore.EventBus; | ||
using Blockcore.Utilities; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Blockcore.Features.SignalR.Broadcasters | ||
namespace Blockcore.Broadcasters | ||
{ | ||
/// <summary> | ||
/// Base class for all SignalR Broadcasters | ||
/// Base class for all Web Socket Broadcasters | ||
/// </summary> | ||
public abstract class ClientBroadcasterBase : IClientEventBroadcaster | ||
{ | ||
private readonly EventsHub eventsHub; | ||
private readonly IEventsSubscriptionService eventsHub; | ||
private readonly INodeLifetime nodeLifetime; | ||
private readonly IAsyncProvider asyncProvider; | ||
protected readonly ILogger logger; | ||
private IAsyncLoop asyncLoop; | ||
protected readonly ILogger log; | ||
|
||
protected ClientBroadcasterBase( | ||
EventsHub eventsHub, | ||
ILoggerFactory loggerFactory, | ||
INodeLifetime nodeLifetime, | ||
IAsyncProvider asyncProvider) | ||
IAsyncProvider asyncProvider, | ||
IEventsSubscriptionService subscriptionService = null | ||
) | ||
{ | ||
this.eventsHub = eventsHub; | ||
this.eventsHub = subscriptionService; | ||
this.nodeLifetime = nodeLifetime; | ||
this.asyncProvider = asyncProvider; | ||
this.logger = loggerFactory.CreateLogger(this.GetType().FullName); | ||
this.log = loggerFactory.CreateLogger(this.GetType().FullName); | ||
} | ||
|
||
public void Init(ClientEventBroadcasterSettings broadcasterSettings) | ||
{ | ||
this.logger.LogDebug($"Initialising SignalR Broadcaster {this.GetType().Name}"); | ||
this.log.LogDebug($"Initialising Web Socket Broadcaster {this.GetType().Name}"); | ||
|
||
this.asyncLoop = this.asyncProvider.CreateAndRunAsyncLoop( | ||
$"Broadcast {this.GetType().Name}", | ||
async token => | ||
{ | ||
foreach (IClientEvent clientEvent in this.GetMessages()) | ||
foreach (EventBase clientEvent in this.GetMessages()) | ||
{ | ||
await this.eventsHub.SendToClientsAsync(clientEvent).ConfigureAwait(false); | ||
this.eventsHub.OnEvent(clientEvent); | ||
} | ||
}, | ||
this.nodeLifetime.ApplicationStopping, | ||
repeatEvery: TimeSpan.FromSeconds(Math.Max(broadcasterSettings.BroadcastFrequencySeconds, 5))); | ||
repeatEvery: TimeSpan.FromSeconds(Math.Max(broadcasterSettings.BroadcastFrequencySeconds, 5)), | ||
startAfter: TimeSpans.FiveSeconds); | ||
} | ||
|
||
protected abstract IEnumerable<IClientEvent> GetMessages(); | ||
protected abstract IEnumerable<EventBase> GetMessages(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Blockcore.Broadcasters | ||
{ | ||
public class ClientEventBroadcasterSettings | ||
{ | ||
public int BroadcastFrequencySeconds { get; set; } | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...atures.SignalR/IClientEventBroadcaster.cs → ...e/Broadcasters/IClientEventBroadcaster.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Threading.Tasks; | ||
using Blockcore.Broadcasters; | ||
using Blockcore.EventBus; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace Blockcore.Broadcasters | ||
{ | ||
public interface IEventsSubscriptionService | ||
{ | ||
void Init(); | ||
|
||
void Subscribe(string id, string name); | ||
|
||
void Unsubscribe(string id, string name); | ||
|
||
void UnsubscribeAll(string id); | ||
|
||
void OnEvent(EventBase @event); | ||
|
||
void SetHub<T>(IHubContext<T> hubContext) where T : Hub; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Net; | ||
using Newtonsoft.Json; | ||
|
||
namespace Blockcore.EventBus.CoreEvents.Peer | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using NBitcoin; | ||
using System; | ||
using NBitcoin; | ||
|
||
namespace Blockcore.EventBus.CoreEvents | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Blockcore.Utilities.Extensions | ||
{ | ||
public static class AssemblyExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the loadable types, ignoring assembly that can't be loaded for any reason. | ||
/// </summary> | ||
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly) | ||
{ | ||
try | ||
{ | ||
return assembly.GetTypes(); | ||
} | ||
catch (ReflectionTypeLoadException e) | ||
{ | ||
return e.Types.Where(t => t != null); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Blockcore/Utilities/JsonConverters/IPAddressConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Blockcore.Utilities.JsonConverters | ||
{ | ||
public class IPAddressConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return (objectType == typeof(IPAddress)); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToString()); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
return IPAddress.Parse((string)reader.Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.