Example #1
0
 public byte[] Serialize(object obj)
 {
     try
     {
         ISpecificRecord record = (ISpecificRecord)obj;
         writer = new SpecificDatumWriter <ISpecificRecord>(record.Schema);
         using (MemoryStream ms = new MemoryStream())
         {
             int function = (int)record.Get(0);
             ms.Write(BitConverter.GetBytes(function), 0, 4);
             BinaryEncoder encoder = new BinaryEncoder(ms);
             writer.Write(record, encoder);
             return(ms.ToArray());
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
     return(null);
 }
        public static void TestCodeGen(string str, object[] result)
        {
            Schema schema = Schema.Parse(str);

            CompilerResults compres = GenerateSchema(schema);

            // instantiate object
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[0]) as ISpecificRecord;

            Assert.IsNotNull(rec);

            // test type of each fields
            for (int i = 1; i < result.Length; ++i)
            {
                object field = rec.Get(i - 1);
                Type   stype;
                if (result[i].GetType() == typeof(string))
                {
                    object obj = compres.CompiledAssembly.CreateInstance((string)result[i]);
                    Assert.IsNotNull(obj);
                    stype = obj.GetType();
                }
                else
                {
                    stype = (Type)result[i];
                }
                if (!stype.IsValueType)
                {
                    Assert.IsNull(field);   // can't test reference type, it will be null
                }
                else if (stype.IsValueType && field == null)
                {
                    Assert.IsNull(field); // nullable value type, so we can't get the type using GetType
                }
                else
                {
                    Assert.AreEqual(stype, field.GetType());
                }
            }
        }
Example #3
0
 /// <summary>
 /// Called when an ObjectFragment or ObjectPart message is recieved.
 /// </summary>
 /// <param name="header">The header.</param>
 /// <param name="message">The message.</param>
 /// <param name="dataObject">The data object.</param>
 private void OnObjectPart(IMessageHeader header, ISpecificRecord message, IDataObject dataObject)
 {
     LogDataObject(new ProtocolEventArgs <ISpecificRecord>(header, message), dataObject, true);
 }
Example #4
0
        public void TestSpecific(string str, object[] result)
        {
            if (compres == null)
            {
                Protocol protocol = Protocol.Parse(str);
                var      codegen  = new CodeGen();
                codegen.AddProtocol(protocol);
                var compileUnit = codegen.GenerateCode();

                // add a constructor to the main class using the passed assignment statements
                CodeTypeDeclaration ctd         = compileUnit.Namespaces[0].Types[(int)result[0]];
                CodeConstructor     constructor = new CodeConstructor();
                constructor.Attributes = MemberAttributes.Public;
                CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);
                constructor.Statements.Add(snippet);
                ctd.Members.Add(constructor);

                // add a function to the main class to populate the data
                // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ.

                CodeMemberMethod method = new CodeMemberMethod();
                method.Attributes = MemberAttributes.Public;
                method.Name       = "Populate";
                CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]);
                method.Statements.Add(snippet2);
                ctd.Members.Add(method);



                // compile
                var comparam = new CompilerParameters(new string[] { "netstandard.dll" });
                comparam.ReferencedAssemblies.Add("System.dll");
                comparam.ReferencedAssemblies.Add(Path.Combine(TestContext.CurrentContext.TestDirectory, "Avro.dll"));
                comparam.GenerateInMemory = true;
                var ccp   = new Microsoft.CSharp.CSharpCodeProvider();
                var units = new CodeCompileUnit[] { compileUnit };
                compres = ccp.CompileAssemblyFromDom(comparam, units);
                Assert.IsNotNull(compres);
                if (compres.Errors.Count > 0)
                {
                    for (int i = 0; i < compres.Errors.Count; i++)
                    {
                        Console.WriteLine(compres.Errors[i]);
                    }
                }
                Assert.IsTrue(compres.Errors.Count == 0);
            }

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            // Call populate to put some data in it.
            Type       recType    = rec.GetType();;
            MethodInfo methodInfo = recType.GetMethod("Populate");

            methodInfo.Invoke(rec, null);

            var x1 = compres.CompiledAssembly.FullName;

            Assert.IsFalse(rec == null);

            // serialize
            var stream = serialize(rec.Schema, rec);

            // deserialize
            var rec2 = deserialize <ISpecificRecord>(stream, rec.Schema, rec.Schema);

            Assert.IsFalse(rec2 == null);
            AssertSpecificRecordEqual(rec, rec2);
        }
Example #5
0
 public IAcknowledge DecodeAcknowledge(ISpecificRecord body)
 {
     return((Acknowledge)body);
 }
Example #6
0
 public IProtocolException DecodeProtocolException(ISpecificRecord body)
 {
     return((ProtocolException)body);
 }
 private object Get(ISpecificRecord record, int fieldPos)
 {
     return(record.Get(fieldPos));
 }
 /// <summary>
 /// Called when the ETP session is initialized.
 /// </summary>
 /// <param name="header">The header.</param>
 /// <param name="message">The message.</param>
 /// <param name="supportedProtocols">The supported protocols.</param>
 private void OnOpenSession(IMessageHeader header, ISpecificRecord message, IList <ISupportedProtocol> supportedProtocols)
 {
     Runtime.Invoke(() => StatusBarText = "Connected");
     IsConnected = true;
     GetResources(EtpUri.RootUri);
 }
Example #9
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var      codegen  = new CodeGen();

            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd         = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor     constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);

            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // add a function to the main class to populate the data
            // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ.

            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public;
            method.Name       = "Populate";
            CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]);

            method.Statements.Add(snippet2);
            ctd.Members.Add(method);



            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });

            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("System.Core.dll");
            comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp     = new Microsoft.CSharp.CSharpCodeProvider();
            var units   = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);

            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                {
                    Console.WriteLine(compres.Errors[i]);
                }
            }
            if (null != compres)
            {
                Assert.IsTrue(compres.Errors.Count == 0);
            }

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            // Call populate to put some data in it.
            Type       recType    = rec.GetType();;
            MethodInfo methodInfo = recType.GetMethod("Populate");

            methodInfo.Invoke(rec, null);

            var x1 = compres.CompiledAssembly.FullName;


            Assert.IsFalse(rec == null);

            // serialize
            var stream     = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer     = new SpecificDefaultWriter(rec.Schema);

            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader  = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2    = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);

            Assert.IsFalse(rec2 == null);
        }
Example #10
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var      codegen  = new CodeGen();

            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd         = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor     constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);

            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });

            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("System.Core.dll");
            comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp     = new Microsoft.CSharp.CSharpCodeProvider();
            var units   = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);

            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                {
                    Console.WriteLine(compres.Errors[i]);
                }
            }
            if (null != compres)
            {
                Assert.IsTrue(compres.Errors.Count == 0);
            }

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            Assert.IsFalse(rec == null);

            // serialize
            var stream     = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer     = new SpecificDefaultWriter(rec.Schema);

            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader  = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2    = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);

            Assert.IsFalse(rec2 == null);
        }
Example #11
0
            public void OnMessage(object obj)
            {
                if (!(obj is ISpecificRecord))
                {
                    return;
                }
                ISpecificRecord avroObj = (ISpecificRecord)obj;
                ObjectType      type    = (ObjectType)avroObj.Get(0);

                switch (type)
                {
                case ObjectType.AmendOrderRequest:
                {
                    AmendOrderRequest req = (AmendOrderRequest)avroObj;
                    logger.Info("Get AmendOrderRequest: " + req.ToString());
                    IDownStreamAdaptor adaptor = _manager.downStreamManager.getAdaptorById(req.exchangeAccount);
                    processAmendOrder(adaptor, req);

                    break;
                }

                case ObjectType.CancelOrderRequest:
                {
                    CancelOrderRequest req = (CancelOrderRequest)avroObj;
                    logger.Info("Get CancelOrderRequest: " + req.ToString());
                    IDownStreamAdaptor adaptor = _manager.downStreamManager.getAdaptorById(req.exchangeAccount);
                    processCancelOrder(adaptor, req);

                    break;
                }

                case ObjectType.NewOrderRequest:
                {
                    NewOrderRequest req = (NewOrderRequest)avroObj;
                    logger.Info("Get NewOrderRequest: " + req.ToString());
                    IDownStreamAdaptor adaptor = _manager.downStreamManager.getAdaptorById(req.exchangeAccount);
                    processNewOrder(adaptor, req);

                    break;
                }

                case ObjectType.SubscribeQuote:
                {
                    SubscribeQuote req = (SubscribeQuote)avroObj;

                    break;
                }

                case ObjectType.UnsubscribeQuote:
                {
                    break;
                }

                default:
                {
                    //Log Error
                    logger.Error("Undefined ObjectType: " + type);
                    return;
                }
                }
            }