public void CommunicatorSendAndRecieve()
        {
            DummyReceiver        receiver = new DummyReceiver();
            ProtoBufPresentation pp       = new ProtoBufPresentation();

            pp.SetReceiver(receiver);

            DummyDataLink datalink = new DummyDataLink(pp);


            Communicator com = new Communicator(datalink, pp);

            Message sendMessage = new Message {
                id = 2, stringData = "Message data..."
            };

            Assert.True(com.SendCommand(sendMessage));

            // No need to wait; a dummy data link is used so sending command is followed up with incomingmessage

            Assert.NotNull(receiver.incomingMessage);

            Assert.AreEqual(receiver.incomingMessage.id, sendMessage.id);
            Assert.AreEqual(receiver.incomingMessage.stringData, sendMessage.stringData);
        }
        public void MessageToBinaryData()
        {
            // Setup
            ProtoBufPresentation pbPres   = new ProtoBufPresentation();
            IMessageReceiver     receiver = NSubstitute.Substitute.For <IMessageReceiver>();

            pbPres.SetReceiver(receiver);

            int    messageTarget = 1;
            string messageData   = "Test message data";

            Message sourceMessage = new Message {
                id = messageTarget, stringData = messageData
            };

            MemoryStream stream = new MemoryStream();

            Serializer.Serialize <Message>(stream, sourceMessage);

            byte[] binaryData = stream.ToArray();

            // Serializing
            byte[] resultData = pbPres.MessageToBinaryData(sourceMessage);

            // Checking
            // Size
            Assert.AreEqual(binaryData.Length, BitConverter.ToInt32(resultData, 0));
            // Data
            for (int i = 0; i < binaryData.Length; i++)
            {
                Assert.AreEqual(binaryData[i], resultData[i + sizeof(Int32)]);
            }
        }
        public void IncomingDataToReceiver()
        {
            int    messageTarget = 1;
            string messageData   = "Test message data";

            Message expectedMessage = new Message {
                id = messageTarget, stringData = messageData
            };
            Message sourceMessage = new Message {
                id = messageTarget, stringData = messageData
            };

            ProtoBufPresentation pbPres   = new ProtoBufPresentation();
            DummyMessageReceiver receiver = new DummyMessageReceiver(expectedMessage);

            pbPres.SetReceiver(receiver);

            byte[] binaryMessage = pbPres.MessageToBinaryData(sourceMessage);

            IDataLink datalink = NSubstitute.Substitute.For <IDataLink>();

            pbPres.IncomingData(binaryMessage, datalink);
            pbPres.IncomingData(binaryMessage, datalink);

            byte[] firstHalf  = new byte[6];
            byte[] secondHalf = new byte[binaryMessage.Length - 6];

            Array.Copy(binaryMessage, 0, firstHalf, 0, 6);
            Array.Copy(binaryMessage, 6, secondHalf, 0, binaryMessage.Length - 6);

            pbPres.IncomingData(firstHalf, datalink);
            pbPres.IncomingData(secondHalf, datalink);
        }
        public void BinaryDataToMessage()
        {
            // Setup
            ProtoBufPresentation pbPres   = new ProtoBufPresentation();
            IMessageReceiver     receiver = NSubstitute.Substitute.For <IMessageReceiver>();

            pbPres.SetReceiver(receiver);

            int    messageTarget = 1;
            string messageData   = "Test message data";

            Message sourceMessage = new Message {
                id = messageTarget, stringData = messageData
            };

            MemoryStream stream = new MemoryStream();

            Serializer.Serialize(stream, sourceMessage);

            List <byte> binaryData = new List <byte>();

            byte[] data = stream.ToArray();

            binaryData.AddRange(BitConverter.GetBytes((Int32)data.Length));
            binaryData.AddRange(data);

            // Deserializing
            Message result = pbPres.BinaryDataToMessage(binaryData.ToArray());

            // Checking
            Assert.NotNull(result);
            Assert.AreEqual(result.id, messageTarget);
            Assert.AreEqual(result.stringData, messageData);
        }
    public void AddDummyBot()
    {
        // Debug.Log("Spoofing prereqs...");

        ProtoBufPresentation pp            = new ProtoBufPresentation();
        DummyReceiver        dummyReceiver = new DummyReceiver();

        pp.SetReceiver(dummyReceiver);

        DummyDataLink datalink = new DummyDataLink(pp);

        Communicator com = new Communicator(datalink, pp);

        //create message, set response

        MessageTarget_ target = MessageTarget_.Robot;
        MessageType_   type   = MessageType_.CustomMessage;

        Message DummyMessage = MessageBuilder.CreateMessage(target, type);

        DummyMessage.SetIdentificationResponse("dummyBot");

        /*
         *    First wait for the other identification messages to be processed to prevent race conditions
         */
        if (_ev.WaitOne(10000))
        {
            _ev.Reset();

            _connection           = com;
            _newMessage           = DummyMessage;
            _continueRegistration = true;

            _ev.WaitOne(1000);
        }
    }