コード例 #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, 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);
        }
コード例 #2
0
 /// <summary>
 /// Initializes an instance of the <see cref="ServerMessageProcessor"/> class.
 /// </summary>
 /// <param name="messageToServiceMapper">The message to service mapper.</param>
 public ServerMessageProcessor(MessageToServiceMapper messageToServiceMapper)
 {
     _messageToServiceMapper = messageToServiceMapper;
     _observers = new ConcurrentDictionary <Type, ObserverCollection>();
 }