Exemple #1
0
        private static async Task AddNotificationReceivers(
            Application application,
            IServiceContainer serviceContainer,
            IMessagingHubClient client,
            ITypeResolver typeResolver,
            IStateManager stateManager,
            ISessionManager sessionManager)
        {
            if (application.NotificationReceivers != null && application.NotificationReceivers.Length > 0)
            {
                foreach (var applicationReceiver in application.NotificationReceivers)
                {
                    INotificationReceiver receiver;
                    if (applicationReceiver.Response?.MediaType != null)
                    {
                        var content = applicationReceiver.Response.ToDocument();
                        receiver =
                            new LambdaNotificationReceiver(
                                (notification, c) => client.SendMessageAsync(content, notification.From, c));
                    }
                    else if (!string.IsNullOrWhiteSpace(applicationReceiver.ForwardTo))
                    {
                        var tunnelExtension = serviceContainer.GetService <ITunnelExtension>();
                        var destination     = Identity.Parse(applicationReceiver.ForwardTo);
                        receiver =
                            new LambdaNotificationReceiver(
                                (notification, c) => tunnelExtension.ForwardNotificationAsync(notification, destination, c));
                    }
                    else
                    {
                        receiver = await CreateAsync <INotificationReceiver>(
                            applicationReceiver.Type, serviceContainer, applicationReceiver.Settings, typeResolver)
                                   .ConfigureAwait(false);
                    }

                    if (applicationReceiver.OutState != null)
                    {
                        receiver = new SetStateNotificationReceiver(receiver, stateManager, applicationReceiver.OutState);
                    }

                    Func <Notification, Task <bool> > notificationPredicate = BuildPredicate <Notification>(applicationReceiver, stateManager, sessionManager);

                    if (applicationReceiver.EventType != null)
                    {
                        var currentNotificationPredicate = notificationPredicate;
                        notificationPredicate = async(n) => await currentNotificationPredicate(n) && n.Event.Equals(applicationReceiver.EventType);
                    }

                    client.AddNotificationReceiver(receiver, notificationPredicate, applicationReceiver.Priority);
                }
            }
        }
Exemple #2
0
        public static async Task AddMessageReceivers(
            Application application,
            IServiceContainer serviceContainer,
            IMessagingHubClient client,
            ITypeResolver typeResolver,
            IStateManager stateManager,
            ISessionManager sessionManager)
        {
            if (application.MessageReceivers != null && application.MessageReceivers.Length > 0)
            {
                foreach (var applicationReceiver in application.MessageReceivers)
                {
                    IMessageReceiver receiver;
                    if (applicationReceiver.Response?.MediaType != null)
                    {
                        var content = applicationReceiver.Response.ToDocument();
                        receiver =
                            new LambdaMessageReceiver(
                                (message, c) => client.SendMessageAsync(content, message.From, c));
                    }
                    else if (!string.IsNullOrWhiteSpace(applicationReceiver.ForwardTo))
                    {
                        var tunnelExtension = serviceContainer.GetService <ITunnelExtension>();
                        var destination     = Identity.Parse(applicationReceiver.ForwardTo);
                        receiver =
                            new LambdaMessageReceiver(
                                (message, c) => tunnelExtension.ForwardMessageAsync(message, destination, c));
                    }
                    else
                    {
                        var receiverType = typeResolver.Resolve(applicationReceiver.Type);
                        receiver = await BuildByLifetimeAsync(
                            applicationReceiver.Lifetime ?? application.DefaultMessageReceiverLifetime,
                            receiverType,
                            applicationReceiver.Settings,
                            serviceContainer);
                    }

                    if (applicationReceiver.OutState != null)
                    {
                        receiver = new SetStateMessageReceiver(receiver, stateManager, applicationReceiver.OutState);
                    }

                    Func <Message, Task <bool> > messagePredicate = BuildPredicate <Message>(applicationReceiver, stateManager, sessionManager);

                    if (applicationReceiver.MediaType != null)
                    {
                        var currentMessagePredicate = messagePredicate;
                        var mediaTypeRegex          = new Regex(applicationReceiver.MediaType, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        messagePredicate = async(m) => await currentMessagePredicate(m) && mediaTypeRegex.IsMatch(m.Type.ToString());
                    }

                    if (applicationReceiver.Content != null)
                    {
                        var currentMessagePredicate = messagePredicate;
                        var contentRegex            = new Regex(applicationReceiver.Content, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        messagePredicate = async(m) => await currentMessagePredicate(m) && contentRegex.IsMatch(m.Content.ToString());
                    }

                    client.AddMessageReceiver(receiver, messagePredicate, applicationReceiver.Priority);
                }
            }
        }