Example #1
0
        public void TestGroupWithoutPresenceMap()
        {
            MessageTemplate template = Template(
                "<template>" +
                "  <group name='priceGroup' presence='optional'>" +
                "    <decimal name='price'><delta/></decimal>" +
                "  </group>" +
                "</template>");

            var encodingContext = new Context();
            var decodingContext = new Context();

            encodingContext.RegisterTemplate(1, template);
            decodingContext.RegisterTemplate(1, template);

            const string encodedBits = "11100000 10000001 11111110 10111111";

            var decoder = new FastDecoder(decodingContext,
                                          new MemoryStream(ByteUtil.ConvertBitStringToFastByteArray(encodedBits)));
            Message message = decoder.ReadMessage();

            Assert.AreEqual(0.63, message.GetGroup("priceGroup").GetDouble("price"), 0.01);

            byte[] encoding = template.Encode(message, encodingContext);
            TestUtil.AssertBitVectorEquals(encodedBits, encoding);
        }
Example #2
0
        public void TestPerf()
        {
            using (FileStream stream = File.Open(LongFile, FileMode.Open, FileAccess.Read))
            {
                var decoder = new FastDecoder(_context, new BufferedStream(stream, 65535));

                foreach (Message msg in decoder)
                {
                    var type = (char)msg.GetInt(0);

                    string symbol   = msg.GetString(1);
                    int    t        = msg.GetInt(2);
                    string exch     = msg.GetString(3);
                    var    seqNum   = (uint)msg.GetInt(4);
                    int    v5       = msg.GetInt(5);
                    var    v6       = (uint)msg.GetInt(6);
                    int    dt       = msg.GetInt(7);
                    int    v8       = msg.GetInt(8);
                    var    itemType = (uint)msg.GetInt(9);
                    int    v10      = msg.GetInt(10);

                    if (type == 'Q')
                    {
                        string v11 = msg.GetString(11);
                        int    v12 = msg.GetInt(12);
                        var    v13 = (uint)msg.GetInt(13);
                    }
                }
            }
        }
Example #3
0
 public void SetUp()
 {
     output = new PipedStream();
     input = new PipedStream(output);
     encodingContext = new Context();
     decodingContext = new Context();
     encoder = new FastEncoder(encodingContext);
     decoder = new FastDecoder(decodingContext, input);
 }
 public void SetUp()
 {
     _output          = new PipedStream();
     _input           = new PipedStream(_output);
     _encodingContext = new Context();
     _decodingContext = new Context();
     _encoder         = new FastEncoder(_encodingContext);
     _decoder         = new FastDecoder(_decodingContext, _input);
 }
Example #5
0
        public void TestByteVectorWithLength()
        {
            MessageTemplate template = Template(
                "<template name='template'>"
                + "  <byteVector name='data'><length name='dataLength'/><copy/></byteVector>" +
                "</template>");
            FastDecoder decoder = Decoder("11100000 10000001 10000001 01010101", template);
            Message     message = decoder.ReadMessage();

            Assert.AreEqual(1, message.GetInt("dataLength"));
        }
Example #6
0
        public void TestStringWithLength()
        {
            MessageTemplate template = Template(
                "<template name='template'>" +
                "  <string name='message' charset='unicode'><length name='messageLength'/><copy/></string>" +
                "</template>");
            FastDecoder decoder = Decoder("11100000 10000001 10000010 01010101 10101010", template);
            Message     message = decoder.ReadMessage();

            Assert.AreEqual(2, message.GetInt("messageLength"));
        }
Example #7
0
        public void TestDecode()
        {
            //   --PMAP-- --TID--- ---#1--- --PMAP-- --TID--- ------------#1------------ ------------#3------------
            const string encoding =
                "11000000 10000001 10001111 11000000 10000010 01001001 01000010 11001101 10000000 00000000 11100110";

            var     decoder     = new FastDecoder(_context, BitStream(encoding));
            Message readMessage = decoder.ReadMessage();

            Assert.AreEqual(_message, readMessage);
        }
Example #8
0
        public void TestDecodeEmptyMessage()
        {
            var    messageTemplate = new MessageTemplate("", new Field[0]);
            Stream input           = ByteUtil.CreateByteStream("11000000 11110001");
            var    context         = new Context();

            context.RegisterTemplate(113, messageTemplate);

            Message message = new FastDecoder(context, input).ReadMessage();

            Assert.AreEqual(113, message.GetInt(0));
        }
Example #9
0
        public void TestDecodeSequentialEmptyMessages()
        {
            var    messageTemplate = new MessageTemplate("", new Field[0]);
            Stream input           = ByteUtil.CreateByteStream("11000000 11110001 10000000");
            var    context         = new Context();

            context.RegisterTemplate(113, messageTemplate);

            var        decoder  = new FastDecoder(context, input);
            GroupValue message  = decoder.ReadMessage();
            GroupValue message2 = decoder.ReadMessage();

            Assert.AreEqual(113, message.GetInt(0));
            Assert.AreEqual(113, message2.GetInt(0));
        }
Example #10
0
        public void TestEncodeDecodeNestedSequence()
        {
            var nestedSequence = new Sequence("nested", new Field[] { new Scalar("string", FASTType.ASCII, Operator.COPY, ScalarValue.UNDEFINED, false) }, true);
            var group          = new Group("group", new Field[] { nestedSequence }, true);
            var t       = new MessageTemplate("template", new Field[] { group });
            var message = new Message(t);

            FastEncoder encoder = Encoder(t);

            AssertEquals("11000000 10000001", encoder.Encode(message));

            FastDecoder decoder = Decoder("11000000 10000001", t);

            Assert.AreEqual(message, decoder.ReadMessage());
        }
Example #11
0
        public void TestDecodeMessageWithStringFieldTypesAndAllOperators()
        {
            var template = new MessageTemplate(
                "",
                new Field[]
            {
                new Scalar("1", FastType.Ascii, Operator.Copy, ScalarValue.Undefined, false),
                new Scalar("2", FastType.Ascii, Operator.Delta, ScalarValue.Undefined, false),
                new Scalar("3", FastType.Ascii, Operator.Constant, new StringValue("e"), false),
                /* NON-TRANSFERRABLE */
                new Scalar("4", FastType.Ascii, Operator.Default, new StringValue("long"), false)
            });

            var message = new Message(template);

            message.SetString(1, "on");
            message.SetString(2, "DCB32");
            message.SetString(3, "e");
            message.SetString(4, "long");

            //   --PMAP-- --TID--- --------#1------- ---------------------#2---------------------
            const string msg1 =
                "11100000 11110001 01101111 11101110 10000000 01000100 01000011 01000010 00110011 10110010";

            //                   --PMAP-- ------------#2------------ ---------------------#4---------------------
            const string msg2 = "10010000 10000010 00110001 10110110 01110011 01101000 01101111 01110010 11110100";

            Stream input   = ByteUtil.CreateByteStream(msg1 + ' ' + msg2);
            var    context = new Context();

            context.RegisterTemplate(113, template);

            var decoder = new FastDecoder(context, input);

            Message readMessage = decoder.ReadMessage();

            Assert.AreEqual(message, readMessage);

            message.SetString(2, "DCB16");
            message.SetString(4, "short");

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);
        }
        public void TestMessageTemplateWithNoFieldsThatUsePresenceMapStillEncodesPresenceMap()
        {
            MessageTemplate template = Template(
                "<template>" +
                "  <string name='string'/>" +
                "  <decimal name='decimal'><delta/></decimal>" +
                "</template>");

            const string encoding = "11000000 10000001 11100001 10000000 10000001";

            FastDecoder decoder = Decoder(encoding, template);
            FastEncoder encoder = Encoder(template);

            Message message = decoder.ReadMessage();

            Assert.AreEqual("a", message.GetString("string"));
            Assert.AreEqual(1.0, message.GetDouble("decimal"), 0.1);

            TestUtil.AssertBitVectorEquals(encoding, encoder.Encode(message));
        }
Example #13
0
        public void TestIntegerSizeTooLarge()
        {
            MessageTemplate template = Template(
                "<template>" +
                "  <uInt32 name='price'/>" +
                "</template>");
            FastDecoder decoder = Decoder("11000000 10000001 00111111 01111111 01111111 01111111 11111111", template);

            try
            {
                decoder.ReadMessage();
                Assert.Fail();
            }
            catch (DynErrorException e)
            {
                Assert.AreEqual(DynError.IntOutOfRange, e.Error);
                //Assert.AreEqual("The value 17179869183 is out of range for type uInt32", e.getCause().getCause().getMessage());
                return;
            }
        }
Example #14
0
        public void testDecodeSimpleMessage()
        {
            var template = new MessageTemplate("",
                                               new Field[] {
                new Scalar("1", FASTType.U32, Operator.COPY, ScalarValue.UNDEFINED, false)
            });
            Stream input   = ByteUtil.CreateByteStream("11100000 11110001 10000001");
            var    context = new Context();

            context.RegisterTemplate(113, template);

            var message = new Message(template);

            message.SetInteger(1, 1);

            var        decoder     = new FastDecoder(context, input);
            GroupValue readMessage = decoder.ReadMessage();

            Assert.AreEqual(message, readMessage);
            Assert.AreEqual(readMessage, message);
        }
Example #15
0
        public void TestConversions()
        {
            MessageTemplate template = Template(
                "<template>" +
                "  <string name=\"string\"/>" +
                "  <uInt32 name=\"uint\"/>" +
                "  <int8 name=\"byte\"/>" +
                "  <int16 name=\"short\"/>" +
                "  <int64 name=\"long\"/>" +
                "  <byteVector name=\"bytevector\"/>" +
                "  <decimal name=\"decimal\"/>" +
                "</template>");

            var message = new Message(template);

            message.SetByteVector("string", byt("7f001a"));
            message.SetDecimal("uint", 150.0);
            message.SetString("byte", "4");
            message.SetString("short", "-5");
            message.SetString("long", "1000000000000000000");
            message.SetString("bytevector", "abcd");
            message.SetString("decimal", "2.3");

            FastEncoder encoder = Encoder(template);

            byte[] encoding = encoder.Encode(message);

            FastDecoder decoder        = Decoder(template, encoding);
            Message     decodedMessage = decoder.ReadMessage();

            Assert.AreEqual("7f001a", decodedMessage.GetString("string"));
            Assert.AreEqual(150, decodedMessage.GetInt("uint"));
            Assert.AreEqual(150, decodedMessage.GetShort("uint"));
            Assert.AreEqual(4, decodedMessage.GetByte("byte"));
            Assert.AreEqual(-5, decodedMessage.GetShort("short"));
            Assert.AreEqual(1000000000000000000L, decodedMessage.GetLong("long"));
            Assert.AreEqual("61626364", decodedMessage.GetString("bytevector"));
        }
Example #16
0
        public void TestDecodeMessageWithSignedIntegerFieldTypesAndAllOperators()
        {
            var template = new MessageTemplate(
                "",
                new Field[]
            {
                new Scalar("1", FastType.I32, Operator.Copy, ScalarValue.Undefined, false),
                new Scalar("2", FastType.I32, Operator.Delta, ScalarValue.Undefined, false),
                new Scalar("3", FastType.I32, Operator.Increment, new IntegerValue(10), false),
                new Scalar("4", FastType.I32, Operator.Increment, ScalarValue.Undefined, false),
                new Scalar("5", FastType.I32, Operator.Constant, new IntegerValue(1), false),
                /* NON-TRANSFERRABLE */
                new Scalar("6", FastType.I32, Operator.Default, new IntegerValue(2), false)
            });

            GroupValue message = new Message(template);

            message.SetInteger(1, 109);
            message.SetInteger(2, 29470);
            message.SetInteger(3, 10);
            message.SetInteger(4, 3);
            message.SetInteger(5, 1);
            message.SetInteger(6, 2);

            //                   --PMAP-- --TID--- --------#1------- ------------#2------------ ---#4---
            const string msg1 = "11101000 11110001 00000000 11101101 00000001 01100110 10011110 10000011";

            //                   --PMAP-- ---#2--- ---#6---
            const string msg2 = "10000100 11111111 10000011";

            //                   --PMAP-- --------#1------- --------#2------- ---#4--- ---#6---
            const string msg3 = "10101100 00000000 11100000 00001000 10000111 10000001 10000011";

            Stream input = ByteUtil.CreateByteStream(msg1 + ' ' + msg2 + ' ' +
                                                     msg3);
            var context = new Context();

            context.RegisterTemplate(113, template);

            var decoder = new FastDecoder(context, input);

            Message readMessage = decoder.ReadMessage();

            Assert.AreEqual(message, readMessage);

            message.SetInteger(2, 29469);
            message.SetInteger(3, 11);
            message.SetInteger(4, 4);
            message.SetInteger(6, 3);

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);

            message.SetInteger(1, 96);
            message.SetInteger(2, 30500);
            message.SetInteger(3, 12);
            message.SetInteger(4, 1);

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);
        }
Example #17
0
        protected virtual void umdfPacketAssembler()
        {
            //UmdfPacket packet = null;
            int lastSeqNum = 0;

            UmdfPacket []          chunks         = null;
            int                    receivedChunks = 0;
            Dictionary <int, long> dctSeqNum      = new Dictionary <int, long>();

            //StreamWriter tracewriter = new StreamWriter("d:\\turingoms\\servicos\\mdsnetpuma2\\logs\\Trace-" + this.channelID + "-" +  this.multicastPortPRI + ".txt", true);

            while (bKeepRunning)
            {
                Context    context = null;
                UmdfPacket packet  = null;

                if (qUmdfPacket.TryDequeue(out packet))
                {
                    int totalBytes = packet.data.Length;
                    try
                    {
                        //if (context != null)
                        //{
                        //    ((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer.Close();
                        //    ((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer.Dispose();
                        //    context = null;
                        //}

                        context = new Context();
                        context.TemplateRegistry = registry;
                        //context.TraceEnabled = true;
                        //context.StartTrace();
                        //((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer = tracewriter;

                        if (packet.noChunks > 1)
                        {
                            logger.Debug("pktAss() noChunks: " + packet.noChunks + " currChunk: " + packet.currChunk + " seqNum: " + packet.seqNum + " lastSeqNum:" + lastSeqNum);
                            if (chunks == null || packet.seqNum != lastSeqNum)
                            {
                                logger.Debug("pktAss() Alocando array para [" + packet.noChunks + "] chunks: null ou seqNum!=");
                                chunks         = new UmdfPacket[packet.noChunks];
                                receivedChunks = 0;
                            }

                            // Verifica se ainda esta no mesmo chunk, e se ainda dentro da mesma
                            // rodada do snapshot
                            if (dctSeqNum.ContainsKey(packet.seqNum))
                            {
                                TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dctSeqNum[packet.seqNum]);
                                if (ts.TotalMilliseconds > PACKET_TIME_WINDOW && processorType.Equals(ProcessorType.MarketIncremental))
                                {
                                    logger.Debug("pktAss() REALOCANDO array para [" + packet.noChunks + "] chunks");
                                    chunks         = new UmdfPacket[packet.noChunks];
                                    receivedChunks = 0;
                                }
                                dctSeqNum[packet.seqNum] = DateTime.Now.Ticks;
                            }
                            else
                            {
                                logger.Debug("pktAss() Alocando array para [" + packet.noChunks + "] chunks");
                                dctSeqNum.Add(packet.seqNum, DateTime.Now.Ticks);
                                chunks         = new UmdfPacket[packet.noChunks];
                                receivedChunks = 0;
                            }


                            if ((packet.currChunk - 1) < chunks.Length)
                            {
                                if (chunks[packet.currChunk - 1] == null)
                                {
                                    receivedChunks++;
                                }
                                chunks[packet.currChunk - 1] = packet;
                            }
                            else
                            {
                                logger.Error("pktAss() Puts...[" + (packet.currChunk - 1) + "] <=> [" + chunks.Length + "] fdp furou!!!!");
                                continue;
                            }


                            logger.Debug("pktAss() noChunks: " + packet.noChunks + " currChunk: " + packet.currChunk);
                            lastSeqNum = packet.seqNum;

                            if (receivedChunks < packet.noChunks)
                            {
                                continue;
                            }

                            dctSeqNum.Remove(packet.seqNum);
                            logger.Debug("pktAss() Remontando pacote noChunks=" + packet.noChunks + " received=" + receivedChunks + " length=" + chunks.Length);

                            byte[] reassembled = UmdfUtils.reassemble(chunks);
                            totalBytes       = reassembled.Length;
                            chunks           = null;
                            receivedChunks   = 0;
                            packet.data      = reassembled;
                            packet.msgLength = totalBytes;

                            System.IO.MemoryStream byteIn = new System.IO.MemoryStream(packet.data, 0, packet.msgLength);

                            FastDecoder decoder = new FastDecoder(context, byteIn);

                            Message message = decoder.ReadMessage();

                            bool bsinaliza = queueToProcessor.IsEmpty;
                            queueToProcessor.Enqueue(message);
                            if (bsinaliza)
                            {
                                lock (syncQueueToProcessor)
                                {
                                    Monitor.Pulse(syncQueueToProcessor);
                                }
                            }

                            // testa se debug esta habilitado por conta do writegroup
                            // mais performatico
                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug(UmdfUtils.writeGroup(message));
                            }
                        }
                        else
                        {
                            lastSeqNum = packet.seqNum;
                            System.IO.MemoryStream byteIn = new System.IO.MemoryStream(packet.data, 0, packet.msgLength);

                            FastDecoder decoder = new FastDecoder(context, byteIn);

                            Message message = decoder.ReadMessage();

                            bool bsinaliza = queueToProcessor.IsEmpty;
                            queueToProcessor.Enqueue(message);
                            if (bsinaliza)
                            {
                                lock (syncQueueToProcessor)
                                {
                                    Monitor.Pulse(syncQueueToProcessor);
                                }
                            }

                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug(UmdfUtils.writeGroup(message));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("pktAss() Erro: " + ex.Message, ex);

                        // Bom, com uma exception dessas, acho melhor iniciar o canal, pode ser falha no decoder
                        string msg    = string.Format("Canal [{0}] precisa ser derrubado, pois deu falha grave no PacketAssembler().\r\n\r\n{1}\r\n\r\n{2}", channelID, ex.Message, ex);
                        string titulo = string.Format("Derrubando canal [{0}] por falha em pktAss()", channelID);
                        MDSUtils.EnviarEmail(titulo, msg);
                        bKeepRunning = false;

                        //lastpkt = 0;
                    }

                    continue;
                }

                lock (syncQueueUmdfPacket)
                {
                    Monitor.Wait(syncQueueUmdfPacket, 100);
                }
            }
        }
 public MessageInputStream(Stream inputStream, Context context)
 {
     _inStream = inputStream;
     _context  = context;
     _decoder  = new FastDecoder(context, _inStream);
 }
Example #19
0
        public void testDecodeMessageWithDecimalFieldTypesAndAllOperators()
        {
            var template = new MessageTemplate(
                "",
                new Field[]
            {
                new Scalar("1", FastType.Decimal, Operator.Copy, ScalarValue.Undefined, false),
                new Scalar("2", FastType.Decimal, Operator.Delta, ScalarValue.Undefined, false),
                new Scalar("3", FastType.Decimal, Operator.Delta, ScalarValue.Undefined, true),
                new Scalar("4", FastType.Decimal, Operator.Delta, new DecimalValue(12.3M), false),
                new Scalar("5", FastType.Decimal, Operator.Constant, new DecimalValue(23.4M), false),
                new Scalar("6", FastType.Decimal, Operator.Default, new DecimalValue(24.5M), false)
            });

            GroupValue message;

            string msg1 = "11100000 11110001 11111111 10001100 11111111 10010111 10000000 10000000 01111111 10110010";
            string msg2 = "10110000 11111000 00001101 01110000 00101101 01010110 00111010 00111011 00010000 00000000 10000001 11111001 00001101 01110000 00101101 01010110 00111010 00111011 00001111 01111111 11101011 11111000 00001101 01110000 00101101 01010110 00111010 00111011 00010000 00000000 10000011 11111001 00001101 01110000 00101101 01010110 00111010 00111011 00001111 01111111 11010111 11111000 00001101 01110000 00101101 01010110 00111010 00111011 00010000 00000000 10000110";
            string msg3 = "10110000 11111000 00101001 01010001 00001001 00000011 00101111 00110001 00110000 00000000 10000001 10000000 00011011 01100000 01011011 00101100 01110100 01110110 00100000 00000000 10000000 10000000 10000000 00011011 01100000 01011011 00101100 01110100 01110110 00100000 00000000 10000000 11111000 00101001 01010001 00001001 00000011 00101111 00110001 00110000 00000000 10000110";
            string msg4 = "10100000 10000000 10000000 10001000 01010110 00101110 01110110 01111100 01010000 01001110 01001111 01111111 11111110 10000000 10000111 01010110 00101110 01110110 01111100 01010000 01001110 01010000 00000000 11110111";

            Stream input = ByteUtil.CreateByteStream(msg1 + ' ' + msg2 + ' ' + msg3 + ' ' + msg4);

            var context = new Context();

            context.RegisterTemplate(113, template);

            var decoder = new FastDecoder(context, input);

            message = new Message(template);
            message.SetDecimal(1, 1.2M);
            message.SetDecimal(2, 2.3M);
            message.SetDecimal(4, 4.5M);
            message.SetDecimal(5, 23.4M);
            message.SetDecimal(6, 24.5M);

            Message readMessage = decoder.ReadMessage();

            Assert.AreEqual(message, readMessage);

            message = new Message(template);
            message.SetDecimal(1, 10000000000.00000001M);
            message.SetDecimal(2, 10000000000.00000002M);
            message.SetDecimal(3, 10000000000.00000003M);
            message.SetDecimal(4, 10000000000.00000004M);
            message.SetDecimal(6, 10000000000.00000006M);

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);

            message = new Message(template);
            message.SetDecimal(1, 30000000000.00000001M);
            message.SetDecimal(2, 30000000000.00000002M);
            message.SetDecimal(4, 30000000000.00000004M);
            message.SetDecimal(6, 30000000000.00000006M);

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);

            message = new Message(template);
            message.SetDecimal(1, 0M);
            message.SetDecimal(2, 0M);
            message.SetDecimal(4, 12.3M);
            message.SetDecimal(6, 24.5M);

            readMessage = decoder.ReadMessage();
            Assert.AreEqual(message, readMessage);
        }