Exemple #1
0
        /// <summary>
        /// Runs the <see cref="Program"/> asynchronously.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Returns an awaitable <see cref="Task"/>.</returns>
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            IConfiguration configuration;

            try
            {
                configuration = ConfigurationLoader.GetConfiguration("appSettings");
            }
            catch (Exception)
            {
                Console.WriteLine("Please check if you have a valid appSettings.json!");
                CancellationTokenSource.Cancel();
                return;
            }
            Startup startup = new Startup(CancellationTokenSource);

            Console.WriteLine("Initializing...");
            await startup.InitializeAsync(configuration, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            Console.WriteLine("Finished initializing!\n");

            Interlocked.Exchange(ref _canReadConsole, 1);
            Interlocked.Exchange(ref _canExit, 1);

            _genericServiceProvider = startup.GetGenericServiceProvider();
            GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await _genericServiceProvider.GetService <ITrainingRoomService>()
                                                                              .GetEnabledTrainingRoomsAsync(new GetEnabledTrainingRoomsRequest());

            foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms)
            {
                Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner.Username}");
            }

            ServerConfiguration serverConfiguration = _genericServiceProvider.GetService <IOptions <ServerConfiguration> >().Value;
            TcpListener         tcpListener         = new TcpListener(IPAddress.Any, serverConfiguration.Port);

            tcpListener.Start();
            Console.WriteLine($"Started listening for clients on port: {serverConfiguration.Port}.");
            while (!cancellationToken.IsCancellationRequested)
            {
                TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();

                Console.WriteLine($"Accepted a new connection: \n\tLocalEndPoint: {tcpClient.Client.LocalEndPoint}\n\tRemoteEndPoint: {tcpClient.Client.RemoteEndPoint}");
                _ = Task.Run(async() =>
                {
                    IMessageProcessor messageProcessor      = new ServerMessageProcessor(_genericServiceProvider.GetService <MessageToServiceMapper>());
                    IMessageSerializer messageSerializer    = new JsonMessageSerializer();
                    SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, tcpClient);
                    await networkConnector.AuthenticateAsServer(serverConfiguration.Certificate, CancellationToken.None);
                    networkConnector.Start();
                }, cancellationToken);
            }
        }
        /// <summary>
        /// Runs the <see cref="Program"/> asynchronously.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Returns an awaitable <see cref="Task"/>.</returns>
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            IConfiguration configuration;

            try
            {
                configuration = ConfigurationLoader.GetConfiguration("appSettings");
            }
            catch (Exception)
            {
                Console.WriteLine("Please check if you have a valid appSettings.json!");
                CancellationTokenSource.Cancel();
                return;
            }
            Startup startup = new Startup(CancellationTokenSource, 60);

            Console.WriteLine("Initializing...");
            await startup.InitializeAsync(configuration, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            Console.WriteLine("Finished initializing!\n");

            _genericServiceProvider = startup.GetGenericServiceProvider();
            GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await _genericServiceProvider.GetService <ITrainingRoomService>()
                                                                              .GetEnabledTrainingRoomsAsync(new GetEnabledTrainingRoomsRequest());

            foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms)
            {
                Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner?.Username}");
            }

            MessageToServiceMapper messageToServiceMapper = _genericServiceProvider.GetService <MessageToServiceMapper>();
            IMessageSerializer     messageSerializer      = _genericServiceProvider.GetService <IMessageSerializer>();
            IMessageProcessor      messageProcessor       = new ServerMessageProcessor(messageToServiceMapper);

            List <Task> tasks = new List <Task>();

            Console.WriteLine("Would you like to start the ClientEndPoint? y/n");
            ConsoleKeyInfo keyInfo = await WaitForReadKey(cancellationToken);

            bool runClientEndPoint = keyInfo.KeyChar == 'y';

            if (runClientEndPoint)
            {
                tasks.Add(Task.Run(() => RunClientEndPoint(cancellationToken, messageProcessor, messageSerializer), cancellationToken));
            }
            Console.WriteLine();

            Console.WriteLine("Would you like to start the RestEndPoint? y/n");
            keyInfo = await WaitForReadKey(cancellationToken);

            bool runRestEndPoint = keyInfo.KeyChar == 'y';

            if (runRestEndPoint)
            {
                tasks.Add(Task.Run(() => RunRestEndPoint(cancellationToken, messageProcessor, messageSerializer), cancellationToken));
            }
            Console.WriteLine();

            Interlocked.Exchange(ref _canReadConsole, 1);
            Interlocked.Exchange(ref _canExit, 1);

            await Task.WhenAll(tasks).ContinueWith(task =>
            {
                if (task.IsCanceled)
                {
                    CancellationTokenSource.Cancel();
                }
            }, cancellationToken);
        }