Example #1
0
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var forwardedHeaders = new Dictionary <string, string>(context.Headers);

        string replyTo = null;

        if (!forwardedHeaders.TryGetValue(Headers.CorrelationId, out var correlationId))
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        correlationId.DecodeTLV((t, v) =>
        {
            if (t == "reply-to")
            {
                replyTo = v;
            }
            if (t == "id")
            {
                forwardedHeaders[Headers.CorrelationId] = v;
            }
        });

        if (replyTo == null)
        {
            throw new UnforwardableMessageException("The reply message does not contain \'reply-to\' correlation parameter required to route the message.");
        }

        var outgoingMessage = new OutgoingMessage(context.MessageId, forwardedHeaders, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(replyTo));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
Example #2
0
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        string replyTo = null;

        if (!context.Headers.TryGetValue(Headers.CorrelationId, out string correlationId))
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        correlationId.DecodeTLV((t, v) =>
        {
            if (t == "reply-to")
            {
                replyTo = v;
            }
        });

        if (replyTo == null)
        {
            throw new UnforwardableMessageException($"The reply has to contain a '{Headers.CorrelationId}' header set by the bridge ramp when sending out the initial message.");
        }

        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(replyTo));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
Example #3
0
 public RuleCreationContext(string interfaceName, EndpointInstances endpointInstances, RawDistributionPolicy distributionPolicy, IRawEndpoint endpoint, RuntimeTypeGenerator typeGenerator, ReadOnlySettings settings)
 {
     InterfaceName      = interfaceName;
     EndpointInstances  = endpointInstances;
     DistributionPolicy = distributionPolicy;
     Endpoint           = endpoint;
     TypeGenerator      = typeGenerator;
     Settings           = settings;
 }
    public async Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var messageTypeString = GetSubscriptionMessageTypeFrom(context);

        if (string.IsNullOrEmpty(messageTypeString))
        {
            throw new UnforwardableMessageException("Message intent is Subscribe, but the subscription message type header is missing.");
        }

        if (intent != MessageIntentEnum.Subscribe && intent != MessageIntentEnum.Unsubscribe)
        {
            throw new UnforwardableMessageException("Subscription messages need to have intent set to Subscribe/Unsubscribe.");
        }

        string subscriberAddress;
        string subscriberEndpoint = null;
        string publisherEndpoint;

        if (!context.Headers.TryGetValue("NServiceBus.Bridge.DestinationEndpoint", out publisherEndpoint))
        {
            throw new UnforwardableMessageException("Subscription message does not contain the 'NServiceBus.Bridge.DestinationEndpoint' header.");
        }

        if (context.Headers.TryGetValue(Headers.SubscriberTransportAddress, out subscriberAddress))
        {
            subscriberEndpoint = context.Headers[Headers.SubscriberEndpoint];
        }
        else
        {
            subscriberAddress = GetReplyToAddress(context);
        }

        if (subscriberAddress == null)
        {
            throw new UnforwardableMessageException("Subscription message arrived without a valid ReplyToAddress.");
        }

        var subscriber  = new Subscriber(subscriberAddress, subscriberEndpoint);
        var messageType = new MessageType(messageTypeString);

        if (intent == MessageIntentEnum.Subscribe)
        {
            await subscriptionStorage.Subscribe(subscriber, messageType, context.Extensions).ConfigureAwait(false);
            await ForwardSubscribe(subscriber, publisherEndpoint, messageTypeString, dispatcher).ConfigureAwait(false);
        }
        else
        {
            await subscriptionStorage.Unsubscribe(subscriber, messageType, context.Extensions).ConfigureAwait(false);
            await ForwardUnsubscribe(subscriber, publisherEndpoint, messageTypeString, dispatcher).ConfigureAwait(false);
        }
    }
Example #5
0
    public Task Route(MessageContext context, IRawEndpoint dispatcher)
    {
        if (!context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes))
        {
            throw new UnforwardableMessageException("Message need to have 'NServiceBus.EnclosedMessageTypes' header in order to be routed.");
        }
        var types = messageTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        var addressTag      = new MulticastAddressTag(typeGenerator.GetType(types.First()));
        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, addressTag);

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
Example #6
0
        public override async Task Start(CancellationToken token)
        {
            var startable = await RawEndpoint.Create(config).ConfigureAwait(false);

            if (!sendOnly)
            {
                endpoint = await startable.Start().ConfigureAwait(false);
            }
            else
            {
                endpoint = startable;
            }

            await onStarting(endpoint).ConfigureAwait(false);
        }
Example #7
0
    public async Task Route(MessageContext context, IRawEndpoint dispatcher)
    {
        if (!context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes))
        {
            throw new UnforwardableMessageException("Message need to have 'NServiceBus.EnclosedMessageTypes' header in order to be routed.");
        }
        var types       = messageTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        var typeObjects = types.Select(t => new MessageType(t));

        var subscribers = await subscriptionStorage.GetSubscriberAddressesForMessage(typeObjects, new ContextBag()).ConfigureAwait(false);

        var destinations    = SelectDestinationsForEachEndpoint(subscribers, context);
        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operations      = destinations.Select(x => new TransportOperation(outgoingMessage, new UnicastAddressTag(x)));

        await dispatcher.Dispatch(new TransportOperations(operations.ToArray()), context.TransportTransaction, context.Extensions).ConfigureAwait(false);
    }
    public Task Route(MessageContext context, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        string destinationEndpoint;

        if (!context.Headers.TryGetValue("NServiceBus.Bridge.DestinationEndpoint", out destinationEndpoint))
        {
            throw new UnforwardableMessageException("Sent message does not contain the 'NServiceBus.Bridge.DestinationEndpoint' header.");
        }
        var address = SelectDestinationAddress(destinationEndpoint, i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i)));

        context.Headers[Headers.ReplyToAddress] = dispatcher.TransportAddress;

        var outgoingMessage = new OutgoingMessage(context.MessageId, context.Headers, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(address));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
Example #9
0
    public Task Route(MessageContext context, IRawEndpoint dispatcher, InterBridgeRoutingSettings routing, string sourcePort)
    {
        if (!context.Headers.TryGetValue(Headers.EnclosedMessageTypes, out var messageTypes))
        {
            throw new UnforwardableMessageException($"Sent message does not contain the '{Headers.EnclosedMessageTypes}' header.");
        }
        var rootType         = messageTypes.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries).First();
        var rootTypeFullName = rootType.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).First();

        if (routing.SendRouteTable.TryGetValue(rootTypeFullName, out var nextHop))
        {
            return(Forward(context, dispatcher, nextHop, sourcePort));
        }

        if (!context.Headers.TryGetValue("NServiceBus.Bridge.DestinationEndpoint", out var destinationEndpoint))
        {
            throw new UnforwardableMessageException("Sent message does not contain the 'NServiceBus.Bridge.DestinationEndpoint' header.");
        }
        return(Forward(context, dispatcher, destinationEndpoint, sourcePort));
    }
    public void PreparePubSub(IRawEndpoint endpoint, out IPublishRouter publishRouter, out SubscriptionReceiver subscriptionReceiver, out SubscriptionForwarder subscriptionForwarder)
    {
        var transport = endpoint.Settings.Get <TransportInfrastructure>();

        if (transport.OutboundRoutingPolicy.Publishes == OutboundRoutingType.Multicast)
        {
            publishRouter         = new NativePublishRouter(typeGenerator);
            subscriptionReceiver  = new NullSubscriptionReceiver();
            subscriptionForwarder = new NativeSubscriptionForwarder(endpoint.SubscriptionManager, typeGenerator, endpointInstances);
        }
        else
        {
            if (subscriptionPersistence == null)
            {
                throw new Exception("Subscription storage has not been configured. Use 'UseSubscriptionPersistence' method to configure it.");
            }
            publishRouter         = new MessageDrivenPublishRouter(subscriptionPersistence, distributionPolicy);
            subscriptionReceiver  = new StorageDrivenSubscriptionReceiver(subscriptionPersistence);
            subscriptionForwarder = new MessageDrivenSubscriptionForwarder(endpointInstances);
        }
    }
Example #11
0
    Task Forward(MessageContext context, IRawEndpoint dispatcher, IRouter subscribeRouter)
    {
        var intent = GetMesssageIntent(context);

        switch (intent)
        {
        case MessageIntentEnum.Subscribe:
        case MessageIntentEnum.Unsubscribe:
            return(subscribeRouter.Route(context, intent, dispatcher));

        case MessageIntentEnum.Send:
            return(sendRouter.Route(context, intent, dispatcher));

        case MessageIntentEnum.Publish:
            return(publishRouter.Route(context, intent, dispatcher));

        case MessageIntentEnum.Reply:
            return(replyRouter.Route(context, intent, dispatcher));

        default:
            throw new UnforwardableMessageException("Unroutable message intent: " + intent);
        }
    }
Example #12
0
    Task Forward(MessageContext context, IRawEndpoint dispatcher, string destinationEndpoint, string sourcePort)
    {
        var forwardedHeaders = new Dictionary <string, string>(context.Headers);

        var address = SelectDestinationAddress(destinationEndpoint, i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i)));

        if (forwardedHeaders.TryGetValue(Headers.ReplyToAddress, out var replyToHeader) &&
            forwardedHeaders.TryGetValue(Headers.CorrelationId, out var correlationId))
        {
            // pipe-separated TLV format
            var newCorrelationId = $"id|{correlationId.Length}|{correlationId}|reply-to|{replyToHeader.Length}|{replyToHeader}";
            if (sourcePort != null)
            {
                newCorrelationId += $"|port|{sourcePort.Length}|{sourcePort}";
            }
            forwardedHeaders[Headers.CorrelationId] = newCorrelationId;
        }
        forwardedHeaders[Headers.ReplyToAddress] = dispatcher.TransportAddress;

        var outgoingMessage = new OutgoingMessage(context.MessageId, forwardedHeaders, context.Body);
        var operation       = new TransportOperation(outgoingMessage, new UnicastAddressTag(address));

        return(dispatcher.Dispatch(new TransportOperations(operation), context.TransportTransaction, context.Extensions));
    }
 public abstract Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding);
Example #14
0
    public static Task Publish(this IRawEndpoint endpoint, Type eventType, Dictionary <string, string> headers, byte[] body)
    {
        var op = new TransportOperation(new OutgoingMessage(Guid.NewGuid().ToString(), headers, body), new MulticastAddressTag(eventType));

        return(endpoint.Dispatch(new TransportOperations(op), new TransportTransaction(), new ContextBag()));
    }
Example #15
0
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding)
    {
        var typeFullName = messageType.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).First();

        if (!forwarding.PublisherTable.TryGetValue(typeFullName, out var nextHopEndpoint))
        {
            return;
        }

        var subscriptionMessage = ControlMessageFactory.Create(intent);

        if (publisherEndpoint != null)
        {
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
        }
        subscriptionMessage.Headers[Headers.SubscriptionMessageType]    = messageType;
        subscriptionMessage.Headers[Headers.ReplyToAddress]             = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberTransportAddress] = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberEndpoint]         = dispatcher.EndpointName;
        subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
        subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

        var publisherInstances = endpointInstances.FindInstances(nextHopEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
Example #16
0
    public override async Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding)
    {
        var type = typeGenerator.GetType(messageType);
        await subscriptionManager.Unsubscribe(type, new ContextBag()).ConfigureAwait(false);

        await Send(subscriber, publisherEndpoint, messageType, MessageIntentEnum.Unsubscribe, dispatcher, forwarding).ConfigureAwait(false);
    }
 public RandomDuplicator(IRawEndpoint dispatcher)
 {
     this.dispatcher = dispatcher;
 }
 public MessageForwarder(string destinationEndpointName, IRawEndpoint destinationEndpoint)
 {
     _destinationEndpointName = destinationEndpointName;
     _destinationEndpoint     = destinationEndpoint;
 }
 protected abstract Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher);
Example #20
0
    protected override Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher)
    {
        var type = GetType(messageType);

        return(subscriptionManager.Unsubscribe(type, new ContextBag()));
    }
 public PostroutingTerminator(IRawEndpoint dispatcher)
 {
     this.dispatcher   = dispatcher;
     this.endpointName = dispatcher.EndpointName;
 }
Example #22
0
    public static Task Send(this IRawEndpoint endpoint, string destination, Dictionary <string, string> headers, byte[] body)
    {
        var op = new TransportOperation(new OutgoingMessage(Guid.NewGuid().ToString(), headers, body), new UnicastAddressTag(destination));

        return(endpoint.Dispatch(new TransportOperations(op), new TransportTransaction(), new ContextBag()));
    }
 protected override Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher)
 {
     return(Send(subscriber, publisherEndpoint, messageType, MessageIntentEnum.Unsubscribe, dispatcher));
 }
Example #24
0
 public override Task ForwardUnsubscribe(Subscriber subscriber, string publisherEndpoint, string messageType, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding)
 {
     return(Send(subscriber, publisherEndpoint, messageType, MessageIntentEnum.Unsubscribe, dispatcher, forwarding));
 }
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var publisherInstances = endpointInstances.FindInstances(publisherEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var subscriptionMessage = ControlMessageFactory.Create(intent);

            subscriptionMessage.Headers[Headers.SubscriptionMessageType]          = messageType;
            subscriptionMessage.Headers[Headers.ReplyToAddress]                   = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberTransportAddress]       = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberEndpoint]               = dispatcher.EndpointName;
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
            subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
            subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
Example #26
0
 public InterceptingDispatcher(IRawEndpoint impl, Dispatch dispatchImpl)
 {
     this.impl         = impl;
     this.dispatchImpl = dispatchImpl;
 }