Skip to content

Commit 1453e99

Browse files
authored
Enable SignalR by default (#442)
1 parent 00a232a commit 1453e99

File tree

9 files changed

+61
-56
lines changed

9 files changed

+61
-56
lines changed

src/Stratis.Bitcoin.Features.SignalR/EventSubscriptionService.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5-
using Microsoft.Extensions.Logging;
5+
using NLog;
66
using Stratis.Bitcoin.EventBus;
77
using Stratis.Bitcoin.Signals;
88

@@ -17,19 +17,18 @@ public class EventSubscriptionService : IEventsSubscriptionService, IDisposable
1717
private readonly SignalROptions options;
1818
private readonly ISignals signals;
1919
private readonly EventsHub eventsHub;
20-
private readonly ILogger<SignalRFeature> logger;
20+
private readonly ILogger logger;
2121
private readonly List<SubscriptionToken> subscriptions = new List<SubscriptionToken>();
2222

2323
public EventSubscriptionService(
2424
SignalROptions options,
25-
ILoggerFactory loggerFactory,
2625
ISignals signals,
2726
EventsHub eventsHub)
2827
{
2928
this.options = options;
3029
this.signals = signals;
3130
this.eventsHub = eventsHub;
32-
this.logger = loggerFactory.CreateLogger<SignalRFeature>();
31+
this.logger = LogManager.GetCurrentClassLogger();
3332
}
3433

3534
public void Init()
@@ -38,7 +37,7 @@ public void Init()
3837
MethodInfo onEventCallbackMethod = typeof(EventSubscriptionService).GetMethod("OnEvent");
3938
foreach (IClientEvent eventToHandle in this.options.EventsToHandle)
4039
{
41-
this.logger.LogDebug("Create subscription for {0}", eventToHandle.NodeEventType);
40+
this.logger.Debug("Create subscription for {0}", eventToHandle.NodeEventType);
4241
MethodInfo subscribeMethodInfo = subscribeMethod.MakeGenericMethod(eventToHandle.NodeEventType);
4342
Type callbackType = typeof(Action<>).MakeGenericType(eventToHandle.NodeEventType);
4443
Delegate onEventDelegate = Delegate.CreateDelegate(callbackType, this, onEventCallbackMethod);
@@ -48,8 +47,7 @@ public void Init()
4847
}
4948
}
5049

51-
// ReSharper disable once UnusedMember.Global
52-
// This is invoked through reflection
50+
/// <summary> This is invoked through reflection.</summary>
5351
public void OnEvent(EventBase @event)
5452
{
5553
Type childType = @event.GetType();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using Stratis.Bitcoin.EventBus;
3+
using Stratis.Bitcoin.Features.PoA.Events;
4+
5+
namespace Stratis.Bitcoin.Features.SignalR.Events
6+
{
7+
public sealed class ReconstructFederationClientEvent : IClientEvent
8+
{
9+
public string Progress { get; set; }
10+
11+
public Type NodeEventType { get; } = typeof(RecontructFederationProgressEvent);
12+
13+
public void BuildFrom(EventBase @event)
14+
{
15+
if (@event is RecontructFederationProgressEvent progressEvent)
16+
{
17+
this.Progress = progressEvent.Progress;
18+
return;
19+
}
20+
21+
throw new ArgumentException();
22+
}
23+
}
24+
}

src/Stratis.Bitcoin.Features.SignalR/EventsHub.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.SignalR;
6-
using Microsoft.Extensions.Logging;
6+
using NLog;
77

88
namespace Stratis.Bitcoin.Features.SignalR
99
{
10-
// ReSharper disable once ClassNeverInstantiated.Global
1110
public class SignalRMessageArgs
1211
{
1312
public string Target { get; set; }
@@ -21,30 +20,27 @@ public string GetValue(string key)
2120

2221
public class EventsHub : Hub
2322
{
24-
private readonly IDictionary<string, List<Action<SignalRMessageArgs>>> featureSubscriptions
25-
= new ConcurrentDictionary<string, List<Action<SignalRMessageArgs>>>(StringComparer.OrdinalIgnoreCase);
23+
private readonly IDictionary<string, List<Action<SignalRMessageArgs>>> featureSubscriptions = new ConcurrentDictionary<string, List<Action<SignalRMessageArgs>>>(StringComparer.OrdinalIgnoreCase);
24+
private readonly ILogger logger;
2625

27-
private readonly ILogger<EventsHub> logger;
28-
29-
public EventsHub(ILoggerFactory loggerFactory)
26+
public EventsHub()
3027
{
31-
this.logger = loggerFactory.CreateLogger<EventsHub>();
28+
this.logger = LogManager.GetCurrentClassLogger();
3229
}
3330

3431
public override Task OnConnectedAsync()
3532
{
36-
this.logger.LogDebug("New client with id {id} connected", this.Context.ConnectionId);
33+
this.logger.Debug("New client with id {id} connected", this.Context.ConnectionId);
3734
return base.OnConnectedAsync();
3835
}
3936

4037
public override Task OnDisconnectedAsync(Exception exception)
4138
{
42-
this.logger.LogDebug("Client with id {id} disconnected", this.Context.ConnectionId);
39+
this.logger.Debug("Client with id {id} disconnected", this.Context.ConnectionId);
4340
return base.OnDisconnectedAsync(exception);
4441
}
4542

4643
/// <summary>Called using reflection from SignalR</summary>
47-
// ReSharper disable once UnusedMember.Global
4844
public void SendMessage(SignalRMessageArgs message)
4945
{
5046
try
@@ -59,7 +55,7 @@ public void SendMessage(SignalRMessageArgs message)
5955
}
6056
catch (Exception e)
6157
{
62-
this.logger.LogError("Error SendMessage", e);
58+
this.logger.Error("Error SendMessage", e);
6359
}
6460
}
6561

@@ -92,7 +88,7 @@ public async Task SendToClientsAsync(IClientEvent @event)
9288
}
9389
catch (Exception ex)
9490
{
95-
this.logger.LogError(ex, "Error sending to clients");
91+
this.logger.Error(ex, "Error sending to clients");
9692
}
9793
}
9894
}

src/Stratis.Bitcoin.Features.SignalR/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace Stratis.Bitcoin.Features.SignalR
99
{
1010
public class Program
1111
{
12-
public static IWebHost Initialize(IEnumerable<ServiceDescriptor> services, FullNode fullNode,
13-
SignalRSettings settings, IWebHostBuilder webHostBuilder)
12+
public static IWebHost Initialize(IEnumerable<ServiceDescriptor> services, FullNode fullNode, SignalRSettings settings, IWebHostBuilder webHostBuilder)
1413
{
1514
Guard.NotNull(fullNode, nameof(fullNode));
1615
Guard.NotNull(webHostBuilder, nameof(webHostBuilder));

src/Stratis.Bitcoin.Features.SignalR/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void ConfigureServices(IServiceCollection services)
2929
.AllowCredentials();
3030
});
3131
});
32+
3233
services.AddSignalR().AddNewtonsoftJsonProtocol(options =>
3334
{
3435
var settings = new JsonSerializerSettings();
@@ -37,8 +38,7 @@ public void ConfigureServices(IServiceCollection services)
3738
});
3839
}
3940

40-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory,
41-
IServiceProvider serviceProvider)
41+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
4242
{
4343
app.UseRouting();
4444

src/Stratis.Bitcoin.Features.SignalR/Stratis.Bitcoin.Features.SignalR.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
<ItemGroup>
2222
<ProjectReference Include="..\Stratis.Bitcoin.Features.Miner\Stratis.Bitcoin.Features.Miner.csproj" />
23+
<ProjectReference Include="..\Stratis.Bitcoin.Features.PoA\Stratis.Bitcoin.Features.PoA.csproj" />
2324
<ProjectReference Include="..\Stratis.Bitcoin.Features.Wallet\Stratis.Bitcoin.Features.Wallet.csproj" />
2425
<ProjectReference Include="..\Stratis.Bitcoin\Stratis.Bitcoin.csproj" />
2526
</ItemGroup>

src/Stratis.Bitcoin/Configuration/NodeSettings.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ public class NodeSettings : IDisposable
109109
/// </summary>
110110
public FeeRate MinRelayTxFeeRate { get; private set; }
111111

112-
/// <summary>
113-
/// If true then the node will add and start the SignalR feature.
114-
/// </summary>
115-
public bool EnableSignalR { get; private set; }
116-
117112
/// <summary>
118113
/// Initializes a new instance of the object.
119114
/// </summary>
@@ -234,8 +229,6 @@ public NodeSettings(Network network = null, ProtocolVersion protocolVersion = Su
234229
this.ReadConfigurationFile();
235230
}
236231

237-
this.EnableSignalR = this.ConfigReader.GetOrDefault<bool>("enableSignalR", false, this.Logger);
238-
239232
// Create the custom logger factory.
240233
this.LoggerFactory.AddFilters(this.LogSettings, this.DataFolder);
241234

src/Stratis.CirrusD/Program.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,25 @@ private static IFullNode GetSideChainFullNode(NodeSettings nodeSettings)
8080
.AddSQLiteWalletRepository()
8181
.UseApi()
8282
.AddRPC()
83-
.UseDiagnosticFeature();
84-
85-
if (nodeSettings.EnableSignalR)
83+
.AddSignalR(options =>
8684
{
87-
nodeBuilder.AddSignalR(options =>
85+
options.EventsToHandle = new[]
8886
{
89-
options.EventsToHandle = new[]
90-
{
91-
(IClientEvent) new BlockConnectedClientEvent(),
92-
new TransactionReceivedClientEvent()
93-
};
87+
(IClientEvent) new BlockConnectedClientEvent(),
88+
new ReconstructFederationClientEvent(),
89+
new TransactionReceivedClientEvent(),
90+
};
9491

95-
options.ClientEventBroadcasters = new[]
92+
options.ClientEventBroadcasters = new[]
93+
{
94+
(Broadcaster: typeof(CirrusWalletInfoBroadcaster),
95+
ClientEventBroadcasterSettings: new ClientEventBroadcasterSettings
9696
{
97-
(Broadcaster: typeof(CirrusWalletInfoBroadcaster),
98-
ClientEventBroadcasterSettings: new ClientEventBroadcasterSettings
99-
{
100-
BroadcastFrequencySeconds = 5
101-
})
102-
};
103-
});
104-
}
97+
BroadcastFrequencySeconds = 5
98+
})
99+
};
100+
})
101+
.UseDiagnosticFeature();
105102

106103
return nodeBuilder.Build();
107104
}

src/Stratis.StraxD/Program.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,12 @@ public static async Task Main(string[] args)
4848
.AddPowPosMining(true)
4949
.UseApi()
5050
.AddRPC()
51-
.UseDiagnosticFeature();
52-
53-
if (nodeSettings.EnableSignalR)
54-
{
55-
nodeBuilder.AddSignalR(options =>
51+
.AddSignalR(options =>
5652
{
5753
options.EventsToHandle = new[]
5854
{
5955
(IClientEvent) new BlockConnectedClientEvent(),
56+
new ReconstructFederationClientEvent(),
6057
new TransactionReceivedClientEvent()
6158
};
6259

@@ -71,8 +68,8 @@ public static async Task Main(string[] args)
7168
BroadcastFrequencySeconds = 5
7269
})
7370
};
74-
});
75-
}
71+
})
72+
.UseDiagnosticFeature();
7673

7774
IFullNode node = nodeBuilder.Build();
7875

0 commit comments

Comments
 (0)