Ejemplo n.º 1
0
        private TmqMessage CreateMessage()
        {
            TmqMessage msg = new TmqMessage(MessageType.Channel, "channel");

            msg.SetMessageId(_generator.Create());
            msg.SetStringContent("Hello, World!");
            return(msg);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fills binary data to the queue
        /// </summary>
        public PushResult FillData(IEnumerable <byte[]> items, bool createAsSaved, bool highPriority)
        {
            if (_queue.Status == QueueStatus.Stopped)
            {
                return(PushResult.StatusNotSupported);
            }

            int max = _queue.HighPriorityLinkedList.Count + _queue.RegularLinkedList.Count + items.Count();

            if (_queue.Options.MessageLimit > 0 && max > _queue.Options.MessageLimit)
            {
                return(PushResult.LimitExceeded);
            }

            foreach (byte[] item in items)
            {
                TmqMessage message = new TmqMessage(MessageType.Channel, _queue.Channel.Name);
                message.FirstAcquirer       = true;
                message.HighPriority        = highPriority;
                message.AcknowledgeRequired = _queue.Options.RequestAcknowledge;
                message.ContentType         = _queue.Id;

                if (_queue.Options.UseMessageId)
                {
                    message.SetMessageId(_queue.Channel.Server.MessageIdGenerator.Create());
                }

                message.Content          = new MemoryStream(item);
                message.Content.Position = 0;
                message.CalculateLengths();

                QueueMessage qm = new QueueMessage(message, createAsSaved);

                if (highPriority)
                {
                    lock (_queue.HighPriorityLinkedList)
                        _queue.HighPriorityLinkedList.AddLast(qm);
                }
                else
                {
                    lock (_queue.RegularLinkedList)
                        _queue.RegularLinkedList.AddLast(qm);
                }
            }

            _queue.Info.UpdateHighPriorityMessageCount(_queue.HighPriorityLinkedList.Count);
            _queue.Info.UpdateRegularMessageCount(_queue.RegularLinkedList.Count);
            return(PushResult.Success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates new response message, with no content, of the message.
        /// </summary>
        public static TmqMessage ResponseStatus(TmqMessage request, ushort status)
        {
            TmqMessage response = new TmqMessage();

            response.Type = MessageType.Response;
            response.SetMessageId(request.MessageId);
            response.ContentType   = status;
            response.FirstAcquirer = true;

            response.SetTarget(request.Type == MessageType.Channel
                                   ? request.Target
                                   : request.Source);

            return(response);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Removes a channel and all queues in it
        /// </summary>
        public async Task <TmqResponseCode> RemoveChannel(string channel, bool verifyResponse)
        {
            TmqMessage message = new TmqMessage();

            message.Type        = MessageType.Server;
            message.ContentType = KnownContentTypes.RemoveChannel;
            message.SetTarget(channel);
            message.ResponseRequired = verifyResponse;

            if (verifyResponse)
            {
                message.SetMessageId(UniqueIdGenerator.Create());
            }

            return(await WaitResponse(message, verifyResponse));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fills JSON object data to the queue.
        /// Creates new TmqMessage and before writing content and adding into queue calls the action.
        /// </summary>
        public async Task <PushResult> FillJson <T>(IEnumerable <T> items, bool createAsSaved, Action <TmqMessage, T> action) where T : class
        {
            if (_queue.Status == QueueStatus.Stopped)
            {
                return(PushResult.StatusNotSupported);
            }

            int max = _queue.HighPriorityLinkedList.Count + _queue.RegularLinkedList.Count + items.Count();

            if (_queue.Options.MessageLimit > 0 && max > _queue.Options.MessageLimit)
            {
                return(PushResult.LimitExceeded);
            }

            foreach (T item in items)
            {
                TmqMessage message = new TmqMessage(MessageType.Channel, _queue.Channel.Name);
                message.FirstAcquirer       = true;
                message.AcknowledgeRequired = _queue.Options.RequestAcknowledge;
                message.ContentType         = _queue.Id;

                if (_queue.Options.UseMessageId)
                {
                    message.SetMessageId(_queue.Channel.Server.MessageIdGenerator.Create());
                }

                action(message, item);
                await message.SetJsonContent(item);

                QueueMessage qm = new QueueMessage(message, createAsSaved);

                if (message.HighPriority)
                {
                    lock (_queue.HighPriorityLinkedList)
                        _queue.HighPriorityLinkedList.AddLast(qm);
                }
                else
                {
                    lock (_queue.RegularLinkedList)
                        _queue.RegularLinkedList.AddLast(qm);
                }
            }

            _queue.Info.UpdateHighPriorityMessageCount(_queue.HighPriorityLinkedList.Count);
            _queue.Info.UpdateRegularMessageCount(_queue.RegularLinkedList.Count);
            return(PushResult.Success);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Updates queue options
        /// </summary>
        public async Task <TmqResponseCode> SetQueueOptions(string channel, ushort queueId, Action <QueueOptions> optionsAction)
        {
            TmqMessage message = new TmqMessage();

            message.Type        = MessageType.Server;
            message.ContentType = KnownContentTypes.UpdateQueue;
            message.SetTarget(channel);
            message.ResponseRequired = true;
            message.SetMessageId(UniqueIdGenerator.Create());

            QueueOptions options = new QueueOptions();

            optionsAction(options);
            message.Content = new MemoryStream(Encoding.UTF8.GetBytes(options.Serialize(queueId)));

            return(await WaitResponse(message, true));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Removes a queue in a channel in server
        /// </summary>
        public async Task <TmqResponseCode> RemoveQueue(string channel, ushort queueId, bool verifyResponse)
        {
            TmqMessage message = new TmqMessage();

            message.Type        = MessageType.Server;
            message.ContentType = KnownContentTypes.RemoveQueue;
            message.SetTarget(channel);
            message.ResponseRequired = verifyResponse;
            message.Content          = new MemoryStream(BitConverter.GetBytes(queueId));

            if (verifyResponse)
            {
                message.SetMessageId(UniqueIdGenerator.Create());
            }

            return(await WaitResponse(message, verifyResponse));
        }