public void RespondBoardRespondingToRetries()
        {
            //Create a SubSystem with dummy factory.
            dummyConversationFactory dumConvoFact = new dummyConversationFactory();
            WaitingRoom    waitingRoom            = new WaitingRoom();
            PlayerAppState playerAppState         = new PlayerAppState(waitingRoom);
            SubSystem      commSubSys             = new SubSystem(dumConvoFact, playerAppState);
            //Note: we dont want to start the threads of the subSystem.

            //Create Conversation
            RespondTurns respondTurnsConvo = new RespondTurns();

            respondTurnsConvo.SubSystem      = commSubSys;
            respondTurnsConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111);

            //Create fake incoming env.
            Envelope IncomingEnv = new Envelope(new ResultMessage(true, 1, true, true, false, -1, -1, new Identifier(0, 1), new Identifier(0, 1)), new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111), new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222), false);

            //Set the incoming env.
            respondTurnsConvo.IncomingEnvelope = IncomingEnv;
            respondTurnsConvo.recievedMessagesDict.TryAdd(IncomingEnv.Message.MessageNumber.ToString(), IncomingEnv);

            //Start the conversation
            respondTurnsConvo.Launch();

            Thread.Sleep(500);

            //check the outgoing message.
            Assert.IsTrue(commSubSys.outQueue.Count >= 1);

            //Give a duplicate message.
            respondTurnsConvo.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.
            respondTurnsConvo.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 TurnsFullProtocolTest()
        {
            //Create a SubSystem with dummy factory.
            dummyConversationFactory dumConvoFact1 = new dummyConversationFactory();
            GMAppState gmAppState  = new GMAppState();
            SubSystem  commSubSys1 = new SubSystem(dumConvoFact1, gmAppState);
            //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 = commSubSys1.ProcessID;

            //Create Initiator conversation
            Turns turnsConvo = new Turns();

            turnsConvo.SubSystem      = commSubSys1;
            turnsConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111);

            ////Create Responder Conversation
            RespondTurns   respondTurnsConvo = new RespondTurns();
            WaitingRoom    waitingRoom       = new WaitingRoom();
            PlayerAppState playerAppState    = new PlayerAppState(waitingRoom);
            SubSystem      commSubSys2       = new SubSystem(dumConvoFact1, playerAppState);

            respondTurnsConvo.SubSystem      = commSubSys2;
            respondTurnsConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("2.2.2.2"), 2222);

            //Start Initiator Conversation
            turnsConvo.Launch();

            //Wait for conversation to send message.
            Thread.Sleep(500);

            //Check for outgoing message.
            Assert.IsTrue(commSubSys1.outQueue.Count >= 1);

            //Get message and put into responder conversation
            Envelope initiatorEnv;

            if (!commSubSys1.outQueue.TryDequeue(out initiatorEnv))
            {
                Assert.Fail();
            }
            respondTurnsConvo.IncomingEnvelope = initiatorEnv;
            respondTurnsConvo.ConversationId   = initiatorEnv.Message.ConversationId;

            //Start the responder conversation
            respondTurnsConvo.Launch();

            //Wait for conversation to send message
            Thread.Sleep(1000);

            //Check for outgoing message from responder conversation.
            Assert.IsTrue(commSubSys2.outQueue.Count >= 1);

            //Get responder reply and give to initiator.
            Envelope responderEnv;

            if (!commSubSys2.outQueue.TryDequeue(out responderEnv))
            {
                Assert.Fail();
            }
            turnsConvo.Process(responderEnv);

            //Check that the process Id was changed. Note: This is the
            Assert.IsTrue(startingProcessId == commSubSys1.ProcessID);

            //Check that gmAppState is correct
            Assert.AreEqual(gmAppState.gameId, 0);
            Assert.IsTrue(gmAppState.P1turn);

            //Check that playerAppState is correct
            Assert.AreEqual(playerAppState.gameId, 0);
            Assert.IsFalse(playerAppState.lastShotHit);
            Assert.IsTrue(playerAppState.turn);
            Assert.IsFalse(playerAppState.won);

            //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 PlayerJoinLobbyConvoRetries()
        {
            //Create a SubSystem with dummy factory.
            dummyCoversationFactory dumConvoFact   = new dummyCoversationFactory();
            WaitingRoom             waitingRoom    = new WaitingRoom();
            PlayerAppState          playerAppState = new PlayerAppState(waitingRoom);
            SubSystem commSubSysPlayer             = new SubSystem(dumConvoFact, playerAppState);

            //Note: we dont want to start the threads of the subSystem.

            //Set the name of the player
            playerAppState.playerName = "Tester Testerson";

            //Create conversation
            PlayerJoinLobby playerJoinLobbyConvo = new PlayerJoinLobby();

            playerJoinLobbyConvo.SubSystem      = commSubSysPlayer;
            playerJoinLobbyConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111); //This is just a dummpy endpoint. Because the TCP and UDP thread are running it may actually send.

            //Start conversation
            playerJoinLobbyConvo.Launch();

            //Wait one second.
            Thread.Sleep(1000);

            //Check outQ for outgoing messages.
            Assert.IsTrue(commSubSysPlayer.outQueue.Count >= 1);

            //Wait for retry
            Thread.Sleep(2000);

            //Chekc outQ for multiple outgoing messages.
            Assert.IsTrue(commSubSysPlayer.outQueue.Count >= 2);

            Thread.Sleep(2000);

            Assert.IsTrue(commSubSysPlayer.outQueue.Count >= 3);

            //Make sure those messages are the same.
            Envelope env1;

            if (!commSubSysPlayer.outQueue.TryDequeue(out env1))
            {
                Assert.Fail();
            }
            Envelope env2;

            if (!commSubSysPlayer.outQueue.TryDequeue(out env2))
            {
                Assert.Fail();
            }
            Envelope env3;

            if (!commSubSysPlayer.outQueue.TryDequeue(out env3))
            {
                Assert.Fail();
            }


            //Make sure the message in the outQ are the same.
            Assert.IsTrue(env1.Message.ConversationId.Equals(env2.Message.ConversationId) && env1.Message.MessageNumber.Equals(env2.Message.MessageNumber));
            Assert.IsTrue(env1.Message.ConversationId.Equals(env3.Message.ConversationId) && env1.Message.MessageNumber.Equals(env3.Message.MessageNumber));

            //Close the communicators for next test.
            commSubSysPlayer.udpcomm.closeCommunicator();
            commSubSysPlayer.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));
        }
示例#6
0
        public void ShotConvoRetries()
        {
            dummyConversationFactory dumConvoFact = new dummyConversationFactory();
            WaitingRoom    waitingRoom            = new WaitingRoom();
            PlayerAppState playerAppState         = null;

            try
            {
                playerAppState = new PlayerAppState(waitingRoom);
            }
            catch (Exception e)
            {
            }
            Tuple <short, short> shot = new Tuple <short, short>(2, 4);

            playerAppState.shotCoordinates = shot;
            playerAppState.playerID        = 1;

            SubSystem commSubSys = new SubSystem(dumConvoFact, playerAppState);

            Shot shotConvo = new Shot();

            shotConvo.SubSystem      = commSubSys;
            shotConvo.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 1111);

            shotConvo.Launch();

            Thread.Sleep(1000);

            Assert.IsTrue(commSubSys.outQueue.Count >= 1);

            Thread.Sleep(2000);

            Assert.IsTrue(commSubSys.outQueue.Count >= 2);

            Thread.Sleep(2000);

            Assert.IsTrue(commSubSys.outQueue.Count >= 3);

            //Make sure those messages are the same.
            Envelope env1;

            if (!commSubSys.outQueue.TryDequeue(out env1))
            {
                Assert.Fail();
            }
            Envelope env2;

            if (!commSubSys.outQueue.TryDequeue(out env2))
            {
                Assert.Fail();
            }
            Envelope env3;

            if (!commSubSys.outQueue.TryDequeue(out env3))
            {
                Assert.Fail();
            }

            //Make sure the message in the outQ are the same.
            Assert.IsTrue(env1.Message.ConversationId.Equals(env2.Message.ConversationId) && env1.Message.MessageNumber.Equals(env2.Message.MessageNumber));
            Assert.IsTrue(env1.Message.ConversationId.Equals(env3.Message.ConversationId) && env1.Message.MessageNumber.Equals(env3.Message.MessageNumber));

            //Close the communicators for next test.
            commSubSys.udpcomm.closeCommunicator();
            commSubSys.tcpcomm.closeCommunicator();
        }