public void TestMapInputWithControlMessageCodec()
        {
            float[] baseMessage = { 0, 1 };

            var config = TangFactory.GetTang().NewConfigurationBuilder()
                         .BindImplementation(GenericType <IStreamingCodec <float[]> > .Class,
                                             GenericType <FloatArrayStreamingCodec> .Class)
                         .Build();

            IStreamingCodec <MapInputWithControlMessage <float[]> > codec =
                TangFactory.GetTang().NewInjector(config).GetInstance <MapInputWithControlMessageCodec <float[]> >();

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

            codec.Write(new MapInputWithControlMessage <float[]>(baseMessage, MapControlMessage.AnotherRound), writer);
            codec.Write(new MapInputWithControlMessage <float[]>(MapControlMessage.Stop), writer);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);

            var message1 = codec.Read(reader);
            var message2 = codec.Read(reader);

            Assert.Equal(message1.Message[0], baseMessage[0]);
            Assert.Equal(message1.Message[1], baseMessage[1]);
            Assert.Null(message2.Message);
            Assert.Equal(message1.ControlMessage, MapControlMessage.AnotherRound);
            Assert.Equal(message2.ControlMessage, MapControlMessage.Stop);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Read the class fields.
        /// </summary>
        /// <param name="reader">The reader from which to read </param>
        /// <returns>The Group Communication Message</returns>
        public GroupCommunicationMessage <T> Read(IDataReader reader)
        {
            int metadataSize = reader.ReadInt32();

            byte[] metadata = new byte[metadataSize];
            reader.Read(ref metadata, 0, metadataSize);
            var res = GenerateMetaDataDecoding(metadata);

            string groupName    = res.Item1;
            string operatorName = res.Item2;
            string source       = res.Item3;
            string destination  = res.Item4;
            int    dataCount    = res.Item5;

            if (dataCount == 0)
            {
                throw new Exception("Data Count in Group Communication Message cannot be zero");
            }

            var data = new T[dataCount];

            for (int index = 0; index < dataCount; index++)
            {
                data[index] = _codec.Read(reader);

                if (data[index] == null)
                {
                    throw new Exception("message instance cannot be created from the IDataReader in Group Communication Message");
                }
            }

            return(new GroupCommunicationMessage <T>(groupName, operatorName, source, destination, data));
        }
Exemplo n.º 4
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);
        }
        /// <summary>
        /// Read the class fields.
        /// </summary>
        /// <param name="reader">The reader from which to read </param>
        public override void Read(IDataReader reader)
        {
            GroupName    = reader.ReadString();
            OperatorName = reader.ReadString();
            Source       = reader.ReadString();
            Destination  = reader.ReadString();

            int dataCount = reader.ReadInt32();

            if (dataCount == 0)
            {
                throw new Exception("Data Count in Group COmmunication Message cannot be zero");
            }

            MsgType = (MessageType)Enum.Parse(typeof(MessageType), reader.ReadString());
            Data    = new T[dataCount];

            for (int index = 0; index < dataCount; index++)
            {
                Data[index] = _codec.Read(reader);

                if (Data[index] == null)
                {
                    throw new Exception("message instance cannot be created from the IDataReader in Group Communication Message");
                }
            }
        }
Exemplo n.º 6
0
        internal IList <TResult> Run()
        {
            var results      = new List <TResult>();
            var updateResult = _updateTask.Initialize();

            while (updateResult.IsNotDone)
            {
                if (updateResult.HasResult)
                {
                    results.Add(updateResult.Result);
                }

                Debug.Assert(updateResult.HasMapInput, "MapInput is needed.");
                var mapinput   = updateResult.MapInput;
                var mapOutputs = new List <TMapOutput>();

                foreach (var mapfunc in _mapfunctions)
                {
                    // We create a copy by doing coding and decoding since the map task might
                    // reuse the fields in next iteration and meanwhile update task might update it.
                    using (MemoryStream mapInputStream = new MemoryStream(), mapOutputStream = new MemoryStream())
                    {
                        var mapInputWriter = new StreamDataWriter(mapInputStream);
                        _mapInputCodec.Write(mapinput, mapInputWriter);
                        mapInputStream.Position = 0;
                        var mapInputReader = new StreamDataReader(mapInputStream);
                        var output         = mapfunc.Map(_mapInputCodec.Read(mapInputReader));

                        var mapOutputWriter = new StreamDataWriter(mapOutputStream);
                        _mapOutputCodec.Write(output, mapOutputWriter);
                        mapOutputStream.Position = 0;
                        var mapOutputReader = new StreamDataReader(mapOutputStream);
                        output = _mapOutputCodec.Read(mapOutputReader);

                        mapOutputs.Add(output);
                    }
                }

                var mapOutput = _reduceTask.Reduce(mapOutputs);
                updateResult = _updateTask.Update(mapOutput);
            }

            if (updateResult.HasResult)
            {
                results.Add(updateResult.Result);
            }

            return(results);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Reads the value from the link synchronously
        /// </summary>
        public T Read()
        {
            if (_disposed)
            {
                Exceptions.Throw(new IllegalStateException("Link has been disposed."), Logger);
            }

            try
            {
                T value = _streamingCodec.Read(_reader);
                return(value);
            }
            catch (Exception e)
            {
                Logger.Log(Level.Warning, "In StreamingLink::Read function unable to read the message {0}.", e.GetType());
                throw;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Reads the value from the link synchronously
        /// </summary>
        public T Read()
        {
            if (_disposed)
            {
                Exceptions.Throw(new IllegalStateException("Link has been disposed."), Logger);
            }

            try
            {
                T value = _streamingCodec.Read(_reader);
                return(value);
            }
            catch (Exception e)
            {
                Logger.Log(Level.Warning, "In Read function unable to read the message.");
                Exceptions.CaughtAndThrow(e, Level.Error, Logger);
                throw;
            }
        }
Exemplo n.º 9
0
        public void TestConfigurationBroadcastSpec()
        {
            FlatTopology <int> topology = new FlatTopology <int>("Operator", "Operator", "task1", "driverid",
                                                                 new BroadcastOperatorSpec("Sender", GetDefaultCodecConfig(), GetDefaultDataConverterConfig()));

            topology.AddTask("task1");
            var conf = topology.GetTaskConfiguration("task1");

            IStreamingCodec <int> codec = TangFactory.GetTang().NewInjector(conf).GetInstance <IStreamingCodec <int> >();

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

            codec.Write(3, writer);
            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int         res    = codec.Read(reader);

            Assert.Equal(3, res);
        }
        /// <summary>
        /// Reads message from reader
        /// </summary>
        /// <param name="reader">reader from which to read the message</param>
        /// <returns>Read message</returns>
        MapInputWithControlMessage <TMapInput> IStreamingCodec <MapInputWithControlMessage <TMapInput> > .Read(
            IDataReader reader)
        {
            byte[] messageType = new byte[1];
            reader.Read(ref messageType, 0, 1);
            MapControlMessage controlMessage;

            switch (messageType[0])
            {
            case 0:
                controlMessage = MapControlMessage.AnotherRound;
                TMapInput message = _baseCodec.Read(reader);
                return(new MapInputWithControlMessage <TMapInput>(message, controlMessage));

            case 1:
                controlMessage = MapControlMessage.Stop;
                return(new MapInputWithControlMessage <TMapInput>(controlMessage));
            }

            Exceptions.Throw(new Exception("Control message type not valid in Codec read"), Logger);
            return(null);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Read the class fields.
 /// </summary>
 /// <param name="reader">The reader from which to read </param>
 /// <returns>The remote event</returns>
 public IRemoteEvent <T> Read(IDataReader reader)
 {
     return(new RemoteEvent <T>(null, null, _codec.Read(reader)));
 }
Exemplo n.º 12
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]);
            }
        }