Exemplo n.º 1
0
        /// <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 IxServer server, CancellationToken cancellationToken,
                                          string shutdownMessage, string shutdownCompleteMessage)
        {
            server.StartFeature();

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

            cancellationToken.Register(state => { ((IxServerLifetime)state).StopApplication(); },
                                       server.xServerLifetime);

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

            server.xServerLifetime.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();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes an instance of the object with specific xServer and logger factory.
        /// </summary>
        /// <param name="server">xServer 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(IxServer server, ILoggerFactory loggerFactory)
        {
            Guard.NotNull(server, nameof(server));

            this.server = server;
            logger      = loggerFactory.CreateLogger(GetType().FullName);
        }
Exemplo n.º 3
0
 private FeatureController(
     IxServer xServer            = null,
     ServerSettings nodeSettings = null,
     ServerNodeBase network      = null)
 {
     this.xServer = xServer;
     Settings     = nodeSettings;
     Network      = network;
 }
Exemplo n.º 4
0
        /// <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 IxServer 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(XServer).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();
                }
            }
        }
Exemplo n.º 5
0
        public ServerNodeContoller(IxServer xServer, ILoggerFactory loggerFactory,
                                   IDateTimeProvider dateTimeProvider,
                                   ServerSettings serverSettings,
                                   DatabaseFeatures databaseFeatures)
        {
            Guard.NotNull(xServer, nameof(xServer));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(serverSettings, nameof(serverSettings));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));
            Guard.NotNull(databaseFeatures, nameof(databaseFeatures));

            this.xServer          = xServer;
            logger                = loggerFactory.CreateLogger(GetType().FullName);
            this.dateTimeProvider = dateTimeProvider;
            nodeSettings          = serverSettings;
            this.databaseFeatures = databaseFeatures;
        }
Exemplo n.º 6
0
 public DashboardController(IxServer xServer)
 {
     this.xServer = xServer;
 }