Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public Main()
        {
            try
            {
                InitializeComponent();
                ProcessFile();
                WriteFile();
                MessageBox.Show("Done!");
                return;
                _mMouseHookManager = new MouseHookListener(new GlobalHooker()) {Enabled = true};
                _mMouseHookManager.MouseUp += mMouseHookManager_MouseUp;

                ReadKeyFile(Properties.Settings.Default.KeyPath);

                IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
                _mySender = aSenderFactory.CreateDuplexTypedMessageSender<AppQA, MyResponse>();

                IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
                IDuplexOutputChannel anOutputChannel =
                    aMessaging.CreateDuplexOutputChannel("tcp://" + Properties.Settings.Default.IpAddress + ":8060/");

                // Attach the input channel and start to listen to messages.
                _mySender.AttachDuplexOutputChannel(anOutputChannel);
            }
            catch (Exception exception)
            {
                WriteAnswer(Properties.Settings.Default.AnswerPath, exception.Message);
            }
        }
Ejemplo n.º 3
0
        protected void Setup(IMessagingSystemFactory messagingSystemFactory, string channelId, ISerializer serializer)
        {
            MessagingSystemFactory = messagingSystemFactory;

            DuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(channelId);
            DuplexInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(channelId);

            IDuplexTypedMessagesFactory aMessageFactory = new DuplexTypedMessagesFactory(serializer);

            Requester = aMessageFactory.CreateDuplexTypedMessageSender <int, int>();
            Responser = aMessageFactory.CreateDuplexTypedMessageReceiver <int, int>();
        }
Ejemplo n.º 4
0
        public Form1()
        {
            InitializeComponent();

            // Create message sender.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();

            mySender = aSenderFactory.CreateDuplexTypedMessageSender <string, string>();

            // Subscribe to handle when a message is received or if the client
            // is disconnected.
            mySender.ConnectionClosed += OnConnectionClosed;
            mySender.ResponseReceived += OnResponseReceived;
        }
Ejemplo n.º 5
0
        public void Openconnection()
        {
            // Create message sender sending request messages of type Person and receiving responses of type string.
            IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();

            myMessageSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <string, Person>();
            myMessageSender.ResponseReceived += OnResponseReceived;
            // Create messaging based on TCP.
            IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory();
            IDuplexOutputChannel    anOutputChannel         = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:8094/");

            // Attach output channel and be able to send messages and receive response messages.
            myMessageSender.AttachDuplexOutputChannel(anOutputChannel);
        }
Ejemplo n.º 6
0
        public void OpenConnection()
        {
            // Establish connection with load scheduling module
            IMessagingSystemFactory     myMessaging     = new TcpMessagingSystemFactory();
            IDuplexOutputChannel        anOutputChannel = myMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8060/");
            IDuplexTypedMessagesFactory aSenderFactory  = new DuplexTypedMessagesFactory();

            mySender = aSenderFactory.CreateDuplexTypedMessageSender <double, Range>();

            // Event handler on receipt of response
            mySender.ResponseReceived += OnResponseReceived;

            // Attach the output channel to send messages and receive responses
            mySender.AttachDuplexOutputChannel(anOutputChannel);
        }
Ejemplo n.º 7
0
        // The method is called when the button to send message is clicked.
        private void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            // Create message sender sending request messages of type Person and receiving responses of type string.
            IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();

            myMessageSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <byte[], byte[]>();
            myMessageSender.ResponseReceived += ResponseReceived;

            // Create messaging based on TCP.
            IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory();
            IDuplexOutputChannel    aDuplexOutputChannel    = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:4502/");

            // Attach output channel and be able to send messages and receive response messages.
            myMessageSender.AttachDuplexOutputChannel(aDuplexOutputChannel);

            myMessageSender.SendRequestMessage(GetBytes(textBox1.Text));
        }
Ejemplo n.º 8
0
        public void OpenConnection()
        {
            // Create TCP messaging for the communication.
            // Note: Requests are sent to the balancer that will forward them
            //       to available services.
            IMessagingSystemFactory myMessaging     = new TcpMessagingSystemFactory();
            IDuplexOutputChannel    anOutputChannel = myMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8060/");

            // Create sender to send requests.
            IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();

            mySender = aSenderFactory.CreateDuplexTypedMessageSender <double, Range>();

            // Subscribe to receive response messages.
            mySender.ResponseReceived += OnResponseReceived;

            // Attach the output channel and be able to send messages and receive responses.
            mySender.AttachDuplexOutputChannel(anOutputChannel);
        }
Ejemplo n.º 9
0
        public MainWindow()
        {
            InitializeComponent();
            IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();

            mySender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <MyResponse, MyRequest>();
            mySender.ResponseReceived += OnResponseReceived;

            // Create messaging based on TCP.
            IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory();
            IDuplexOutputChannel    anOutputChannel         = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://192.168.2.9:8060/");

            // Attach output channel and be able to send messages and receive response messages.
            mySender.AttachDuplexOutputChannel(anOutputChannel);
            MyRequest test = new MyRequest {
                side = "L", strength = 10
            };

            mySender.SendRequestMessage(test);
            MyRequest reset = new MyRequest {
                side = "L", strength = 0
            };

            mySender.SendRequestMessage(reset);
            try
            {
                USBInterface usb = new USBInterface("vid_044f", "pid_b108");
                usb.Connect();
                usb.enableUsbBufferEvent(new System.EventHandler(myEventCacher));
                usb.startRead();

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Ejemplo n.º 10
0
        public void SendReceive_1Message_PerClientSerializer()
        {
            string aClient1Id = null;

            IDuplexTypedMessagesFactory aSender1Factory  = new DuplexTypedMessagesFactory(new XmlStringSerializer());
            IDuplexTypedMessagesFactory aSender2Factory  = new DuplexTypedMessagesFactory(new BinarySerializer());
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory()
            {
                SerializerProvider = x => (x == aClient1Id) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer()
            };

            IDuplexTypedMessageSender <int, int>   aSender1  = aSender1Factory.CreateDuplexTypedMessageSender <int, int>();
            IDuplexTypedMessageSender <int, int>   aSender2  = aSender2Factory.CreateDuplexTypedMessageSender <int, int>();
            IDuplexTypedMessageReceiver <int, int> aReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver <int, int>();

            aReceiver.ResponseReceiverConnected += (x, y) => aClient1Id = aClient1Id ?? y.ResponseReceiverId;


            int aReceivedMessage = 0;

            aReceiver.MessageReceived += (x, y) =>
            {
                aReceivedMessage = y.RequestMessage;

                // Send the response
                aReceiver.SendResponseMessage(y.ResponseReceiverId, 1000);
            };

            AutoResetEvent aSender1MessageReceivedEvent = new AutoResetEvent(false);
            int            aSender1ReceivedResponse     = 0;

            aSender1.ResponseReceived += (x, y) =>
            {
                aSender1ReceivedResponse = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender1MessageReceivedEvent.Set();
            };


            AutoResetEvent aSender2MessageReceivedEvent = new AutoResetEvent(false);
            int            aSender2ReceivedResponse     = 0;

            aSender2.ResponseReceived += (x, y) =>
            {
                aSender2ReceivedResponse = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender2MessageReceivedEvent.Set();
            };

            try
            {
                aReceiver.AttachDuplexInputChannel(DuplexInputChannel);
                aSender1.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));
                aSender2.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));

                aSender1.SendRequestMessage(2000);
                aSender2.SendRequestMessage(2000);

                // Wait for the signal that the message is received.
                aSender1MessageReceivedEvent.WaitIfNotDebugging(2000);
                aSender2MessageReceivedEvent.WaitIfNotDebugging(2000);
                //Assert.IsTrue(aMessageReceivedEvent.WaitOne(2000));
            }
            finally
            {
                aSender1.DetachDuplexOutputChannel();
                aSender2.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();

                // Give some time to complete tracing.
                Thread.Sleep(300);
            }

            // Check received values
            Assert.AreEqual(2000, aReceivedMessage);
            Assert.AreEqual(1000, aSender1ReceivedResponse);
            Assert.AreEqual(1000, aSender2ReceivedResponse);
        }
Ejemplo n.º 11
0
        // The method is called when the button to send message is clicked.
        private void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            // Create message sender sending request messages of type Person and receiving responses of type string.
            IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();
            myMessageSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender<byte[], byte[]>();
            myMessageSender.ResponseReceived += ResponseReceived;

            // Create messaging based on TCP.
            IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory();
            IDuplexOutputChannel aDuplexOutputChannel = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:4502/");

            // Attach output channel and be able to send messages and receive response messages.
            myMessageSender.AttachDuplexOutputChannel(aDuplexOutputChannel);

            myMessageSender.SendRequestMessage(GetBytes(textBox1.Text));
        }
Ejemplo n.º 12
0
        public void CalculatePi()
        {
            //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug;
            //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt");

            IMessagingSystemFactory aThreadMessaging = new ThreadMessagingSystemFactory();

            List <CalculatorService> aServices = new List <CalculatorService>();

            ILoadBalancer aDistributor = null;
            IDuplexTypedMessageSender <double, Interval> aSender = null;

            // Create 50 calculating services.
            try
            {
                for (int i = 0; i < 50; ++i)
                {
                    aServices.Add(new CalculatorService("a" + i.ToString(), aThreadMessaging));
                }

                // Create Distributor
                ILoadBalancerFactory aDistributorFactory = new RoundRobinBalancerFactory(aThreadMessaging);
                aDistributor = aDistributorFactory.CreateLoadBalancer();

                // Attach available services to the distributor.
                for (int i = 0; i < aServices.Count; ++i)
                {
                    aDistributor.AddDuplexOutputChannel("a" + i.ToString());
                }

                // Attach input channel to the distributor.
                IDuplexInputChannel anInputChannel = aThreadMessaging.CreateDuplexInputChannel("DistributorAddress");
                aDistributor.AttachDuplexInputChannel(anInputChannel);


                // Create client that needs to calculate PI.
                IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();
                aSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <double, Interval>();

                AutoResetEvent aCalculationCompletedEvent = new AutoResetEvent(false);
                int            aCount = 0;
                double         aPi    = 0.0;
                aSender.ResponseReceived += (x, y) =>
                {
                    ++aCount;
                    EneterTrace.Debug("Completed interval: " + aCount.ToString());

                    aPi += y.ResponseMessage;

                    if (aCount == 400)
                    {
                        aCalculationCompletedEvent.Set();
                    }
                };

                IDuplexOutputChannel anOutputChannel = aThreadMessaging.CreateDuplexOutputChannel("DistributorAddress");
                aSender.AttachDuplexOutputChannel(anOutputChannel);

                // Sender sends several parallel requests to calculate specified intervals.
                // 2 / 0.005 = 400 intervals.
                for (double i = -1.0; i <= 1.0; i += 0.005)
                {
                    Interval anInterval = new Interval(i, i + 0.005);
                    aSender.SendRequestMessage(anInterval);
                }

                // Wait until all requests are calculated.
                EneterTrace.Debug("Test waits until completion.");
                aCalculationCompletedEvent.WaitOne();

                EneterTrace.Info("Calculated PI = " + aPi.ToString());
            }
            catch (Exception err)
            {
                EneterTrace.Error("Test failed", err);
                throw;
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aDistributor.DetachDuplexInputChannel();
                aServices.ForEach(x => x.Dispose());
            }
        }