/// <summary>
        ///     Starts a x42 server, sets up cancellation tokens for its shutdown, and waits until it terminates.
        /// </summary>
        /// <param name="server">x42 server to run.</param>
        /// <param name="cancellationToken">Cancellation token that triggers when the server should be shut down.</param>
        /// <param name="shutdownMessage">Message to display on the console to instruct the user on how to invoke the shutdown.</param>
        /// <param name="shutdownCompleteMessage">Message to display on the console when the shutdown is complete.</param>
        public static async Task RunAsync(this IX42Server server, CancellationToken cancellationToken,
                                          string shutdownMessage, string shutdownCompleteMessage)
        {
            server.Start();

            if (!string.IsNullOrEmpty(shutdownMessage))
            {
                Console.WriteLine();
                Console.WriteLine(shutdownMessage);
                Console.WriteLine();
            }

            cancellationToken.Register(state => { ((IX42ServerLifetime)state).StopApplication(); },
                                       server.X42ServerLifetime);

            TaskCompletionSource <object> waitForStop =
                new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

            server.X42ServerLifetime.ApplicationStopping.Register(obj =>
            {
                TaskCompletionSource <object> tcs = (TaskCompletionSource <object>)obj;
                tcs.TrySetResult(null);
            }, waitForStop);

            await waitForStop.Task.ConfigureAwait(false);

            server.Dispose();

            if (!string.IsNullOrEmpty(shutdownCompleteMessage))
            {
                Console.WriteLine();
                Console.WriteLine(shutdownCompleteMessage);
                Console.WriteLine();
            }
        }
        /// <summary>
        ///     Initializes an instance of the object with specific x42 server and logger factory.
        /// </summary>
        /// <param name="server">x42 server which features are to be managed by this executor.</param>
        /// <param name="loggerFactory">Factory to be used to create logger for the object.</param>
        public ServerFeatureExecutor(IX42Server server, ILoggerFactory loggerFactory)
        {
            Guard.NotNull(server, nameof(server));

            this.server = server;
            logger      = loggerFactory.CreateLogger(GetType().FullName);
        }
示例#3
0
 private FeatureController(
     IX42Server x42Server        = null,
     ServerSettings nodeSettings = null,
     MasterNodeBase network      = null)
 {
     X42Server = x42Server;
     Settings  = nodeSettings;
     Network   = network;
 }
        /// <summary>
        ///     Installs handlers for graceful shutdown in the console, starts the x42 server and waits until it terminates.
        /// </summary>
        /// <param name="server">X42 Server to run.</param>
        public static async Task RunAsync(this IX42Server server)
        {
            ManualResetEventSlim done = new ManualResetEventSlim(false);

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                Action shutdown = () =>
                {
                    if (!cts.IsCancellationRequested)
                    {
                        Console.WriteLine("Application is shutting down.");
                        try
                        {
                            cts.Cancel();
                        }
                        catch (ObjectDisposedException exception)
                        {
                            Console.WriteLine(exception.Message);
                        }
                    }

                    done.Wait();
                };

                AssemblyLoadContext assemblyLoadContext =
                    AssemblyLoadContext.GetLoadContext(typeof(X42Server).GetTypeInfo().Assembly);
                assemblyLoadContext.Unloading += context => shutdown();

                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    shutdown();
                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;
                };

                try
                {
                    await server.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down.",
                                          "Application stopped.").ConfigureAwait(false);
                }
                finally
                {
                    done.Set();
                }
            }
        }
        public MasterNodeContoller(IX42Server x42Server, ILoggerFactory loggerFactory,
                                   IDateTimeProvider dateTimeProvider,
                                   ServerSettings serverSettings,
                                   MasterNodeBase masterNode)
        {
            Guard.NotNull(x42Server, nameof(x42Server));
            Guard.NotNull(masterNode, nameof(masterNode));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(serverSettings, nameof(serverSettings));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));

            this.x42Server        = x42Server;
            logger                = loggerFactory.CreateLogger(GetType().FullName);
            this.dateTimeProvider = dateTimeProvider;
            nodeSettings          = serverSettings;
            this.masterNode       = masterNode;
        }
 public DashboardController(IX42Server x42Server)
 {
     this.x42Server = x42Server;
 }