Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / C#

Pipes and Filters concurrent Design Pattern using the Concurrency and Coordination Runtime

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
8 Oct 2008Ms-PL4 min read 37.6K   373   21  
A modular approach for concurrent message passing.
using Microsoft.Ccr.Core;
using System.Collections.Generic;

namespace WorkCardDemo
{
    class DemuxStateMachine
               : DemuxMessageFilter<StateMachineFilter,
                                    PortSet<CorrectPinMessage, WrongPinMessage>,
                                    PortSet<OpenAlarm, CloseAlarm>,
                                    string>
    {
        public DemuxStateMachine()
            : base(new WorkerKeyValueFilter())
        {

        }
    }

    class WorkerKeyValueFilter : IMessageFilter<PortSet<CorrectPinMessage, WrongPinMessage>, Port<KeyValuePair<string, object>>>
    {

        #region IMessageFilter<PortSet<SuccessMessage,FailureMessage>,Port<KeyValuePair<string,object>> Members

        public PortSet<CorrectPinMessage, WrongPinMessage> InputPort { get; private set; }

        public Port<KeyValuePair<string, object>> OutputPort { get; private set; }

        public void Initialize(DispatcherQueue dispatcherQueue, PortSet<CorrectPinMessage, WrongPinMessage> inputPort, Port<KeyValuePair<string, object>> outputPort)
        {
            InputPort = inputPort;
            OutputPort = outputPort;

            Arbiter.Activate(
                dispatcherQueue,
                Arbiter.Interleave(
                        new TeardownReceiverGroup(),
                        new ExclusiveReceiverGroup(),
                        new ConcurrentReceiverGroup(
                            Arbiter.Receive(true, InputPort.P0, HandleMessage),
                            Arbiter.Receive(true, InputPort.P1, HandleMessage))));
        }

        #endregion

        void HandleMessage<T>(T message) where T : WorkCardSwipeMessageBase
        {
            OutputPort.Post(new KeyValuePair<string, object>(message.Name, message));
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions