Exemplo n.º 1
0
        public void Send(MemoryStream buffer, TransportMessage message)
        {
            if (_isInClosedState)
            {
                if (_closedStateStopwatch.Elapsed < _options.ClosedStateDuration)
                {
                    _logger.DebugFormat("Send ignored in closed state, Peer: {0}, MessageTypeId: {1}, MessageId: {2}", _peerId, message.MessageTypeId, message.Id);
                    return;
                }
                SwitchToOpenState();
            }

            if (_socket.SendWithTimeout(buffer.GetBuffer(), (int)buffer.Position, _options.SendTimeout) >= 0)
            {
                _failedSendCount = 0;
                return;
            }

            _logger.ErrorFormat("Unable to send message, destination peer: {0}, MessageTypeId: {1}, MessageId: {2}", _peerId, message.MessageTypeId, message.Id);

            if (_failedSendCount >= _options.SendRetriesBeforeSwitchingToClosedState)
                SwitchToClosedState();

            ++_failedSendCount;
        }
        public void should_send_MessageProcessingFailed_if_unable_to_deserialize_message()
        {
            SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);

            _bus.Start();

            var command = new FakeCommand(123);
            _messageSerializer.AddSerializationExceptionFor(command.TypeId(), "Serialization error");

            using (SystemDateTime.PauseTime())
            using (MessageId.PauseIdGeneration())
            {
                var transportMessage = command.ToTransportMessage();

                var messageProcessingFailedBytes = new MessageProcessingFailed(null, null, null, DateTime.UtcNow, null).ToTransportMessage().MessageBytes;
                _messageSerializer.AddSerializationFuncFor<MessageProcessingFailed>(x =>
                {
                    x.FailingMessage.ShouldEqual(transportMessage);
                    x.ExceptionUtcTime.ShouldEqual(SystemDateTime.UtcNow);
                    x.ExceptionMessage.ShouldContain("Unable to deserialize message");
                    return messageProcessingFailedBytes;
                });
                
                _transport.RaiseMessageReceived(transportMessage);

                var processingFailedTransportMessage = new TransportMessage(MessageUtil.TypeId<MessageProcessingFailed>(), messageProcessingFailedBytes, _self);
                _transport.ExpectExactly(new TransportMessageSent(processingFailedTransportMessage, _peerUp));
            }
        }
Exemplo n.º 3
0
 public static MessageContext CreateNew(TransportMessage transportMessage)
 {
     return new MessageContext
     {
         MessageId = transportMessage.Id,
         Originator = transportMessage.Originator,
     };
 }
Exemplo n.º 4
0
 public MessageProcessingFailed(TransportMessage failingMessage, string failingMessageJson, string exceptionMessage, DateTime exceptionUtcTime, string[] failingHandlerNames)
 {
     FailingMessage = failingMessage;
     FailingMessageJson = failingMessageJson;
     ExceptionMessage = exceptionMessage;
     ExceptionUtcTime = exceptionUtcTime;
     FailingHandlerNames = failingHandlerNames ?? ArrayUtil.Empty<string>();
 }
Exemplo n.º 5
0
        public void Send(TransportMessage message, IEnumerable<Peer> peerIds)
        {
            var targets = peerIds.ToArray();
            if (targets.Length != 0)
                _messages.Add(new TransportMessageSent(message, targets));

            var deserializedMessage = _messageSerializer.Deserialize(message.MessageTypeId, message.MessageBytes);
            if (deserializedMessage != null)
                MessagesSent.Add(deserializedMessage);
        }
Exemplo n.º 6
0
 public static IMessage ToMessage(this IMessageSerializer serializer, TransportMessage transportMessage)
 {
     return serializer.Deserialize(transportMessage.MessageTypeId, transportMessage.MessageBytes);
 }
Exemplo n.º 7
0
 public void ReconnectFor(string endPoint, TransportMessage message)
 {
     Disconnect();
     EndPoint = endPoint;
     ConnectFor(message);
 }
Exemplo n.º 8
0
 public PersistMessageCommand(TransportMessage transportMessage, PeerId[] targets)
 {
     TransportMessage = transportMessage;
     Targets = targets;
 }
Exemplo n.º 9
0
 public abstract void OnRealTimeMessage(TransportMessage transportMessage);
Exemplo n.º 10
0
 public MessageReplayed(Guid replayId, TransportMessage message)
 {
     ReplayId = replayId;
     Message = message;
 }
Exemplo n.º 11
0
 public void Send(TransportMessage message, IEnumerable <Peer> peers, SendContext context)
 {
     _outboundSocketActions.Add(OutboundSocketAction.Send(message, peers, context));
 }
Exemplo n.º 12
0
 private static void Serialize(MemoryStream outputBuffer, TransportMessage transportMessage)
 {
     outputBuffer.Position = 0;
     Serializer.Serialize(outputBuffer, transportMessage);
 }
Exemplo n.º 13
0
 private void TriggerMessageReceived(TransportMessage transportMessage)
 {
     MessageReceived(transportMessage);
 }
Exemplo n.º 14
0
 public ReplayMessageCommand(TransportMessage messageToReplay, string[] handlerTypes)
 {
     MessageToReplay = messageToReplay;
     HandlerTypes = handlerTypes ?? ArrayUtil.Empty<string>();
 }
Exemplo n.º 15
0
        public void should_send_various_sized_messages()
        {
            var senderTransport = CreateAndStartZmqTransport();
            senderTransport.SocketOptions.SendHighWaterMark = 3;

            var receviedMessages = new List<TransportMessage>();
            var receiverTransport = CreateAndStartZmqTransport(onMessageReceived: receviedMessages.Add);
            var receiver = new Peer(new PeerId("Abc.Testing.Receiver.Up"), receiverTransport.InboundEndPoint);

            var messageBytes = new byte[5000];
            new Random().NextBytes(messageBytes);

            var bigMessage = new TransportMessage(new MessageTypeId(typeof(FakeCommand)), messageBytes, new PeerId("X"), senderTransport.InboundEndPoint, MessageId.NextId());
            senderTransport.Send(bigMessage, new[] { receiver });

            Wait.Until(() => receviedMessages.Count == 1, 150.Milliseconds());

            receviedMessages[0].ShouldHaveSamePropertiesAs(bigMessage);

            var smallMessage = new TransportMessage(new MessageTypeId(typeof(FakeCommand)), new byte[1], new PeerId("X"), senderTransport.InboundEndPoint, MessageId.NextId());
            senderTransport.Send(smallMessage, new[] { receiver });

            Wait.Until(() => receviedMessages.Count == 2, 150.Milliseconds());

            receviedMessages[1].ShouldHaveSamePropertiesAs(smallMessage);
        }
Exemplo n.º 16
0
        public void Send(TransportMessage message, IEnumerable<Peer> peerIds)
        {
            var peerList = (peerIds as IList<Peer>) ?? peerIds.ToList();
            var upPeers = (peerList.Count == 1 && peerList[0].IsUp) ? peerList : peerList.Where(peer => peer.IsUp);

            _innerTransport.Send(message, upPeers);

            if (!message.MessageTypeId.IsPersistent())
                return;

            var persistentPeerIds = peerList.Where(x => _peerDirectory.IsPersistent(x.Id))
                                            .Select(x => x.Id)
                                            .ToArray();

            if (!persistentPeerIds.Any())
                return;

            var persistMessageCommand = new PersistMessageCommand(message, persistentPeerIds);
            EnqueueOrSendToPersistenceService(persistMessageCommand);
        }
Exemplo n.º 17
0
 public static OutboundSocketAction Send(TransportMessage message, IEnumerable <Peer> peers, SendContext context)
 {
     return(new OutboundSocketAction(message, peers, context));
 }
Exemplo n.º 18
0
 private OutboundSocketAction(TransportMessage message, IEnumerable <Peer> targets, SendContext context)
 {
     Message = message;
     Targets = targets as List <Peer> ?? targets.ToList();
     Context = context;
 }
Exemplo n.º 19
0
 public void AckMessage(TransportMessage transportMessage)
 {
 }
Exemplo n.º 20
0
 private OutboundSocketAction(TransportMessage message, IEnumerable <Peer> peers)
 {
     Message = message;
     Peers   = peers;
 }
Exemplo n.º 21
0
 public void AckMessage(TransportMessage transportMessage)
 {
     _ackedMessages.Add(transportMessage);
 }
Exemplo n.º 22
0
 public TransportMessageSent(TransportMessage transportMessage, params Peer[] targets)
 {
     TransportMessage = transportMessage;
     Targets = targets;
 }
Exemplo n.º 23
0
 public void Send(TransportMessage message, IEnumerable <Peer> peers)
 {
     Send(message, peers, new SendContext());
 }
Exemplo n.º 24
0
        private void OnTransportMessageReceived(TransportMessage transportMessage)
        {
            if (_replayMessageTypeIds.Contains(transportMessage.MessageTypeId))
            {
                var replayEvent = (IReplayEvent)_serializer.ToMessage(transportMessage);
                if (replayEvent.ReplayId == _currentReplayId)
                    _phase.OnReplayEvent(replayEvent);

                return;
            }

            if (transportMessage.MessageTypeId == MessageTypeId.PersistenceStopping)
            {
                _persistenceIsDown = true;

                var ackMessage = _innerTransport.CreateInfrastructureTransportMessage(MessageTypeId.PersistenceStoppingAck);

                _logger.InfoFormat("Sending PersistenceStoppingAck to {0}", transportMessage.Originator.SenderId);
                _innerTransport.Send(ackMessage, new[] { new Peer(transportMessage.Originator.SenderId, transportMessage.Originator.SenderEndPoint) });

                return;
            }

            if (transportMessage.MessageTypeId.IsInfrastructure())
                TriggerMessageReceived(transportMessage);
            else
                _phase.OnRealTimeMessage(transportMessage);
        }
Exemplo n.º 25
0
            public override void ProcessPendingReceive(TransportMessage transportMessage)
            {
                if (Transport._receivedMessagesIds.ContainsKey(transportMessage.Id))
                    return;

                try
                {
                    Transport.TriggerMessageReceived(transportMessage);
                }
                catch (Exception exception)
                {
                    var errorMessage = string.Format("Unable to handle message {0} during SafetyPhase.", transportMessage.MessageTypeId.FullName);
                    _logger.Error(errorMessage, exception);
                }
                Transport._receivedMessagesIds.TryAdd(transportMessage.Id, true);
            }
Exemplo n.º 26
0
        public void AckMessage(TransportMessage transportMessage)
        {
            if (transportMessage.ForcePersistenceAck || (_isPersistent && transportMessage.MessageTypeId.IsPersistent()))
            {
                _logger.DebugFormat("PERSIST ACK: {0} {1}", transportMessage.MessageTypeId, transportMessage.Id);

                EnqueueOrSendToPersistenceService(new MessageHandled(transportMessage.Id));
            }
        }
Exemplo n.º 27
0
 public override void OnRealTimeMessage(TransportMessage transportMessage)
 {
     Transport._pendingReceives.Add(transportMessage);
 }
Exemplo n.º 28
0
 public override void OnRealTimeMessage(TransportMessage transportMessage)
 {
     Transport.TriggerMessageReceived(transportMessage);
 }
Exemplo n.º 29
0
 public virtual void ProcessPendingReceive(TransportMessage transportMessage)
 {
     Transport._logger.ErrorFormat("DISCARDING MESSAGE {0} {1}", transportMessage.MessageTypeId, transportMessage.Id);
 }
Exemplo n.º 30
0
 public void RaiseMessageReceived(TransportMessage transportMessage)
 {
     MessageReceived(transportMessage);
 }
Exemplo n.º 31
0
        public void Send(TransportMessage message, IEnumerable <Peer> peerIds)
        {
            message.Environment = _environment;

            _outboundSocketActions.Add(OutboundSocketAction.Send(message, peerIds.ToList()));
        }