/// <summary>
        /// Quick way to connect a Subscriber port to a Publisher port.  Useful connecting channels and Agents
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="receive"></param>
        /// <returns></returns>
        public static IDisposable Connect <T>(this ISubscriberPort <T> port,
                                              IPublisherPort <T> receive)
        {
            var stub = StubFiber.StartNew();

            port.Subscribe(stub, receive.Publish);
            return(stub);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Subscribe with a message predicate to filter messages
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="fiber"></param>
        /// <param name="receive"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static IDisposable Subscribe <T>(this ISubscriberPort <T> port,
                                                IFiber fiber,
                                                Action <T> receive,
                                                Predicate <T> filter)
        {
            Action <T> filteredReceiver = x =>
            {
                if (filter(x))
                {
                    fiber.Enqueue(() => receive(x));
                }
            };

            //we use a stub fiber to force the filtering onto the publisher thread.
            return(port.Subscribe(StubFiber.StartNew(), filteredReceiver));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Subscribe with a message predicate to filter messages
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="port"></param>
        /// <param name="fiber"></param>
        /// <param name="receive"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static IDisposable Subscribe <T>(this IFiber fiber,
                                                ISubscriberPort <T> port,
                                                Action <T> receive,
                                                Predicate <T> filter)
        {
            void FilteredReceiver(T x)
            {
                if (filter(x))
                {
                    fiber.Enqueue(() => receive(x));
                }
            }

            //we use a stub fiber to force the filtering onto the publisher thread.
            var stub = StubFiber.StartNew();

            port.Subscribe(stub, FilteredReceiver);
            return(stub);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Quick way to connect a Subscriber port to a Publisher port.  Useful connecting channels and Agents
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="port"></param>
 /// <param name="fiber"></param>
 /// <param name="receive"></param>
 /// <returns></returns>
 public static IDisposable Connect <T>(this ISubscriberPort <T> port,
                                       IFiber fiber,
                                       IPublisherPort <T> receive)
 {
     return(port.Subscribe(StubFiber.StartNew(), x => receive.Publish(x)));
 }