Exemplo n.º 1
0
        /// <summary>
        /// Creates new queue and sends response
        /// </summary>
        private async Task UpdateQueue(MqClient client, HorseMessage message)
        {
            NetworkOptionsBuilder builder = await System.Text.Json.JsonSerializer.DeserializeAsync <NetworkOptionsBuilder>(message.Content);

            HorseQueue queue = _server.FindQueue(message.Target);

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

                return;
            }

            foreach (IAdminAuthorization authorization in _server.AdminAuthorizations)
            {
                bool grant = await authorization.CanUpdateQueueOptions(client, queue, builder);

                if (!grant)
                {
                    if (message.WaitResponse)
                    {
                        await client.SendAsync(message.CreateResponse(HorseResultCode.Unauthorized));
                    }

                    return;
                }
            }

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

            _server.OnQueueUpdated.Trigger(queue);

            //if creation successful, sends response
            if (message.WaitResponse)
            {
                await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates new queue and sends response
        /// </summary>
        private async Task CreateQueue(MqClient client, HorseMessage message)
        {
            NetworkOptionsBuilder builder = null;

            if (message.Length > 0)
            {
                builder = await System.Text.Json.JsonSerializer.DeserializeAsync <NetworkOptionsBuilder>(message.Content);
            }

            HorseQueue queue = _server.FindQueue(message.Target);

            //if queue exists, we can't create. return duplicate response.
            if (queue != null)
            {
                if (message.WaitResponse)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Duplicate));
                }

                return;
            }

            //check authority if client can create queue
            foreach (IClientAuthorization authorization in _server.Authorizations)
            {
                bool grant = await authorization.CanCreateQueue(client, message.Target, builder);

                if (!grant)
                {
                    if (message.WaitResponse)
                    {
                        await client.SendAsync(message.CreateResponse(HorseResultCode.Unauthorized));
                    }

                    return;
                }
            }

            //creates new queue
            QueueOptions options = QueueOptions.CloneFrom(_server.Options);

            if (builder != null)
            {
                builder.ApplyToQueue(options);
            }

            queue = await _server.CreateQueue(message.Target, options, message, _server.DeliveryHandlerFactory, true, false);

            //if creation successful, sends response
            if (message.WaitResponse)
            {
                if (queue != null)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));
                }
                else
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Failed));
                }
            }
        }
 public Task <bool> CanUpdateQueueOptions(MqClient client, HorseQueue queue, NetworkOptionsBuilder options)
 {
     return(Task.FromResult(true));
 }
Exemplo n.º 7
0
 public async Task <bool> CanUpdateQueueOptions(MqClient client, Channel channel, ChannelQueue queue, NetworkOptionsBuilder options)
 {
     return(await Task.FromResult(true));
 }
Exemplo n.º 8
0
        public async Task <bool> CanCreateQueue(MqClient client, Channel channel, ushort contentType, NetworkOptionsBuilder options)
        {
            bool grant = client.Type.Equals("producer");

            Console.WriteLine("Can create new queue: " + grant);
            return(await Task.FromResult(grant));
        }