public void RespondJoinLobbyRespondingToRetreis() { //Create a SubSystem with dummy factory. dummyCoversationFactory dumConvoFact = new dummyCoversationFactory(); LobbyAppState lobbyAppState = new LobbyAppState(); SubSystem commSubSys = new SubSystem(dumConvoFact, lobbyAppState); //Note: we dont want to start the threads of the subSystem. //Create Conversation RespondJoinLobby resJoinLobbyConvo = new RespondJoinLobby(); resJoinLobbyConvo.SubSystem = commSubSys; resJoinLobbyConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111); //Create fake incoming env. string strXML = "<RSAKeyValue><Modulus>CmZ5Hasfdldkjrjghtyremeddfjdn4738ejdjHGTFEeeeeeeeeeefjdkeldkfjguehendjeufi8ejdmGFDSqaalskebejdu==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; Envelope IncomingEnv = new Envelope(new GMJoinLobbyMessage(strXML, new Identifier(0, 1), new Identifier(0, 1), 1111), new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111), new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222), false); //Set the incoming env. resJoinLobbyConvo.IncomingEnvelope = IncomingEnv; resJoinLobbyConvo.recievedMessagesDict.TryAdd(IncomingEnv.Message.MessageNumber.ToString(), IncomingEnv); //Start the conversation resJoinLobbyConvo.Launch(); Thread.Sleep(500); //check the outgoing message. Assert.IsTrue(commSubSys.outQueue.Count >= 1); //Give a duplicate message. resJoinLobbyConvo.Process(IncomingEnv); Thread.Sleep(500); //check the outgoing message. Assert.IsTrue(commSubSys.outQueue.Count >= 2); //get count of outgoing messages for later. int countOfOutGoingMessages = commSubSys.outQueue.Count; //Wait for timeout time Thread.Sleep(2000); //Check that there are no new messages. Assert.IsTrue(countOfOutGoingMessages == commSubSys.outQueue.Count); //Cheack that thread has ended. resJoinLobbyConvo.Process(IncomingEnv); //If thread was running outgoing messages should go up. Thread.Sleep(500); Assert.IsTrue(countOfOutGoingMessages == commSubSys.outQueue.Count); //Close the communicators for next test. commSubSys.udpcomm.closeCommunicator(); commSubSys.tcpcomm.closeCommunicator(); }
public void EndGameFull() { dummyConversationFactory lobbyDumConvoFac = new dummyConversationFactory(); LobbyAppState lobbyAppState = new LobbyAppState(); SubSystem commSubSysLobby = new SubSystem(lobbyDumConvoFac, lobbyAppState); dummyConversationFactory gMDumConvoFac = new dummyConversationFactory(); GMAppState gMAppState = new GMAppState(); SubSystem comSubSysGM = new SubSystem(gMDumConvoFac, gMAppState); //Setting a running game in the lobby appState lobbyAppState.addGM(new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111), new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222), 5); lobbyAppState.addPlayer(new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111), 1, "name"); lobbyAppState.addPlayer(new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111), 2, "something"); lobbyAppState.startGame(); //pulls the players and GM into game. Assert.IsTrue(lobbyAppState.GamesBeingPlayed.Count == 1); //Setting the GMID gMAppState.gameId = 5; //Create the initators conversation EndGame endGame = new EndGame(); endGame.SubSystem = comSubSysGM; endGame.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 111); //Create the responder RespondEndGame resEndGame = new RespondEndGame(); resEndGame.SubSystem = commSubSysLobby; resEndGame.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("2.2.2.2"), 222); //Start initiator conversatin endGame.Launch(); //wait for conversation to send message Thread.Sleep(1000); //Check for outgoing message. Assert.IsTrue(comSubSysGM.outQueue.Count >= 1); //Get the message from initiator to responder. Envelope initiatorEnv; if (!comSubSysGM.outQueue.TryDequeue(out initiatorEnv)) { Assert.Fail(); } resEndGame.IncomingEnvelope = initiatorEnv; resEndGame.ConversationId = initiatorEnv.Message.ConversationId; //start the responder conversation resEndGame.Launch(); //wait a bit Thread.Sleep(1000); //Check of a responce from the conversation Assert.IsTrue(commSubSysLobby.outQueue.Count >= 1); //Get the responderenv Envelope responderEnv; if (!commSubSysLobby.outQueue.TryDequeue(out responderEnv)) { Assert.Fail(); } Assert.IsTrue(resEndGame.ConversationId == endGame.ConversationId); Assert.IsTrue(lobbyAppState.GamesBeingPlayed.Count == 0); }
public void GMJoinLobbyFullProtocolTest() { //Create a SubSystem with dummy factory. dummyCoversationFactory dumConvoFact1 = new dummyCoversationFactory(); LobbyAppState lobbyAppState = new LobbyAppState(); GMAppState gMAppState = new GMAppState(); SubSystem commSubSys1 = new SubSystem(dumConvoFact1, lobbyAppState); SubSystem commSubSysGM = new SubSystem(dumConvoFact1, gMAppState, new IPEndPoint(IPAddress.Any, 3001), new IPEndPoint(IPAddress.Any, 3002)); //dummyCoversationFactory dumConvoFact2 = new dummyCoversationFactory(); //SubSystem commSubSys2; //Note: Its a little strange that we are only using one subsystem here. Using two wont work because we have hard coded the Sockets that UDP and TCP are using and we can't bind to the same sockets. //Note: we dont want to start the threads of the subSystem. //get the initial process id. This test will check that the conversation changes it. int startingProcessId = commSubSysGM.ProcessID; //Create Initiator conversation GMJoinLobby GMJoinLobbyConversation = new GMJoinLobby(); GMJoinLobbyConversation.SubSystem = commSubSysGM; GMJoinLobbyConversation.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111); ////Create Responder Conversation RespondJoinLobby resJoinLobbyConvo = new RespondJoinLobby(); resJoinLobbyConvo.SubSystem = commSubSys1; resJoinLobbyConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222); //Start Initiator Conversation GMJoinLobbyConversation.Launch(); //Wait for conversation to send message. Thread.Sleep(500); //Check for outgoing message. Assert.IsTrue(commSubSysGM.outQueue.Count >= 1); //Get message and put into responder conversation Envelope initiatorEnv; if (!commSubSysGM.outQueue.TryDequeue(out initiatorEnv)) { Assert.Fail(); } initiatorEnv.From = commSubSysGM.udpcomm.GetEndPoint(); //Added to resolve a message. resJoinLobbyConvo.IncomingEnvelope = initiatorEnv; resJoinLobbyConvo.ConversationId = initiatorEnv.Message.ConversationId; //Start the responder conversation resJoinLobbyConvo.Launch(); //Wait for conversation to send message Thread.Sleep(500); //Check for outgoing message from responder conversation. Assert.IsTrue(commSubSys1.outQueue.Count >= 1); //Get responder reply and give to initiator. Envelope responderEnv; if (!commSubSys1.outQueue.TryDequeue(out responderEnv)) { Assert.Fail(); } GMJoinLobbyConversation.Process(responderEnv); //Check that the process Id was changed. Note: This is the Assert.IsTrue(startingProcessId == commSubSysGM.ProcessID); Console.WriteLine(startingProcessId); Console.WriteLine(commSubSysGM.ProcessID); //Check that the GM was added to the Lobby. Assert.IsTrue(lobbyAppState.GMs.Count >= 1); //Close the communicators for next test. commSubSys1.udpcomm.closeCommunicator(); commSubSys1.tcpcomm.closeCommunicator(); }
public void PLayerJoinLobbyFullProtocolTest() { //Create a SubSystem with dummy factory. dummyCoversationFactory dumConvoFact1 = new dummyCoversationFactory(); LobbyAppState lobbyAppState = new LobbyAppState(); SubSystem commSubSysLobby = new SubSystem(dumConvoFact1, lobbyAppState); WaitingRoom waitingRoom = new WaitingRoom(); PlayerAppState playerAppState = new PlayerAppState(waitingRoom); SubSystem commSubSysPlayer = new SubSystem(dumConvoFact1, playerAppState); //Set the name of the player in the App State. playerAppState.playerName = "Tester Testerson"; //get the initial process id. This test will check that the conversation changes it. int startingProcessId = commSubSysPlayer.ProcessID; //Create Initiator conversation PlayerJoinLobby PlayerJoinLobby = new PlayerJoinLobby(); PlayerJoinLobby.SubSystem = commSubSysPlayer; PlayerJoinLobby.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111); ////Create Responder Conversation RespondJoinLobby resJoinLobbyConvo = new RespondJoinLobby(); resJoinLobbyConvo.SubSystem = commSubSysLobby; resJoinLobbyConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222); //Start Initiator Conversation PlayerJoinLobby.Launch(); //Wait for conversation to send message. Thread.Sleep(500); //Check for outgoing message. Assert.IsTrue(commSubSysPlayer.outQueue.Count >= 1); //Get message and put into responder conversation Envelope initiatorEnv; if (!commSubSysPlayer.outQueue.TryDequeue(out initiatorEnv)) { Assert.Fail(); } resJoinLobbyConvo.IncomingEnvelope = initiatorEnv; resJoinLobbyConvo.ConversationId = initiatorEnv.Message.ConversationId; //Start the responder conversation resJoinLobbyConvo.Launch(); //Wait for conversation to send message Thread.Sleep(500); //Check for outgoing message from responder conversation. Assert.IsTrue(commSubSysLobby.outQueue.Count >= 1); //Get responder reply and give to initiator. Envelope responderEnv; if (!commSubSysLobby.outQueue.TryDequeue(out responderEnv)) { Assert.Fail(); } PlayerJoinLobby.Process(responderEnv); //Check that the process Id was changed. Note: This is the Assert.IsTrue(startingProcessId == commSubSysPlayer.ProcessID); Console.WriteLine(startingProcessId); Console.WriteLine(commSubSysLobby.ProcessID); //Check that the GM was added to the Lobby. Assert.IsTrue(lobbyAppState.Pls.Count >= 1); //Close the communicators for next test. commSubSysLobby.udpcomm.closeCommunicator(); commSubSysLobby.tcpcomm.closeCommunicator(); }
public void PassOffFullProtocol() { //Set up //Create the Players dummyConversationFactory dumConvoFactPlayer1 = new dummyConversationFactory(); WaitingRoom waitingRoom = new WaitingRoom(); PlayerAppState player1AppState = new PlayerAppState(waitingRoom); SubSystem subSystemPlayer1 = new SubSystem(dumConvoFactPlayer1, player1AppState); subSystemPlayer1.ProcessID = 3; dummyConversationFactory dumConvoFactPlayer2 = new dummyConversationFactory(); WaitingRoom waitingRoom2 = new WaitingRoom(); PlayerAppState player2AppState = new PlayerAppState(waitingRoom2); SubSystem subSystemPlayer2 = new SubSystem(dumConvoFactPlayer2, player2AppState); subSystemPlayer2.ProcessID = 4; //Create the GM dummyConversationFactory dumConvoFactGM = new dummyConversationFactory(); GMAppState GMAppState = new GMAppState(); SubSystem subSystemGM = new SubSystem(dumConvoFactGM, GMAppState); subSystemGM.ProcessID = 2; //Create the Lobby. dummyConversationFactory dumConvoFactLobby = new dummyConversationFactory(); LobbyAppState lobbyAppState = new LobbyAppState(); SubSystem subSystemLobby = new SubSystem(dumConvoFactLobby, lobbyAppState); subSystemLobby.ProcessID = 1; //Add GM to app state. lobbyAppState.addGM(new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222), new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2221), 2); //Add Players to app state. lobbyAppState.addPlayer(new IPEndPoint(IPAddress.Parse("3.3.3.3"), 333), 3, "player1"); lobbyAppState.addPlayer(new IPEndPoint(IPAddress.Parse("4.4.4.4"), 444), 4, "Player2"); //Execution and Testing. //Start the game in the app state. short gameIDToStart; gameIDToStart = lobbyAppState.startGame(); //make sure the game started Assert.IsTrue(gameIDToStart > 0); //Create initiator conversations and set the conversation. PassOff passOffLobby = new PassOff(); passOffLobby.SubSystem = subSystemLobby; passOffLobby.ConversationId = new Identifier(passOffLobby.SubSystem.ProcessID, SubSystem.GetNextSeqNumber()); passOffLobby.setEPs(gameIDToStart); //Note: remote Endpoint set in constructor of passOffLobby. //Create Responder conversations GameManager.RespondPassOff GMRespondPassOff = new GameManager.RespondPassOff(); GMRespondPassOff.SubSystem = subSystemGM; Player.RespondPassOff Pl1RespondPassOff = new Player.RespondPassOff(); Pl1RespondPassOff.SubSystem = subSystemPlayer1; Player.RespondPassOff Pl2RespondPassOff = new Player.RespondPassOff(); Pl2RespondPassOff.SubSystem = subSystemPlayer2; //Launch conversation. passOffLobby.Launch(); //Wait a weee bit. Thread.Sleep(500); //------------------ PlayerID from Lobby to GM Envelope envPlayerIDFromLobbyToGM = new Envelope(null, null, null, false); //Empty env. //The loop below is need to make sure that we are pulling the correct message out of the out queue. Because of the reliablility its possible to get duplicate messages going out. int attemptForCorrectMessage = 0; while (attemptForCorrectMessage < 6) { subSystemLobby.outQueue.waitIncomingEnv(3000); Thread.Sleep(500); if (!subSystemLobby.outQueue.TryDequeue(out envPlayerIDFromLobbyToGM) && envPlayerIDFromLobbyToGM?.Message.GetType() == typeof(PlayerIdMessage)) { continue; //no message was dequeued or wrong type was dequeued. } if (envPlayerIDFromLobbyToGM.To.Equals(new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222))) { break; //We got it. } attemptForCorrectMessage++; if (attemptForCorrectMessage >= 6) { Assert.Fail(); } } //Check the message. Assert.AreEqual(new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222), envPlayerIDFromLobbyToGM.To); Envelope tmp; //Clear the outgoint queue. while (subSystemGM.outQueue.TryDequeue(out tmp)) { } //Give the conversation the Message and launch responder conversation. GMRespondPassOff.IncomingEnvelope = envPlayerIDFromLobbyToGM; GMRespondPassOff.recievedMessagesDict.TryAdd(envPlayerIDFromLobbyToGM.Message.MessageNumber.ToString(), envPlayerIDFromLobbyToGM); GMRespondPassOff.Launch(); //Wait a lil bit. Thread.Sleep(1000); //------------------ Ack trom GM to Lobby. //Get the new env. if (!subSystemGM.outQueue.waitIncomingEnv(3000)) { Assert.Fail(); } Thread.Sleep(500); Envelope envFromGMToLobbyACK; if (!subSystemGM.outQueue.TryDequeue(out envFromGMToLobbyACK)) { Assert.Fail(); } //Check the message Assert.IsTrue(envFromGMToLobbyACK.Message.GetType() == typeof(AckMessage)); //Clear the outgoing queue of lobby. while (subSystemLobby.outQueue.TryDequeue(out tmp)) { } //Give lobby the envGameIDFromLobbyToPlayer1. passOffLobby.Process(envFromGMToLobbyACK); //Wait a weee bit Thread.Sleep(300); //------------------ GameId from Lobby to Player1 //Get the new env. Envelope envGameIDFromLobbyToPlayer1 = new Envelope(null, null, null, false); //Empty env. //The loop below is need to make sure that we are pulling the correct message out of the out queue. Because of the reliablility its possible to get duplicate messages going out. attemptForCorrectMessage = 0; while (attemptForCorrectMessage < 6) { subSystemLobby.outQueue.waitIncomingEnv(3000); Thread.Sleep(500); if (!subSystemLobby.outQueue.TryDequeue(out envGameIDFromLobbyToPlayer1) && envGameIDFromLobbyToPlayer1.Message.GetType() == typeof(GameIdMessage)) { continue; //no message was dequeued, or wrong type of message was dequeued. } if (envGameIDFromLobbyToPlayer1.To.Equals(new IPEndPoint(IPAddress.Parse("3.3.3.3"), 333))) { break; //We got it. } attemptForCorrectMessage++; if (attemptForCorrectMessage == 6) { Assert.Fail(); } } //Check the message. Assert.AreEqual(new IPEndPoint(IPAddress.Parse("3.3.3.3"), 333), envGameIDFromLobbyToPlayer1.To); //Clear the outgoint queue. while (subSystemPlayer1.outQueue.TryDequeue(out tmp)) { } //The the conversation the Message, and launch the conversation. Pl1RespondPassOff.IncomingEnvelope = envGameIDFromLobbyToPlayer1; Pl1RespondPassOff.recievedMessagesDict.TryAdd(envGameIDFromLobbyToPlayer1.Message.MessageNumber.ToString(), envGameIDFromLobbyToPlayer1); Pl1RespondPassOff.Launch(); //------------------ Ack from Player1 to Lobby. //Get the new env. if (!subSystemPlayer1.outQueue.waitIncomingEnv(3000)) { Assert.Fail(); } Thread.Sleep(500); Envelope envAckFromPl1ToLobby;//Empty env. if (!subSystemPlayer1.outQueue.TryDequeue(out envAckFromPl1ToLobby)) { Assert.Fail(); } //Clear the outgoing queue of lobby. while (subSystemLobby.outQueue.TryDequeue(out tmp)) { } //Give lobby the envGameIDFromLobbyToPlayer1. passOffLobby.Process(envAckFromPl1ToLobby); //------------------ GameId from Lobby to Player2. Envelope envGameIDFromLobbyToPlayer2 = new Envelope(null, null, null, false); //Empty env. //The loop below is need to make sure that we are pulling the correct message out of the out queue. Because of the reliablility its possible to get duplicate messages going out. attemptForCorrectMessage = 0; while (attemptForCorrectMessage < 6) { subSystemLobby.outQueue.waitIncomingEnv(3000); Thread.Sleep(500); if (!subSystemLobby.outQueue.TryDequeue(out envGameIDFromLobbyToPlayer2) && envGameIDFromLobbyToPlayer2.Message.GetType() == typeof(GameIdMessage)) { continue; //no message was dequeued or wrong type was dequeued. } if (envGameIDFromLobbyToPlayer2.To.Equals(new IPEndPoint(IPAddress.Parse("4.4.4.4"), 444))) { break; //We got it. } attemptForCorrectMessage++; if (attemptForCorrectMessage == 6) { Assert.Fail(); } } //Check the message. Assert.AreEqual(new IPEndPoint(IPAddress.Parse("4.4.4.4"), 444), envGameIDFromLobbyToPlayer2.To); //Clear the outgoint queue. while (subSystemPlayer2.outQueue.TryDequeue(out tmp)) { } //The the conversation the Message. Pl2RespondPassOff.IncomingEnvelope = envGameIDFromLobbyToPlayer2; Pl2RespondPassOff.recievedMessagesDict.TryAdd(envGameIDFromLobbyToPlayer2.Message.MessageNumber.ToString(), envGameIDFromLobbyToPlayer2); Pl2RespondPassOff.Launch(); //------------------ Ack from Player2 to Lobby. //Get the new env. if (!subSystemPlayer2.outQueue.waitIncomingEnv(3000)) { Assert.Fail(); } Thread.Sleep(500); Envelope envAckFromPl2ToLobby;//Empty env. if (!subSystemPlayer2.outQueue.TryDequeue(out envAckFromPl2ToLobby)) { Assert.Fail(); } //Clear the outgoing queue of lobby. while (subSystemLobby.outQueue.TryDequeue(out tmp)) { } //Give lobby the envGameIDFromLobbyToPlayer1. passOffLobby.Process(envAckFromPl2ToLobby); //------------------ Make sure all the EndPoints are set correctly. Assert.AreEqual(player1AppState.GMEndPoint, new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2221)); Assert.AreEqual(player2AppState.GMEndPoint, new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2221)); Assert.AreEqual(GMAppState.P1EndPoint, new IPEndPoint(IPAddress.Parse("3.3.3.3"), 333)); Assert.AreEqual(GMAppState.P2EndPoint, new IPEndPoint(IPAddress.Parse("4.4.4.4"), 444)); }