Beispiel #1
0
        public void GenerateShouldGenerateNewMessages()
        {
            // Arrange
            // Act
            var messages     = _messageCreator.CreateMessages(_messageInitializer);
            var firstMessage = messages.First();

            var innerMessages = (Message[])firstMessage.Data;
            var innerMessage  = innerMessages.First();

            // Assert
            Assert.That(firstMessage.DataSize, Is.Zero);
            Assert.That(firstMessage.ServiceSize, Is.EqualTo(AllConstants.SendingRequestMessageSize));
            Assert.That(firstMessage.ReceiverId, Is.EqualTo(_messageInitializer.ReceiverId));
            Assert.That(firstMessage.SenderId, Is.EqualTo(_messageInitializer.SenderId));
            Assert.That(firstMessage.MessageType, Is.EqualTo(MessageType.SendingRequest));

            Assert.That(innerMessage.Data, Is.TypeOf(typeof(Message[])));
            Assert.That(innerMessage.MessageType, Is.EqualTo(_messageInitializer.MessageType));
            Assert.That(innerMessage.SenderId, Is.EqualTo(_messageInitializer.SenderId));
            Assert.That(innerMessage.ReceiverId, Is.EqualTo(_messageInitializer.ReceiverId));
            Assert.That(innerMessages.Sum(m => m.DataSize), Is.EqualTo(_messageInitializer.Size));
            Assert.That(innerMessage.ServiceSize, Is.EqualTo(AllConstants.ServicePartSize));

            Assert.That(firstMessage.ParentId, Is.EqualTo(innerMessage.ParentId));
        }
Beispiel #2
0
        public void GenerateShouldGenerateNewGeneralMessages()
        {
            // Arrange
            // Act
            var messages     = _messageCreator.CreateMessages(_messageInitializer);
            var firstMessage = messages.First();

            // Assert
            Assert.That(messages.Length, Is.GreaterThanOrEqualTo(1));
            Assert.That(firstMessage.ReceiverId, Is.EqualTo(_messageInitializer.ReceiverId));
            Assert.That(firstMessage.SenderId, Is.EqualTo(_messageInitializer.SenderId));
            Assert.That(firstMessage.Data, Is.TypeOf(typeof(Message[])));
            Assert.That(firstMessage.MessageType, Is.EqualTo(_messageInitializer.MessageType));
            Assert.That(messages.All(m => m.ServiceSize == AllConstants.ServicePartSize));
            Assert.That(messages.Where((message, i) => i != messages.Length - 1)
                        .All(m => m.DataSize == AllConstants.PackageSize));
        }
Beispiel #3
0
        private void HandleFailedSendingRequest(Node node, Message request)
        {
            var responseInitializer = CreateNegativeResponseInitializer(node, request);

            var responses = _negativeResponseMessageCreator.CreateMessages(responseInitializer);

            UpdateResponsesByRequest(responses, request);

            _negativeResponseMessageCreator.RemoveFromQueue(new[] { request }, request.LastTransferNodeId);
        }
Beispiel #4
0
        private void HandleNegativeSendingResponse(Message response)
        {
            var requests = (Message[])response.Data;
            var request  = requests.First();

            var oldMessages  = (Message[])request.Data;
            var firstMessage = oldMessages.First();

            var sumSize = oldMessages.Sum(oldMessage => oldMessage.Size)
                          - oldMessages.Length * AllConstants.ServicePartSize;

            var messageInitializer = new MessageInitializer
            {
                MessageType = firstMessage.MessageType,
                ReceiverId  = firstMessage.ReceiverId,
                SenderId    = firstMessage.SenderId,
                Size        = sumSize,
                Data        = firstMessage.Data
            };

            var messages = _generalMessageCreator.CreateMessages(messageInitializer);

            if (messages != null)
            {
                UpdateInnerMessagesIds(messages, firstMessage.ParentId);

                var receiver = _network.GetNodeById(response.ReceiverId);
                request.IsCanceled = true;
                receiver.CanceledMessages.Add(request);

                _generalMessageCreator.AddInQueue(messages, firstMessage.SenderId);
            }
            else
            {
                request.Route = firstMessage.Route.ToArray();
                request.LastTransferNodeId = request.SenderId;

                _generalMessageCreator.AddInQueue(requests, firstMessage.SenderId);
            }

            _generalMessageCreator.RemoveFromQueue(new[] { response }, response.ReceiverId);
        }
Beispiel #5
0
        public void GenerateShouldGenerateNewPositiveResponse()
        {
            // Arrange
            var route = ((Message[])_messageInitializer.Data)
                        .First().Route;

            // Act
            var messages     = _messageCreator.CreateMessages(_messageInitializer);
            var firstMessage = messages.First();

            // Assert
            Assert.That(messages.Length, Is.EqualTo(1));
            Assert.That(firstMessage.ReceiverId, Is.EqualTo(_messageInitializer.ReceiverId));
            Assert.That(firstMessage.SenderId, Is.EqualTo(_messageInitializer.SenderId));
            Assert.That(firstMessage.Data, Is.EqualTo(_messageInitializer.Data));
            Assert.That(firstMessage.MessageType, Is.EqualTo(MessageType.PositiveSendingResponse));
            Assert.That(messages.All(m => m.ServiceSize == AllConstants.SendingResponseMessageSize));
            Assert.That(messages.Where((message, i) => i != messages.Length - 1)
                        .All(m => m.DataSize == 0));
            CollectionAssert.AreEqual(route, firstMessage.Route.Reverse());
        }
Beispiel #6
0
        private void CreateRandomMessages(Node sender, Node receiver)
        {
            var messageInitializer = CreateMessageInitializer(sender.Id, receiver.Id);

            var messages = _messageCreator.CreateMessages(messageInitializer);

            if (messages != null)
            {
                _messageCreator.AddInQueue(messages, messages.First().SenderId);

                LastGeneratedMessage = messages.LastOrDefault();
            }
        }
Beispiel #7
0
        private void MessageCreate(MessageInitializer messageInitializer)
        {
            var messages = _messageCreator?.CreateMessages(messageInitializer);

            if (messages != null)
            {
                var senderId = messageInitializer.SenderId;
                _messageCreator.AddInQueue(messages, senderId);
            }
            else
            {
                throw new MessageException("Message Cannot Be Created");
            }
        }
Beispiel #8
0
        private void HandleSendingRequest(Message request)
        {
            var responseInitializer = new MessageInitializer
            {
                ReceiverId  = request.SenderId,
                SenderId    = request.ReceiverId,
                MessageType = MessageType.PositiveSendingResponse,
                Data        = request.Data
            };

            var responses = _positiveResponseMessageCreator.CreateMessages(responseInitializer);

            foreach (var response in responses)
            {
                response.ParentId = request.ParentId;
            }

            _positiveResponseMessageCreator.AddInQueue(responses, request.ReceiverId);
        }
Beispiel #9
0
        public void CreateMessagesShouldDoIt()
        {
            // Arrange
            // Act
            var messages     = _messageCreator.CreateMessages(_messageInitializer);
            var firstMessage = messages.First();

            // Assert
            Assert.That(messages.Length, Is.EqualTo(1));
            Assert.That(firstMessage.ReceiverId, Is.EqualTo(_messageInitializer.ReceiverId));
            Assert.That(firstMessage.SenderId, Is.EqualTo(_messageInitializer.SenderId));
            Assert.That(firstMessage.Data, Is.TypeOf(typeof(Message[])));
            Assert.That(firstMessage.MessageType, Is.EqualTo(_messageInitializer.MessageType));
            Assert.That(firstMessage.DataSize, Is.EqualTo(_messageInitializer.Size));
            Assert.That(firstMessage.Size, Is.EqualTo(_messageInitializer.Size + AllConstants.ServicePartSize));
            Assert.That(firstMessage.ServiceSize, Is.EqualTo(AllConstants.ServicePartSize));
        }