示例#1
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            bytes.AddObjects(Width, Height);                // Write out Width and Height
            Int16 SidewalkCount = (SidewalkSquares == null) ? (Int16)0 : Convert.ToInt16(SidewalkSquares.Count);

            bytes.Add(SidewalkCount);
            if (SidewalkSquares != null)
            {
                foreach (FieldLocation loc in SidewalkSquares)
                {
                    bytes.Add(loc);
                }
            }

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#2
0
        /// <summary>
        /// This method decodes of this classes from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                Width  = bytes.GetInt16();
                Height = bytes.GetInt16();

                SidewalkSquares = new List <FieldLocation>();
                int SidewalkCount = bytes.GetInt16();
                for (int i = 0; i < SidewalkCount; i++)
                {
                    SidewalkSquares.Add(bytes.GetDistributableObject() as FieldLocation);
                }

                bytes.RestorePreviosReadLimit();
            }
        }
示例#3
0
        /// <summary>
        /// Factor method to create a FieldLocation from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static PlayingFieldLayout Create(ByteList bytes)
        {
            PlayingFieldLayout result = new PlayingFieldLayout();

            result.Decode(bytes);
            return(result);
        }
示例#4
0
        /// <summary>
        /// Factor method to create a FieldLocation from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static FieldLocation Create(ByteList bytes)
        {
            FieldLocation result = new FieldLocation();

            result.Decode(bytes);
            return(result);
        }
示例#5
0
        /// <summary>
        /// Factor method to create a FieldLocation from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static EndPoint Create(ByteList bytes)
        {
            EndPoint result = new EndPoint();

            result.Decode(bytes);
            return(result);
        }
        /// <summary>
        /// This method decodes of this classes from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                CreatorId = bytes.GetInt16();
                Ticks     = new List <Tick>();
                int count = bytes.GetInt16();
                for (int i = 0; i < count; i++)
                {
                    Ticks.Add(bytes.GetDistributableObject() as Tick);
                }

                RequestTick = bytes.GetDistributableObject() as Tick;

                bytes.RestorePreviosReadLimit();
            }
        }
示例#7
0
        /// <summary>
        /// This method decodes of this classes from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else if (Immutable)
            {
                throw new ApplicationException("Cannot use Decode to alter an immutable FieldLocation object");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                X         = bytes.GetInt16();
                Y         = bytes.GetInt16();
                immutable = bytes.GetBool();

                bytes.RestorePreviosReadLimit();
            }
        }
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            bytes.Add(CreatorId);                           // Write out Creator's Id

            if (Ticks == null)
            {
                Ticks = new List <Tick>();                  // Write out the ticks that made up this whining twine
            }
            bytes.Add(Convert.ToInt16(Ticks.Count));
            foreach (Tick tick in Ticks)
            {
                bytes.Add(tick);
            }

            bytes.Add(RequestTick);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#9
0
        public ByteList GetByteList(int length)
        {
            ByteList result = new ByteList();

            result.FromBytes(GetBytes(length));
            return(result);
        }
示例#10
0
        /// <summary>
        /// This method decodes a message from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                base.Decode(bytes);

                agentType   = (PossibleAgentType)bytes.GetByte();
                agentStatus = (PossibleAgentStatus)bytes.GetByte();
                aNumber     = bytes.GetString();
                firstName   = bytes.GetString();
                lastName    = bytes.GetString();
                strength    = bytes.GetDouble();
                speed       = bytes.GetDouble();
                points      = bytes.GetDouble();
                location    = bytes.GetDistributableObject() as FieldLocation;

                bytes.RestorePreviosReadLimit();
            }
        }
        public void StatusReply_TestEverything()
        {
            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A0001", FirstName = "Joe", LastName = "Jone" };

            StatusReply r1 = new StatusReply(Reply.PossibleStatus.Success, agentInfo);
            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.AreSame(agentInfo, r1.Info);

            r1 = new StatusReply(Reply.PossibleStatus.Success, agentInfo, "test note");
            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.AreSame(agentInfo, r1.Info);
            Assert.AreEqual("test note", r1.Note);

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

            Message msg = Message.Create(byteList);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is StatusReply);
            StatusReply r2 = msg as StatusReply;
            Assert.AreEqual(r1.Status, r2.Status);
            Assert.AreEqual(r1.Info.Id, r2.Info.Id);
            Assert.AreEqual(r1.Info.LastName, r2.Info.LastName);
            Assert.AreEqual(r1.Note, r2.Note);
        }
示例#12
0
        /// <summary>
        /// Factor method to create a Excuse from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static Bomb Create(ByteList bytes)
        {
            Bomb result = new Bomb();

            result.Decode(bytes);
            return(result);
        }
示例#13
0
        /// <summary>
        /// This method decodes a message from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                lock (myLock)
                {
                    Clear();
                    Int16 count = bytes.GetInt16();
                    for (int i = 0; i < count; i++)
                    {
                        agents.Add(bytes.GetDistributableObject() as AgentInfo);
                    }
                }

                bytes.RestorePreviosReadLimit();
            }
        }
示例#14
0
        /// <summary>
        /// Factor method to create an object of this class from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static AgentList Create(ByteList bytes)
        {
            AgentList result = new AgentList();

            result.Decode(bytes);
            return(result);
        }
示例#15
0
        /// <summary>
        /// Factor method to create a Excuse from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static Excuse Create(ByteList bytes)
        {
            Excuse result = new Excuse();

            result.Decode(bytes);
            return(result);
        }
        /// <summary>
        /// Factor method to create a WhiningTwine from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static WhiningTwine Create(ByteList bytes)
        {
            WhiningTwine result = new WhiningTwine();

            result.Decode(bytes);
            return(result);
        }
示例#17
0
 public void Add(ByteList value)
 {
     if (value != null)
     {
         for (int i = 0; i <= value._addCurrentSection; i++)
         {
             Add(value._sections[i], 0, (i < value._addCurrentSection) ? SectionSize : value._addCurrentOffset);
         }
     }
 }
        public void WhiningSpinner_CheckEncodeAndDecode()
        {
            Tick t1 = new Tick();
            Tick t2 = new Tick();
            Tick t3 = new Tick();
            Tick t4 = new Tick();
            List<Tick> ticks = new List<Tick> { t1, t2, t3 };
            WhiningTwine e1 = new WhiningTwine(10, ticks, t4);
            Assert.AreEqual(10, e1.CreatorId);
            Assert.IsNotNull(e1.Ticks);
            Assert.AreEqual(3, e1.Ticks.Count);
            Assert.AreSame(t1, e1.Ticks[0]);
            Assert.AreSame(t2, e1.Ticks[1]);
            Assert.AreSame(t3, e1.Ticks[2]);
            Assert.AreSame(t4, e1.RequestTick);

            ByteList bytes = new ByteList();
            e1.Encode(bytes);
            WhiningTwine e2 = WhiningTwine.Create(bytes);
            Assert.AreEqual(e1.CreatorId, e2.CreatorId);
            Assert.AreEqual(e1.Ticks.Count, e2.Ticks.Count);
            Assert.AreEqual(e1.Ticks[0].LogicalClock, e2.Ticks[0].LogicalClock);
            Assert.AreEqual(e1.Ticks[0].HashCode, e2.Ticks[0].HashCode);
            Assert.AreEqual(e1.Ticks[1].LogicalClock, e2.Ticks[1].LogicalClock);
            Assert.AreEqual(e1.Ticks[1].HashCode, e2.Ticks[1].HashCode);
            Assert.AreEqual(e1.Ticks[2].LogicalClock, e2.Ticks[2].LogicalClock);
            Assert.AreEqual(e1.Ticks[2].HashCode, e2.Ticks[2].HashCode);
            Assert.AreEqual(e1.RequestTick.LogicalClock, e2.RequestTick.LogicalClock);
            Assert.AreEqual(e1.RequestTick.HashCode, e2.RequestTick.HashCode);

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

            bytes.Clear();
            e1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                e2 = WhiningTwine.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
示例#19
0
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            bytes.RestorePreviosReadLimit();
        }
        public void AgentInfo_CheckEncodeAndDecode()
        {
            EndPoint ep = new EndPoint("129.123.7.24:1345");
            AgentInfo info1 = new AgentInfo(20, AgentInfo.PossibleAgentType.WhiningSpinner, ep)
            {
                ANumber = "A00001",
                FirstName = "Joe",
                LastName = "Jones",
                Location = new FieldLocation(10, 20, false),
                Strength = 1200.5,
                Speed = 1500.0
            };

            ByteList bytes = new ByteList();
            info1.Encode(bytes);
            AgentInfo info2 = AgentInfo.Create(bytes);
            Assert.AreEqual(info1.Id, info2.Id);
            Assert.AreEqual(info1.AgentType, info2.AgentType);
            Assert.AreEqual(info1.ANumber, info2.ANumber);
            Assert.AreEqual(info1.FirstName, info2.FirstName);
            Assert.AreEqual(info1.LastName, info2.LastName);
            Assert.AreEqual(info1.Strength, info2.Strength);
            Assert.AreEqual(info1.Speed, info2.Speed);
            Assert.AreEqual(info1.Points, info2.Points);
            Assert.AreEqual(info1.Location.X, info2.Location.X);
            Assert.AreEqual(info1.Location.Y, info2.Location.Y);
            Assert.AreEqual(info1.CommunicationEndPoint.Address, info2.CommunicationEndPoint.Address);
            Assert.AreEqual(info1.CommunicationEndPoint.Port, info2.CommunicationEndPoint.Port);

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

            bytes.Clear();
            info1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                info2 = AgentInfo.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        /// <summary>
        /// This method decodes a message from a byte list
        /// </summary>
        /// <param name="bytes"></param>
        protected override void Decode(ByteList bytes)
        {
            Int16 objType   = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            ProcessId = bytes.GetInt16();
            SeqNumber = bytes.GetInt16();

            bytes.RestorePreviosReadLimit();
        }
        public void ByteList_CreateLogString()
        {
            byte[] bigArray = new byte[300];
            for (int i = 0; i < 300; i++)
                bigArray[i] = Convert.ToByte(i*11 & 255);
            ByteList bigList = new ByteList(bigArray);

            string logString = bigList.CreateLogString();
            Assert.IsNotNull(logString);

            Console.WriteLine(logString);
        }
        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);
        }
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            Configuration = bytes.GetDistributableObject() as GameConfiguration;

            bytes.RestorePreviosReadLimit();
        }
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            Component = bytes.GetDistributableObject() as AgentInfo;

            bytes.RestorePreviosReadLimit();
        }
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                              // Write out this class id first

            Int16 lengthPos = bytes.CurrentWritePosition;    // Get the current write position, so we
                                                                    // can write the length here later
            bytes.Add((Int16)0);                             // Write out a place holder for the length

            base.Encode(bytes);                              // Encode the part of the object defined
                                                                    // by the base class
            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);
            bytes.WriteInt16To(lengthPos, length);           // Write out the length of this object
        }
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            EnablingTick = bytes.GetDistributableObject() as Tick;

            bytes.RestorePreviosReadLimit();
        }
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            Layout = bytes.GetDistributableObject() as PlayingFieldLayout;

            bytes.RestorePreviosReadLimit();
        }
        public void TickTester_CheckEncodeDecode()
        {
            Tick tick1 = new Tick(10);
            tick1.LogicalClock = 100;
            ByteList bytes = new ByteList();
            tick1.Encode(bytes);
            Tick tick2 = Tick.Create(bytes);
            Assert.AreEqual(10, tick1.ForAgentId);
            Assert.AreEqual(tick1.LogicalClock, tick2.LogicalClock);

            tick1.LogicalClock = 0;
            bytes = new ByteList();
            tick1.Encode(bytes);
            tick2 = Tick.Create(bytes);
            Assert.AreEqual(tick1.LogicalClock, tick2.LogicalClock);

            tick1.LogicalClock = Int32.MaxValue;
            bytes = new ByteList();
            tick1.Encode(bytes);
            tick2 = Tick.Create(bytes);
            Assert.AreEqual(tick1.ForAgentId, tick1.ForAgentId);
            Assert.AreEqual(tick1.LogicalClock, tick2.LogicalClock);

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

            bytes.Clear();
            tick1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                tick2 = Tick.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public static DistributableObject Create(ByteList bytes)
        {
            DistributableObject result = null;

            if (bytes == null || bytes.RemainingToRead < 4)
                throw new ApplicationException("Invalid byte array");

            DISTRIBUTABLE_CLASS_IDS objType = (DISTRIBUTABLE_CLASS_IDS) bytes.PeekInt16();
            switch (objType)
            {
                case DISTRIBUTABLE_CLASS_IDS.MessageNumber:
                    result = MessageNumber.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.Bomb:
                    result = Bomb.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.AgentInfo:
                    result = AgentInfo.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.AgentList:
                    result = AgentList.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.EndPoint:
                    result = EndPoint.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.Excuse:
                    result = Excuse.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.FieldLocation:
                    result = FieldLocation.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.GameConfiguration:
                    result = GameConfiguration.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.PlayingFieldLayout:
                    result = PlayingFieldLayout.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.Tick:
                    result = Tick.Create(bytes);
                    break;
                case DISTRIBUTABLE_CLASS_IDS.WhiningTwine:
                    result = WhiningTwine.Create(bytes);
                    break;
                default:
                    throw new ApplicationException(string.Format("Invalid Class Id={0}", objType));
            }
            return result;
        }
示例#31
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            bytes.AddObjects(X, Y, Immutable);              // Write out X, Y, and Immutable properties

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#32
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                                  // Write out a place holder for the length

            bytes.AddObjects(ForAgentId, LogicalClock, HashCode); // Write out Address and Port

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
        /// <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 AddComponent Create(ByteList messageBytes)
        {
            AddComponent result = null;

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

            return result;
        }
示例#34
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the message will be decoded</param>
        /// <returns>A new message of the right specialization</returns>
        public static new ThrowBomb Create(ByteList bytes)
        {
            ThrowBomb result = null;

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

            return result;
        }
        /// <summary>
        /// This method encodes
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                              // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;    // Get the current write position, so we

            // can write the length here later
            bytes.Add((Int16)0);                             // Write out a place holder for the length

            bytes.Add(ProcessId);                            // Write out a place holder for the length
            bytes.Add(SeqNumber);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);           // Write out the length of this object
        }
示例#36
0
 public void Add(ByteList value)
 {
     if (value != null)
     {
         for (int i = 0; i <= value._addCurrentSection; i++)
         {
             if (i < value._addCurrentSection)
             {
                 Add(value._sections[i], 0, SECTION_SIZE);
             }
             else
             {
                 Add(value._sections[i], 0, value._addCurrentOffset);
             }
         }
     }
 }
        public void Bomb_CheckEncodeAndDecode()
        {
            Tick t1 = new Tick();
            List<Excuse> eList = new List<Excuse> { CreateExcuse(10), CreateExcuse(11), CreateExcuse(12) };
            List<WhiningTwine> wtList = new List<WhiningTwine> { CreateTwine(20), CreateTwine(21), CreateTwine(22) };
            Bomb b1 = new Bomb(1, eList, wtList, t1);
            Assert.AreEqual(1, b1.CreatorId);
            Assert.AreSame(eList, b1.Excuses);
            Assert.AreSame(wtList, b1.Twine);
            Assert.AreSame(t1, b1.BuiltOnTick);

            ByteList bytes = new ByteList();
            b1.Encode(bytes);
            Bomb b2 = Bomb.Create(bytes);
            Assert.AreEqual(b1.CreatorId, b2.CreatorId);
            Assert.AreEqual(b1.Excuses.Count, b2.Excuses.Count);
            Assert.AreEqual(b1.Twine.Count, b2.Twine.Count);

            Assert.AreEqual(b1.BuiltOnTick.LogicalClock, b2.BuiltOnTick.LogicalClock);
            Assert.AreEqual(b1.BuiltOnTick.HashCode, b2.BuiltOnTick.HashCode);

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

            bytes.Clear();
            b1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                b2 = Bomb.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        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)
            {
            }
        }
        public void AgentList_CheckEncodeAndDecode()
        {
            AgentInfo info1 = new AgentInfo(10, AgentInfo.PossibleAgentType.ExcuseGenerator);
            AgentInfo info2 = new AgentInfo(11, AgentInfo.PossibleAgentType.WhiningSpinner);
            AgentInfo info3 = new AgentInfo(12, AgentInfo.PossibleAgentType.BrilliantStudent);

            AgentList list1 = new AgentList();
            list1.Add(info1);
            list1.Add(info2);
            list1.Add(info3);

            ByteList bytes = new ByteList();
            list1.Encode(bytes);
            AgentList list2 = AgentList.Create(bytes);
            Assert.AreEqual(3, list2.Count);
            Assert.AreEqual(10, list2[0].Id);
            Assert.AreEqual(11, list2[1].Id);
            Assert.AreEqual(12, list2[2].Id);

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

            bytes.Clear();
            list1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                list2 = AgentList.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we
                                                            // can write the length here later

            bytes.Add((Int16) 0);                           // Write out a place holder for the length

            bytes.AddObjects(Width, Height);                // Write out Width and Height
            Int16 SidewalkCount = (SidewalkSquares == null) ? (Int16) 0 : Convert.ToInt16(SidewalkSquares.Count);
            bytes.Add(SidewalkCount);
            if (SidewalkSquares!=null)
                foreach (FieldLocation loc in SidewalkSquares)
                    bytes.Add(loc);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);
            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
        /// <summary>
        /// Factory Method that creates a message from a byte string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        new public static MessageNumber Create(ByteList bytes)
        {
            MessageNumber result = null;

            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid message byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid message class id");
            }
            else
            {
                result = new MessageNumber();
                result.Decode(bytes);
            }
            return(result);
        }
        public void ReadyReply_TestEverything()
        {
            ReadyReply r1 = new ReadyReply(Reply.PossibleStatus.Failure);
            Assert.AreEqual(Reply.PossibleStatus.Failure, r1.Status);

            r1 = new ReadyReply(Reply.PossibleStatus.Success, "test note");
            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.AreEqual("test note", r1.Note);

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

            Message msg = Message.Create(byteList);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is ReadyReply);
            ReadyReply r2 = msg as ReadyReply;
            Assert.AreEqual(r1.Status, r2.Status);
            Assert.AreEqual(r1.Note, r2.Note);
        }
        public void ByteList_TestConstuctors()
        {
            // Check out the default constructor
            ByteList myBytes = new ByteList();
            Assert.IsNotNull(myBytes);
            Assert.AreEqual(0, myBytes.Length);

            // Check out the general constructor that take any number of objects
            // Case 1: A single boolean object
            myBytes = new ByteList(true);
            Assert.IsNotNull(myBytes);
            Assert.AreEqual(1, myBytes.Length);
            Assert.AreEqual(1, myBytes[0]);

            // Case 2: 3 different objects
            myBytes = new ByteList(true, 123, "Hello");
            Assert.IsNotNull(myBytes);
            Assert.AreEqual(1 + 4 + (2 + 2*5), myBytes.Length);

            // Case 3: 3 strings of lengths 5, 5, and 52
            myBytes = new ByteList("Hello", "There", "You amazing software developer and brilliant student");
            Assert.IsNotNull(myBytes);
            Assert.AreEqual((2 + 2*5) + (2 + 2*5) + (2 + 2*52), myBytes.Length);

            // Case 4: with a bunch of other parameters types
            ByteList moreBytes = new ByteList(  myBytes,
                                                (Int16)10,
                                                (Int64)20,
                                                (Single)30.0,
                                                (Double)40.0,
                                                new byte[] { 1, 2, 3 });
            Assert.IsNotNull(moreBytes);
            Assert.AreEqual(myBytes.Length + 2 + 8 + 4 + 8 + 3, moreBytes.Length);

            byte[] bigArray = new byte[10000];
            for (int i = 0; i < 10000; i++)
                bigArray[i] = Convert.ToByte(i & 255);
            ByteList bigList = new ByteList(bigArray);
            byte[] bigArray2 = bigList.GetBytes(8192);
            for (int i = 0; i < 8192; i++)
                Assert.AreEqual(bigArray[i], bigArray2[i]);
        }
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we
                                                            // can write the length here later

            bytes.Add((Int16) 0);                           // Write out a place holder for the length

            bytes.Add(CreatorId);                           // Write out Creator's Id

            if (Ticks == null) Ticks = new List<Tick>();    // Write out the ticks that made up this whining twine
            bytes.Add(Convert.ToInt16(Ticks.Count));
            foreach (Tick tick in Ticks)
                bytes.Add(tick);

            bytes.Add(RequestTick);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);
            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#45
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            lock (myLock)
            {
                bytes.Add(Convert.ToInt16(agents.Count));
                foreach (AgentInfo component in agents)
                {
                    bytes.Add(component);
                }
            }
            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#46
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            base.Encode(bytes);

            if (ANumber == null)
            {
                ANumber = string.Empty;
            }
            if (FirstName == null)
            {
                FirstName = string.Empty;
            }
            if (LastName == null)
            {
                LastName = string.Empty;
            }

            bytes.AddObjects((byte)AgentType,
                             (byte)AgentStatus,
                             ANumber,
                             FirstName,
                             LastName,
                             Strength,
                             Speed,
                             Points,
                             Location);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#47
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we

            // can write the length here later

            bytes.Add((Int16)0);                            // Write out a place holder for the length

            bytes.Add(CreatorId);                           // Write out Creator's Id

            if (Excuses == null)
            {
                Excuses = new List <Excuse>();
            }
            bytes.Add(Convert.ToInt16(Excuses.Count));
            foreach (Excuse excuse in Excuses)
            {
                bytes.Add(excuse);
            }

            if (Twine == null)
            {
                Twine = new List <WhiningTwine>();
            }
            bytes.Add(Convert.ToInt16(Twine.Count));
            foreach (WhiningTwine twine in Twine)
            {
                bytes.Add(twine);
            }

            bytes.Add(BuiltOnTick);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);

            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }
示例#48
0
        /// <summary>
        /// This method decodes of this classes from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                Address = bytes.GetInt32();
                Port    = bytes.GetInt32();

                bytes.RestorePreviosReadLimit();
            }
        }
        public void JoinGame_EncodingAndDecoding()
        {
            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A0001", FirstName = "Joe", LastName = "Jone" };
            JoinGame jg1 = new JoinGame(10, agentInfo);
            Assert.AreEqual(10, jg1.GameId);
            Assert.AreSame(agentInfo, jg1.AgentInfo);

            ByteList bytes = new ByteList();
            jg1.Encode(bytes);
            JoinGame jg2 = JoinGame.Create(bytes);
            Assert.AreEqual(jg1.GameId, jg2.GameId);

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

            bytes.Clear();
            jg1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                jg2 = JoinGame.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void FieldLayoutReply_Everything()
        {
            PlayingFieldLayout pfl = new PlayingFieldLayout(100, 120);
            Assert.AreEqual(100, pfl.Width);
            Assert.AreEqual(120, pfl.Height);
            Assert.IsNotNull(pfl.SidewalkSquares);

            PlayingFieldReply r1 = new PlayingFieldReply(Reply.PossibleStatus.Success, pfl, "Test");

            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.IsNotNull(r1.Layout);
            Assert.AreSame(pfl, r1.Layout);

            ByteList bytes = new ByteList();
            r1.Encode(bytes);
            Message msg = Message.Create(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is PlayingFieldReply);
            PlayingFieldReply r2 = msg as PlayingFieldReply;
            Assert.AreEqual(Reply.PossibleStatus.Success, r2.Status);

            Assert.AreEqual(pfl.Height, r2.Layout.Height);
            Assert.AreEqual(pfl.Width, r2.Layout.Width);
        }
示例#51
0
        /// <summary>
        /// This method decodes a message from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                id = bytes.GetInt16();
                communicationEndPoint = bytes.GetDistributableObject() as EndPoint;
                RaiseChangedEvent();

                bytes.RestorePreviosReadLimit();
            }
        }
        /// <summary>
        /// This method decodes of this classes from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                PlayingFieldWidth  = bytes.GetInt16();
                PlayingFieldHeight = bytes.GetInt16();

                BrilliantStudentRegistrationMin = bytes.GetInt16();
                BrilliantStudentRegistrationMax = bytes.GetInt16();

                BrilliantStudentInitialStrength         = bytes.GetFloat();
                BrilliantStudentBaseSpeed               = bytes.GetFloat();
                BrilliantStudentSidewalkSpeedMultiplier = bytes.GetFloat();
                BrilliantStudentDeathToZombieDelay      = bytes.GetFloat();

                ExcuseGeneratorRegistrationMin       = bytes.GetInt16();
                ExcuseGeneratorRegistrationMax       = bytes.GetInt16();
                ExcuseGeneratorInitialStrength       = bytes.GetFloat();
                NumberOfTicksRequiredToBuildAnExcuse = bytes.GetByte();

                WhiningSpinnerRegistrationMin     = bytes.GetInt16();
                WhiningSpinnerRegistrationMax     = bytes.GetInt16();
                WhiningSpinnerInitialStrength     = bytes.GetFloat();
                NumberOfTicksRequiredToBuildTwine = bytes.GetByte();

                ZombieInitialStrengthMin      = bytes.GetInt16();
                ZombieInitialStrengthMax      = bytes.GetInt16();
                ZombieInitialSpeedMax         = bytes.GetFloat();
                ZombieInitialSpeedMin         = bytes.GetFloat();
                ZombieSidewalkSpeedMultiplier = bytes.GetFloat();
                ZombieCreationRate            = bytes.GetFloat();
                ZombieCreationAcceleration    = bytes.GetFloat();
                ZombieEatingRate = bytes.GetFloat();
                ZombieStrengthIncreaseForEatingStudent   = bytes.GetFloat();
                ZombieStrengthIncreaseForExcuseGenerator = bytes.GetFloat();
                ZombieStrengthIncreaseForWhiningSpinner  = bytes.GetFloat();
                MinEatingDistance = bytes.GetFloat();
                MaxEatingDistance = bytes.GetFloat();

                RefereeRegistrationMin = bytes.GetInt16();
                RefereeRegistrationMax = bytes.GetInt16();

                BombExcuseDamage             = bytes.GetInt16();
                BombTwinePerSquareOfDistance = bytes.GetFloat();
                BombDamageDiffusionFactor    = bytes.GetFloat();

                TickInterval         = bytes.GetInt16();
                TickLifetime         = bytes.GetInt16();
                TicksToStrengthRatio = bytes.GetFloat();

                bytes.RestorePreviosReadLimit();
            }
        }
示例#53
0
 public virtual void Encode(ByteList bytes)
 {
 }
示例#54
0
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                              // Write out this class id first

            Int16 lengthPos = bytes.CurrentWritePosition;    // Get the current write position, so we
                                                                    // can write the length here later
            bytes.Add((Int16)0);                             // Write out a place holder for the length

            base.Encode(bytes);                              // Encode the part of the object defined
                                                             // by the base class

            bytes.AddObjects(ThrowingBrilliantStudentId, Bomb, TowardsSquare, EnablingTick);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);
            bytes.WriteInt16To(lengthPos, length);           // Write out the length of this object
        }
示例#55
0
        public override void Decode(ByteList bytes)
        {
            Int16 objType = bytes.GetInt16();
            Int16 objLength = bytes.GetInt16();

            bytes.SetNewReadLimit(objLength);

            base.Decode(bytes);

            ThrowingBrilliantStudentId = bytes.GetInt16();
            Bomb = bytes.GetDistributableObject() as Bomb;
            TowardsSquare = bytes.GetDistributableObject() as FieldLocation;
            EnablingTick = bytes.GetDistributableObject() as Tick;

            bytes.RestorePreviosReadLimit();
        }
        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
        }
示例#57
0
 protected virtual void Decode(ByteList bytes)
 {
 }
        public void GameConfiguration_CheckEncodeAndDecode()
        {
            GameConfiguration gc1 = new GameConfiguration();

            gc1.PlayingFieldWidth = 50;
            gc1.PlayingFieldHeight = 51;

            gc1.BrilliantStudentRegistrationMin = 10;
            gc1.BrilliantStudentRegistrationMax = 11;

            gc1.BrilliantStudentInitialStrength = 2.1F;
            gc1.BrilliantStudentBaseSpeed = 2.2F;
            gc1.BrilliantStudentSidewalkSpeedMultiplier = 2.3F;
            gc1.BrilliantStudentDeathToZombieDelay = 2.4F;

            gc1.ExcuseGeneratorRegistrationMin = 12;
            gc1.ExcuseGeneratorRegistrationMax = 13;
            gc1.ExcuseGeneratorInitialStrength = 2.5F;
            gc1.NumberOfTicksRequiredToBuildTwine = 3;

            gc1.WhiningSpinnerRegistrationMin = 14;
            gc1.WhiningSpinnerRegistrationMax = 15;
            gc1.WhiningSpinnerInitialStrength = 2.8F;
            gc1.NumberOfTicksRequiredToBuildTwine = 2;

            gc1.ZombieInitialStrengthMin = 16;
            gc1.ZombieInitialStrengthMax = 17;
            gc1.ZombieInitialSpeedMax = 3.1F;
            gc1.ZombieInitialSpeedMin = 3.2F;
            gc1.ZombieSidewalkSpeedMultiplier = 3.3F;
            gc1.ZombieCreationRate = 3.4F;
            gc1.ZombieCreationAcceleration = 3.5F;
            gc1.ZombieEatingRate = 3.6F;
            gc1.ZombieStrengthIncreaseForEatingStudent = 3.7F;
            gc1.ZombieStrengthIncreaseForExcuseGenerator = 3.8F;
            gc1.ZombieStrengthIncreaseForWhiningSpinner = 3.9F;
            gc1.MaxEatingDistance = 0.5F;
            gc1.MaxEatingDistance = 4.5F;

            gc1.RefereeRegistrationMin = 22;
            gc1.RefereeRegistrationMax = 23;

            gc1.BombExcuseDamage = 18;
            gc1.BombTwinePerSquareOfDistance = 4.0F;
            gc1.BombDamageDiffusionFactor = 4.1F;

            gc1.TickLifetime = 19;
            gc1.TicksToStrengthRatio = 4.2F;

            ByteList bytes = new ByteList();
            gc1.Encode(bytes);
            GameConfiguration gc2 = GameConfiguration.Create(bytes);
            Assert.AreEqual(gc1.PlayingFieldWidth, gc2.PlayingFieldWidth);
            Assert.AreEqual(gc1.PlayingFieldHeight, gc2.PlayingFieldHeight);

            Assert.AreEqual(gc1.BrilliantStudentRegistrationMin, gc2.BrilliantStudentRegistrationMin);
            Assert.AreEqual(gc1.BrilliantStudentRegistrationMax, gc2.BrilliantStudentRegistrationMax);

            Assert.AreEqual(gc1.BrilliantStudentInitialStrength, gc2.BrilliantStudentInitialStrength);
            Assert.AreEqual(gc1.BrilliantStudentBaseSpeed, gc2.BrilliantStudentBaseSpeed);
            Assert.AreEqual(gc1.BrilliantStudentSidewalkSpeedMultiplier, gc2.BrilliantStudentSidewalkSpeedMultiplier);
            Assert.AreEqual(gc1.BrilliantStudentDeathToZombieDelay, gc2.BrilliantStudentDeathToZombieDelay);

            Assert.AreEqual(gc1.ExcuseGeneratorRegistrationMin, gc2.ExcuseGeneratorRegistrationMin);
            Assert.AreEqual(gc1.ExcuseGeneratorRegistrationMax, gc2.ExcuseGeneratorRegistrationMax);
            Assert.AreEqual(gc1.ExcuseGeneratorInitialStrength, gc2.ExcuseGeneratorInitialStrength);
            Assert.AreEqual(gc1.NumberOfTicksRequiredToBuildAnExcuse, gc2.NumberOfTicksRequiredToBuildAnExcuse);

            Assert.AreEqual(gc1.WhiningSpinnerRegistrationMin, gc2.WhiningSpinnerRegistrationMin);
            Assert.AreEqual(gc1.WhiningSpinnerRegistrationMax, gc2.WhiningSpinnerRegistrationMax);
            Assert.AreEqual(gc1.WhiningSpinnerInitialStrength, gc2.WhiningSpinnerInitialStrength);
            Assert.AreEqual(gc1.NumberOfTicksRequiredToBuildTwine, gc2.NumberOfTicksRequiredToBuildTwine);

            Assert.AreEqual(gc1.ZombieInitialStrengthMin, gc2.ZombieInitialStrengthMin);
            Assert.AreEqual(gc1.ZombieInitialStrengthMax, gc2.ZombieInitialStrengthMax);
            Assert.AreEqual(gc1.ZombieInitialSpeedMax, gc2.ZombieInitialSpeedMax);
            Assert.AreEqual(gc1.ZombieInitialSpeedMin, gc2.ZombieInitialSpeedMin);
            Assert.AreEqual(gc1.ZombieSidewalkSpeedMultiplier, gc2.ZombieSidewalkSpeedMultiplier);
            Assert.AreEqual(gc1.ZombieCreationRate, gc2.ZombieCreationRate);
            Assert.AreEqual(gc1.ZombieCreationAcceleration, gc2.ZombieCreationAcceleration);
            Assert.AreEqual(gc1.ZombieEatingRate, gc2.ZombieEatingRate);
            Assert.AreEqual(gc1.ZombieStrengthIncreaseForEatingStudent, gc2.ZombieStrengthIncreaseForEatingStudent);
            Assert.AreEqual(gc1.ZombieStrengthIncreaseForExcuseGenerator, gc2.ZombieStrengthIncreaseForExcuseGenerator);
            Assert.AreEqual(gc1.ZombieStrengthIncreaseForWhiningSpinner, gc2.ZombieStrengthIncreaseForWhiningSpinner);
            Assert.AreEqual(gc1.MinEatingDistance, gc2.MinEatingDistance);
            Assert.AreEqual(gc1.MaxEatingDistance, gc2.MaxEatingDistance);

            Assert.AreEqual(gc1.RefereeRegistrationMin, gc2.RefereeRegistrationMin);
            Assert.AreEqual(gc1.RefereeRegistrationMax, gc2.RefereeRegistrationMax);

            Assert.AreEqual(gc1.BombExcuseDamage, gc2.BombExcuseDamage);
            Assert.AreEqual(gc1.BombTwinePerSquareOfDistance, gc2.BombTwinePerSquareOfDistance);
            Assert.AreEqual(gc1.BombDamageDiffusionFactor, gc2.BombDamageDiffusionFactor);

            Assert.AreEqual(gc1.TickLifetime, gc2.TickLifetime);
            Assert.AreEqual(gc1.TicksToStrengthRatio, gc2.TicksToStrengthRatio);

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

            bytes.Clear();
            gc1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                gc2 = GameConfiguration.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void FieldLocation_CheckEncodeDecode()
        {
            ByteList bytes = new ByteList();

            FieldLocation loc1 = new FieldLocation { X = 100, Y = 200 };
            loc1.Encode(bytes);
            Assert.AreEqual(9, bytes.Length);
            Assert.AreEqual(3, bytes[0]);
            Assert.AreEqual(238, bytes[1]);
            Assert.AreEqual(0, bytes[2]);
            Assert.AreEqual(5, bytes[3]);
            Assert.AreEqual(0, bytes[4]);
            Assert.AreEqual(100, bytes[5]);
            Assert.AreEqual(0, bytes[6]);
            Assert.AreEqual(200, bytes[7]);
            Assert.AreEqual(0, bytes[8]);

            FieldLocation loc2 = FieldLocation.Create(bytes);
            Assert.AreEqual(loc1.X, loc2.X);
            Assert.AreEqual(loc1.Y, loc2.Y);
            Assert.AreEqual(false, loc2.Immutable);
        }
示例#60
0
        /// <summary>
        /// This method encodes an object of this class into a byte list
        /// </summary>
        /// <param name="bytes"></param>
        public override void Encode(ByteList bytes)
        {
            bytes.Add(ClassId);                             // Write out the class type

            Int16 lengthPos = bytes.CurrentWritePosition;   // Get the current write position, so we
                                                            // can write the length here later

            bytes.Add((Int16) 0);                           // Write out a place holder for the length

            bytes.Add(CreatorId);                           // Write out Creator's Id

            if (Excuses == null) Excuses = new List<Excuse>();
            bytes.Add(Convert.ToInt16(Excuses.Count));
            foreach (Excuse excuse in Excuses)
                bytes.Add(excuse);

            if (Twine == null) Twine = new List<WhiningTwine>();
            bytes.Add(Convert.ToInt16(Twine.Count));
            foreach (WhiningTwine twine in Twine)
                bytes.Add(twine);

            bytes.Add(BuiltOnTick);

            Int16 length = Convert.ToInt16(bytes.CurrentWritePosition - lengthPos - 2);
            bytes.WriteInt16To(lengthPos, length);          // Write out the length of this object
        }