Esempio n. 1
0
        private Task <PersistentResponse> GetResponse(long messageId)
        {
            // Get all messages for the current set of signals
            return(GetMessages(messageId, Signals)
                   .Success(messageTask =>
            {
                var results = messageTask.Result;
                if (!results.Any())
                {
                    return WaitForSignal(messageId);
                }

                var response = new PersistentResponse();

                var commands = results.Where(m => m.SignalKey.EndsWith(PersistentConnection.SignalrCommand));

                ProcessCommands(commands);

                messageId = results.Max(p => p.Id);

                // Get the message values and the max message id we received
                var messageValues = results.Except(commands)
                                    .Select(m => m.Value)
                                    .ToList();

                response.MessageId = messageId;
                response.Messages = messageValues;

                // Set the groups on the outgoing transport data
                response.TransportData["Groups"] = _groups;

                return TaskAsyncHelper.FromResult(response);
            })
                   .Unwrap());
        }
        public Task <long?> GetLastId()
        {
            if (_lastMessageId > 0)
            {
                return(TaskAsyncHelper.FromResult <long?>(_lastMessageId));
            }

            return(TaskAsyncHelper.FromResult <long?>(null));
        }
Esempio n. 3
0
        public Task <string> GetLastId()
        {
            if (_lastMessageId > 0)
            {
                return(TaskAsyncHelper.FromResult(_lastMessageId.ToString(CultureInfo.InvariantCulture)));
            }

            return(TaskAsyncHelper.FromResult((string)null));
        }
Esempio n. 4
0
        public Task <string> GetLastId()
        {
            if (_lastMessageId > 0)
            {
                return(TaskAsyncHelper.FromResult(Interlocked.Read(ref _lastMessageId).ToString(CultureInfo.InvariantCulture)));
            }

            return(_zeroTask);
        }
Esempio n. 5
0
        public Task <IEnumerable <Message> > GetAllSince(string key, long id)
        {
            if (id > _lastMessageId)
            {
                id = 0;
            }

            var items = GetAllCoreSince(key, id).OrderBy(item => item.Id);

            return(TaskAsyncHelper.FromResult <IEnumerable <Message> >(items));
        }
Esempio n. 6
0
        public Task <IOrderedEnumerable <Message> > GetAllSince(IEnumerable <string> keys, long id)
        {
            if (id > _lastMessageId)
            {
                id = 0;
            }

            var items = keys.SelectMany(k => GetAllCore(k).Where(item => item.Id > id))
                        .OrderBy(item => item.Id);

            return(TaskAsyncHelper.FromResult <IOrderedEnumerable <Message> >(items));
        }
Esempio n. 7
0
        public Task <IEnumerable <Message> > GetAllSince(string key, long id)
        {
            if (id > _lastMessageId)
            {
                id = 0;
            }

            var items = GetAllCore(key).Where(item => Int64.Parse(item.Id, CultureInfo.InvariantCulture) > id)
                        .OrderBy(item => item.Id);

            return(TaskAsyncHelper.FromResult <IEnumerable <Message> >(items));
        }
Esempio n. 8
0
        private Task <PersistentResponse> ProcessSignal(Task <SignalResult> signalTask, long?messageId = null)
        {
            if (signalTask.Result.TimedOut)
            {
                PersistentResponse response = GetEmptyResponse(messageId);

                // Return a task wrapping the result
                return(TaskAsyncHelper.FromResult(response));
            }

            // Get the response for this message id
            return(GetResponse(messageId ?? 0).Success(task => task.Result ?? GetEmptyResponse(messageId)));
        }
Esempio n. 9
0
        private Task <PersistentResponse> ProcessSignal(SignalResult result, long?messageId = null)
        {
            if (result.TimedOut)
            {
                PersistentResponse response = GetEmptyResponse(messageId);

                // Return a task wrapping the result
                return(TaskAsyncHelper.FromResult(response));
            }

            // Get the response for this message id
            return(GetResponse(messageId ?? 0)
                   .Then <PersistentResponse, long?>((response, id) => response ?? GetEmptyResponse(id), messageId));
        }
Esempio n. 10
0
        private Task <IEnumerable <Message> > GetMessages(long id, IEnumerable <string> signals)
        {
            var pendingMessagesTasks = (from signal in signals
                                        select _store.GetAllSince(signal, id)).ToArray();

            // If there are no pending messages, we need to shortcut since ContinueWhenAll
            // blows up for empty arrays.
            if (!pendingMessagesTasks.Any())
            {
                return(TaskAsyncHelper.FromResult(Enumerable.Empty <Message>()));
            }

            // Wait until all of the tasks are done before we return
            return(pendingMessagesTasks.AllSucceeded(() => (IEnumerable <Message>)pendingMessagesTasks.SelectMany(t => t.Result).OrderBy(m => m.Id).ToList()));
        }
Esempio n. 11
0
        public Task <IOrderedEnumerable <Message> > GetAllSince(IEnumerable <string> keys, string id)
        {
            long longId;

            if (!Int64.TryParse(id, NumberStyles.Any, CultureInfo.InvariantCulture, out longId))
            {
                throw new ArgumentException("id must be a valid integer", "id");
            }

            if (longId > _lastMessageId)
            {
                longId = 0;
            }

            var items = keys.SelectMany(k => GetAllCore(k).Where(item => Int64.Parse(item.Id, CultureInfo.InvariantCulture) > longId))
                        .OrderBy(item => item.Id);

            return(TaskAsyncHelper.FromResult <IOrderedEnumerable <Message> >(items));
        }
Esempio n. 12
0
        private Task <PersistentResponse> ProcessSignal(Task <SignalResult> signalTask, long?messageId = null)
        {
            if (signalTask.Result.TimedOut)
            {
                // If we timed out waiting for a signal we have a message id then return null
                PersistentResponse response = null;

                // Otherwise ee need to return 0 so that the next request we'll get all messages
                // on the next try
                if (messageId == null)
                {
                    response = new PersistentResponse
                    {
                        MessageId = 0
                    };
                }

                // Return a task wrapping the result
                return(TaskAsyncHelper.FromResult(response));
            }

            // Get the response for this message id
            return(GetResponse(messageId ?? 0));
        }