From 8b965483b4373d94cdbd65ddc8cee586e31d2c73 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 18 Jul 2018 13:29:03 -0400 Subject: [PATCH] More IrcProvider stuff, implement message monitor --- .../Components/Chat/Chat.cs | 79 ++++++++++++++++++- .../Components/Chat/Providers/IrcProvider.cs | 11 +-- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Chat/Chat.cs b/src/Tgstation.Server.Host/Components/Chat/Chat.cs index fb9b656ef0..b1d0b28349 100644 --- a/src/Tgstation.Server.Host/Components/Chat/Chat.cs +++ b/src/Tgstation.Server.Host/Components/Chat/Chat.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -45,6 +46,16 @@ namespace Tgstation.Server.Host.Components.Chat /// readonly List trackingContexts; + /// + /// The for + /// + readonly CancellationTokenSource handlerCts; + + /// + /// The that monitors incoming chat messages + /// + Task chatHandler; + /// /// The for the /// @@ -75,12 +86,14 @@ namespace Tgstation.Server.Host.Components.Chat providers = new Dictionary(); mappedChannels = new Dictionary(); trackingContexts = new List(); + handlerCts = new CancellationTokenSource(); channelIdCounter = 1; } /// public void Dispose() { + handlerCts.Dispose(); foreach (var I in providers) I.Value.Dispose(); } @@ -114,6 +127,64 @@ namespace Tgstation.Server.Host.Components.Chat return provider; } + /// + /// Processes a + /// + /// The who recevied + /// The to process + /// The for the operation + /// A representing the running operation + async Task ProcessMessage(IProvider provider, Message message, CancellationToken cancellationToken) + { + if (!(message.Content.StartsWith(CommonMention, StringComparison.InvariantCultureIgnoreCase) || message.Content.StartsWith(provider.BotMention, StringComparison.Ordinal))) + //no mention + return; + + await Task.Yield(); + lock (this) + throw new NotImplementedException(); + } + + /// + /// Monitors active providers for new s + /// + /// The for the operation + /// A representing the running operation + async Task MonitorMessages(CancellationToken cancellationToken) + { + var messageTasks = new Dictionary>(); + try + { + while (!cancellationToken.IsCancellationRequested) + { + //prune disconnected providers + foreach (var I in messageTasks) + if (!I.Key.Connected) + messageTasks.Remove(I.Key); + + //add new ones + foreach (var I in providers) + if (I.Value.Connected && !messageTasks.ContainsKey(I.Value)) + messageTasks.Add(I.Value, I.Value.NextMessage(cancellationToken)); + + //wait for a message + var tasks = messageTasks.Select(x => x.Value); + await Task.WhenAny().ConfigureAwait(false); + + //process completed ones + foreach (var I in messageTasks.Where(x => x.Value.IsCompleted)) + { + messageTasks.Remove(I.Key); + + var message = await I.Value.ConfigureAwait(false); + + await ProcessMessage(I.Key, message, cancellationToken).ConfigureAwait(false); + } + } + } + catch (OperationCanceledException) { } + } + /// public async Task ChangeChannels(long connectionId, IEnumerable newChannels, CancellationToken cancellationToken) { @@ -221,11 +292,17 @@ namespace Tgstation.Server.Host.Components.Chat public async Task StartAsync(CancellationToken cancellationToken) { await Task.WhenAll(providers.Select(x => x.Value).Select(x => x.Connect(cancellationToken))).ConfigureAwait(false); + chatHandler = MonitorMessages(handlerCts.Token); started = true; } /// - public Task StopAsync(CancellationToken cancellationToken) => Task.WhenAll(providers.Select(x => x.Value).Select(x => x.Disconnect(cancellationToken))); + public async Task StopAsync(CancellationToken cancellationToken) + { + handlerCts.Cancel(); + await chatHandler.ConfigureAwait(false); + await Task.WhenAll(providers.Select(x => x.Value).Select(x => x.Disconnect(cancellationToken))).ConfigureAwait(false); + } /// public async Task TrackJsons(string basePath, string channelsJsonName, string commandsJsonName, CancellationToken cancellationToken) diff --git a/src/Tgstation.Server.Host/Components/Chat/Providers/IrcProvider.cs b/src/Tgstation.Server.Host/Components/Chat/Providers/IrcProvider.cs index 49410fc57d..f1ddcf7a30 100644 --- a/src/Tgstation.Server.Host/Components/Chat/Providers/IrcProvider.cs +++ b/src/Tgstation.Server.Host/Components/Chat/Providers/IrcProvider.cs @@ -16,10 +16,10 @@ namespace Tgstation.Server.Host.Components.Chat.Providers sealed class IrcProvider : IProvider { /// - public bool Connected => throw new NotImplementedException(); + public bool Connected => client.IsConnected; /// - public string BotMention => throw new NotImplementedException(); + public string BotMention => client.Nickname; /// /// The for the @@ -149,10 +149,11 @@ namespace Tgstation.Server.Host.Components.Chat.Providers }, cancellationToken,TaskCreationOptions.LongRunning, TaskScheduler.Current); /// - public Task Disconnect(CancellationToken cancellationToken) + public Task Disconnect(CancellationToken cancellationToken) => Task.Factory.StartNew(() => { - throw new NotImplementedException(); - } + + + }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current); /// public Task> MapChannels(IEnumerable channels, CancellationToken cancellationToken)