Пример #1
0
        /// <summary>
        /// Creates new queue and sends response
        /// </summary>
        private async Task CreateQueue(MqClient client, TmqMessage message)
        {
            ushort?contentType;
            NetworkOptionsBuilder builder = null;

            if (message.Length == 2)
            {
                byte[] bytes = new byte[2];
                await message.Content.ReadAsync(bytes);

                contentType = BitConverter.ToUInt16(bytes);
            }
            else
            {
                builder = new NetworkOptionsBuilder();
                builder.Load(message.ToString());
                contentType = builder.Id;
            }

            Channel channel = await CreateChannel(client, message, true);

            ChannelQueue queue = channel.FindQueue(contentType.Value);

            //if queue exists, we can't create. return duplicate response.
            if (queue != null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Duplicate));
                }

                return;
            }

            //check authority if client can create queue
            if (_server.Authorization != null)
            {
                bool grant = await _server.Authorization.CanCreateQueue(client, channel, contentType.Value, builder);

                if (!grant)
                {
                    if (message.ResponseRequired)
                    {
                        await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                    }

                    return;
                }
            }

            //creates new queue
            ChannelQueueOptions     options  = ChannelQueueOptions.CloneFrom(channel.Options);
            IMessageDeliveryHandler delivery = channel.DeliveryHandler;

            if (builder != null)
            {
                builder.ApplyToQueue(options);
                if (!string.IsNullOrEmpty(builder.MessageDeliveryHandler))
                {
                    IMessageDeliveryHandler found = _server.Registry.GetMessageDelivery(builder.MessageDeliveryHandler);
                    if (found != null)
                    {
                        delivery = found;
                    }
                }
            }

            queue = await channel.CreateQueue(contentType.Value, options, delivery);

            //if creation successful, sends response
            if (queue != null && message.ResponseRequired)
            {
                await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));
            }
        }
Пример #2
0
        /// <summary>
        /// Creates new queue and sends response
        /// </summary>
        private async Task UpdateQueue(MqClient client, TmqMessage message)
        {
            if (_server.AdminAuthorization == null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                }

                return;
            }

            NetworkOptionsBuilder builder = new NetworkOptionsBuilder();

            builder.Load(message.ToString());

            Channel channel = _server.FindChannel(message.Target);

            if (channel == null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound));
                }

                return;
            }

            ChannelQueue queue = channel.FindQueue(builder.Id);

            if (queue == null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound));
                }

                return;
            }

            bool grant = await _server.AdminAuthorization.CanUpdateQueueOptions(client, channel, queue, builder);

            if (!grant)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                }

                return;
            }

            builder.ApplyToQueue(queue.Options);
            if (builder.Status.HasValue)
            {
                await queue.SetStatus(builder.Status.Value);
            }

            //if creation successful, sends response
            if (message.ResponseRequired)
            {
                await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));
            }
        }
Пример #3
0
        /// <summary>
        /// Creates new channel
        /// </summary>
        private async Task <Channel> CreateChannel(MqClient client, TmqMessage message, bool createForQueue)
        {
            Channel channel = _server.FindChannel(message.Target);

            if (channel != null)
            {
                return(channel);
            }

            //check create channel access
            if (_server.Authorization != null)
            {
                bool grant = await _server.Authorization.CanCreateChannel(client, _server, message.Target);

                if (!grant)
                {
                    if (message.ResponseRequired)
                    {
                        await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                    }

                    return(null);
                }
            }

            if (!createForQueue && message.Length > 0 && message.Content != null && message.Content.Length > 0)
            {
                NetworkOptionsBuilder builder = new NetworkOptionsBuilder();
                builder.Load(message.ToString());

                ChannelOptions options = ChannelOptions.CloneFrom(_server.Options);
                builder.ApplyToChannel(options);

                IChannelEventHandler eventHandler = _server.DefaultChannelEventHandler;
                if (!string.IsNullOrEmpty(builder.ChannelEventHandler))
                {
                    IChannelEventHandler e = _server.Registry.GetChannelEvent(builder.ChannelEventHandler);
                    if (e != null)
                    {
                        eventHandler = e;
                    }
                }

                IChannelAuthenticator authenticator = _server.DefaultChannelAuthenticator;
                if (!string.IsNullOrEmpty(builder.ChannelAuthenticator))
                {
                    IChannelAuthenticator e = _server.Registry.GetChannelAuthenticator(builder.ChannelAuthenticator);
                    if (e != null)
                    {
                        authenticator = e;
                    }
                }

                IMessageDeliveryHandler deliveryHandler = _server.DefaultDeliveryHandler;
                if (!string.IsNullOrEmpty(builder.MessageDeliveryHandler))
                {
                    IMessageDeliveryHandler e = _server.Registry.GetMessageDelivery(builder.MessageDeliveryHandler);
                    if (e != null)
                    {
                        deliveryHandler = e;
                    }
                }

                Channel ch = _server.CreateChannel(message.Target, authenticator, eventHandler, deliveryHandler, options);

                if (ch != null && message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));
                }
            }

            Channel c = _server.CreateChannel(message.Target);

            if (!createForQueue && c != null && message.ResponseRequired)
            {
                await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));
            }

            return(c);
        }