/// <summary> /// Adds new binding to a router /// </summary> /// <param name="routerName">Router name of the binding</param> /// <param name="type">Binding type</param> /// <param name="name">Binding name</param> /// <param name="target">Binding target. Queue name, tag name, direct receiver id, name, type, etc.</param> /// <param name="interaction">Binding interaction</param> /// <param name="bindingMethod">Binding method is used when multiple receivers available in same binding. It's used for Direct and Tag bindings.</param> /// <param name="contentType">Overwritten content type if specified</param> /// <param name="priority">Binding priority</param> /// <returns></returns> public async Task <HorseResult> AddBinding(string routerName, BindingType type, string name, string target, BindingInteraction interaction, RouteMethod bindingMethod = RouteMethod.Distribute, ushort?contentType = null, int priority = 1) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.AddBinding; message.SetTarget(routerName); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); BindingInformation info = new BindingInformation { Name = name, Target = target, Interaction = interaction, ContentType = contentType, Priority = priority, BindingType = type, Method = bindingMethod }; message.Serialize(info, new NewtonsoftContentSerializer()); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Sends a memory stream message by full name /// </summary> private async Task <HorseResult> SendById(string id, ushort contentType, MemoryStream content, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage message = new HorseMessage(); message.SetTarget(id); message.ContentType = contentType; message.Content = content; message.Type = MessageType.DirectMessage; if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (waitAcknowledge) { return(await _client.SendAndGetAck(message)); } return(await _client.SendAsync(message)); }
/// <summary> /// Pushes new message into the queue /// </summary> public Task <PushResult> Push(HorseMessage message) { message.Type = MessageType.QueueMessage; message.SetTarget(Name); return(Push(new QueueMessage(message), null)); }
/// <summary> /// Clears messages in a queue. /// Required administration permission. /// If server has no implementation for administration authorization, request is not allowed. /// </summary> public Task <HorseResult> ClearMessages(string queue, bool clearPriorityMessages, bool clearMessages) { if (!clearPriorityMessages && !clearMessages) { return(Task.FromResult(HorseResult.Failed())); } HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.ClearMessages; message.SetTarget(queue); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); if (clearPriorityMessages) { message.AddHeader(HorseHeaders.PRIORITY_MESSAGES, "yes"); } if (clearMessages) { message.AddHeader(HorseHeaders.MESSAGES, "yes"); } return(_client.WaitResponse(message, true)); }
/// <summary> /// Sends a JSON message by full name /// </summary> public async Task <HorseResult> SendJsonById <T>(string id, ushort contentType, T model, bool waitAcknowledge, IEnumerable <KeyValuePair <string, string> > messageHeaders = null) { HorseMessage message = new HorseMessage(); message.SetTarget(id); message.Type = MessageType.DirectMessage; message.ContentType = contentType; message.Serialize(model, _client.JsonSerializer); if (messageHeaders != null) { foreach (KeyValuePair <string, string> pair in messageHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (waitAcknowledge) { return(await _client.SendAndGetAck(message)); } return(await _client.SendAsync(message)); }
/// <summary> /// Sends the message to binding receivers /// </summary> public override async Task <bool> Send(MqClient sender, HorseMessage message) { try { HorseQueue queue = await GetQueue(message); if (queue == null) { return(false); } string messageId = Interaction == BindingInteraction.None ? Router.Server.MessageIdGenerator.Create() : message.MessageId; HorseMessage msg = message.Clone(true, true, messageId); msg.Type = MessageType.QueueMessage; msg.SetTarget(Target); msg.WaitResponse = Interaction == BindingInteraction.Response; QueueMessage queueMessage = new QueueMessage(msg); queueMessage.Source = sender; PushResult result = await queue.Push(queueMessage, sender); return(result == PushResult.Success); } catch (Exception e) { Router.Server.SendError("BINDING_SEND", e, $"Type:AutoQueue, Binding:{Name}"); return(false); } }
/// <summary> /// Sends the message to binding receivers /// </summary> public override async Task <bool> Send(MqClient sender, HorseMessage message) { try { MqClient[] clients = GetClients(); if (clients.Length == 0) { return(false); } message.Type = MessageType.DirectMessage; message.SetTarget(Target); if (ContentType.HasValue) { message.ContentType = ContentType.Value; } message.WaitResponse = Interaction == BindingInteraction.Response; switch (RouteMethod) { case RouteMethod.OnlyFirst: var first = clients.FirstOrDefault(); if (first == null) { return(false); } return(await first.SendAsync(message)); case RouteMethod.Distribute: bool atLeastOneSent = false; foreach (MqClient client in clients) { bool sent = await client.SendAsync(message); if (sent && !atLeastOneSent) { atLeastOneSent = true; } } return(atLeastOneSent); case RouteMethod.RoundRobin: return(await SendRoundRobin(message)); default: return(false); } } catch (Exception e) { Router.Server.SendError("BINDING_SEND", e, $"Type:Direct, Binding:{Name}"); return(false); } }
/// <summary> /// Gets all bindings of a router /// </summary> public async Task <HorseModelResult <List <BindingInformation> > > GetBindings(string routerName) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.ListBindings; message.SetTarget(routerName); return(await _client.SendAndGetJson <List <BindingInformation> >(message)); }
/// <summary> /// Creates new status code message /// </summary> public static HorseMessage StatusCodeMessage(ushort contentType, string target = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = contentType; message.SetTarget(target); return(message); }
/// <summary> /// Gets all consumers of queue /// </summary> public Task <HorseModelResult <List <ClientInformation> > > GetConnectedClients(string typeFilter = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.ClientList; message.SetTarget(typeFilter); return(_client.SendAndGetJson <List <ClientInformation> >(message)); }
/// <summary> /// Creates new Pull Request response message with no content /// </summary> internal static HorseMessage CreateNoContentPullResponse(HorseMessage request, string reason) { HorseMessage msg = new HorseMessage(MessageType.QueueMessage); msg.SetMessageId(request.MessageId); msg.SetTarget(request.Target); msg.ContentType = request.ContentType; msg.AddHeader(HorseHeaders.REQUEST_ID, request.MessageId); msg.AddHeader(HorseHeaders.NO_CONTENT, reason); return(msg); }
/// <summary> /// Removes a router. /// Returns success result if router doesn't exists. /// </summary> public async Task <HorseResult> Remove(string name) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.RemoveRouter; message.SetTarget(name); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Creates new router. /// Returns success result if router already exists. /// </summary> public async Task <HorseResult> Create(string name, RouteMethod method) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.CreateRouter; message.SetTarget(name); message.WaitResponse = true; message.AddHeader(HorseHeaders.ROUTE_METHOD, Convert.ToInt32(method).ToString()); message.SetMessageId(_client.UniqueIdGenerator.Create()); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Remove a binding from a router /// </summary> public async Task <HorseResult> RemoveBinding(string routerName, string bindingName) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.RemoveBinding; message.SetTarget(routerName); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.BINDING_NAME, bindingName); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Gets all consumers of queue /// </summary> public async Task <HorseModelResult <List <ClientInformation> > > GetConsumers(string queue) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.SetTarget(queue); message.ContentType = KnownContentTypes.QueueConsumers; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); return(await _client.SendAndGetJson <List <ClientInformation> >(message)); }
/// <summary> /// Creates new response message, with no content, of the message. /// </summary> public static HorseMessage StatusResponse(HorseMessage request, ushort status) { HorseMessage response = new HorseMessage(); response.Type = MessageType.Response; response.SetMessageId(request.MessageId); response.ContentType = status; response.SetTarget(request.Type == MessageType.QueueMessage ? request.Target : request.Source); return(response); }
/// <summary> /// Unsubscribes from a queue /// </summary> public async Task <HorseResult> Unsubscribe(string queue, bool verifyResponse) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.Unsubscribe; message.SetTarget(queue); message.WaitResponse = verifyResponse; if (verifyResponse) { message.SetMessageId(_client.UniqueIdGenerator.Create()); } return(await _client.WaitResponse(message, verifyResponse)); }
/// <summary> /// Updates queue options /// </summary> public async Task <HorseResult> SetOptions(string queue, Action <QueueOptions> optionsAction) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.UpdateQueue; message.SetTarget(queue); message.WaitResponse = true; message.SetMessageId(_client.UniqueIdGenerator.Create()); message.AddHeader(HorseHeaders.QUEUE_NAME, queue); QueueOptions options = new QueueOptions(); optionsAction(options); message.Content = new MemoryStream(); await JsonSerializer.SerializeAsync(message.Content, options); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Creates new queue in server /// </summary> public async Task <HorseResult> Create(string queue, Action <QueueOptions> optionsAction, string deliveryHandlerHeader = null, IEnumerable <KeyValuePair <string, string> > additionalHeaders = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.CreateQueue; message.SetTarget(queue); message.WaitResponse = true; message.AddHeader(HorseHeaders.QUEUE_NAME, queue); if (!string.IsNullOrEmpty(deliveryHandlerHeader)) { message.AddHeader(HorseHeaders.DELIVERY_HANDLER, deliveryHandlerHeader); } if (additionalHeaders != null) { foreach (KeyValuePair <string, string> pair in additionalHeaders) { message.AddHeader(pair.Key, pair.Value); } } if (optionsAction != null) { QueueOptions options = new QueueOptions(); optionsAction(options); message.Content = new MemoryStream(); await JsonSerializer.SerializeAsync(message.Content, options); } message.SetMessageId(_client.UniqueIdGenerator.Create()); return(await _client.WaitResponse(message, true)); }
/// <summary> /// Subscribes to a queue /// </summary> public async Task <HorseResult> Subscribe(string queue, bool verifyResponse, IEnumerable <KeyValuePair <string, string> > headers = null) { HorseMessage message = new HorseMessage(); message.Type = MessageType.Server; message.ContentType = KnownContentTypes.Subscribe; message.SetTarget(queue); message.WaitResponse = verifyResponse; if (headers != null) { foreach (KeyValuePair <string, string> header in headers) { message.AddHeader(header.Key, header.Value); } } if (verifyResponse) { message.SetMessageId(_client.UniqueIdGenerator.Create()); } return(await _client.WaitResponse(message, verifyResponse)); }