Exemplo n.º 1
0
        public void Publish_GateWayRepository_GetMessageSenders()
        {
            var message = new MessageBus
                              {
                                  Body = "A",
                                  Header = new MessageHeader
                                               {
                                                   BodyType = "B"
                                               }
                              };

            _subject.Publish(message);

            _mockGateWaysRepository.Verify(x => x.GetMessageSenders(It.IsAny<string>()));
        }
Exemplo n.º 2
0
        public void SerializeSpanishLettersMessageBus()
        {
            var jsonDataContractSerializer = new JsonDataContractSerializer();

            var messageBus = new MessageBus
            {
                Body = "El señor de l'ametlla del vallès está de vacaciones",
                Header = { BodyType = "System.string", CreatedAt = new DateTime(2010, 10, 10) }
            };

            var serialize = jsonDataContractSerializer.Serialize(messageBus);

            var deserialized = jsonDataContractSerializer.Deserialize(serialize, Encoding.UTF8, typeof(MessageBus)) as MessageBus;

            Assert.AreEqual(messageBus.Body, deserialized.Body);
            Assert.AreEqual(messageBus.Header, deserialized.Header);
        }
Exemplo n.º 3
0
        public void SerializeMessageBus()
        {
            var jsonDataContractSerializer = new JsonDataContractSerializer();
            var callerContexts = new Stack<CallerContext>();
            callerContexts.Push(new CallerContext { Identification = new Identification() { Id = "IdTest", Type = "TypeTest" } });
            var messageBus = new MessageBus
                                 {
                                     Body = "Test",
                                     Header = { BodyType = "Un tipo de body cualquiera me vale.", CreatedAt = new DateTime(2010, 10, 10), CallStack = callerContexts }
                                 };

            var serialize = jsonDataContractSerializer.Serialize(messageBus);

            var bytes = MessageBusParser.ToBytes(messageBus);
            var routerHeader = MessageBusParser.GetHeaderFromBytes(bytes);
            var message = MessageBusParser.GetMessageFromBytes(bytes);

            Assert.AreEqual(serialize, message);
            Assert.AreEqual("IdTest", routerHeader.Identification.Id);
            Assert.AreEqual("TypeTest", routerHeader.Identification.Type);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates the specified identification.
        /// </summary>
        /// <param name="identification">The identification.</param>
        /// <param name="message">The message.</param>
        /// <param name="dataContractSerializer">The data contract serializer.</param>
        /// <returns></returns>
        public static MessageBus Create(Identification identification, IMessage message, IDataContractSerializer dataContractSerializer)
        {
            var messageBus = new MessageBus
                                 {
                                     Header =
                                         {
                                             CreatedAt = DateTime.UtcNow,
                                             IdentificationService =
                                                 {
                                                     Id = identification.Id,
                                                     Type = identification.Type
                                                 },
                                             BodyType = GetKey(message.GetType()),
                                             Type = MessageBusType.Generic,
                                             EncodingCodePage = dataContractSerializer.Encoding.CodePage
                                         },
                                     Body = dataContractSerializer.Serialize(message)
                                 };

            return messageBus;
        }
Exemplo n.º 5
0
        public void Publish_GateWayRepository_Send()
        {
            var mockOutput = new Mock<IOutputGateway<byte[]>>();
            var mockEndPoint = new Mock<IEndPoint>();
            mockEndPoint.SetupGet(point => point.Uri).Returns(new Uri("http://localhost"));
            mockOutput.SetupGet(gateway => gateway.EndPoint).Returns(mockEndPoint.Object);

            _mockGateWaysRepository.Setup(x => x.GetMessageSenders(It.IsAny<string>())).Returns(new[] {mockOutput.Object});

            var message = new MessageBus
            {
                Body = "A",
                Header = new MessageHeader
                {
                    BodyType = "B"
                }
            };

            _subject.Publish(message);

            mockOutput.Verify(x => x.Send(It.IsAny<byte[]>(), It.IsAny<int>()));
        }
Exemplo n.º 6
0
 /// <summary>
 ///     Sends the specified message bus.
 /// </summary>
 /// <param name="messageBus">The message bus.</param>
 public void Publish(MessageBus messageBus)
 {
     byte[] serializedMessage = Encoding.UTF8.GetBytes(_dataContractSerializer.Serialize(messageBus));
     //TODO: Crear la serializacion del message bus
     Publish(messageBus.Header.BodyType, messageBus.Header.Priority, serializedMessage);
 }
Exemplo n.º 7
0
 /// <summary>
 ///     Called when [on router error].
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="exception">The exception.</param>
 private void OnOnRouterError(MessageBus message, Exception exception)
 {
     ErrorOnRouterEventHandler handler = OnRouterError;
     if (handler != null)
     {
         var thread = new Thread(() => handler(this, new ErrorOnRouterEventHandlerArgs { Message = message, Exception = exception }));
         thread.Start();
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Equalses the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns></returns>
 public bool Equals(MessageBus other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.Header, Header) && Equals(other.Body, Body);
 }
Exemplo n.º 9
0
        public static byte[] ToBytes(MessageBus messageBus)
        {
            var serializedMessage = Serializer.Serialize(messageBus);

            var bytesMessage = GzipCompression.Zip(serializedMessage);
            var bytesBodyType = Encoding.UTF8.GetBytes(messageBus.Header.BodyType);

            CallerContext callerContext = null;
            if (messageBus.Header.CallStack.Count > 0)
                callerContext = messageBus.Header.CallStack.Peek();
            byte[] idBytes = new byte[0];
            byte[] typeBytes = new byte[0];
            int bufferLength = 0;
            if (callerContext != null)
            {
                idBytes = Encoding.UTF8.GetBytes(callerContext.Identification.Id);
                typeBytes = Encoding.UTF8.GetBytes(callerContext.Identification.Type);
                bufferLength = 15 + bytesBodyType.Length + 4 + bytesMessage.Length + 4 + idBytes.Length + 4 +
                               typeBytes.Length;
            }
            else
            {
                bufferLength = 15 + bytesBodyType.Length + 4 + bytesMessage.Length + 4;
            }

            var bytes = new byte[bufferLength];
            bytes[0] = (byte)messageBus.Header.Type;
            bytes[1] = (byte)messageBus.Header.Priority;

            var ticks = messageBus.Header.CreatedAt.Ticks;
            bytes[3] = (byte)(ticks);
            bytes[4] = (byte)(ticks >> 8);
            bytes[5] = (byte)(ticks >> 16);
            bytes[6] = (byte)(ticks >> 24);
            bytes[7] = (byte)(ticks >> 32);
            bytes[8] = (byte)(ticks >> 40);
            bytes[9] = (byte)(ticks >> 48);
            bytes[10] = (byte)(ticks >> 56);

            bytes[11] = (byte)(bytesBodyType.Length);
            bytes[12] = (byte)(bytesBodyType.Length >> 8);
            bytes[13] = (byte)(bytesBodyType.Length >> 16);
            bytes[14] = (byte)(bytesBodyType.Length >> 24);

            Array.Copy(bytesBodyType, 0, bytes, 15, bytesBodyType.Length);

            var messagePosition = 15 + bytesBodyType.Length;

            bytes[messagePosition] = (byte)(bytesMessage.Length);
            bytes[messagePosition + 1] = (byte)(bytesMessage.Length >> 8);
            bytes[messagePosition + 2] = (byte)(bytesMessage.Length >> 16);
            bytes[messagePosition + 3] = (byte)(bytesMessage.Length >> 24);

            Array.Copy(bytesMessage, 0, bytes, messagePosition + 4, bytesMessage.Length);

            var idPosition = messagePosition + 4 + bytesMessage.Length;
            bytes[idPosition] = (byte)(idBytes.Length);
            bytes[idPosition + 1] = (byte)(idBytes.Length >> 8);
            bytes[idPosition + 2] = (byte)(idBytes.Length >> 16);
            bytes[idPosition + 3] = (byte)(idBytes.Length >> 24);

            if (callerContext != null)
            {
                Array.Copy(idBytes, 0, bytes, idPosition + 4, idBytes.Length);

                var typePosition = idPosition + 4 + idBytes.Length;
                bytes[typePosition] = (byte)(typeBytes.Length);
                bytes[typePosition + 1] = (byte)(typeBytes.Length >> 8);
                bytes[typePosition + 2] = (byte)(typeBytes.Length >> 16);
                bytes[typePosition + 3] = (byte)(typeBytes.Length >> 24);

                Array.Copy(typeBytes, 0, bytes, typePosition + 4, typeBytes.Length);
            }
            return bytes;
        }