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();
        }
Exemple #2
0
        public void UdpCommunicator_ReceivingOfBadMessages()
        {
            // Setup a socket from which to send bad data
            var localEp = new IPEndPoint(IPAddress.Any, 0);
            var sender  = new UdpClient(localEp);

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

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            byte[] bytesToSend     = Encoding.ASCII.GetBytes("Garabage");
            var    targetIpAddress = new IPEndPoint(IPAddress.Loopback, commReciever.Port);

            sender.Send(bytesToSend, bytesToSend.Length, targetIpAddress);

            // Give the receiver time to run
            Thread.Sleep(100);

            // No message should received
            Assert.IsNull(_lastIncomingEnvelope4);

            sender.Close();
            commReciever.Stop();
        }
        public void UserApp_EndTurnConversation_Test()
        {
            TestAppWorker testAppWorker = new TestAppWorker();

            testAppWorker.StartTest();

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

            fakeGameManager.Start();

            IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeGameManager.Port);
            Message    msg1           = new EndTurnMessage(1, 6);
            Envelope   env            = new Envelope(msg1, targetEndPoint);

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

            testAppWorker.commFacility.Process(env);

            Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            Thread.Sleep(100);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
        }
        public void TestSendAndReceive()
        {
            var blockComm1 = new ManualResetEvent(false);
            var blockComm2 = new ManualResetEvent(false);

            var expectedCount = 0;

            var comm1 = new UdpCommunicator(CommEndPoint1, Converter);
            var comm2 = new UdpCommunicator(CommEndPoint2, Converter);

            comm1.OnEnvelope += (Envelope env) =>
            {
                blockComm1.Set();
                Assert.True(env.MessageContent.SenderId == CommId2);
                Assert.True(env.MessageContent.ConversationId == ConvoId);
                Assert.True(env.MessageContent.MessageCount == expectedCount);

                expectedCount++;
            };

            comm2.OnEnvelope += (Envelope env) =>
            {
                blockComm2.Set();
                Assert.True(env.MessageContent.SenderId == CommId1);
                Assert.True(env.MessageContent.ConversationId == ConvoId);
                Assert.True(env.MessageContent.MessageCount == expectedCount);

                expectedCount++;
            };

            comm1.Start();
            comm2.Start();

            var startMessage = new Message()
            {
                SenderId       = CommId1,
                MessageId      = MessageType.UNKNOWN,
                ConversationId = ConvoId,
                MessageCount   = expectedCount
            };

            comm1.SendEnvelope(new Envelope(startMessage, CommEndPoint2));

            Assert.True(blockComm2.WaitOne(UdpCommunicator.Timeout * WaitFactor), "Communicator 2 never received message");

            var nextMessage = new Message()
            {
                SenderId       = CommId2,
                MessageId      = MessageType.UNKNOWN,
                ConversationId = ConvoId,
                MessageCount   = expectedCount
            };

            comm2.SendEnvelope(new Envelope(nextMessage, CommEndPoint1));

            Assert.True(blockComm1.WaitOne(UdpCommunicator.Timeout * WaitFactor), "Communicator 1 never received message");

            comm1.Stop();
            comm2.Stop();
        }
Exemple #5
0
        public void Start()
        {
            var converter = new JsonMessageConverter();
            var comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 4000), converter);

            comm.Start();
            var router = new MessageRouter(comm);

            var content = new Content()
            {
                FileName    = "test_file.txt",
                ByteSize    = 1024,
                ContentHash = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF },
                Description = "Test file",
                LocalPath   = "."
            };

            var chunks = new HashSet <int>(new List <int>()
            {
                0, 1, 2, 3
            });

            var worker = new ChunkReceiver(content, chunks, new Guid("11f684d9-53a5-4fd9-948f-5526e881f60c"));

            router.AddConversation(worker, new IPEndPoint(IPAddress.Loopback, 4001));

            Console.ReadLine();
            comm.Stop();
        }
Exemple #6
0
        public void UdpCommunicator_Multicast()
        {
            var commSender = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = NoOp
            };

            commSender.Start();

            var commReciever = new UdpCommunicator()
            {
                MinPort         = 5000,
                MaxPort         = 5000,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope5
            };

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            // Note, we can't test multiple receivers on the same host (localhost), because the
            // port number has to be the same for all receivers

            // Have the receivers join a Group Multicast address
            var multiCastAddress = new IPAddress(new byte[] { 224, 1, 1, 2 });

            commReciever.JoinMulticastGroup(multiCastAddress);

            // Send message to Group Multicast address
            var msg            = new StartGameMessage(gameId);
            var targetEndPoint = new IPEndPoint(IPAddress.Loopback, 5000);
            var env            = new Envelope(msg, targetEndPoint);

            commSender.Send(env);

            Thread.Sleep(100);

            Assert.AreNotSame(msg, _lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5.message);
            Assert.AreEqual(msg.msgId, _lastIncomingEnvelope5.message.msgId);
            Assert.AreEqual(msg.convId, _lastIncomingEnvelope5.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope5.message as StartGameMessage;

            Assert.IsNotNull(msg2);
            Assert.AreEqual(msg.GameId, msg2.GameId);

            commReciever.DropMulticastGroup(multiCastAddress);
            commReciever.Stop();
            commSender.Stop();
        }
        public void UserApp_JoinGameConversation_Test()
        {
            TestAppWorker testAppWorker = new TestAppWorker();

            testAppWorker.StartTest();

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

            fakeRegistry.Start();

            IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeRegistry.Port);

            Message  msg1 = new RequestGameMessage();
            Envelope env  = new Envelope(msg1, targetEndPoint);

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

            testAppWorker.commFacility.Process(env);

            Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            Thread.Sleep(100);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            RequestGameMessage msg2 = _lastIncomingEnvelope1.message as RequestGameMessage;

            Assert.IsNotNull(msg2);

            GameInfoMessage msg3 = new GameInfoMessage(msg1.convId, 1, 1, "GMAddress", "UAAddress");

            Assert.AreNotSame((IPEndPoint)fakeRegistry._myUdpClient.Client.LocalEndPoint, _lastIncomingEnvelope1.remoteEndPoint);

            Envelope env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            fakeRegistry.Send(env2);

            Thread.Sleep(100);

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

            fakeRegistry.Stop();
            testAppWorker.StopTest();
        }
Exemple #8
0
        public void Registry_JoinGameConversation_Test()
        {
            IPEndPoint registryEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);

            RegistryData  registryData  = new RegistryData();
            TestAppWorker testAppWorker = new TestAppWorker(registryData, registryEndPoint);

            testAppWorker.StartTest();

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

            comm1.Start();

            Message  msg1 = new RequestGameMessage();
            Envelope env  = new Envelope(msg1, registryEndPoint);

            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
            Assert.IsNull(registryData.GetAvailableGameManager());

            comm1.Send(env);

            Thread.Sleep(1000);

            /*
             * //The conversation happens to fast. After the sleep, the conv is already gone
             * Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
             */

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            GameInfoMessage msg2 = _lastIncomingEnvelope1.message as GameInfoMessage;

            Assert.IsNotNull(msg2);

            Assert.IsNotNull(registryData.GetAvailableGameManager());
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //-------------STOPPING------------//
            testAppWorker.StopTest();
            comm1.Stop();
        }
        public void TestStartAndStop()
        {
            var blockComm = new ManualResetEvent(false);

            var comm = new UdpCommunicator(CommEndPoint1, Converter);

            Assert.False(comm.IsRunning);

            comm.Start();

            Thread.Yield();
            Assert.True(comm.IsRunning);

            comm.Stop();

            Thread.Yield();
            Assert.False(comm.IsRunning);
        }
Exemple #10
0
        public Form1()
        {
            InitializeComponent();
            seederDataGridView.CellFormatting += formatBytes;

            this.FormClosing += CleanUp;

            persistenceManager = new PersistenceManager();
            persistenceManager.Clear(PersistenceManager.StorageType.Remote);
            seederDataGridView.DataSource = persistenceManager.ReadContent().LocalContent;
            searchDataGridView.DataSource = persistenceManager.ReadContent().RemoteContent;

            converter = new JsonMessageConverter();
            comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, Properties.Settings.Default.HostPort), converter);
            comm.Start();
            router = new MessageRouter(comm);

            router.OnConversationRequest += HandleNewConversation;

            RegistryEndpoint = new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.RegistryAddress), Properties.Settings.Default.RegistryPort);
        }
Exemple #11
0
        public void UdpSendReceiveTest()
        {
            var        testGuid = Guid.NewGuid();
            IPEndPoint ep       = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12000);
            Message    message  = new CanvasMessage()
            {
                ConversationId = new Tuple <Guid, short>(testGuid, 1),
                MessageNumber  = new Tuple <Guid, short>(testGuid, 1),
                CanvasId       = 1
            };
            Envelope envelope = new Envelope()
            {
                RemoteEP = ep, Message = message
            };
            UdpCommunicator communicator1 = new UdpCommunicator()
            {
                EnvelopeHandler = FirstEnvelope
            };

            communicator1.SetPort(12001);
            communicator1.Start();

            UdpCommunicator communicator2 = new UdpCommunicator()
            {
                EnvelopeHandler = SecondEnvelope
            };

            communicator2.SetPort(12000);
            communicator2.Start();

            Thread.Sleep(2000);
            communicator1.Send(envelope);
            Thread.Sleep(1000);

            Assert.IsNotNull(envelope2);
            Assert.IsNotNull(envelope2.Message);
            Assert.AreEqual(envelope.Message.ConversationId, envelope2.Message.ConversationId);
            Assert.AreEqual(envelope.Message.MessageNumber, envelope2.Message.MessageNumber);
            Assert.AreEqual((envelope.Message as CanvasMessage).CanvasId, (envelope2.Message as CanvasMessage).CanvasId);
        }
Exemple #12
0
        public void Start()
        {
            var converter = new JsonMessageConverter();
            var comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 4001), converter);

            comm.Start();
            var router = new MessageRouter(comm);

            router.OnConversationRequest += HandleNewConversation;

            ContentInfo = new Content()
            {
                FileName    = "test_file.txt",
                ByteSize    = 1024,
                ContentHash = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF },
                Description = "Test file",
                LocalPath   = "TestData"
            };

            Console.ReadLine();
            comm.Stop();
        }
Exemple #13
0
        public void UserApp_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            UserInfo userInfo = new UserInfo()
            {
                PlayerId        = 0,
                CurrentPlayerId = -1,
                AcceptUserInput = false
            };

            TestAppWorker testAppWorker = new TestAppWorker(userInfo);

            testAppWorker.StartTest();

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

            gameManager.Start();

            IPEndPoint userAppEp = new IPEndPoint(IPAddress.Loopback, testAppWorker.commFacility.udpCommunicator.Port);

            testAppWorker.UserInfo.GameManagerEP = new IPEndPoint(IPAddress.Loopback, gameManager.Port);

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

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

            gameManager.Send(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.IsTrue(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId) is StartGameConversation);

            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);

            //--------------SEND START GAME STATE UPDATE MESSAGE-------------------//
            GameStateUpdateMessage msg3 = new GameStateUpdateMessage(msg1.convId, 1, 0, new short[4], new short[4], new short[84], 0, "TEST APP MOVE");
            Envelope env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;

            // User should not know it is their turn
            Assert.IsFalse(userInfo.IsTurn);

            gameManager.Send(env2);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            // User should know it is their turn
            Assert.IsTrue(userInfo.IsTurn);

            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 msg4 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg4);

            //--------------SEND FIRST PLAYER GO ACK MESSAGE-------------------//
            AckMessage msg5 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env3 = new Envelope(msg5, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;

            // Check user isn't able to input
            Assert.IsFalse(userInfo.AcceptUserInput);

            gameManager.Send(env3);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            // Check user is able to input
            Assert.IsTrue(userInfo.AcceptUserInput);

            // Check conversation ended
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            gameManager.Stop();
        }
Exemple #14
0
        public void UdpCommunicator_SimpleSendReceives()
        {
            var comm1 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            comm1.Start();

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

            comm2.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            StartGameMessage msg            = new StartGameMessage(gameId);
            IPEndPoint       targetEndPoint = new IPEndPoint(IPAddress.Loopback, comm2.Port);
            Envelope         env            = new Envelope(msg, targetEndPoint);

            comm1.Send(env);

            Thread.Sleep(100);

            Assert.AreNotSame(msg, _lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);
            Assert.AreEqual(msg.msgId, _lastIncomingEnvelope2.message.msgId);
            Assert.AreEqual(msg.convId, _lastIncomingEnvelope2.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope2.message as StartGameMessage;

            Assert.IsNotNull(msg2);
            Assert.AreEqual(msg.GameId, msg2.GameId);

            AckMessage msg3 = new AckMessage(msg2.convId, msg2.GameId);

            Assert.AreNotEqual(msg2.msgId, msg3.msgId);
            Assert.AreEqual(msg2.convId, msg3.convId);
            targetEndPoint = new IPEndPoint(IPAddress.Loopback, comm1.Port);
            Envelope env3 = new Envelope(msg3, targetEndPoint);

            comm2.Send(env3);

            Thread.Sleep(100);

            Assert.AreNotSame(msg3, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg3.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg3.convId, _lastIncomingEnvelope1.message.convId);
            AckMessage msg4 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg4);
            Assert.AreEqual(msg3.GameId, msg4.GameId);

            comm1.Stop();
            comm2.Stop();
        }
Exemple #15
0
        public void UdpCommunicator_SendingOfBadEnvelopes()
        {
            var commSender = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = NoOp
            };

            commSender.Start();

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

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            // Send a message to a non-existant remote end point
            //     Expected behavior - no error, but no delivery
            var msg            = new StartGameMessage(gameId);
            var targetEndPoint = new IPEndPoint(IPAddress.Loopback, 1012);
            var env            = new Envelope(msg, targetEndPoint);
            var error          = commSender.Send(env);

            Assert.IsNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a message to a remote end point with a 0 for the port
            //     Expected behavior - error
            env.remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a message to a remote end point with a 0.0.0.0 for the address
            //     Expected behavior - error
            env.remoteEndPoint = new IPEndPoint(IPAddress.Any, 1245);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send to a null remote end point
            //      Expected behavior -- error
            env.remoteEndPoint = null;
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a null message
            //      Expected behavior -- error
            env.message        = null;
            env.remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 1012);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a null envelope
            //      Expected behavior -- error
            error = commSender.Send(null);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            commSender.Stop();
            commReciever.Stop();
        }
        public void GameManager_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            string[]      args          = { "Test Arguments" };
            GameInfo      gameInfo      = new GameInfo();
            TestAppWorker testAppWorker = new TestAppWorker(args, gameInfo);

            testAppWorker.StartTest();

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

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

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

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

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

            ua1.Start();
            ua2.Start();
            ua3.Start();
            ua4.Start();
            registry.Start();

            IPEndPoint ua1Ep      = new IPEndPoint(IPAddress.Loopback, ua1.Port);
            IPEndPoint ua2Ep      = new IPEndPoint(IPAddress.Loopback, ua2.Port);
            IPEndPoint ua3Ep      = new IPEndPoint(IPAddress.Loopback, ua3.Port);
            IPEndPoint ua4Ep      = new IPEndPoint(IPAddress.Loopback, ua4.Port);
            IPEndPoint registryEp = new IPEndPoint(IPAddress.Loopback, registry.Port);

            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(0, ua1Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(1, ua2Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(2, ua3Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(3, ua4Ep));

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

            //--------------TEST INITIAL SET UP AND START CONVERSATION-------------------//
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            testAppWorker.commFacility.Process(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.IsTrue(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId) is StartGameConversation);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

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

            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);

            Assert.IsNotNull(_lastIncomingEnvelope3);
            Assert.IsNotNull(_lastIncomingEnvelope3.message);

            Assert.IsNotNull(_lastIncomingEnvelope4);
            Assert.IsNotNull(_lastIncomingEnvelope4.message);

            Assert.IsNotNull(_lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5.message);

            // Make sure received messages are StartGameMessages
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope1.message as StartGameMessage;

            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope2.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope3.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope4.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope5.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            //--------------SEND START ACK MESSAGE-------------------//
            AckMessage msg3 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;
            _lastIncomingEnvelope2 = null;
            _lastIncomingEnvelope3 = null;
            _lastIncomingEnvelope4 = null;
            _lastIncomingEnvelope5 = null;

            ua1.Send(env2);
            ua2.Send(env2);
            ua3.Send(env2);
            ua4.Send(env2);
            registry.Send(env2);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

            // Make sure received messages aren't null, except registry's
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);

            Assert.IsNotNull(_lastIncomingEnvelope3);
            Assert.IsNotNull(_lastIncomingEnvelope3.message);

            Assert.IsNotNull(_lastIncomingEnvelope4);
            Assert.IsNotNull(_lastIncomingEnvelope4.message);

            Assert.IsNull(_lastIncomingEnvelope5);

            // Make sure received messages are StartGameMessages
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);

            GameStateUpdateMessage msg4;

            msg4 = _lastIncomingEnvelope1.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope2.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope3.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope4.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            //--------------SEND UPDATE ACK MESSAGE-------------------//
            AckMessage msg5 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env3 = new Envelope(msg5, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;
            _lastIncomingEnvelope2 = null;
            _lastIncomingEnvelope3 = null;
            _lastIncomingEnvelope4 = null;
            _lastIncomingEnvelope5 = null;

            ua1.Send(env3);
            ua2.Send(env3);
            ua3.Send(env3);
            ua4.Send(env3);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

            // Make sure all received messages are null except ua1's
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            Assert.IsNull(_lastIncomingEnvelope2);

            Assert.IsNull(_lastIncomingEnvelope3);

            Assert.IsNull(_lastIncomingEnvelope4);

            Assert.IsNull(_lastIncomingEnvelope5);

            // Check conversation ended
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            ua1.Stop();
            ua2.Stop();
            ua3.Stop();
            ua4.Stop();
            registry.Stop();
        }