Example #1
0
        /// <summary>
        /// Finds and subscribes to the queue and sends response
        /// </summary>
        private async Task Subscribe(MqClient client, HorseMessage message)
        {
            HorseQueue queue = _server.FindQueue(message.Target);

            //if auto creation active, try to create queue
            if (queue == null && _server.Options.AutoQueueCreation)
            {
                QueueOptions options = QueueOptions.CloneFrom(_server.Options);
                queue = await _server.CreateQueue(message.Target, options, message, _server.DeliveryHandlerFactory, true, true);
            }

            if (queue == null)
            {
                if (message.WaitResponse)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.NotFound));
                }

                return;
            }

            QueueClient found = queue.FindClient(client.UniqueId);

            if (found != null)
            {
                if (message.WaitResponse)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));
                }

                return;
            }

            QueueSubscriptionResult result = await queue.AddClient(client);

            if (message.WaitResponse)
            {
                switch (result)
                {
                case QueueSubscriptionResult.Success:
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));

                    break;

                case QueueSubscriptionResult.Unauthorized:
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Unauthorized));

                    break;

                case QueueSubscriptionResult.Full:
                    await client.SendAsync(message.CreateResponse(HorseResultCode.LimitExceeded));

                    break;
                }
            }
        }
        /// <summary>
        /// Handles pulling a message from a queue
        /// </summary>
        private async Task HandlePullRequest(MqClient client, HorseMessage message, HorseQueue queue)
        {
            //only pull statused queues can handle this request
            if (queue.Status != QueueStatus.Pull)
            {
                if (!string.IsNullOrEmpty(message.MessageId))
                {
                    await client.SendAsync(MessageBuilder.CreateNoContentPullResponse(message, HorseHeaders.UNACCEPTABLE));
                }

                return;
            }

            //client cannot pull message from the queue not in
            QueueClient queueClient = queue.FindClient(client);

            if (queueClient == null)
            {
                if (!string.IsNullOrEmpty(message.MessageId))
                {
                    await client.SendAsync(MessageBuilder.CreateNoContentPullResponse(message, HorseHeaders.UNACCEPTABLE));
                }

                return;
            }

            //check authorization
            foreach (IClientAuthorization authorization in _server.Authorizations)
            {
                bool grant = await authorization.CanPullFromQueue(queueClient, queue);

                if (!grant)
                {
                    if (!string.IsNullOrEmpty(message.MessageId))
                    {
                        await client.SendAsync(MessageBuilder.CreateNoContentPullResponse(message, HorseHeaders.UNAUTHORIZED));
                    }
                    return;
                }
            }

            await queue.State.Pull(queueClient, message);
        }