public void UserApp_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            RegistryData registryData = new RegistryData();

            RegistryData.GameInfo gameInfo = new RegistryData.GameInfo();

            TestAppWorker testAppWorker = new TestAppWorker(registryData);

            testAppWorker.StartTest();

            var gameManager = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            gameManager.Start();

            IPEndPoint registryEp    = new IPEndPoint(IPAddress.Loopback, testAppWorker.commFacility.udpCommunicator.Port);
            IPEndPoint gameManagerEp = new IPEndPoint(IPAddress.Loopback, gameManager.Port);

            gameInfo.RemoteEndPoint = gameManagerEp;
            gameInfo.GameActive     = false;

            Assert.IsTrue(registryData.AddGame(gameInfo));

            StartGameMessage msg1 = new StartGameMessage(1);
            Envelope         env1 = new Envelope(msg1, registryEp);

            //--------------TEST INITIAL SET UP AND SEND INITIAL MESSAGE-------------------//
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
            Assert.IsFalse(gameInfo.GameActive);

            gameManager.Send(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);

            // Make sure received message isn't null
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            // Make sure received message is AckMessage
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            AckMessage msg2 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg2);

            Assert.IsTrue(gameInfo.GameActive);
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            gameManager.Stop();
        }
예제 #2
0
        public override void Execute()
        {
            Envelope e;

            if (myConversation.envelopeQueue.Count > 0)
            {
                if (myConversation.envelopeQueue.TryTake(out e))
                {
                    if (e.message.MessageType == Message.ACK)
                    {
                        RegistryData.GameInfo gameInfo = new RegistryData.GameInfo();

                        gameInfo.GameActive     = false;
                        gameInfo.GameId         = RegistryData.GetNextGameId();
                        gameInfo.Players        = 1;
                        gameInfo.RemoteEndPoint = e.remoteEndPoint;

                        if (((RegistryAppWorker)myConversation.commFacility.myAppWorker).RegistryData.AddGame(gameInfo))
                        {
                            ((JoinGameConversation)myConversation).SendGameInfoMessage(gameInfo);
                        }
                        else
                        {
                            Logger.Error("Registry was unable to add game to dictionary");
                        }

                        myConversation.Stop();
                    }
                    else
                    {
                        Logger.Error("Join Game Conversation Expected an Ack message but received message type " + e.message.MessageType);
                    }
                }
            }
        }
예제 #3
0
        public void SendGameInfoMessage(RegistryData.GameInfo gameInfo)
        {
            MessageId  currentConvId = FirstEnvelope.message.convId;
            IPEndPoint userEndPoint  = FirstEnvelope.remoteEndPoint;

            gameInfo.Players++;
            Message  msg            = new GameInfoMessage(currentConvId, gameInfo.GameId, gameInfo.Players, gameInfo.RemoteEndPoint.ToString(), userEndPoint.ToString());
            Envelope userEnv        = new Envelope(msg, userEndPoint);
            Envelope gameManagerEnv = new Envelope(msg, gameInfo.RemoteEndPoint);

            commFacility.Send(userEnv);
            commFacility.Send(gameManagerEnv);
        }
        /// <summary>
        /// Activates the game that this end point maps to
        /// </summary>
        /// <param name="envelope">Envelope from GameManager that should be registered with Registry</param>
        /// <returns>true if it successfully activated game, else false</returns>
        private bool ActivateGame(Envelope envelope)
        {
            RegistryAppWorker userAppWorker = (RegistryAppWorker)myConversation.commFacility.myAppWorker;

            RegistryData.GameInfo game = userAppWorker.RegistryData.GetGame(envelope.remoteEndPoint);

            if (game != null)
            {
                game.GameActive = true;
                return(true);
            }

            return(false);
        }
        public override void Execute()
        {
            RegistryData.GameInfo gameInfo = ((RegistryAppWorker)myConversation.commFacility.myAppWorker).RegistryData.GetAvailableGameManager();

            if (gameInfo == null)
            {
                LaunchCommandLineApp();
                myConversation.currentState = ((JoinGameConversation)myConversation).WaitForGameManagerState;
            }
            else
            {
                ((JoinGameConversation)myConversation).SendGameInfoMessage(gameInfo);
            }
        }