Beispiel #1
0
        public void SendPersonalInfo(string name, int numberOfItems)
        {
            Person aPerson = new Person {
                Name = name, NumberofItem = numberOfItems
            };

            // Send the type Person.
            myMessageSender.SendRequestMessage(aPerson);
        }
Beispiel #2
0
 private void SendMessageBtn_Click(object sender, EventArgs e)
 {
     // If the client is connected to the service.
     if (mySender.IsDuplexOutputChannelAttached &&
         mySender.AttachedDuplexOutputChannel.IsConnected)
     {
         string aMessage = MessageTextBox.Text;
         mySender.SendRequestMessage(aMessage);
     }
 }
Beispiel #3
0
        public string SendRequest(RequestType request)
        {
            string result = "";

            switch (MessagesFactoryType)
            {
            case YZXMessagesFactoryType.Duplex:
                DSender.SendRequestMessage(request);
                break;
            }
            return(result);
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        public void Send(string sign, string name, string value)
        {
            switch (sign)
            {
            case "+":
                plusSender.SendRequestMessage(new TestInput {
                    Name = name, Value = value
                });
                break;

            case "-":
                minusSender.SendRequestMessage(new TestInput {
                    Name = name, Value = value
                });
                break;

            case ".":
                dotSender.SendRequestMessage(new TestInput {
                    Name = name, Value = value
                });
                break;
            }
        }
Beispiel #6
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);
        }
        // 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));
        }
Beispiel #8
0
        private void myEventCacher(object sender, EventArgs e)
        {
            int y          = 50;
            int speedRight = 50;
            int x          = 0;

            Console.Out.WriteLine("Event caught");
            if (USBHIDDRIVER.USBInterface.usbBuffer.Count > 0)
            {
                byte[] currentRecord = null;
                int    counter       = 0;
                while ((byte[])USBHIDDRIVER.USBInterface.usbBuffer[counter] == null)
                {
                    lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
                    {
                        USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
                    }
                }
                currentRecord = (byte[])USBHIDDRIVER.USBInterface.usbBuffer[0];
                lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
                {
                    USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
                }
                string hex = BitConverter.ToString(currentRecord);
                Console.WriteLine(hex);
                char[]   splitby = { '-' };
                string[] hexxes  = hex.Split(splitby);
                for (int i = 0; i < 23; i++)
                {
                    switch (i)
                    {
                    case 4:
                        int decValue3   = Convert.ToInt32(hexxes[5], 16) * 255 + Convert.ToInt32(hexxes[4], 16);
                        int percentage4 = (int)((((100f * (decValue3)) / 1020)) - 50) * 2;
                        //Console.WriteLine(decValue3);
                        //Console.WriteLine("x: {0}", percentage4);
                        x = percentage4;
                        break;

                    case 6:
                        int decValue   = Convert.ToInt32(hexxes[7], 16) * 255 + Convert.ToInt32(hexxes[6], 16);
                        int percentage = (int)(0.5f + ((100f * (1020 - decValue)) / 1020));
                        speedRight = percentage;
                        break;

                    case 8:
                        int decValue2   = Convert.ToInt32(hexxes[8], 16);
                        int percentage2 = (int)(((100f * (255 - decValue2)) / 255) - 50) * 2;
                        //Console.WriteLine(decValue2);
                        //Console.WriteLine("y: {0}", percentage2);
                        y = percentage2;
                        break;
                    }
                }
                x = 0 - x;
                double V = (100f - Math.Abs(x)) * (y / 100f) + y;
                double W = (100f - Math.Abs(y)) * (x / 100f) + x;

                double R = (V + W) / 2;
                double L = (V - W) / 2;

                R = R / 100f * 15;
                L = L / 100f * 15;
                int       strength1 = (int)L;
                int       strength  = (int)R;
                MyRequest request   = new MyRequest {
                    side = "L", strength = strength
                };
                mySender.SendRequestMessage(request);
                MyRequest request2 = new MyRequest {
                    side = "R", strength = strength1
                };
                mySender.SendRequestMessage(request2);
            }
        }
        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());
            }
        }