예제 #1
0
        public void UdpCommTest()
        {
            //Create 4 queue for both systems
            EnvelopeQueue inQ1  = new EnvelopeQueue();
            EnvelopeQueue outQ1 = new EnvelopeQueue();
            EnvelopeQueue inQ2  = new EnvelopeQueue();
            EnvelopeQueue outQ2 = new EnvelopeQueue();

            //Create the communicators
            UdpCommunicator udpCom1 = new UdpCommunicator(inQ1, outQ1, new IPEndPoint(IPAddress.Loopback, 0));
            UdpCommunicator udpCom2 = new UdpCommunicator(inQ2, outQ2, new IPEndPoint(IPAddress.Loopback, 0));

            //Start communicators
            udpCom1.startThreads();
            udpCom2.startThreads();

            //Create a message to send.
            AssignIdMessage mess = new AssignIdMessage(4, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));

            //Try to send a message between them
            Envelope env = new Envelope(mess, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);

            outQ1.Enqueue(env);

            //Wait for the message.
            Thread.Sleep(4000);

            //Make sure we received something.
            Assert.IsTrue(!inQ2.IsEmpty);

            //Check the message.
            Envelope tmpEnv;

            inQ2.TryDequeue(out tmpEnv);

            Assert.AreEqual(mess.getId(), ((AssignIdMessage)tmpEnv.Message).getId());


            udpCom1.closeCommunicator();
            udpCom2.closeCommunicator();
        }
예제 #2
0
        public void UdpAndTcpTest()
        {
            //Create 4 queue for both systems
            EnvelopeQueue inQ1A  = new EnvelopeQueue();
            EnvelopeQueue outQ1A = new EnvelopeQueue();
            EnvelopeQueue inQ2A  = new EnvelopeQueue();
            EnvelopeQueue outQ2A = new EnvelopeQueue();

            //Create the communicators
            TcpCommunicator tcpCom1 = new TcpCommunicator(inQ1A, outQ1A, new IPEndPoint(IPAddress.Loopback, 2210)); //Sender 1
            TcpCommunicator tcpCom2 = new TcpCommunicator(inQ2A, outQ2A, new IPEndPoint(IPAddress.Loopback, 2211)); //Reciever2

            //Create the communicators
            UdpCommunicator udpCom1 = new UdpCommunicator(inQ1A, outQ1A, new IPEndPoint(IPAddress.Loopback, 2212)); //Sender 1
            UdpCommunicator udpCom2 = new UdpCommunicator(inQ2A, outQ2A, new IPEndPoint(IPAddress.Loopback, 2213)); //Reciever2

            //Start communicator threads
            tcpCom1.startThreads();
            tcpCom2.startThreads();
            udpCom1.startThreads();
            udpCom2.startThreads();

            //Create the messages
            AssignIdMessage mess1 = new AssignIdMessage(1, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess2 = new AssignIdMessage(2, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess3 = new AssignIdMessage(3, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess4 = new AssignIdMessage(4, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));

            //Make the Envelopes
            Envelope env1 = new Envelope(mess1, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);
            Envelope env2 = new Envelope(mess2, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);
            Envelope env3 = new Envelope(mess3, tcpCom1.getListenerEndPoint(), tcpCom2.getListenerEndPoint(), true);
            Envelope env4 = new Envelope(mess4, tcpCom1.getListenerEndPoint(), tcpCom2.getListenerEndPoint(), true);


            //Put in sending Q.
            outQ1A.Enqueue(env1);
            outQ1A.Enqueue(env3);
            outQ1A.Enqueue(env2);
            outQ1A.Enqueue(env4);

            Thread.Sleep(3000); //Wait for messages to send.

            //Pull out messages
            Envelope tmpEnv1;
            Envelope tmpEnv2;
            Envelope tmpEnv3;
            Envelope tmpEnv4;

            //Is there something in the Q?
            Assert.IsTrue(!inQ2A.IsEmpty);

            inQ2A.TryDequeue(out tmpEnv1);
            inQ2A.TryDequeue(out tmpEnv2);
            inQ2A.TryDequeue(out tmpEnv3);
            inQ2A.TryDequeue(out tmpEnv4);

            Console.WriteLine(((AssignIdMessage)tmpEnv1.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv1.Message).getId() | 2 == ((AssignIdMessage)tmpEnv1.Message).getId() | 3 == ((AssignIdMessage)tmpEnv1.Message).getId() | 4 == ((AssignIdMessage)tmpEnv1.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv2.Message).getId() | 2 == ((AssignIdMessage)tmpEnv2.Message).getId() | 3 == ((AssignIdMessage)tmpEnv2.Message).getId() | 4 == ((AssignIdMessage)tmpEnv2.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv3.Message).getId() | 2 == ((AssignIdMessage)tmpEnv3.Message).getId() | 3 == ((AssignIdMessage)tmpEnv3.Message).getId() | 4 == ((AssignIdMessage)tmpEnv3.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv4.Message).getId() | 2 == ((AssignIdMessage)tmpEnv4.Message).getId() | 3 == ((AssignIdMessage)tmpEnv4.Message).getId() | 4 == ((AssignIdMessage)tmpEnv4.Message).getId());

            tcpCom1.closeCommunicator();
            tcpCom2.closeCommunicator();
            udpCom1.closeCommunicator();
            udpCom2.closeCommunicator();
        }