public void AckNak_CheckConstructorsAndFactories()
        {
            Tick t1 = new Tick();
            AckNak m = new AckNak(Reply.PossibleStatus.Success, 10, t1, "Test Message", "Test Note");
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Success, m.Status);
            Assert.AreEqual(10, m.IntResult);
            Assert.AreSame(t1, m.ObjResult);
            Assert.AreEqual("Test Message", m.Message);
            Assert.AreEqual("Test Note", m.Note);

            m = new AckNak(Reply.PossibleStatus.Failure, 20);
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Failure, m.Status);
            Assert.AreEqual(20, m.IntResult);
            Assert.IsNull(m.ObjResult);
            Assert.AreEqual("", m.Message);
            Assert.AreEqual("", m.Note);

            m = new AckNak(Reply.PossibleStatus.Failure, 20, "Test Message");
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Failure, m.Status);
            Assert.AreEqual(20, m.IntResult);
            Assert.IsNull(m.ObjResult);
            Assert.AreEqual("Test Message", m.Message);
            Assert.AreEqual("", m.Note);

            m = new AckNak(Reply.PossibleStatus.Failure, t1);
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Failure, m.Status);
            Assert.AreEqual(0, m.IntResult);
            Assert.AreSame(t1, m.ObjResult);
            Assert.AreEqual("", m.Message);
            Assert.AreEqual("", m.Note);

            m = new AckNak(Reply.PossibleStatus.Failure, t1, "Test Message");
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Failure, m.Status);
            Assert.AreEqual(0, m.IntResult);
            Assert.AreSame(t1, m.ObjResult);
            Assert.AreEqual("Test Message", m.Message);
            Assert.AreEqual("", m.Note);

            ByteList bytes = new ByteList();
            m.Encode(bytes);
            Message msg = Message.Create(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is AckNak);
            AckNak m2 = msg as AckNak;
            Assert.AreEqual(m.Status, m2.Status);
            Assert.AreEqual(m.Note, m2.Note);
        }
Example #2
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="messageBytes">A byte list from which the message will be decoded</param>
        /// <returns>A new message of the right specialization</returns>
        new public static Reply Create(ByteList messageBytes)
        {
            Reply result = null;

            if (messageBytes == null || messageBytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid message byte array");
            }

            Int16 msgType = messageBytes.PeekInt16();

            switch (msgType)
            {
            case (Int16)MESSAGE_CLASS_IDS.AckNak:
                result = AckNak.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ReadyReply:
                result = ReadyReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ResourceReply:
                result = ResourceReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ConfigurationReply:
                result = ConfigurationReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.PlayingFieldReply:
                result = PlayingFieldReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.AgentListReply:
                result = AgentListReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.StatusReply:
                result = StatusReply.Create(messageBytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.EndUpdateStream:
                result = EndUpdateStream.Create(messageBytes);
                break;

            default:
                throw new ApplicationException("Invalid Message Class Id");
            }

            return(result);
        }
        public void AckNak_CheckEncodeDecode()
        {
            Tick t1 = new Tick();
            AckNak m1 = new AckNak(Reply.PossibleStatus.Success, 10, t1, "Test Message", "Test Note");
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m1.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Success, m1.Status);
            Assert.AreEqual(10, m1.IntResult);
            Assert.AreSame(t1, m1.ObjResult);
            Assert.AreEqual("Test Message", m1.Message);
            Assert.AreEqual("Test Note", m1.Note);

            ByteList bytes = new ByteList();
            m1.Encode(bytes);
            AckNak m2 = AckNak.Create(bytes);
            Assert.AreEqual(m1.Status, m2.Status);
            Assert.AreEqual(m1.IntResult, m2.IntResult);
            Assert.AreEqual(((Tick)m1.ObjResult).LogicalClock, ((Tick)m2.ObjResult).LogicalClock);
            Assert.AreEqual(m1.Message, m2.Message);
            Assert.AreEqual(m1.Note, m2.Note);

            bytes.Clear();
            m1.Encode(bytes);
            bytes.GetByte();            // Read one byte, which will throw the length off
            try
            {
                m2 = AckNak.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }

            bytes.Clear();
            m1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                m2 = AckNak.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
Example #4
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="messageBytes">A byte list from which the message will be decoded</param>
        /// <returns>A new message of the right specialization</returns>
        new public static AckNak Create(ByteList messageBytes)
        {
            AckNak result = null;

            if (messageBytes == null || messageBytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid message byte array");
            }
            if (messageBytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid message class id");
            }
            else
            {
                result = new AckNak();
                result.Decode(messageBytes);
            }

            return(result);
        }
        public void CreateEncodingSamples()
        {
            StreamWriter writer = new StreamWriter("MessageSamples.txt");

            MessageNumber msgNumber = MessageNumber.Create(100, 120);
            MessageNumber conversationNumber = MessageNumber.Create(200, 240);
            AgentInfo agentInfo = new AgentInfo(10, AgentInfo.PossibleAgentType.BrilliantStudent, new Common.EndPoint("129.123.5.10:1234"))
                                        {
                                            AgentStatus = AgentInfo.PossibleAgentStatus.InGame,
                                            ANumber = "A00001",
                                            FirstName = "Joe",
                                            LastName = "Jones",
                                            Location = new FieldLocation(10, 20),
                                            Points = 100,
                                            Strength = 200,
                                            Speed = 1.2
                                        };

            AckNak ackNak = new AckNak(Reply.PossibleStatus.Success, agentInfo, "Test Message")
                                        {
                                            MessageNr = msgNumber,
                                            ConversationId = conversationNumber,
                                            IntResult = 99,
                                            Note = "Test Note"
                                        };
            writer.WriteLine("AckNak");
            writer.WriteLine("\tMessageNr={0}", ackNak.MessageNr.ToString());
            writer.WriteLine("\tConversationId={0}", ackNak.ConversationId.ToString());
            writer.WriteLine("\tReplyType={0}", ackNak.ReplyType);
            writer.WriteLine("\tAckNak Status={0}", ackNak.Status);
            writer.WriteLine("\tAgent Info:");
            writer.WriteLine("\t\tId={0}", agentInfo.Id);
            writer.WriteLine("\t\tAgentStatus={0}", agentInfo.AgentStatus);
            writer.WriteLine("\t\tANumber={0}", agentInfo.ANumber);
            writer.WriteLine("\t\tFirstName={0}", agentInfo.FirstName);
            writer.WriteLine("\t\tLastName={0}", agentInfo.LastName);
            writer.WriteLine("\t\tLocation={0}", agentInfo.Location.ToString());
            writer.WriteLine("\t\tPoints={0}", agentInfo.Points);
            writer.WriteLine("\t\tStrength={0}", agentInfo.Strength);
            writer.WriteLine("\t\tSpeed={0}", agentInfo.Speed);

            ByteList byteList = new ByteList();
            ackNak.Encode(byteList);

            writer.WriteLine("");
            writer.WriteLine("Encoding:");
            writer.WriteLine(byteList.CreateLogString());
            writer.WriteLine("");
            writer.WriteLine("------------------------------------");
            writer.WriteLine("");

            JoinGame joinGame = new JoinGame(20, agentInfo)
                                        {
                                            MessageNr = msgNumber,
                                            ConversationId = conversationNumber,
                                        };

            writer.WriteLine("JoinGame");
            writer.WriteLine("\tMessageNr={0}", joinGame.MessageNr.ToString());
            writer.WriteLine("\tConversationId={0}", joinGame.ConversationId.ToString());
            writer.WriteLine("\tGameId={0}", joinGame.GameId);
            writer.WriteLine("\tAgent Info:");
            writer.WriteLine("\t\tId={0}", agentInfo.Id);
            writer.WriteLine("\t\tAgentStatus={0}", agentInfo.AgentStatus);
            writer.WriteLine("\t\tANumber={0}", agentInfo.ANumber);
            writer.WriteLine("\t\tFirstName={0}", agentInfo.FirstName);
            writer.WriteLine("\t\tLastName={0}", agentInfo.LastName);
            writer.WriteLine("\t\tLocation={0}", agentInfo.Location.ToString());
            writer.WriteLine("\t\tPoints={0}", agentInfo.Points);
            writer.WriteLine("\t\tStrength={0}", agentInfo.Strength);
            writer.WriteLine("\t\tSpeed={0}", agentInfo.Speed);

            byteList = new ByteList();
            joinGame.Encode(byteList);

            writer.WriteLine("");
            writer.WriteLine("Encoding:");
            writer.WriteLine(byteList.CreateLogString());
            writer.WriteLine("");
            writer.WriteLine("------------------------------------");
            writer.WriteLine("");

            // TODO: All of the other message types
        }
        public void AckNak_CheckProperties()
        {
            Tick t1 = new Tick();
            AckNak m = new AckNak(Reply.PossibleStatus.Success, 10, t1, "Test Message", "Test Note");
            Assert.AreEqual(Reply.PossibleTypes.AckNak, m.ReplyType);
            Assert.AreEqual(Reply.PossibleStatus.Success, m.Status);
            Assert.AreEqual(10, m.IntResult);
            Assert.AreSame(t1, m.ObjResult);
            Assert.AreEqual("Test Message", m.Message);
            Assert.AreEqual("Test Note", m.Note);

            m.IntResult = 200;
            Assert.AreEqual(200, m.IntResult);

            m.ObjResult = null;
            Assert.IsNull(m.ObjResult);
            m.ObjResult = t1;
            Assert.AreSame(t1, m.ObjResult);

            m.Message = "Testing";
            Assert.AreEqual("Testing", m.Message);

            m.Note = "Test Note";
            Assert.AreEqual("Test Note", m.Note);

            Assert.AreEqual( Message.MESSAGE_CLASS_IDS.AckNak, m.MessageTypeId());
        }
Example #7
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="messageBytes">A byte list from which the message will be decoded</param>
        /// <returns>A new message of the right specialization</returns>
        public static new AckNak Create(ByteList messageBytes)
        {
            AckNak result = null;

            if (messageBytes==null || messageBytes.RemainingToRead<MinimumEncodingLength)
                throw new ApplicationException("Invalid message byte array");
            if (messageBytes.PeekInt16() != ClassId)
                throw new ApplicationException("Invalid message class id");
            else
            {
                result = new AckNak();
                result.Decode(messageBytes);
            }

            return result;
        }