public static string GetCategoryNames(ImmutableMessage messages)
        {
            var item = messages.Content;

            if (item is IFunctionalCommand || item is IFunctionalEvent || item is InstanceStarted)
            {
                return("system");
            }

            var @event = item as IEvent <IIdentity>;

            if (@event != null)
            {
                return(@event.Id.ToString());
            }

            var command = item as ICommand <IIdentity>;

            if (command != null)
            {
                return(command.Id.ToString());
            }


            throw new InvalidOperationException("Unknown envelope category name.");
        }
Example #2
0
        public MessageBuilder AddItem(ImmutableMessage message)
        {
            var item = new MessageBuilder(message.MappedType, message.Content);

            foreach (var attribute in message.GetAllAttributes())
            {
                item.AddAttribute(attribute.Key, attribute.Value);
            }
            Items.Add(item);
            return(item);
        }
Example #3
0
        public ImmutableEnvelope Build()
        {
            var attributes = _attributes.Select(p => new ImmutableAttribute(p.Key, p.Value)).ToArray();
            var items      = new ImmutableMessage[Items.Count];

            for (int i = 0; i < items.Length; i++)
            {
                var save    = Items[i];
                var attribs = save.Attributes.Select(p => new ImmutableAttribute(p.Key, p.Value)).ToArray();
                items[i] = new ImmutableMessage(save.MappedType, save.Content, attribs, i);
            }

            return(new ImmutableEnvelope(EnvelopeId, attributes, items, _deliverOnUtc, _createdOnUtc));
        }
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            var header = EnvelopeHeaderContract.ReadHeader(buffer);


            if (header.MessageFormatVersion != EnvelopeHeaderContract.Schema2DataFormat)
            {
                throw new InvalidOperationException("Unexpected message format");
            }


            EnvelopeContract envelope;

            using (var stream = new MemoryStream(buffer, EnvelopeHeaderContract.FixedSize, (int)header.EnvelopeBytes))
            {
                envelope = _envelopeSerializer.DeserializeEnvelope(stream);
            }

            var items = new ImmutableMessage[envelope.Messages.Length];

            for (var i = 0; i < items.Length; i++)
            {
                var  itemContract = envelope.Messages[i];
                var  attributes   = EnvelopeConvert.ItemAttributesFromContract(itemContract.Attributes);
                Type contractType;

                var itemPosition = EnvelopeHeaderContract.FixedSize + (int)header.EnvelopeBytes + (int)itemContract.ContentPosition;
                var itemSize     = (int)itemContract.ContentSize;
                if (_dataSerializer.TryGetContractTypeByName(itemContract.ContractName, out contractType))
                {
                    using (var stream = new MemoryStream(buffer, itemPosition, itemSize))
                    {
                        var instance = _dataSerializer.Deserialize(stream, contractType);

                        items[i] = new ImmutableMessage(contractType, instance, attributes, i);
                    }
                }
                else
                {
                    // we can't deserialize. Keep it as buffer
                    var bufferInstance = new byte[itemContract.ContentSize];
                    Buffer.BlockCopy(buffer, itemPosition, bufferInstance, 0, itemSize);
                    items[i] = new ImmutableMessage(null, bufferInstance, attributes, i);
                }
            }

            var envelopeAttributes = EnvelopeConvert.EnvelopeAttributesFromContract(envelope.EnvelopeAttributes);

            return(new ImmutableEnvelope(envelope.EnvelopeId, envelopeAttributes, items, envelope.DeliverOnUtc, envelope.CreatedOnUtc));
        }
        public static MyMessageContext Factory(ImmutableEnvelope envelope, ImmutableMessage message)
        {
            ImmutableAttribute address =
                envelope.GetAllAttributes().Where(a => a.Key == ContextAttributes.ORIGINATING_MACHINE).FirstOrDefault();

            return(new MyMessageContext
            {
                OriginatingMachine = address == null ? "" : address.Value,
                CreatedOnUtc = envelope.CreatedOnUtc,
                DeliveredOnUtc = envelope.DeliverOnUtc,
                EnvelopeId = envelope.EnvelopeId,
                MessageIndex = message.Index,
                MappedType = message.MappedType.AssemblyQualifiedName
            });
        }
Example #6
0
        public void Deserialize_restores_immutable_message_correctly()
        {
            // Arrange
            var sut = new JsonMessageSerializer();
            ImmutableMessage message    = fixture.Create <ImmutableMessage>();
            string           serialized = sut.Serialize(message);

            TestContext.WriteLine(serialized);

            // Act
            object actual = sut.Deserialize(serialized);

            // Assert
            actual.Should().BeOfType <ImmutableMessage>();
            actual.Should().BeEquivalentTo(message);
        }
        public static Color GetBackgroundColorForContract(ImmutableMessage message)
        {
            var commad = message.Content as ISampleCommand;

            if (commad != null)
            {
                return CommonColors.Green;
            }

            var @event = message.Content as ISampleEvent;

            if (@event != null)
            {
                return CommonColors.Blue;
            }
            return CommonColors.Gray;
        }
        public static Color GetBackgroundColorForContract(ImmutableMessage message)
        {
            var commad = message.Content as IRecipeCommand;

            if (commad != null)
            {
                return(CommonColors.Green);
            }

            var @event = message.Content as IRecipeEvent;

            if (@event != null)
            {
                return(CommonColors.Blue);
            }
            return(CommonColors.Gray);
        }
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            var header = EnvelopeHeaderContract.ReadHeader(buffer);

            if (header.MessageFormatVersion != EnvelopeHeaderContract.Schema2DataFormat)
                throw new InvalidOperationException("Unexpected message format");

            EnvelopeContract envelope;
            using (var stream = new MemoryStream(buffer, EnvelopeHeaderContract.FixedSize, (int) header.EnvelopeBytes))
            {
                envelope = _envelopeSerializer.DeserializeEnvelope(stream);
            }

            var items = new ImmutableMessage[envelope.Messages.Length];

            for (var i = 0; i < items.Length; i++)
            {
                var itemContract = envelope.Messages[i];
                var attributes = EnvelopeConvert.ItemAttributesFromContract(itemContract.Attributes);
                Type contractType;

                var itemPosition = EnvelopeHeaderContract.FixedSize + (int)header.EnvelopeBytes + (int)itemContract.ContentPosition;
                var itemSize = (int)itemContract.ContentSize;
                if (_dataSerializer.TryGetContractTypeByName(itemContract.ContractName, out contractType))
                {
                    using (var stream = new MemoryStream(buffer, itemPosition, itemSize))
                    {
                        var instance = _dataSerializer.Deserialize(stream, contractType);

                        items[i] = new ImmutableMessage(contractType, instance, attributes, i);
                    }
                }
                else
                {
                    // we can't deserialize. Keep it as buffer
                    var bufferInstance = new byte[itemContract.ContentSize];
                    Buffer.BlockCopy(buffer, itemPosition, bufferInstance, 0, itemSize);
                    items[i] = new ImmutableMessage(null, bufferInstance, attributes, i);
                }
            }

            var envelopeAttributes = EnvelopeConvert.EnvelopeAttributesFromContract(envelope.EnvelopeAttributes);
            return new ImmutableEnvelope(envelope.EnvelopeId, envelopeAttributes, items, envelope.DeliverOnUtc, envelope.CreatedOnUtc);
        }
Example #10
0
 public void Dispatch(Type consumerType, ImmutableEnvelope envelop, ImmutableMessage message)
 {
     using (var inner = _envelopeScope.BeginLifetimeScope(DispatchLifetimeScopeTags.MessageItemScopeTag))
     {
         var instance = inner.Resolve(consumerType);
         var consume  = _hint(consumerType, message.MappedType);
         try
         {
             _context.SetContext(envelop, message);
             consume.Invoke(instance, new[] { message.Content });
         }
         catch (TargetInvocationException e)
         {
             throw InvocationUtil.Inner(e);
         }
         finally
         {
             _context.ClearContext();
         }
     }
 }
 public void Dispatch(Type consumerType, ImmutableEnvelope envelop, ImmutableMessage message)
 {
     using (var inner = _envelopeScope.BeginLifetimeScope(DispatchLifetimeScopeTags.MessageItemScopeTag))
     {
         var instance = inner.Resolve(consumerType);
         var consume = _hint(consumerType, message.MappedType);
         try
         {
             _context.SetContext(envelop, message);
             consume.Invoke(instance, new[] { message.Content });
         }
         catch (TargetInvocationException e)
         {
             throw InvocationUtil.Inner(e);
         }
         finally
         {
             _context.ClearContext();
         }
     }
 }
        public static Color GetBackgroundColorForContract(ImmutableMessage message)
        {
            var commad = message.Content as ICommand;

            if (commad != null)
            {
                return(CommonColors.Green);
            }

            var @event = message.Content as IEvent;

            if (@event != null)
            {
                //if (@event is PollingApiForForecastsTimedOut)
                //{
                //    return CommonColors.Red;
                //}

                return(CommonColors.Blue);
            }
            return(CommonColors.Gray);
        }
        public static string GetCategoryNames(ImmutableMessage messages)
        {
            var item = messages.Content;

            if (item is IFunctionalCommand || item is IFunctionalEvent || item is InstanceStarted)
                return "system";

            var @event = item as IEvent<IIdentity>;

            if (@event != null)
            {
                return @event.Id.ToString();
            }

            var command = item as ICommand<IIdentity>;

            if (command != null)
            {
                return command.Id.ToString();
            }

            throw new InvalidOperationException("Unknown envelope category name.");
        }
Example #14
0
 public void SetContext(ImmutableEnvelope envelope, ImmutableMessage message)
 {
     _context.Value = _factory(envelope, message);
 }
 private bool Equals(ImmutableMessage other)
 {
     return(String.Equals(Name, (string)other.Name) && Age == other.Age);
 }