示例#1
0
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            var header = EnvelopeHeaderContract.ReadHeader(buffer);

            if (header.MessageFormatVersion != EnvelopeHeaderContract.Schema2DataFormat)
            {
                throw new InvalidOperationException("Unexpected bytes in enveloper header");
            }


            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))
                {
                    try
                    {
                        // we can deserialize. Convert it to a message
                        using (var stream = new MemoryStream(buffer, itemPosition, itemSize))
                        {
                            var instance = _dataSerializer.Deserialize(stream, contractType);
                            items[i] = new ImmutableMessage(contractType, instance, attributes, i);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Failed to deserialize: " + itemContract.ContractName, ex);
                    }
                }
                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));
        }
示例#2
0
 public ImmutableEnvelope(string envelopeId, ImmutableAttribute[] attributes, ImmutableMessage[] items,
     DateTime deliverOnUtc, DateTime createdOnUtc)
 {
     EnvelopeId = envelopeId;
     DeliverOnUtc = deliverOnUtc;
     _attributes = attributes;
     Items = items;
     CreatedOnUtc = createdOnUtc;
 }
示例#3
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);
        }
示例#4
0
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            var header = EnvelopeHeaderContract.ReadHeader(buffer);

            if (header.MessageFormatVersion != EnvelopeHeaderContract.Schema2DataFormat)
                throw new InvalidOperationException("Unexpected bytes in enveloper header");

            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))
                {
                    try
                    {
                        // we can deserialize. Convert it to a message
                        using (var stream = new MemoryStream(buffer, itemPosition, itemSize))
                        {
                            var instance = _dataSerializer.Deserialize(stream, contractType);
                            items[i] = new ImmutableMessage(contractType, instance, attributes, i);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Failed to deserialize: " + itemContract.ContractName, ex);
                    }
                }
                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);
        }
示例#5
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));
        }
示例#6
0
        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;
        }
示例#7
0
        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.");
        }