Ejemplo n.º 1
0
        public ChannelWrapper()
        {
            // Internal messaging used for messaging between channel unwrapper
            // and typed message receivers.
            // We want that requests do not block each other. So every request will be processed in its own thread.
            IMessagingSystemFactory anInternalMessaging = new ThreadPoolMessagingSystemFactory();

            // All messages are received via one channel. So we must provide "unwrapper" forwarding incoming messages
            // to correct receivers.
            IChannelWrapperFactory aChannelWrapperFactory = new ChannelWrapperFactory();

            myDuplexChannelUnwrapper = aChannelWrapperFactory.CreateDuplexChannelUnwrapper(anInternalMessaging);

            // To connect receivers and the unwrapper with duplex channels we can use the following helper class.
            IConnectionProviderFactory aConnectionProviderFactory = new ConnectionProviderFactory();
            IConnectionProvider        aConnectionProvider        = aConnectionProviderFactory.CreateConnectionProvider(anInternalMessaging);

            // Factory to create message receivers.
            IDuplexTypedMessagesFactory aMessageReceiverFactory = new DuplexTypedMessagesFactory();

            // Create receiver to sum two numbers.
            plusReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>();
            plusReceiver.MessageReceived += (s, e) => {
                Console.WriteLine("PLUS: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value);
                plusReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput {
                    Value = e.RequestMessage.Name + "+" + e.RequestMessage.Value
                });
            };
            // attach method handling the request
            aConnectionProvider.Attach(plusReceiver, "plus"); // attach the input channel to get messages from unwrapper


            // Create receiver to sum two numbers.
            minusReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>();
            minusReceiver.MessageReceived += (s, e) => {
                Console.WriteLine("MINUS: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value);
                minusReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput {
                    Value = e.RequestMessage.Name + "-" + e.RequestMessage.Value
                });
            };
            // attach method handling the request
            aConnectionProvider.Attach(minusReceiver, "minus"); // attach the input channel to get messages from unwrapper

            // Create receiver to sum two numbers.
            dotReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>();
            dotReceiver.MessageReceived += (s, e) => {
                Console.WriteLine("DOT: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value);
                dotReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput {
                    Value = e.RequestMessage.Name + "." + e.RequestMessage.Value
                });
            };
            // attach method handling the request
            aConnectionProvider.Attach(dotReceiver, "dot"); // attach the input channel to get messages from unwrapper
        }
Ejemplo n.º 2
0
        public ChannelWrapper()
        {
            IMessagingSystemFactory anInternalMessaging = new SynchronousMessagingSystemFactory();

            // The service receives messages via one channel (i.e. it listens on one address).
            // The incoming messages are unwrapped on the server side.
            // Therefore the client must use wrapper to send messages via one channel.
            IChannelWrapperFactory aChannelWrapperFactory = new ChannelWrapperFactory();

            myDuplexChannelWrapper = aChannelWrapperFactory.CreateDuplexChannelWrapper();


            // To connect message senders and the wrapper with duplex channels we can use the following helper class.
            IConnectionProviderFactory aConnectionProviderFactory = new ConnectionProviderFactory();
            IConnectionProvider        aConnectionProvider        = aConnectionProviderFactory.CreateConnectionProvider(anInternalMessaging);


            // Factory to create message senders.
            // Sent messages will be serialized in Xml.
            IDuplexTypedMessagesFactory aCommandsFactory = new DuplexTypedMessagesFactory();

            plusSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>();
            plusSender.ResponseReceived += (s, e) => {
                Console.WriteLine("CW.V+: " + e.ResponseMessage.Value);
            };
            // attach method handling the request
            aConnectionProvider.Connect(myDuplexChannelWrapper, plusSender, "plus"); // attach the input channel to get messages from unwrapper

            minusSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>();
            minusSender.ResponseReceived += (s, e) => {
                Console.WriteLine("CW.V-: " + e.ResponseMessage.Value);
            };
            // attach method handling the request
            aConnectionProvider.Connect(myDuplexChannelWrapper, minusSender, "minus"); // attach the input channel to get messages from unwrapper

            dotSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>();
            dotSender.ResponseReceived += (s, e) => {
                Console.WriteLine("CW.V.: " + e.ResponseMessage.Value);
            };
            // attach method handling the request
            aConnectionProvider.Connect(myDuplexChannelWrapper, dotSender, "dot"); // attach the input channel to get messages from unwrapper

            IMessagingSystemFactory aTcpMessagingSystem = new TcpMessagingSystemFactory();

            // Create output channel to send requests to the service.
            IDuplexOutputChannel anOutputChannel = aTcpMessagingSystem.CreateDuplexOutputChannel("tcp://127.0.0.1:8091/");

            // Attach the output channel to the wrapper - so that we are able to send messages
            // and receive response messages.
            // Note: The service has the coresponding unwrapper.
            myDuplexChannelWrapper.AttachDuplexOutputChannel(anOutputChannel);
        }
        public void Setup()
        {
            IMessagingSystemFactory aGlobalChannelFactory = new SynchronousMessagingSystemFactory();

            myDuplexGlobalOutputChannel = aGlobalChannelFactory.CreateDuplexOutputChannel("MainChannel");
            myDuplexGlobalInputChannel  = aGlobalChannelFactory.CreateDuplexInputChannel("MainChannel");

            myMessagingForWrapper   = new SynchronousMessagingSystemFactory();
            myMessagingForUnwrapper = new SynchronousMessagingSystemFactory();

            IChannelWrapperFactory aFactory = new ChannelWrapperFactory();

            myDuplexChannelWrapper   = aFactory.CreateDuplexChannelWrapper();
            myDuplexChannelUnwrapper = aFactory.CreateDuplexChannelUnwrapper(myMessagingForUnwrapper);
        }