Ejemplo n.º 1
0
        public void Registrar_TestEndPointReflection()
        {
            RegistrarClient registrar = new RegistrarClient("LocalHttpBinding_IRegistrar");
            // RegistrarClient registrar = new RegistrarClient("ProdHttpBinding_IRegistrar");

            string reflectorEndPointString = registrar.EndPointReflector();

            Assert.IsNotNull(reflectorEndPointString);
            Common.EndPoint reflectorEndPoint = new Common.EndPoint(reflectorEndPointString);

            UdpClient testClient = new UdpClient(0, AddressFamily.InterNetwork);

            byte[] sendBuffer = ASCIIEncoding.ASCII.GetBytes("hello");
            testClient.Send(sendBuffer, sendBuffer.Length, reflectorEndPoint.GetIPEndPoint());

            testClient.Client.ReceiveTimeout = 1000;
            IPEndPoint sendingEP = new IPEndPoint(IPAddress.Any, 0);

            byte[] receiveBuffer = testClient.Receive(ref sendingEP);

            Assert.IsNotNull(receiveBuffer);
            string reflectedEP = ASCIIEncoding.ASCII.GetString(receiveBuffer);

            Assert.IsNotNull(reflectedEP);
            Assert.IsTrue(reflectedEP.Length > 8);
            string[] tmp = reflectedEP.Split(':');
            Assert.IsTrue(tmp.Length == 2);
        }
Ejemplo n.º 2
0
 public GameInfo(Int16 id, string label, EndPoint ep, string status)
     : this(id, label, ep)
 {
     Int16 tmp = 0;
     Int16.TryParse(status, out tmp);
     Status = (GameStatus) tmp;
 }
Ejemplo n.º 3
0
 protected GameInfo(Int16 id, string label, EndPoint ep)
 {
     Id = id;
     Label = label;
     CommunicationEndPoint = ep;
     AliveTimestamp = DateTime.Now;
 }
        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)
            {
            }
        }
Ejemplo n.º 5
0
        public void Registrar_TestEverything()
        {
            RegistrarClient registrar = new RegistrarClient("LocalHttpBinding_IRegistrar");

            // RegistrarClient registrar = new RegistrarClient("ProdHttpBinding_IRegistrar");

            Common.EndPoint ep0 = new Common.EndPoint("129.143.23.10:2300");
            GameInfo        g0  = registrar.RegisterGame("Test Game 0", ep0);

            Assert.IsNotNull(g0);
            Assert.AreEqual("Test Game 0", g0.Label);
            Assert.AreEqual(ep0.Address, g0.CommunicationEndPoint.Address);
            Assert.AreEqual(ep0.Port, g0.CommunicationEndPoint.Port);
            Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status);

            Common.EndPoint ep1 = new Common.EndPoint("129.143.23.10:2304");
            GameInfo        g1  = registrar.RegisterGame("Test Game 1", ep1);

            Assert.IsNotNull(g1);
            Assert.AreEqual("Test Game 1", g1.Label);
            Assert.AreEqual(ep1.Address, g1.CommunicationEndPoint.Address);
            Assert.AreEqual(ep1.Port, g1.CommunicationEndPoint.Port);
            Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status);

            GameInfo[] games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContain(games, g0.Id, g0.Label));
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));

            GameInfoAlt[] gamesAlt = registrar.GetGamesAlt(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContainAlt(gamesAlt, g0.Id, g0.Label));
            Assert.IsTrue(GamesContainAlt(gamesAlt, g1.Id, g1.Label));

            registrar.ChangeStatus(g1.Id, GameInfo.GameStatus.AVAILABLE);
            games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContain(games, g0.Id, g0.Label));
            games = registrar.GetGames(GameInfo.GameStatus.AVAILABLE);
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));

            for (int i = 0; i < 90; i++)
            {
                Thread.Sleep(1000);
                registrar.AmAlive(g1.Id);
            }

            games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsFalse(GamesContain(games, g0.Id, g0.Label));
            games = registrar.GetGames(GameInfo.GameStatus.AVAILABLE);
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));
        }
        public void Registrar_TestEverything()
        {
            RegistrarClient registrar = new RegistrarClient("LocalHttpBinding_IRegistrar");
            // RegistrarClient registrar = new RegistrarClient("ProdHttpBinding_IRegistrar");

            Common.EndPoint ep0 = new Common.EndPoint("129.143.23.10:2300");
            GameInfo g0 = registrar.RegisterGame("Test Game 0", ep0);
            Assert.IsNotNull(g0);
            Assert.AreEqual("Test Game 0", g0.Label);
            Assert.AreEqual(ep0.Address, g0.CommunicationEndPoint.Address);
            Assert.AreEqual(ep0.Port, g0.CommunicationEndPoint.Port);
            Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status);

            Common.EndPoint ep1 = new Common.EndPoint("129.143.23.10:2304");
            GameInfo g1 = registrar.RegisterGame("Test Game 1", ep1);
            Assert.IsNotNull(g1);
            Assert.AreEqual("Test Game 1", g1.Label);
            Assert.AreEqual(ep1.Address, g1.CommunicationEndPoint.Address);
            Assert.AreEqual(ep1.Port, g1.CommunicationEndPoint.Port);
            Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status);

            GameInfo[] games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContain(games, g0.Id, g0.Label));
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));

            GameInfoAlt[] gamesAlt = registrar.GetGamesAlt(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContainAlt(gamesAlt, g0.Id, g0.Label));
            Assert.IsTrue(GamesContainAlt(gamesAlt, g1.Id, g1.Label));

            registrar.ChangeStatus(g1.Id, GameInfo.GameStatus.AVAILABLE);
            games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsTrue(GamesContain(games, g0.Id, g0.Label));
            games = registrar.GetGames(GameInfo.GameStatus.AVAILABLE);
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));

            for (int i = 0; i < 90; i++)
            {
                Thread.Sleep(1000);
                registrar.AmAlive(g1.Id);
            }

            games = registrar.GetGames(GameInfo.GameStatus.NOT_INITIAlIZED);
            Assert.IsFalse(GamesContain(games, g0.Id, g0.Label));
            games = registrar.GetGames(GameInfo.GameStatus.AVAILABLE);
            Assert.IsTrue(GamesContain(games, g1.Id, g1.Label));
        }
Ejemplo n.º 7
0
        public void AgentInfo_CheckEquals()
        {
            Common.EndPoint ep1 = new Common.EndPoint("129.123.7.49:3520");
            Common.EndPoint ep2 = new Common.EndPoint("129.123.7.49:3520");
            Common.EndPoint ep3 = new Common.EndPoint("129.123.7.49:3521");
            Common.EndPoint ep4 = new Common.EndPoint("129.123.7.48:3521");

            Assert.IsTrue(ep1.Equals(ep2));
            Assert.IsTrue(ep2.Equals(ep1));
            Assert.IsFalse(ep1.Equals(ep3));
            Assert.IsFalse(ep3.Equals(ep1));
            Assert.IsFalse(ep1.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep1));
            Assert.IsFalse(ep2.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep2));
            Assert.IsFalse(ep3.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep3));

            ep3.Port = 3520;
            Assert.IsTrue(ep1.Equals(ep3));
            Assert.IsTrue(ep3.Equals(ep1));

            ep4.Address = ep1.Address;
            ep4.Port    = ep1.Port;
            Assert.IsTrue(ep1.Equals(ep4));
            Assert.IsTrue(ep4.Equals(ep1));

            List <Common.EndPoint> epList = new List <Common.EndPoint>();

            epList.Add(new Common.EndPoint("129.123.7.49:3530"));
            epList.Add(new Common.EndPoint("129.123.7.49:3531"));
            epList.Add(new Common.EndPoint("129.123.7.49:3532"));
            epList.Add(new Common.EndPoint("129.123.7.49:3533"));
            epList.Add(new Common.EndPoint("129.123.7.49:3534"));
            epList.Add(new Common.EndPoint("129.123.7.49:3535"));
            Assert.AreEqual(6, epList.Count);

            Common.EndPoint t1 = new Common.EndPoint("129.123.7.49:3530");
            Common.EndPoint t2 = new Common.EndPoint("129.123.7.49:3532");
            Common.EndPoint t3 = new Common.EndPoint("129.123.7.49:3535");
            Common.EndPoint t4 = new Common.EndPoint("129.123.7.49:9999");
            Assert.AreEqual(0, epList.FindIndex(delegate(Common.EndPoint ep) { return(ep.Equals(t1)); }));
            Assert.AreEqual(2, epList.FindIndex(delegate(Common.EndPoint ep) { return(ep.Equals(t2)); }));
            Assert.AreEqual(5, epList.FindIndex(delegate(Common.EndPoint ep) { return(ep.Equals(t3)); }));
            Assert.AreEqual(-1, epList.FindIndex(delegate(Common.EndPoint ep) { return(ep.Equals(t4)); }));
        }
        public void AgentInfo_CheckEquals()
        {
            Common.EndPoint ep1 = new Common.EndPoint("129.123.7.49:3520");
            Common.EndPoint ep2 = new Common.EndPoint("129.123.7.49:3520");
            Common.EndPoint ep3 = new Common.EndPoint("129.123.7.49:3521");
            Common.EndPoint ep4 = new Common.EndPoint("129.123.7.48:3521");

            Assert.IsTrue(ep1.Equals(ep2));
            Assert.IsTrue(ep2.Equals(ep1));
            Assert.IsFalse(ep1.Equals(ep3));
            Assert.IsFalse(ep3.Equals(ep1));
            Assert.IsFalse(ep1.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep1));
            Assert.IsFalse(ep2.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep2));
            Assert.IsFalse(ep3.Equals(ep4));
            Assert.IsFalse(ep4.Equals(ep3));

            ep3.Port = 3520;
            Assert.IsTrue(ep1.Equals(ep3));
            Assert.IsTrue(ep3.Equals(ep1));

            ep4.Address = ep1.Address;
            ep4.Port = ep1.Port;
            Assert.IsTrue(ep1.Equals(ep4));
            Assert.IsTrue(ep4.Equals(ep1));

            List<Common.EndPoint> epList = new List<Common.EndPoint>();
            epList.Add(new Common.EndPoint("129.123.7.49:3530"));
            epList.Add(new Common.EndPoint("129.123.7.49:3531"));
            epList.Add(new Common.EndPoint("129.123.7.49:3532"));
            epList.Add(new Common.EndPoint("129.123.7.49:3533"));
            epList.Add(new Common.EndPoint("129.123.7.49:3534"));
            epList.Add(new Common.EndPoint("129.123.7.49:3535"));
            Assert.AreEqual(6, epList.Count);

            Common.EndPoint t1 = new Common.EndPoint("129.123.7.49:3530");
            Common.EndPoint t2 = new Common.EndPoint("129.123.7.49:3532");
            Common.EndPoint t3 = new Common.EndPoint("129.123.7.49:3535");
            Common.EndPoint t4 = new Common.EndPoint("129.123.7.49:9999");
            Assert.AreEqual(0, epList.FindIndex(delegate(Common.EndPoint ep) { return ep.Equals(t1); }));
            Assert.AreEqual(2, epList.FindIndex(delegate(Common.EndPoint ep) { return ep.Equals(t2); }));
            Assert.AreEqual(5, epList.FindIndex(delegate(Common.EndPoint ep) { return ep.Equals(t3); }));
            Assert.AreEqual(-1, epList.FindIndex(delegate(Common.EndPoint ep) { return ep.Equals(t4); }));
        }
        public void GameInfo_TestEverything()
        {
            GameInfo g0 = new GameInfo();
            Assert.AreEqual(0, g0.Id);
            Assert.IsNull(g0.Label);
            Assert.IsNotNull(g0.AliveTimestamp);
            Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(3000) > DateTime.Now);
            Assert.IsNull(g0.CommunicationEndPoint);
            Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status);

            EndPoint ep = new EndPoint("113.24.4.1:1325");
            g0 = new GameInfo()
                    {
                            Id = 10,
                            Label = "Test",
                            CommunicationEndPoint = ep,
                            AliveTimestamp = DateTime.Now,
                            Status = GameInfo.GameStatus.RUNNING
                    };
            Assert.AreEqual(10, g0.Id);
            Assert.AreEqual("Test", g0.Label);
            Assert.IsNotNull(g0.AliveTimestamp);
            Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now);
            Assert.AreSame(ep, g0.CommunicationEndPoint);
            Assert.AreEqual(GameInfo.GameStatus.RUNNING, g0.Status);

            g0 = new GameInfo(200, "Testing", ep, GameInfo.GameStatus.COMPLETED);
            Assert.AreEqual(200, g0.Id);
            Assert.AreEqual("Testing", g0.Label);
            Assert.IsNotNull(g0.AliveTimestamp);
            Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now);
            Assert.AreSame(ep, g0.CommunicationEndPoint);
            Assert.AreEqual(GameInfo.GameStatus.COMPLETED, g0.Status);

            g0 = new GameInfo(300, "More Testing", ep, "1");
            Assert.AreEqual(300, g0.Id);
            Assert.AreEqual("More Testing", g0.Label);
            Assert.IsNotNull(g0.AliveTimestamp);
            Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now);
            Assert.AreSame(ep, g0.CommunicationEndPoint);
            Assert.AreEqual(GameInfo.GameStatus.AVAILABLE, g0.Status);
        }
Ejemplo n.º 10
0
        public void EndPoint_CheckEncodeAndDecode()
        {
            Common.EndPoint ep1 = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep1.Address);
            Assert.AreEqual(3004, ep1.Port);

            ByteList bytes = new ByteList();

            ep1.Encode(bytes);
            Common.EndPoint ep2 = Common.EndPoint.Create(bytes);
            Assert.AreEqual(ep1.Address, ep2.Address);
            Assert.AreEqual(ep1.Port, ep2.Port);

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

            bytes.Clear();
            ep1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                ep2 = Common.EndPoint.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void AgentInfo_CheckConstructors()
        {
            AgentInfo info = new AgentInfo();
            Assert.AreEqual(0, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.IsNull(info.CommunicationEndPoint);

            info = new AgentInfo(10, AgentInfo.PossibleAgentType.ExcuseGenerator);
            Assert.AreEqual(10, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.IsNull(info.CommunicationEndPoint);
            Assert.AreEqual(AgentInfo.PossibleAgentType.ExcuseGenerator, info.AgentType);

            EndPoint ep = new EndPoint("129.123.7.24:1345");
            info = new AgentInfo(20, AgentInfo.PossibleAgentType.WhiningSpinner, ep);
            Assert.AreEqual(20, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.AreSame(ep, info.CommunicationEndPoint);
            Assert.AreEqual(AgentInfo.PossibleAgentType.WhiningSpinner, info.AgentType);
        }
        public void Registrar_TestEndPointReflection()
        {
            RegistrarClient registrar = new RegistrarClient("LocalHttpBinding_IRegistrar");
            // RegistrarClient registrar = new RegistrarClient("ProdHttpBinding_IRegistrar");

            string reflectorEndPointString = registrar.EndPointReflector();
            Assert.IsNotNull(reflectorEndPointString);
            Common.EndPoint reflectorEndPoint = new Common.EndPoint(reflectorEndPointString);

            UdpClient testClient = new UdpClient(0, AddressFamily.InterNetwork);
            byte[] sendBuffer = ASCIIEncoding.ASCII.GetBytes("hello");
            testClient.Send(sendBuffer, sendBuffer.Length, reflectorEndPoint.GetIPEndPoint());

            testClient.Client.ReceiveTimeout = 1000;
            IPEndPoint sendingEP = new IPEndPoint(IPAddress.Any, 0);
            byte[] receiveBuffer = testClient.Receive(ref sendingEP);

            Assert.IsNotNull(receiveBuffer);
            string reflectedEP = ASCIIEncoding.ASCII.GetString(receiveBuffer);
            Assert.IsNotNull(reflectedEP);
            Assert.IsTrue(reflectedEP.Length > 8);
            string[] tmp = reflectedEP.Split(':');
            Assert.IsTrue(tmp.Length == 2);
        }
        public void EndPoint_CheckConstructors()
        {
            Common.EndPoint ep = new Common.EndPoint();
            Assert.AreEqual(0, ep.Address);
            Assert.AreEqual(0, ep.Port);

            byte[] addressBytes = new byte[4];
            addressBytes[0] = 10;
            addressBytes[1] = 211;
            addressBytes[2] = 55;
            addressBytes[3] = 20;

            ep = new Common.EndPoint(addressBytes, 2001);
            byte[] tmpBytes = BitConverter.GetBytes(ep.Address);
            Assert.AreEqual(10, tmpBytes[0]);
            Assert.AreEqual(211, tmpBytes[1]);
            Assert.AreEqual(55, tmpBytes[2]);
            Assert.AreEqual(20, tmpBytes[3]);
            Assert.AreEqual(2001, ep.Port);

            ep = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep.Address);
            Assert.AreEqual(3004, ep.Port);
        }
Ejemplo n.º 14
0
        public void EndPoint_CheckConstructors()
        {
            Common.EndPoint ep = new Common.EndPoint();
            Assert.AreEqual(0, ep.Address);
            Assert.AreEqual(0, ep.Port);

            byte[] addressBytes = new byte[4];
            addressBytes[0] = 10;
            addressBytes[1] = 211;
            addressBytes[2] = 55;
            addressBytes[3] = 20;

            ep = new Common.EndPoint(addressBytes, 2001);
            byte[] tmpBytes = BitConverter.GetBytes(ep.Address);
            Assert.AreEqual(10, tmpBytes[0]);
            Assert.AreEqual(211, tmpBytes[1]);
            Assert.AreEqual(55, tmpBytes[2]);
            Assert.AreEqual(20, tmpBytes[3]);
            Assert.AreEqual(2001, ep.Port);

            ep = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep.Address);
            Assert.AreEqual(3004, ep.Port);
        }
Ejemplo n.º 15
0
        public GameInfo RegisterGame(string label, Common.EndPoint publicEP)
        {
            log.Debug("In RegisterGame");
            GameInfo game = null;

            if (!string.IsNullOrWhiteSpace(label))
            {
                log.DebugFormat("Register {0} at {1}", label, publicEP.ToString());

                game = new GameInfo()
                {
                    Label = label, CommunicationEndPoint = publicEP, AliveTimestamp = DateTime.Now
                };
                game.Id = GetNextIdNumber();
                log.DebugFormat("New game's id={0}", game.Id);
                lock (myLock)
                {
                    games.Add(game.Id, game);
                }
                Save();
                LogContents();
            }
            return(game);
        }
        public void AgentInfo_CheckProperties()
        {
            EndPoint ep = new EndPoint("129.123.7.24:1345");
            AgentInfo info = 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,
                        Points = 3332.42 };

            Assert.AreEqual(20, info.Id);
            Assert.AreEqual(AgentInfo.PossibleAgentType.WhiningSpinner, info.AgentType);
            Assert.AreEqual("A00001", info.ANumber);
            Assert.AreEqual("Joe", info.FirstName);
            Assert.AreEqual("Jones", info.LastName);
            Assert.AreEqual(1200.5, info.Strength);
            Assert.AreEqual(1500.0, info.Speed);
            Assert.AreEqual(3332.42, info.Points);
            Assert.AreEqual(10, info.Location.X);
            Assert.AreEqual(20, info.Location.Y);
            Assert.AreSame(ep, info.CommunicationEndPoint);

            info.Changed += ChangedEventHandler;

            // Id Property
            recentStateChange = null;
            info.Id = 1002;
            Assert.AreEqual(1002, info.Id);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            recentStateChange = null;
            info.Id = 0;
            Assert.AreEqual(0, info.Id);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.Id = Int16.MaxValue;
            Assert.AreEqual(Int16.MaxValue, info.Id);
            info.Id = 10;
            Assert.AreEqual(10, info.Id);

            // AgentType
            recentStateChange = null;
            info.AgentType = AgentInfo.PossibleAgentType.BrilliantStudent;
            Assert.AreEqual(AgentInfo.PossibleAgentType.BrilliantStudent, info.AgentType);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Status
            info.AgentStatus = AgentInfo.PossibleAgentStatus.InGame;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.InGame, info.AgentStatus);
            info.AgentStatus = AgentInfo.PossibleAgentStatus.TryingToJoin;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.TryingToJoin, info.AgentStatus);
            info.AgentStatus = AgentInfo.PossibleAgentStatus.WonGame;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.WonGame, info.AgentStatus);

            // ANumber
            recentStateChange = null;
            info.ANumber = "A000234";
            Assert.AreEqual("A000234", info.ANumber);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.ANumber = null;
            Assert.IsNull(info.ANumber);
            info.ANumber = "A012345";
            Assert.AreEqual("A012345", info.ANumber);

            // FirstName
            recentStateChange = null;
            info.FirstName = "Henry";
            Assert.AreEqual("Henry", info.FirstName);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.FirstName = null;
            Assert.IsNull(info.FirstName);
            info.FirstName = "John";
            Assert.AreEqual("John", info.FirstName);

            // LastName
            recentStateChange = null;
            info.LastName = "Franks";
            Assert.AreEqual("Franks", info.LastName);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.LastName = null;
            Assert.IsNull(info.LastName);
            info.LastName = "Jones";
            Assert.AreEqual("Jones", info.LastName);

            // Strength
            recentStateChange = null;
            info.Strength = 123.45;
            Assert.AreEqual(123.45, info.Strength);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Speed
            recentStateChange = null;
            info.Speed = 23.456;
            Assert.AreEqual(23.456, info.Speed);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Speed
            recentStateChange = null;
            info.Points = 53.6;
            Assert.AreEqual(53.6, info.Points);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Location
            recentStateChange = null;
            FieldLocation f = new FieldLocation(10, 20);
            info.Location = f;
            Assert.AreSame(f, info.Location);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // CommunicationEndPoint
            recentStateChange = null;
            EndPoint ep1 = new EndPoint(3242, 1000);
            info.CommunicationEndPoint = ep1;
            Assert.AreSame(ep1, info.CommunicationEndPoint);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);
        }
Ejemplo n.º 17
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();
            }
        }
Ejemplo n.º 18
0
 public ComponentInfo(Int16 id, EndPoint endPoint)
     : this(id)
 {
     CommunicationEndPoint = endPoint;
 }
        public void EndPoint_CheckPropertiesAndMethods()
        {
            Common.EndPoint ep1 = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep1.Address);
            Assert.AreEqual(3004, ep1.Port);

            ep1.Address = 54365439;
            ep1.Port = 4354;
            Assert.AreEqual(54365439, ep1.Address);
            Assert.AreEqual(4354, ep1.Port);

            ep1.Address = 0;
            ep1.Port = 0;
            Assert.AreEqual(0, ep1.Address);
            Assert.AreEqual(0, ep1.Port);

            ep1.Address = Int32.MaxValue;
            ep1.Port = IPEndPoint.MaxPort;
            Assert.AreEqual(Int32.MaxValue, ep1.Address);
            Assert.AreEqual(IPEndPoint.MaxPort, ep1.Port);
            ep1.Port = IPEndPoint.MinPort;
            Assert.AreEqual(IPEndPoint.MinPort, ep1.Port);

            try
            {
                ep1.Port = IPEndPoint.MaxPort + 1;
                Assert.Fail("Excepted exception not thrown for too big of a port number");
            }
            catch (ApplicationException) { }

            try
            {
                ep1.Port = IPEndPoint.MinPort - 1;
                Assert.Fail("Excepted exception not thrown for too small of a port number");
            }
            catch (ApplicationException) { }

            ep1.Address = 54365439;
            ep1.Port = 4354;
            Common.EndPoint ep2 = new Common.EndPoint(34445, 3255);
            Assert.IsFalse(Common.EndPoint.Match(ep1, ep2));
            Assert.IsFalse(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));
            ep2.Port = 4354;
            Assert.IsFalse(Common.EndPoint.Match(ep1, ep2));
            Assert.IsFalse(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));
            ep2.Address = 54365439;
            Assert.IsTrue(Common.EndPoint.Match(ep1, ep2));
            Assert.IsTrue(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));

            byte[] addressBytes = new byte[4];
            addressBytes[0] = 10;
            addressBytes[1] = 211;
            addressBytes[2] = 55;
            addressBytes[3] = 20;

            Common.EndPoint ep3 = new Common.EndPoint(addressBytes, 2001);
            byte[] tmpBytes = BitConverter.GetBytes(ep3.Address);
            Assert.AreEqual(10, tmpBytes[0]);
            Assert.AreEqual(211, tmpBytes[1]);
            Assert.AreEqual(55, tmpBytes[2]);
            Assert.AreEqual(20, tmpBytes[3]);
            Assert.AreEqual(2001, ep3.Port);

            IPEndPoint ipEp = ep3.GetIPEndPoint();
            Assert.AreEqual(10, ipEp.Address.GetAddressBytes()[0]);
            Assert.AreEqual(211, ipEp.Address.GetAddressBytes()[1]);
            Assert.AreEqual(55, ipEp.Address.GetAddressBytes()[2]);
            Assert.AreEqual(20, ipEp.Address.GetAddressBytes()[3]);
            Assert.AreEqual(2001, ipEp.Port);
        }
Ejemplo n.º 20
0
 public GameInfo(Int16 id, string label, EndPoint ep, GameStatus status = GameStatus.NOT_INITIAlIZED)
     : this(id, label, ep)
 {
     Status = status;
 }
Ejemplo n.º 21
0
 public GameInfo RegisterGame(string label, Common.EndPoint publicEP)
 {
     return(Registry.Instance.RegisterGame(label, publicEP));
 }
Ejemplo n.º 22
0
        public void EndPoint_CheckPropertiesAndMethods()
        {
            Common.EndPoint ep1 = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep1.Address);
            Assert.AreEqual(3004, ep1.Port);

            ep1.Address = 54365439;
            ep1.Port    = 4354;
            Assert.AreEqual(54365439, ep1.Address);
            Assert.AreEqual(4354, ep1.Port);

            ep1.Address = 0;
            ep1.Port    = 0;
            Assert.AreEqual(0, ep1.Address);
            Assert.AreEqual(0, ep1.Port);

            ep1.Address = Int32.MaxValue;
            ep1.Port    = IPEndPoint.MaxPort;
            Assert.AreEqual(Int32.MaxValue, ep1.Address);
            Assert.AreEqual(IPEndPoint.MaxPort, ep1.Port);
            ep1.Port = IPEndPoint.MinPort;
            Assert.AreEqual(IPEndPoint.MinPort, ep1.Port);

            try
            {
                ep1.Port = IPEndPoint.MaxPort + 1;
                Assert.Fail("Excepted exception not thrown for too big of a port number");
            }
            catch (ApplicationException) { }

            try
            {
                ep1.Port = IPEndPoint.MinPort - 1;
                Assert.Fail("Excepted exception not thrown for too small of a port number");
            }
            catch (ApplicationException) { }

            ep1.Address = 54365439;
            ep1.Port    = 4354;
            Common.EndPoint ep2 = new Common.EndPoint(34445, 3255);
            Assert.IsFalse(Common.EndPoint.Match(ep1, ep2));
            Assert.IsFalse(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));
            ep2.Port = 4354;
            Assert.IsFalse(Common.EndPoint.Match(ep1, ep2));
            Assert.IsFalse(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));
            ep2.Address = 54365439;
            Assert.IsTrue(Common.EndPoint.Match(ep1, ep2));
            Assert.IsTrue(Common.EndPoint.Match(ep1.GetIPEndPoint(), ep2.GetIPEndPoint()));

            byte[] addressBytes = new byte[4];
            addressBytes[0] = 10;
            addressBytes[1] = 211;
            addressBytes[2] = 55;
            addressBytes[3] = 20;

            Common.EndPoint ep3      = new Common.EndPoint(addressBytes, 2001);
            byte[]          tmpBytes = BitConverter.GetBytes(ep3.Address);
            Assert.AreEqual(10, tmpBytes[0]);
            Assert.AreEqual(211, tmpBytes[1]);
            Assert.AreEqual(55, tmpBytes[2]);
            Assert.AreEqual(20, tmpBytes[3]);
            Assert.AreEqual(2001, ep3.Port);

            IPEndPoint ipEp = ep3.GetIPEndPoint();

            Assert.AreEqual(10, ipEp.Address.GetAddressBytes()[0]);
            Assert.AreEqual(211, ipEp.Address.GetAddressBytes()[1]);
            Assert.AreEqual(55, ipEp.Address.GetAddressBytes()[2]);
            Assert.AreEqual(20, ipEp.Address.GetAddressBytes()[3]);
            Assert.AreEqual(2001, ipEp.Port);
        }
        public void EndPoint_CheckEncodeAndDecode()
        {
            Common.EndPoint ep1 = new Common.EndPoint(3255420, 3004);
            Assert.AreEqual(3255420, ep1.Address);
            Assert.AreEqual(3004, ep1.Port);

            ByteList bytes = new ByteList();
            ep1.Encode(bytes);
            Common.EndPoint ep2 = Common.EndPoint.Create(bytes);
            Assert.AreEqual(ep1.Address, ep2.Address);
            Assert.AreEqual(ep1.Port, ep2.Port);

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

            bytes.Clear();
            ep1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                ep2 = Common.EndPoint.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }