Example #1
0
        public void Initialize()
        {
            Logger.Debug("Initializing Commsubsystem");

            Communicator = new Communicator();
            EnvelopeQueueDictionary = new EnvelopeQueueDictionary();
            Dispatcher = new Dispatcher() { CommSubsystem = this, Label = "Dispatcher" };
            ProcessAddressBook = new ProcessAddressBook();
            ConversationFactory.CommSubsystem = this;
        }
Example #2
0
        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();
        }
Example #3
0
        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();
        }
Example #4
0
        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();
        }
Example #5
0
        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();
        }
Example #6
0
        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();
        }
Example #7
0
        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);
        }
Example #8
0
        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();
        }