public static void Run(this IFullNode node, CancellationToken cancellationToken, string shutdownMessage) { using (node) { node.Start(); if (!string.IsNullOrEmpty(shutdownMessage)) { Console.WriteLine(); Console.WriteLine(shutdownMessage); Console.WriteLine(); } cancellationToken.Register(state => { ((INodeLifetime)state).StopApplication(); }, node.NodeLifetime); var waitForStop = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); node.NodeLifetime.ApplicationStopping.Register(obj => { var tcs = (TaskCompletionSource <object>)obj; tcs.TrySetResult(null); }, waitForStop); //await waitForStop.Task; waitForStop.Task.GetAwaiter().GetResult(); node.Stop(); } }
/// <summary> /// Starts a full node, sets up cancellation tokens for its shutdown, and waits until it terminates. /// </summary> /// <param name="node">Full node to run.</param> /// <param name="cancellationToken">Cancellation token that triggers when the node should be shut down.</param> public static async Task RunAsync(this IFullNode node, CancellationToken cancellationToken) { // node.NodeLifetime is not initialized yet. Use this temporary variable as to avoid side-effects to node. var nodeLifetime = node.Services.ServiceProvider.GetRequiredService <INodeLifetime>() as NodeLifetime; cancellationToken.Register(state => { ((INodeLifetime)state).StopApplication(); }, nodeLifetime); var waitForStop = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); nodeLifetime.ApplicationStopping.Register(obj => { var tcs = (TaskCompletionSource <object>)obj; tcs.TrySetResult(null); }, waitForStop); Console.WriteLine(); Console.WriteLine("Application starting, press Ctrl+C to cancel."); Console.WriteLine(); node.Start(); Console.WriteLine(); Console.WriteLine("Application started, press Ctrl+C to stop."); Console.WriteLine(); await waitForStop.Task.ConfigureAwait(false); node.Dispose(); Console.WriteLine(); Console.WriteLine("Application stopped."); Console.WriteLine(); }
public void CanCall_AddNode() { string testDirectory = CreateTestDir(this); IFullNode fullNode = this.BuildServicedNode(testDirectory); fullNode.Start(); var controller = fullNode.Services.ServiceProvider.GetService <ConnectionManagerController>(); Assert.True(controller.AddNodeRPC("0.0.0.0", "add")); Assert.Throws <ArgumentException>(() => { controller.AddNodeRPC("0.0.0.0", "notarealcommand"); }); Assert.ThrowsAny <SocketException>(() => { controller.AddNodeRPC("a.b.c.d", "onetry"); }); Assert.True(controller.AddNodeRPC("0.0.0.0", "remove")); }