Пример #1
0
        private void Ingress(IStreamObserver <TKey, TPayload> observer)
        {
            bool done     = false;
            int  messages = 0;

            try
            {
                var serializer = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(new SerializerSettings());
                while (!done)
                {
                    var message = serializer.Deserialize(this.stream);
                    if (message.Kind == MessageKind.Completed)
                    {
                        done = true;
                    }
                    observer.OnNext(message.Message);
                    messages++;
                    if (this.numMessages != 0 && messages == this.numMessages)
                    {
                        if (message.Kind != MessageKind.Completed)
                        {
                            observer.OnCompleted();
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                observer.OnError(e);
            }
        }
Пример #2
0
        public BinaryIngressStreamable(Stream binaryStream, int numMessages, IIngressScheduler scheduler, StreamProperties <TKey, TPayload> inputProperties, bool readPropertiesFromStream, QueryContainer container, string identifier)
            : base((inputProperties ?? StreamProperties <TKey, TPayload> .Default).SetQueryContainer(container))
        {
            if (readPropertiesFromStream)
            {
                var propSer = StreamableSerializer.Create <SerializedProperties>();
                var props   = propSer.Deserialize(binaryStream);
                this.properties = props.ToStreamProperties <TKey, TPayload>();
            }

            this.stream = binaryStream;
            if (this.stream.CanSeek)
            {
                this.streamStartPosition = binaryStream.Position;
            }
            this.numMessages           = numMessages;
            this.scheduler             = scheduler;
            this.container             = container;
            this.IngressSiteIdentifier = identifier ?? Guid.NewGuid().ToString();
            this.delayed = container != null;
            if (this.delayed)
            {
                container.RegisterIngressSite(identifier);
            }
        }
Пример #3
0
        private void Ingress(IStreamObserver <TKey, TPayload> observer)
        {
            int messages = 0;

            try
            {
                var serializer = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(
                    new SerializerSettings()
                {
                    KnownTypes = StreamMessageManager.GeneratedTypes()
                });
                while (true)
                {
                    var message = serializer.Deserialize(this.stream);
                    if (message.Kind != MessageKind.Completed)
                    {
                        observer.OnNext(message.Message);
                        messages++;
                    }
                    if (message.Kind == MessageKind.Completed ||
                        (this.numMessages != 0 && messages == this.numMessages))
                    {
                        observer.OnCompleted();
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                observer.OnError(e);
            }

            this.onSubscriptionCompleted();
        }
Пример #4
0
 public BinaryStreamObserver(StreamProperties <TKey, TPayload> streamProperties, Stream stream)
 {
     this.serializer = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(
         new SerializerSettings()
     {
         KnownTypes = StreamMessageManager.GeneratedTypes()
     });
     this.stream = stream;
 }
Пример #5
0
 /// <summary>
 /// Serialize streamable into a binary stream
 /// </summary>
 /// <typeparam name="TKey"></typeparam>
 /// <typeparam name="TPayload"></typeparam>
 /// <param name="container">The query container to which an egress point is being added.</param>
 /// <param name="identifier">A string that can uniquely identify the point of egress in the query.</param>
 /// <param name="input"></param>
 /// <param name="binaryStream"></param>
 /// <param name="writePropertiesToStream"></param>
 public static void RegisterBinaryOutput <TKey, TPayload>(this QueryContainer container, IStreamable <TKey, TPayload> input, Stream binaryStream, bool writePropertiesToStream = false, string identifier = null)
 {
     if (writePropertiesToStream)
     {
         var propSer = StreamableSerializer.Create <SerializedProperties>();
         propSer.Serialize(binaryStream, SerializedProperties.FromStreamProperties(input.Properties));
     }
     container.RegisterOutputAsStreamMessages(input, identifier)
     .Subscribe(new BinaryStreamObserver <TKey, TPayload>(input.Properties, binaryStream));
 }
Пример #6
0
 /// <summary>
 /// Serialize streamable into a binary stream
 /// </summary>
 /// <typeparam name="TKey"></typeparam>
 /// <typeparam name="TPayload"></typeparam>
 /// <param name="input"></param>
 /// <param name="binaryStream"></param>
 /// <param name="writePropertiesToStream"></param>
 public static void ToBinaryStream <TKey, TPayload>(this IStreamable <TKey, TPayload> input, Stream binaryStream, bool writePropertiesToStream = false)
 {
     if (writePropertiesToStream)
     {
         var propSer = StreamableSerializer.Create <SerializedProperties>();
         propSer.Serialize(binaryStream, SerializedProperties.FromStreamProperties(input.Properties));
     }
     input.ToStreamMessageObservable()
     .Subscribe(new BinaryStreamObserver <TKey, TPayload>(input.Properties, binaryStream));
 }
Пример #7
0
        // TODO: This appears to be copied code from Binary egress - can we unify?
        public ShardedSerializerObserver(Stream destination, StreamProperties <TKey, TPayload> sourceProps, bool writePropertiesToStream = false)
        {
            this.destination = destination;
            if (writePropertiesToStream)
            {
                var propSer = StreamableSerializer.Create <SerializedProperties>();
                propSer.Serialize(destination, SerializedProperties.FromStreamProperties(sourceProps));
            }

            this.serializer = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(new SerializerSettings());
        }
        private static void TestSerialization <T>(T enumerable)
            where T : IEnumerable <int>
        {
            var serializer = StreamableSerializer.Create <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, enumerable);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                var copy = serializer.Deserialize(stream);

                Assert.IsTrue(enumerable.SequenceEqual(copy));
            }
        }
Пример #9
0
        /// <summary>
        /// Write stream properties to specified .NET stream
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TPayload"></typeparam>
        /// <param name="streamable"></param>
        /// <param name="stream"></param>
        public static void WritePropertiesToStream <TKey, TPayload>(this IShardedStreamable <TKey, TPayload> streamable, Stream stream)
        {
            var properties        = StreamProperties <TKey, TPayload> .Default;
            var shardedStreamable = (ShardedStreamable <TKey, TPayload>)streamable;

            if (shardedStreamable != null)
            {
                if (shardedStreamable.Streamables.Length > 0)
                {
                    properties = shardedStreamable.Streamables[0].Properties;
                }
            }
            var propSer = StreamableSerializer.Create <SerializedProperties>();

            propSer.Serialize(stream, SerializedProperties.FromStreamProperties(properties));
        }
Пример #10
0
        public BinaryIngressStreamablePassive(Stream binaryStream, StreamProperties <TKey, TPayload> inputProperties, bool readPropertiesFromStream, QueryContainer container, string identifier)
            : base((inputProperties ?? StreamProperties <TKey, TPayload> .Default).SetQueryContainer(container))
        {
            if (readPropertiesFromStream)
            {
                var propSer = StreamableSerializer.Create <SerializedProperties>();
                var props   = propSer.Deserialize(binaryStream);
                this.properties = props.ToStreamProperties <TKey, TPayload>();
            }

            this.stream                = binaryStream;
            this.serializer            = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(new SerializerSettings());
            this.container             = container;
            this.IngressSiteIdentifier = identifier ?? Guid.NewGuid().ToString();
            container?.RegisterIngressSite(identifier);
            this.restored = container == null;
        }
Пример #11
0
 public BinaryStreamObserver(StreamProperties <TKey, TPayload> streamProperties, Stream stream)
 {
     this.serializer = StreamableSerializer.Create <QueuedMessage <StreamMessage <TKey, TPayload> > >(new SerializerSettings());
     this.stream     = stream;
 }