Beispiel #1
0
        public async override Task <ResourceResponse[]> SendActivities(ITurnContext context, Activity[] activities)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (activities == null)
            {
                throw new ArgumentNullException(nameof(activities));
            }

            if (activities.Length == 0)
            {
                throw new ArgumentException("Expecting one or more activities, but the array was empty.", nameof(activities));
            }

            var responses = new ResourceResponse[activities.Length];

            // NOTE: we're using for here (vs. foreach) because we want to simultaneously index into the
            // activities array to get the activity to process as well as use that index to assign
            // the response to the responses array and this is the most cost effective way to do that.
            for (var index = 0; index < activities.Length; index++)
            {
                var activity = activities[index];

                if (String.IsNullOrEmpty(activity.Id))
                {
                    activity.Id = Guid.NewGuid().ToString("n");
                }

                if (activity.Timestamp == null)
                {
                    activity.Timestamp = DateTime.UtcNow;
                }


                if (activity.Type == ActivityTypesEx.Delay)
                {
                    // The BotFrameworkAdapter and Console adapter implement this
                    // hack directly in the POST method. Replicating that here
                    // to keep the behavior as close as possible to facillitate
                    // more realistic tests.
                    int delayMs = (int)activity.Value;

                    await Task.Delay(delayMs);
                }
                else
                {
                    lock (_activeQueueLock)
                    {
                        ActiveQueue.Enqueue(activity);
                    }
                }

                responses[index] = new ResourceResponse(activity.Id);
            }

            return(responses);
        }
Beispiel #2
0
        /// <summary>
        /// Adds a row to current Log file.
        /// </summary>
        /// <param name="columns">Columns to write on row.</param>
        /// <param name="separator">Column separator.</param>
        public void AddRow(string[] columns, string separator = "   ")
        {
            var row = new StringBuilder();

            row.Append(DateTime.UtcNow);
            foreach (string column in columns)
            {
                row.Append(separator);
                row.Append(column);
            }

            if (Async)
            {
                ActiveQueue.Enqueue(row.ToString());
                if (!Writing)
                {
                    StartTask();
                }
            }
            else
            {
                CheckIfNewFileNeeded();
                TryWrite(CurrentLogFile, row.ToString());
            }
        }
        private void Enqueue(Activity activity)
        {
            lock (_activeQueueLock)
            {
                // if there are pending requests, fulfill them with the activity.
                while (_queuedRequests.Any())
                {
                    var tcs = _queuedRequests.Dequeue();
                    if (tcs.Task.IsCanceled == false)
                    {
                        tcs.SetResult(activity);
                        return;
                    }
                }

                // else we enqueue for next requester
                ActiveQueue.Enqueue(activity);
            }
        }
Beispiel #4
0
 public override Task DeleteActivity(ITurnContext context, ConversationReference reference)
 {
     lock (_activeQueueLock)
     {
         var replies = ActiveQueue.ToList();
         for (int i = 0; i < ActiveQueue.Count; i++)
         {
             if (replies[i].Id == reference.ActivityId)
             {
                 replies.RemoveAt(i);
                 ActiveQueue.Clear();
                 foreach (var item in replies)
                 {
                     ActiveQueue.Enqueue(item);
                 }
                 break;
             }
         }
     }
     return(Task.CompletedTask);
 }
Beispiel #5
0
 public override Task <ResourceResponse> UpdateActivity(ITurnContext context, Activity activity)
 {
     lock (_activeQueueLock)
     {
         var replies = ActiveQueue.ToList();
         for (int i = 0; i < ActiveQueue.Count; i++)
         {
             if (replies[i].Id == activity.Id)
             {
                 replies[i] = activity;
                 ActiveQueue.Clear();
                 foreach (var item in replies)
                 {
                     ActiveQueue.Enqueue(item);
                 }
                 return(Task.FromResult(new ResourceResponse(activity.Id)));
             }
         }
     }
     return(Task.FromResult(new ResourceResponse()));
 }