Esempio n. 1
0
        public async Task TestCodecToStreamingCodecConfiguration()
        {
            var config = CodecToStreamingCodecConfiguration <int> .Conf
                         .Set(CodecToStreamingCodecConfiguration <int> .Codec, GenericType <IntCodec> .Class)
                         .Build();

            IStreamingCodec <PipelineMessage <int> > streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance <IStreamingCodec <PipelineMessage <int> > >();

            CancellationToken token = new CancellationToken();

            int obj = 5;
            PipelineMessage <int> message = new PipelineMessage <int>(obj, true);
            var         stream            = new MemoryStream();
            IDataWriter writer            = new StreamDataWriter(stream);

            streamingCodec.Write(message, writer);
            PipelineMessage <int> message1 = new PipelineMessage <int>(obj + 1, false);
            await streamingCodec.WriteAsync(message1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            var         res1   = streamingCodec.Read(reader);
            var         res2   = await streamingCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1.Data);
            Assert.Equal(obj + 1, res2.Data);
            Assert.Equal(true, res1.IsLast);
            Assert.Equal(false, res2.IsLast);
        }
Esempio n. 2
0
        public async Task TestCodecToStreamingCodec()
        {
            var config = TangFactory.GetTang().NewConfigurationBuilder()
                         .BindImplementation(GenericType <ICodec <int> > .Class, GenericType <IntCodec> .Class)
                         .BindImplementation(GenericType <IStreamingCodec <int> > .Class,
                                             GenericType <CodecToStreamingCodec <int> > .Class)
                         .Build();

            IStreamingCodec <int> streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance <IStreamingCodec <int> >();

            CancellationToken token = new CancellationToken();

            int         obj    = 5;
            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            streamingCodec.Write(obj, writer);
            await streamingCodec.WriteAsync(obj + 1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int         res1   = streamingCodec.Read(reader);
            int         res2   = await streamingCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1);
            Assert.Equal(obj + 1, res2);
        }
Esempio n. 3
0
        /// <summary>
        /// Writes the value to this link asynchronously
        /// </summary>
        /// <param name="value">The data to write</param>
        /// <param name="token">The cancellation token</param>
        public async Task WriteAsync(T value, CancellationToken token)
        {
            if (_disposed)
            {
                Exceptions.Throw(new IllegalStateException("StreamingLink has been closed."), Logger);
            }

            await _streamingCodec.WriteAsync(value, _writer, token);
        }
Esempio n. 4
0
        /// <summary>
        /// Writes the class fields.
        /// </summary>
        /// <param name="obj">The message to write</param>
        /// <param name="writer">The writer to which to write</param>
        /// <param name="token">The cancellation token</param>
        public async System.Threading.Tasks.Task WriteAsync(GroupCommunicationMessage <T> obj, IDataWriter writer, CancellationToken token)
        {
            byte[] encodedMetadata = GenerateMetaDataEncoding(obj);
            byte[] encodedInt      = BitConverter.GetBytes(encodedMetadata.Length);
            byte[] totalEncoding   = encodedInt.Concat(encodedMetadata).ToArray();
            await writer.WriteAsync(totalEncoding, 0, totalEncoding.Length, token);

            foreach (var data in obj.Data)
            {
                await _codec.WriteAsync(data, writer, token);
            }
        }
        /// <summary>
        /// Writes message asynchronously to the writer
        /// </summary>
        /// <param name="obj">Message to write</param>
        /// <param name="writer">Writer used to write the message</param>
        /// <param name="token">Cancellation token</param>
        async Task IStreamingCodec <MapInputWithControlMessage <TMapInput> > .WriteAsync(
            MapInputWithControlMessage <TMapInput> obj, IDataWriter writer, CancellationToken token)
        {
            switch (obj.ControlMessage)
            {
            case MapControlMessage.AnotherRound:
                await writer.WriteAsync(new byte[] { 0 }, 0, 1, token);

                await _baseCodec.WriteAsync(obj.Message, writer, token);

                break;

            case MapControlMessage.Stop:
                writer.Write(new byte[] { 1 }, 0, 1);
                break;
            }
        }
        /// <summary>
        /// Writes the class fields.
        /// </summary>
        /// <param name="writer">The writer to which to write</param>
        /// <param name="token">The cancellation token</param>
        public override async System.Threading.Tasks.Task WriteAsync(IDataWriter writer, CancellationToken token)
        {
            await writer.WriteStringAsync(GroupName, token);

            await writer.WriteStringAsync(OperatorName, token);

            await writer.WriteStringAsync(Source, token);

            await writer.WriteStringAsync(Destination, token);

            await writer.WriteInt32Async(Data.Length, token);

            await writer.WriteStringAsync(MsgType.ToString(), token);

            foreach (var data in Data)
            {
                await _codec.WriteAsync(data, writer, token);
            }
        }
Esempio n. 7
0
 /// <summary>
 /// Writes the class fields.
 /// </summary>
 /// <param name="value">The remote event</param>
 /// <param name="writer">The writer to which to write</param>
 /// <param name="token">The cancellation token</param>
 public async Task WriteAsync(IRemoteEvent <T> value, IDataWriter writer, CancellationToken token)
 {
     await _codec.WriteAsync(value.Value, writer, token);
 }
Esempio n. 8
0
        public async Task TestCommonStreamingCodecs()
        {
            IInjector                injector    = TangFactory.GetTang().NewInjector();
            IStreamingCodec <int>    intCodec    = injector.GetInstance <IntStreamingCodec>();
            IStreamingCodec <double> doubleCodec = injector.GetInstance <DoubleStreamingCodec>();
            IStreamingCodec <float>  floatCodec  = injector.GetInstance <FloatStreamingCodec>();

            IStreamingCodec <int[]>    intArrCodec    = injector.GetInstance <IntArrayStreamingCodec>();
            IStreamingCodec <double[]> doubleArrCodec = injector.GetInstance <DoubleArrayStreamingCodec>();
            IStreamingCodec <float[]>  floatArrCodec  = injector.GetInstance <FloatArrayStreamingCodec>();

            IStreamingCodec <string> stringCodec = injector.GetInstance <StringStreamingCodec>();

            CancellationToken token = new CancellationToken();

            int obj = 5;

            int[]    intArr    = { 1, 2 };
            double[] doubleArr = { 1, 2 };
            float[]  floatArr  = { 1, 2 };
            string   stringObj = "hello";

            var         stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);

            intCodec.Write(obj, writer);
            await intCodec.WriteAsync(obj + 1, writer, token);

            doubleCodec.Write(obj + 2, writer);
            await doubleCodec.WriteAsync(obj + 3, writer, token);

            floatCodec.Write(obj + 4, writer);
            await floatCodec.WriteAsync(obj + 5, writer, token);

            intArrCodec.Write(intArr, writer);
            await intArrCodec.WriteAsync(intArr, writer, token);

            doubleArrCodec.Write(doubleArr, writer);
            await doubleArrCodec.WriteAsync(doubleArr, writer, token);

            floatArrCodec.Write(floatArr, writer);
            await floatArrCodec.WriteAsync(floatArr, writer, token);

            stringCodec.Write(stringObj, writer);
            await stringCodec.WriteAsync(stringObj, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int         res1   = intCodec.Read(reader);
            int         res2   = await intCodec.ReadAsync(reader, token);

            double res3 = doubleCodec.Read(reader);
            double res4 = await doubleCodec.ReadAsync(reader, token);

            float res5 = floatCodec.Read(reader);
            float res6 = await floatCodec.ReadAsync(reader, token);

            int[] resArr1 = intArrCodec.Read(reader);
            int[] resArr2 = await intArrCodec.ReadAsync(reader, token);

            double[] resArr3 = doubleArrCodec.Read(reader);
            double[] resArr4 = await doubleArrCodec.ReadAsync(reader, token);

            float[] resArr5 = floatArrCodec.Read(reader);
            float[] resArr6 = await floatArrCodec.ReadAsync(reader, token);

            string resArr7 = stringCodec.Read(reader);
            string resArr8 = await stringCodec.ReadAsync(reader, token);

            Assert.Equal(obj, res1);
            Assert.Equal(obj + 1, res2);
            Assert.Equal(obj + 2, res3);
            Assert.Equal(obj + 3, res4);
            Assert.Equal(obj + 4, res5);
            Assert.Equal(obj + 5, res6);
            Assert.Equal(stringObj, resArr7);
            Assert.Equal(stringObj, resArr8);

            for (int i = 0; i < intArr.Length; i++)
            {
                Assert.Equal(intArr[i], resArr1[i]);
                Assert.Equal(intArr[i], resArr2[i]);
            }

            for (int i = 0; i < doubleArr.Length; i++)
            {
                Assert.Equal(doubleArr[i], resArr3[i]);
                Assert.Equal(doubleArr[i], resArr4[i]);
            }

            for (int i = 0; i < floatArr.Length; i++)
            {
                Assert.Equal(floatArr[i], resArr5[i]);
                Assert.Equal(floatArr[i], resArr6[i]);
            }
        }