Beispiel #1
0
 /// <summary>
 /// This method is run on the server thread.
 /// Acts as a server, waiting for any request through the HttpListener.
 /// Expects a request on a file present in the local file's directory. Whenever it receives one, it reads the file and writes its content in the request's OutputStream.
 /// The request must contain a query parameter named behaviour, with the desired behaviour to be simulated by this server while answering the respective request.
 /// </summary>
 private static void ServerThread()
 {
     while (true)
     {
         HttpListenerContext context = httpListener.GetContext();
         int    behaviour            = int.Parse(context.Request.QueryString["behaviour"]);
         string file = ServerFilesDirectory + context.Request.Url.LocalPath;
         if (!File.Exists(file))
         {
             context.Response.StatusCode = 404;
             context.Response.Close();
             continue;
         }
         byte[] buffer = new byte[DownloaderConfigs.BUFFER_SIZE];
         if (NormalBehaviour.IsThisKind(behaviour))
         {
             new NormalBehaviour().ComputeBehaviour(context, buffer, file);
         }
         if (LatencyBehaviour.IsThisKind(behaviour))
         {
             new LatencyBehaviour().ComputeBehaviour(context, buffer, file);
         }
         if (TimeoutBehaviour.IsThisKind(behaviour))
         {
             new TimeoutBehaviour().ComputeBehaviour(context, buffer, file);
         }
         if (InconsistentBehaviour.IsThisKind(behaviour))
         {
             new InconsistentBehaviour().ComputeBehaviour(context, buffer, file);
         }
         context.Response.Close();
     }
 }
Beispiel #2
0
 public TimeoutAction(TimerDirection direction_, double timeout_, TimeoutBehaviour timeoutBehaviour_, Action action_)
 {
     triggered        = false;
     direction        = direction_;
     timeout          = timeout_;
     timeoutBehaviour = timeoutBehaviour_;
     action           = action_;
 }
Beispiel #3
0
 /// <summary>
 /// Creates a new instance of <see cref="InteractivityConfiguration"/>, copying the properties of another configuration.
 /// </summary>
 /// <param name="other">Configuration the properties of which are to be copied.</param>
 public InteractivityConfiguration(InteractivityConfiguration other)
 {
     this.PaginationBehavior = other.PaginationBehavior;
     this.PaginationTimeout  = other.PaginationTimeout;
     this.Timeout            = other.Timeout;
 }
Beispiel #4
0
        public async Task SendPaginatedMessage(DiscordChannel channel, DiscordUser user, IEnumerable <Page> message_pages, TimeSpan?timeoutoverride = null, TimeoutBehaviour?timeoutbehaviouroverride = null)
        {
            if (channel == null)
            {
                throw new ArgumentNullException(nameof(channel));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (message_pages == null)
            {
                throw new ArgumentNullException(nameof(message_pages));
            }
            if (message_pages.Count() < 1)
            {
                throw new InvalidOperationException("This method can only be executed with a minimum of one page!");
            }

            TimeSpan timeout = Config.Timeout;

            if (timeoutoverride != null)
            {
                timeout = (TimeSpan)timeoutoverride;
            }

            TimeoutBehaviour timeout_behaviour = Config.PaginationBehaviour;

            if (timeoutbehaviouroverride != null)
            {
                timeout_behaviour = (TimeoutBehaviour)timeoutbehaviouroverride;
            }

            List <Page> pages = message_pages.ToList();

            if (pages.Count() == 0)
            {
                throw new ArgumentException("You need to provide at least 1 page!");
            }

            var tsc = new TaskCompletionSource <string>();
            var ct  = new CancellationTokenSource(timeout);

            ct.Token.Register(() => tsc.TrySetResult(null));

            DiscordMessage m = await this.Client.SendMessageAsync(channel, string.IsNullOrEmpty(pages.First().Content)? "" : pages.First().Content, embed : pages.First().Embed);

            PaginatedMessage pm = new PaginatedMessage()
            {
                CurrentIndex = 0,
                Pages        = pages,
                Timeout      = timeout
            };

            await this.GeneratePaginationReactions(m);

            try
            {
                this.Client.MessageReactionsCleared += ReactionClearHandler;
                this.Client.MessageReactionAdded    += ReactionAddHandler;
                this.Client.MessageReactionRemoved  += ReactionRemoveHandler;

                await tsc.Task;

                switch (timeout_behaviour)
                {
                case TimeoutBehaviour.Default:
                case TimeoutBehaviour.Ignore:
                    await m.DeleteAllReactionsAsync();

                    break;

                case TimeoutBehaviour.Delete:
                    await m.DeleteAllReactionsAsync();

                    await m.DeleteAsync();

                    break;
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                this.Client.MessageReactionsCleared -= ReactionClearHandler;
                this.Client.MessageReactionAdded    -= ReactionAddHandler;
                this.Client.MessageReactionRemoved  -= ReactionRemoveHandler;
            }

            #region Handlers
            async Task ReactionAddHandler(MessageReactionAddEventArgs e)
            {
                if (e.Message.Id == m.Id && e.User.Id != this.Client.CurrentUser.Id && e.User.Id == user.Id)
                {
                    await this.DoPagination(e.Emoji, m, pm, ct);
                }
            }

            async Task ReactionRemoveHandler(MessageReactionRemoveEventArgs e)
            {
                if (e.Message.Id == m.Id && e.User.Id != this.Client.CurrentUser.Id && e.User.Id == user.Id)
                {
                    await this.DoPagination(e.Emoji, m, pm, ct);
                }
            }

            async Task ReactionClearHandler(MessageReactionsClearEventArgs e)
            {
                await this.GeneratePaginationReactions(m);
            }

            #endregion
        }
Beispiel #5
0
 //Setters
 public int addAction(TimerDirection direction, double timeout, TimeoutBehaviour timeoutBehaviour, Action action)
 {
     actions.Add(new TimeoutAction(direction, timeout, timeoutBehaviour, action));
     return(actions.Count - 1);
 }
Beispiel #6
0
        public async Task SendPaginatedMessage(DiscordChannel channel, DiscordUser user, IEnumerable <Page> message_pages, TimeSpan timeout, TimeoutBehaviour timeout_behaviour)
        {
            List <Page> pages = message_pages.ToList();

            if (pages.Count() == 0)
            {
                throw new ArgumentException("You need to provide at least 1 page!");
            }

            var tsc = new TaskCompletionSource <string>();
            var ct  = new CancellationTokenSource(timeout);

            ct.Token.Register(() => tsc.TrySetResult(null));

            DiscordMessage m = await this.Client.SendMessageAsync(channel, string.IsNullOrEmpty(pages.First().Content)? "" : pages.First().Content, embed : pages.First().Embed);

            PaginatedMessage pm = new PaginatedMessage()
            {
                CurrentIndex = 0,
                Pages        = pages,
                Timeout      = timeout
            };

            await this.GeneratePaginationReactions(m);

            AsyncEventHandler <MessageReactionsClearEventArgs> _reaction_removed_all = async e =>
            {
                await this.GeneratePaginationReactions(m);
            };

            this.Client.MessageReactionsCleared += _reaction_removed_all;

            AsyncEventHandler <MessageReactionAddEventArgs> _reaction_added = async e =>
            {
                if (e.Message.Id == m.Id && e.User.Id != this.Client.CurrentUser.Id && e.User.Id == user.Id)
                {
                    await this.DoPagination(e.Emoji, m, pm, ct);
                }
            };

            this.Client.MessageReactionAdded += _reaction_added;

            AsyncEventHandler <MessageReactionRemoveEventArgs> _reaction_removed = async e =>
            {
                if (e.Message.Id == m.Id && e.User.Id != this.Client.CurrentUser.Id && e.User.Id == user.Id)
                {
                    await this.DoPagination(e.Emoji, m, pm, ct);
                }
            };

            this.Client.MessageReactionRemoved += _reaction_removed;

            await tsc.Task;

            switch (timeout_behaviour)
            {
            case TimeoutBehaviour.Default:
            case TimeoutBehaviour.Ignore:
                await m.DeleteAllReactionsAsync();

                break;

            case TimeoutBehaviour.Delete:
                await m.DeleteAllReactionsAsync();

                await m.DeleteAsync();

                break;
            }

            this.Client.MessageReactionsCleared -= _reaction_removed_all;
            this.Client.MessageReactionAdded    -= _reaction_added;
            this.Client.MessageReactionRemoved  -= _reaction_removed;
        }