public void ProcessInfo_TestEverything() { ProcessInfo p1 = new ProcessInfo(); Assert.AreEqual(0, p1.ProcessId); Assert.AreEqual(ProcessInfo.ProcessType.Unknown, p1.Type); Assert.IsNull(p1.EndPoint); Assert.IsNull(p1.Label); Assert.AreEqual(ProcessInfo.StatusCode.Unknown, p1.Status); Assert.IsNull(p1.AliveTimestamp); PublicEndPoint ep1 = new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 32541 }; DateTime t1 = DateTime.Now; ProcessInfo p2 = new ProcessInfo() { ProcessId = 10, Type = ProcessInfo.ProcessType.Player, EndPoint = ep1, Label = "Test Process", Status = ProcessInfo.StatusCode.Initializing, AliveTimestamp = t1 }; Assert.AreEqual(10, p2.ProcessId); Assert.AreEqual(ProcessInfo.ProcessType.Player, p2.Type); Assert.AreEqual(ep1, p2.EndPoint); Assert.AreEqual("Test Process", p2.Label); Assert.AreEqual(ProcessInfo.StatusCode.Initializing, p2.Status); Assert.AreEqual("Initializing", p2.StatusString); Assert.AreEqual(t1, p2.AliveTimestamp); }
public static int Compare(PublicEndPoint a, PublicEndPoint b) { int result = 0; if (!ReferenceEquals(a, b)) { if (((object)a == null) && ((object)b != null)) { result = -1; } else if (((object)a != null) && ((object)b == null)) { result = 1; } else { result = String.Compare(a?.Host, b?.Host, StringComparison.Ordinal); if (result == 0) { if (a?.Port < b?.Port) { result = -1; } else if (a?.Port > b?.Port) { result = 1; } } } } return(result); }
public void Send(Message message, PublicEndPoint ep) { try { byte[] bytes = message.Encode(); udpClient.Send(bytes, bytes.Length, ep.IPEndPoint); } catch (Exception e) { logger.Debug("Exception: " + e.Message); } }
public override bool Equals(object obj) { bool result = false; PublicEndPoint other = obj as PublicEndPoint; if (other != null) { result = (HostAndPort == other.HostAndPort); } return(result); }
public void PublicEndPoint_Constructor() { PublicEndPoint ep1 = new PublicEndPoint(); Assert.AreEqual("0.0.0.0", ep1.Host); Assert.AreEqual(0, ep1.Port); Assert.IsNotNull(ep1.IPEndPoint); Assert.AreEqual("0.0.0.0:0", ep1.IPEndPoint.ToString()); Assert.AreEqual(0, ep1.IPEndPoint.Port); PublicEndPoint ep2 = new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 12001 }; Assert.AreEqual("swcwin.serv.usu.edu", ep2.Host); Assert.AreEqual(12001, ep2.Port); Assert.AreEqual("129.123.41.13:12001", ep2.IPEndPoint.ToString()); }
public void Initialize() { MyProcessInfo = new ProcessInfo() { Status = ProcessInfo.StatusCode.Initializing, Type = ProcessInfo.ProcessType.BalloonStore, Label = Options.Label }; RegistryEndPoint = new PublicEndPoint(Options.Registry); GameManagerEndPoint = new PublicEndPoint(Options.GameManagerEndPoint); Identity = new IdentityInfo() { Alias = Options.Alias, ANumber = Options.ANumber, FirstName = Options.FirstName, LastName = Options.LastName }; SetupCommSubsystem(new BalloonStoreConversationFactory() { DefaultMaxRetries = Options.Retries, DefaultTimeout = Options.Timeout, Process = this }, minPort: Options.MinPort, maxPort: Options.MaxPort); Game = new GameInfo(); PennyBankPublicKey = new PublicKey(); WaterSources = new List<GameProcessData>(); BalloonStores = new List<GameProcessData>(); UmbrellaSuppliers = new List<GameProcessData>(); Players = new List<GameProcessData>(); Balloons = new ResourceSet<Balloon>(); CachedPennies = new List<Penny>(); rsa = new RSACryptoServiceProvider(); rsaSigner = new RSAPKCS1SignatureFormatter(rsa); rsaSigner.SetHashAlgorithm("SHA1"); Hasher = new SHA1Managed(); RSAParameters parameters = rsa.ExportParameters(false); PublicKey = new PublicKey() { Exponent = parameters.Exponent, Modulus = parameters.Modulus }; NextId = 0; NumIds = 0; }
public void Initialize() { MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; RegistryEndPoint = new PublicEndPoint(Options.Registry); Identity = new IdentityInfo() { Alias = Options.Alias, ANumber = Options.ANumber, FirstName = Options.FirstName, LastName = Options.LastName }; SetupCommSubsystem(new PlayerConversationFactory() { DefaultMaxRetries = Options.Retries, DefaultTimeout = Options.Timeout, Process = this }, minPort:Options.MinPort, maxPort:Options.MaxPort); }
public void PublicEndPoint_PublicProperties() { PublicEndPoint ep1 = new PublicEndPoint() { Host = "www.usu.edu", Port = 12010 }; Assert.AreEqual("www.usu.edu", ep1.Host); Assert.AreEqual(12010, ep1.Port); Assert.AreEqual("129.123.54.210:12010", ep1.IPEndPoint.ToString()); ep1.HostAndPort = "swcwin.serv.usu.edu:12001"; Assert.AreEqual("swcwin.serv.usu.edu", ep1.Host); Assert.AreEqual(12001, ep1.Port); Assert.AreEqual("129.123.41.13:12001", ep1.IPEndPoint.ToString()); ep1.Host = "www.usu.edu"; Assert.AreEqual("www.usu.edu", ep1.Host); Assert.AreEqual(12001, ep1.Port); Assert.AreEqual("129.123.54.210:12001", ep1.IPEndPoint.ToString()); ep1.Port = 12010; Assert.AreEqual("www.usu.edu", ep1.Host); Assert.AreEqual(12010, ep1.Port); Assert.AreEqual("129.123.54.210:12010", ep1.IPEndPoint.ToString()); ep1.Host = null; Assert.AreEqual("0.0.0.0", ep1.Host); Assert.AreEqual(12010, ep1.Port); Assert.AreEqual("0.0.0.0:12010", ep1.IPEndPoint.ToString()); ep1.HostAndPort = "swcwin.serv.usu.edu:12001"; Assert.AreEqual("swcwin.serv.usu.edu", ep1.Host); Assert.AreEqual(12001, ep1.Port); Assert.AreEqual("129.123.41.13:12001", ep1.IPEndPoint.ToString()); ep1.Host = ""; Assert.AreEqual("0.0.0.0", ep1.Host); Assert.AreEqual(12001, ep1.Port); Assert.AreEqual("0.0.0.0:12001", ep1.IPEndPoint.ToString()); ep1.Port = 0; Assert.AreEqual("0.0.0.0", ep1.Host); Assert.AreEqual(0, ep1.Port); Assert.AreEqual("0.0.0.0:0", ep1.IPEndPoint.ToString()); }
public void PublicEndPoint_Clone() { PublicEndPoint ep1 = new PublicEndPoint(); PublicEndPoint ep1_copy = ep1.Clone(); Assert.AreNotSame(ep1, ep1_copy); Assert.AreEqual("0.0.0.0", ep1_copy.Host); Assert.AreEqual(0, ep1_copy.Port); Assert.IsNotNull(ep1_copy.IPEndPoint); Assert.AreEqual("0.0.0.0:0", ep1_copy.IPEndPoint.ToString()); Assert.AreEqual(0, ep1_copy.IPEndPoint.Port); PublicEndPoint ep2 = new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 12001 }; PublicEndPoint ep2_copy = ep2.Clone() as PublicEndPoint; Assert.AreNotSame(ep2, ep2_copy); Assert.AreEqual("swcwin.serv.usu.edu", ep2_copy.Host); Assert.AreEqual(12001, ep2_copy.Port); Assert.AreEqual("129.123.41.13:12001", ep2_copy.IPEndPoint.ToString()); }
public void PublicEndPoint_OtherPublicMethods() { PublicEndPoint ep1 = new PublicEndPoint(); PublicEndPoint ep2 = new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 12001 }; PublicEndPoint ep3 = new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 12001 }; PublicEndPoint ep4 = new PublicEndPoint() { HostAndPort = "www.usu.edu:2300" }; Assert.IsTrue(ep3.Equals(ep2)); Assert.IsTrue(ep2.Equals(ep3)); Assert.IsFalse(ep3.Equals(null)); Assert.IsFalse(ep3.Equals(ep1)); Assert.IsFalse(ep1.Equals(ep3)); Assert.IsFalse(ep3.Equals(ep4)); Assert.IsFalse(ep4.Equals(ep3)); Assert.IsFalse(ep3.Equals(new PublicEndPoint() { Host = "127.0.0.1", Port = 12001 })); Assert.IsFalse(ep3.Equals(new PublicEndPoint() { Host = "swcwin.serv.usu.edu", Port = 23324 })); Assert.IsFalse(ep3.Equals(new PublicEndPoint() { Host = "", Port = 12001 })); Assert.IsFalse(ep3.Equals(new PublicEndPoint() { Host = null, Port = 12001 })); }
public void BalloonStore_TestStartGameConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; balloonStore.RegistryEndPoint = mockEp; balloonStore.GetConversation().Launch(); // launch a bogus conversation in order to get the balloon store endpoint Envelope env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEP = env.IPEndPoint; // test successful conversation balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoinedGame; env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new StartGame() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Success = true } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.PlayingGame)); // test failed conversation balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoinedGame; env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new StartGame() { ConvId = new MessageNumber() { Pid = 1, Seq = 2 }, MsgId = new MessageNumber() { Pid = 1, Seq = 2 }, Success = false } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.LeavingGame)); mockRegistry.Stop(); }
public void BalloonStore_TestLoginConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.RegistryEndPoint = mockEp; balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; RequestReply conv = balloonStore.GetConversation(); conv.Launch(); Envelope env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEP = env.IPEndPoint; LoginRequest req = env.ActualMessage as LoginRequest; Assert.That(req.ProcessType, Is.EqualTo(ProcessInfo.ProcessType.BalloonStore)); //send a bad response type Envelope res = new Envelope() { Message = new LoginRequest(), IPEndPoint = balloonStoreEP }; mockRegistry.Send(res); Thread.Sleep(1100); Assert.That(balloonStore.ProxyEndPoint, Is.Null); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.Initializing)); conv = balloonStore.GetConversation(); conv.Launch(); env = mockRegistry.Receive(1000); //send a false success res = new Envelope() { IPEndPoint = balloonStoreEP, Message = new LoginReply() { ProxyEndPoint = mockEp, Success = false, ProcessInfo = new ProcessInfo() { ProcessId = 2, Status = ProcessInfo.StatusCode.Registered }, ConvId = new MessageNumber() { Pid = 0, Seq = 2 }, MsgId = new MessageNumber() { Pid = 1, Seq = 2 } } }; mockRegistry.Send(res); Thread.Sleep(1100); Assert.That(balloonStore.ProxyEndPoint, Is.Null); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.Initializing)); conv = balloonStore.GetConversation(); conv.Launch(); env = mockRegistry.Receive(100); //send a good response res = new Envelope() { IPEndPoint = balloonStoreEP, Message = new LoginReply() { ProxyEndPoint = mockEp, Success = true, ProcessInfo = new ProcessInfo() { ProcessId = 2, Status = ProcessInfo.StatusCode.Registered }, ConvId = new MessageNumber() { Pid = 0, Seq = 3 }, MsgId = new MessageNumber() { Pid = 1, Seq = 3 } } }; mockRegistry.Send(res); Thread.Sleep(100); // now execute the next id conversation env = mockRegistry.Receive(2000); Assert.That(env, Is.Not.Null); NextIdRequest idReq = env.ActualMessage as NextIdRequest; Assert.That(idReq.NumberOfIds, Is.EqualTo(BALLOONS)); env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new NextIdReply() { Success = true, NextId = 5, ConvId = idReq.ConvId, MsgId = new MessageNumber() { Pid = 1, Seq = 4 } } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.ProxyEndPoint, Is.EqualTo(mockEp)); Assert.That(balloonStore.MyProcessInfo.ProcessId, Is.EqualTo(2)); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.Registered)); Assert.That(balloonStore.Balloons.AvailableCount, Is.EqualTo(BALLOONS)); Assert.That(balloonStore.Balloons.Get(5), Is.Not.Null); mockRegistry.Stop(); }
public void BalloonStore_TestAliveConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; balloonStore.RegistryEndPoint = mockEp; balloonStore.GetConversation().Launch(); // launch a bogus conversation in order to get the balloon store endpoint Envelope env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEP = env.IPEndPoint; balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoinedGame; env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new AliveRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 } } }; mockRegistry.Send(env); env = mockRegistry.Receive(2000); Assert.That(env, Is.Not.Null); Reply reply = env.ActualMessage as Reply; Assert.That(reply.Success, Is.True); }
public void BalloonStore_TestBuyBalloonConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; balloonStore.RegistryEndPoint = mockEp; balloonStore.GetConversation().Launch(); // launch a bogus conversation in order to get the balloon store endpoint Envelope env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEp = env.IPEndPoint; Penny penny = new Penny() { Id = 1 }; // test not in game balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.PlayingGame; env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); BalloonReply reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Null); Assert.That(reply.Success, Is.False); Assert.That(reply.Note, Is.StringContaining("You are not in the Game")); // test no balloons balloonStore.Players.Add(new GameProcessData() { ProcessId = 1 }); env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Null); Assert.That(reply.Success, Is.False); Assert.That(reply.Note, Is.StringContaining("No balloons left in inventory")); // test cached penny balloonStore.CachedPennies.Add(penny); balloonStore.CreateBalloons(); env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Null); Assert.That(reply.Success, Is.False); Assert.That(reply.Note, Is.StringContaining("Invalid Penny, I've seen this one before")); balloonStore.CachedPennies.Remove(penny); // test invalid signature RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSAParameters parameters = rsa.ExportParameters(false); balloonStore.PennyBankPublicKey = new PublicKey() { Exponent = parameters.Exponent, Modulus = parameters.Modulus }; penny.DigitalSignature = new byte[] { 1, 2, 3 }; env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Null); Assert.That(reply.Success, Is.False); Assert.That(reply.Note, Is.StringContaining("Invalid Penny, the signature doesn't check out")); // test valid signature, but failed ValidatePennyConversation balloonStore.PennyBankEndPoint = balloonStore.RegistryEndPoint; RSAPKCS1SignatureFormatter rsaSigner = new RSAPKCS1SignatureFormatter(rsa); rsaSigner.SetHashAlgorithm("SHA1"); SHA1Managed hasher = new SHA1Managed(); byte[] bytes = penny.DataBytes(); byte[] hash = hasher.ComputeHash(bytes); penny.DigitalSignature = rsaSigner.CreateSignature(hash); env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); // balloon store would have started a PennyValidation conversation PennyValidation request = mockRegistry.Receive(2000).ActualMessage as PennyValidation; Assert.That(request.MarkAsUsedIfValid, Is.True); Assert.That(request.Pennies[0].Id, Is.EqualTo(penny.Id)); env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new Reply() { ConvId = request.ConvId, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Success = false } }; reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Null); Assert.That(reply.Success, Is.False); Assert.That(reply.Note, Is.StringContaining("Invalid Penny, PennyBank said no")); // test successful conversation env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new BuyBalloonRequest() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Penny = penny } }; mockRegistry.Send(env); // balloon store would have started a PennyValidation conversation request = mockRegistry.Receive(500).ActualMessage as PennyValidation; Assert.That(request.MarkAsUsedIfValid, Is.True); Assert.That(request.Pennies[0].Id, Is.EqualTo(penny.Id)); env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new Reply() { ConvId = request.ConvId, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Success = true } }; mockRegistry.Send(env); reply = mockRegistry.Receive(2000).ActualMessage as BalloonReply; Assert.That(reply.Balloon, Is.Not.Null); Assert.That(reply.Success, Is.True); Assert.That(reply.Note, Is.StringContaining("Nice doing business")); Assert.That(balloonStore.CachedPennies.Any(p => p.Id == penny.Id), Is.True); mockRegistry.Stop(); }
public void TestLogin() { // Create udp client to mock registry UdpClient mockClient = new UdpClient(0); int mockClientPort = ((IPEndPoint)mockClient.Client.LocalEndPoint).Port; PublicEndPoint mockClientEP = new PublicEndPoint() { Host = "127.0.0.1", Port = mockClientPort }; // Create fake player TestablePlayer player = new TestablePlayer() { RegistryEndPoint = mockClientEP, IdentityInfo = new IdentityInfo() { FirstName = "Test", LastName = "Player", ANumber = "A01234983", Alias = "TP" }, ProcessLabel = "Test TP" }; // Run player Thread loginThread = new Thread(new ThreadStart(player.Start)); loginThread.Start(); // Get message from registry client IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); byte[] bytes = mockClient.Receive(ref senderEP); Assert.IsNotNull(bytes); Assert.AreNotEqual(0, bytes.Length); // Assert on LoginRequest Message msg = Message.Decode(bytes); Assert.IsNotNull(msg); Assert.IsTrue(msg is LoginRequest); LoginRequest request = msg as LoginRequest; Assert.AreEqual(player.IdentityInfo.Alias, request.Identity.Alias); Assert.AreEqual(player.IdentityInfo.ANumber, request.Identity.ANumber); Assert.AreEqual(player.IdentityInfo.FirstName, request.Identity.FirstName); Assert.AreEqual(player.IdentityInfo.LastName, request.Identity.LastName); Assert.AreEqual(player.ProcessLabel, request.ProcessLabel); Assert.AreEqual(ProcessInfo.ProcessType.Player,request.ProcessType); // Send LoginReply from registry to player LoginReply reply = new LoginReply() { ProcessInfo = new ProcessInfo() { ProcessId = 5, Type = ProcessInfo.ProcessType.Player, EndPoint = new PublicEndPoint() { HostAndPort = senderEP.ToString() }, Label = "Test Player", Status = ProcessInfo.StatusCode.Initializing }, Success = true }; bytes = reply.Encode(); mockClient.Send(bytes, bytes.Length, senderEP); // Assert on LoginReply, player state Thread.Sleep(2000); player.Stop(); loginThread.Join(); Assert.AreEqual(player.ProcessInfo.ProcessId, reply.ProcessInfo.ProcessId); Assert.AreEqual(player.ProcessInfo.Type, reply.ProcessInfo.Type); Assert.AreEqual(player.ProcessInfo.EndPoint.HostAndPort, reply.ProcessInfo.EndPoint.HostAndPort); Assert.AreEqual(player.ProcessInfo.Label, reply.ProcessInfo.Label); Assert.AreEqual(player.ProcessInfo.Status, reply.ProcessInfo.Status); Assert.IsTrue(player.CurrentState is GetGamesState); }
public void BalloonStore_TestGameStatusNotification() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Initializing; balloonStore.RegistryEndPoint = mockEp; balloonStore.GetConversation().Launch(); // launch a bogus conversation in order to get the balloon store endpoint Envelope env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEp = env.IPEndPoint; balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoinedGame; GameInfo gameEnding = new GameInfo() { Status = GameInfo.StatusCode.Ending }; GameInfo gameComplete = new GameInfo() { Status = GameInfo.StatusCode.Complete }; GameInfo gameInProgress = new GameInfo() { Status = GameInfo.StatusCode.InProgress }; // test game ending balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.PlayingGame; env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new GameStatusNotification() { ConvId = new MessageNumber() { Pid = 1, Seq = 1 }, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Game = gameEnding } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.LeavingGame)); // test game complete balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.PlayingGame; env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new GameStatusNotification() { ConvId = new MessageNumber() { Pid = 1, Seq = 2 }, MsgId = new MessageNumber() { Pid = 1, Seq = 2 }, Game = gameComplete } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.LeavingGame)); // test game in progress balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoinedGame; env = new Envelope() { IPEndPoint = balloonStoreEp, Message = new GameStatusNotification() { ConvId = new MessageNumber() { Pid = 1, Seq = 3 }, MsgId = new MessageNumber() { Pid = 1, Seq = 3 }, Game = gameInProgress } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.PlayingGame)); mockRegistry.Stop(); }
public void GameInfo_TestEverything() { GameInfo g1 = new GameInfo(); Assert.AreEqual(0, g1.GameId); Assert.IsNull(g1.Label); Assert.AreEqual(GameInfo.StatusCode.NotInitialized, g1.Status); Assert.AreEqual(2, g1.MinPlayers); Assert.AreEqual(20, g1.MaxPlayers); Assert.IsNull(g1.GameManager); Assert.AreEqual(0, g1.StartingNumberOfPlayers); Assert.IsNull(g1.CurrentProcesses); Assert.AreEqual(0, g1.Winner); PublicEndPoint ep1 = new PublicEndPoint() { Host = "buzz.serv.usu.edu", Port = 20011 }; PublicEndPoint ep2 = new PublicEndPoint() { Host = "buzz.serv.usu.edu", Port = 20012 }; PublicEndPoint ep3 = new PublicEndPoint() { Host = "buzz.serv.usu.edu", Port = 20013 }; ProcessInfo gm1 = new ProcessInfo() { ProcessId = 2, Type= ProcessInfo.ProcessType.GameManager, Label = "Game Manager 1", EndPoint = ep1, Status = ProcessInfo.StatusCode.Registered, AliveTimestamp = DateTime.Now }; ProcessInfo player1 = new ProcessInfo() { ProcessId = 11, Type = ProcessInfo.ProcessType.Player, Label = "Player 1", EndPoint = ep2, Status = ProcessInfo.StatusCode.Registered, AliveTimestamp = DateTime.Now }; ProcessInfo player2 = new ProcessInfo() { ProcessId = 12, Type = ProcessInfo.ProcessType.Player, Label = "Player 3", EndPoint = ep3, Status = ProcessInfo.StatusCode.Registered, AliveTimestamp = DateTime.Now }; GameInfo g2 = new GameInfo() { GameId = 10, Label="Test Game", MinPlayers = 2, MaxPlayers = 5, Status = GameInfo.StatusCode.NotInitialized, GameManager = gm1, StartingNumberOfPlayers = 3, CurrentProcesses = new ProcessInfo[] { player1, player2 }, Winner = player1.ProcessId }; Assert.AreEqual(10, g2.GameId); Assert.AreEqual("Test Game", g2.Label); Assert.AreEqual(2, g2.MinPlayers); Assert.AreEqual(5, g2.MaxPlayers); Assert.AreEqual(GameInfo.StatusCode.NotInitialized, g2.Status); Assert.AreSame(gm1, g2.GameManager); Assert.AreEqual(3, g2.StartingNumberOfPlayers); Assert.AreEqual(2, g2.CurrentProcesses.Length); Assert.AreSame(player1, g2.CurrentProcesses[0]); Assert.AreSame(player2, g2.CurrentProcesses[1]); Assert.AreEqual(player1.ProcessId, g2.Winner); GameInfo g3 = g2.Clone(); Assert.IsNotNull(g3); Assert.AreNotSame(g2, g3); Assert.AreEqual(10, g3.GameId); Assert.AreEqual("Test Game", g3.Label); Assert.AreEqual(2, g3.MinPlayers); Assert.AreEqual(5, g3.MaxPlayers); Assert.AreEqual(GameInfo.StatusCode.NotInitialized, g3.Status); Assert.AreEqual(2, g3.GameManager.ProcessId); Assert.AreEqual(ep1, g3.GameManager.EndPoint); Assert.AreEqual(2, g2.CurrentProcesses.Length); Assert.AreEqual(player1.ProcessId, g2.CurrentProcesses[0].ProcessId); Assert.AreEqual(player2.ProcessId, g2.CurrentProcesses[1].ProcessId); Assert.AreEqual(player1.ProcessId, g2.Winner); }
public void BalloonStore_TestLogoutConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.RegistryEndPoint = mockEp; //test wrong status balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.NotInitialized; LogoutConversation conv = balloonStore.CommSubsystem.ConversationFactory.CreateFromConversationType<LogoutConversation>(); conv.TargetEndPoint = mockEp; conv.Launch(); Envelope env = mockRegistry.Receive(1000); Assert.That(env, Is.Null); //test successful conversation balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.PlayingGame; conv = balloonStore.CommSubsystem.ConversationFactory.CreateFromConversationType<LogoutConversation>(); conv.TargetEndPoint = mockEp; conv.Launch(); env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEP = env.IPEndPoint; LogoutRequest req = env.ActualMessage as LogoutRequest; env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new Reply() { ConvId = req.ConvId, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Success = true } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.Terminating)); mockRegistry.Stop(); }
public void BalloonStore_TestJoinGameConversation() { Communicator mockRegistry = new Communicator() { MinPort = 13000, MaxPort = 13050 }; mockRegistry.Start(); PublicEndPoint mockEp = new PublicEndPoint(String.Format("127.0.0.1:{0}", mockRegistry.Port)); balloonStore.ProxyEndPoint = mockEp; //test wrong status balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.Registered; JoinGameConversation conv = balloonStore.CommSubsystem.ConversationFactory.CreateFromConversationType<JoinGameConversation>(); conv.ToProcessId = 2; conv.Launch(); Envelope env = mockRegistry.Receive(1000); Assert.That(env, Is.Null); //test successful conversation balloonStore.MyProcessInfo.Status = ProcessInfo.StatusCode.JoiningGame; conv = balloonStore.CommSubsystem.ConversationFactory.CreateFromConversationType<JoinGameConversation>(); conv.ToProcessId = GM_ID; conv.Launch(); env = mockRegistry.Receive(1000); IPEndPoint balloonStoreEP = env.IPEndPoint; JoinGameRequest req = env.ActualMessage as JoinGameRequest; Assert.That(req.GameId, Is.EqualTo(3)); Assert.That(req.Process.LabelAndId, Is.EqualTo(balloonStore.MyProcessInfo.LabelAndId)); env = new Envelope() { IPEndPoint = balloonStoreEP, Message = new JoinGameReply() { ConvId = req.ConvId, MsgId = new MessageNumber() { Pid = 1, Seq = 1 }, Success = true, GameId = 3, InitialLifePoints = 100 } }; mockRegistry.Send(env); Thread.Sleep(2000); Assert.That(balloonStore.Game.GameManagerId, Is.EqualTo(GM_ID)); Assert.That(balloonStore.Game.GameId, Is.EqualTo(GAME_ID)); Assert.That(balloonStore.Game.Status, Is.EqualTo(GameInfo.StatusCode.Available)); Assert.That(balloonStore.MyProcessInfo.Status, Is.EqualTo(ProcessInfo.StatusCode.JoinedGame)); mockRegistry.Stop(); }
public void TestJoinGame() { // Create udp client to mock registry UdpClient mockClient = new UdpClient(0); int mockClientPort = ((IPEndPoint)mockClient.Client.LocalEndPoint).Port; PublicEndPoint mockClientEP = new PublicEndPoint() { Host = "127.0.0.1", Port = mockClientPort }; // Create fake player TestablePlayer player = new TestablePlayer() { RegistryEndPoint = mockClientEP, ProcessInfo = new ProcessInfo() { ProcessId = 5, Type = ProcessInfo.ProcessType.Player, EndPoint = mockClientEP, Label = "Test Player", Status = ProcessInfo.StatusCode.Initializing } }; player.GamesList = new GameInfo[] { new GameInfo() { GameId = 7, Label = "Test Game", Status = GameInfo.StatusCode.Available, GameManager = new ProcessInfo() { EndPoint = mockClientEP } } }; player.CurrentState = new JoinGameState(player); // Run player Thread playerThread = new Thread(new ThreadStart(player.Start)); playerThread.Start(); // Get message registry client IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); byte[] bytes = mockClient.Receive(ref senderEP); // Assert on GameListRequest Message msg = Message.Decode(bytes); Assert.IsNotNull(msg); Assert.IsTrue(msg is JoinGameRequest); JoinGameRequest request = msg as JoinGameRequest; Assert.AreEqual(request.GameId, player.GamesList[0].GameId); Assert.AreEqual(request.Player.ProcessId, player.ProcessInfo.ProcessId); Assert.AreEqual(request.Player.Type, player.ProcessInfo.Type); Assert.AreEqual(request.Player.Label, player.ProcessInfo.Label); // Send GameListReply from registry to player JoinGameReply reply = new JoinGameReply() { GameId = request.GameId, InitialLifePoints = 20, Success = true }; bytes = reply.Encode(); mockClient.Send(bytes, bytes.Length, senderEP); // Assert on GameListReply, player state Thread.Sleep(2000); player.Stop(); playerThread.Join(); Assert.AreEqual(player.LifePoints, reply.InitialLifePoints); Assert.IsTrue(player.CurrentState is NullState); Assert.AreEqual(player.ProcessInfo.Status, ProcessInfo.StatusCode.JoinedGame); }
public void TestGetGames() { // Create udp client to mock registry UdpClient mockClient = new UdpClient(0); int mockClientPort = ((IPEndPoint)mockClient.Client.LocalEndPoint).Port; PublicEndPoint mockClientEP = new PublicEndPoint() { Host = "127.0.0.1", Port = mockClientPort }; // Create fake player TestablePlayer player = new TestablePlayer() { RegistryEndPoint = mockClientEP }; player.CurrentState = new GetGamesState(player); // Run player Thread playerThread = new Thread(new ThreadStart(player.Start)); playerThread.Start(); // Get message registry client IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); byte[] bytes = mockClient.Receive(ref senderEP); // Assert on GameListRequest Message msg = Message.Decode(bytes); Assert.IsNotNull(msg); Assert.IsTrue(msg is GameListRequest); GameListRequest request = msg as GameListRequest; Assert.AreEqual(request.StatusFilter, (int)GameInfo.StatusCode.Available); // Send GameListReply from registry to player GameListReply reply = new GameListReply() { GameInfo = new GameInfo[] { new GameInfo() { GameId = 7, Label = "Test Game", Status = GameInfo.StatusCode.Available, GameManager = new ProcessInfo() { EndPoint = new PublicEndPoint() { HostAndPort = senderEP.ToString() } } } }, Success = true }; bytes = reply.Encode(); mockClient.Send(bytes, bytes.Length, senderEP); // Assert on GameListReply, player state Thread.Sleep(2000); player.Stop(); playerThread.Join(); Assert.AreEqual(player.GamesList.Length, 1); GameInfo playerInfo = player.GamesList[0]; GameInfo replyInfo = reply.GameInfo[0]; Assert.AreEqual(playerInfo.GameId, replyInfo.GameId); Assert.AreEqual(playerInfo.Label, replyInfo.Label); Assert.AreEqual(playerInfo.Status, replyInfo.Status); Assert.AreEqual(playerInfo.GameManager.EndPoint.HostAndPort, replyInfo.GameManager.EndPoint.HostAndPort); Assert.IsTrue(player.CurrentState is JoinGameState); }