public void TestFieldComparer()
        {
            var marsh = new Marshaller(new BinaryConfiguration
            {
                TypeConfigurations = new[]
                {
                    new BinaryTypeConfiguration(typeof(Foo))
                    {
                        EqualityComparer = new BinaryFieldEqualityComparer("Name", "Id")
                    }
                }
            });

            var val = new Foo {
                Id = 58, Name = "John"
            };
            var binObj  = marsh.Unmarshal <IBinaryObject>(marsh.Marshal(val), BinaryMode.ForceBinary);
            var expHash = val.Name.GetHashCode() * 31 + val.Id.GetHashCode();

            Assert.AreEqual(expHash, binObj.GetHashCode());

            val = new Foo {
                Id = 95
            };
            binObj  = marsh.Unmarshal <IBinaryObject>(marsh.Marshal(val), BinaryMode.ForceBinary);
            expHash = val.Id.GetHashCode();
            Assert.AreEqual(expHash, binObj.GetHashCode());
        }
Пример #2
0
        /// <summary>
        /// Checks the enum value serialization.
        /// </summary>
        private static void CheckValue <T>(T val, bool isBinaryEnum = true)
        {
            var marsh = new Marshaller(null)
            {
                CompactFooter = false
            };
            var bytes  = marsh.Marshal(val);
            var res    = marsh.Unmarshal <T>(bytes);
            var binRes = marsh.Unmarshal <IBinaryObject>(bytes, BinaryMode.ForceBinary);

            Assert.AreEqual(val, res);
            Assert.AreEqual(val, binRes.Deserialize <T>());

            if (isBinaryEnum)
            {
                Assert.AreEqual(TypeCaster <int> .Cast(val), binRes.EnumValue);
            }
            else
            {
                Assert.AreEqual(val, binRes.GetField <T>("value__"));
            }

            // Check array.
            var arr    = new[] { val, val };
            var arrRes = TestUtils.SerializeDeserialize(arr);

            Assert.AreEqual(arr, arrRes);
        }
        /// <summary>
        /// 패킷을 전송한다.
        /// </summary>
        /// <param name="packet">전송할 패킷</param>
        public override void Send(IPacket packet)
        {
            string message = Marshaller.Marshal(packet);
            ArraySegment <byte> binaryData = cConvertUtil.ConvertStringToBinary(message);

            Send(binaryData);
        }
Пример #4
0
        /// <summary>
        /// Serializes and deserializes back an object.
        /// </summary>
        public static T SerializeDeserialize <T>(T obj, bool raw = false)
        {
            var cfg = new BinaryConfiguration
            {
                Serializer = raw ? new BinaryReflectiveSerializer {
                    RawMode = true
                } : null
            };

#if NETCOREAPP2_0
            var marshType = typeof(IIgnite).Assembly.GetType("Apache.Ignite.Core.Impl.Binary.Marshaller");
            var marsh     = Activator.CreateInstance(marshType, new object[] { cfg, null });
            marshType.GetProperty("CompactFooter").SetValue(marsh, false);

            var bytes = marshType.GetMethod("Marshal").MakeGenericMethod(typeof(object))
                        .Invoke(marsh, new object[] { obj });

            var res = marshType.GetMethods().Single(mi =>
                                                    mi.Name == "Unmarshal" && mi.GetParameters().First().ParameterType == typeof(byte[]))
                      .MakeGenericMethod(typeof(object)).Invoke(marsh, new[] { bytes, 0 });

            return((T)res);
#else
            var marsh = new Marshaller(cfg)
            {
                CompactFooter = false
            };

            return(marsh.Unmarshal <T>(marsh.Marshal(obj)));
#endif
        }
Пример #5
0
        /// <summary>
        /// Serializes and deserializes back an object.
        /// </summary>
        public static T SerializeDeserialize <T>(T obj)
        {
            var marsh = new Marshaller(null)
            {
                CompactFooter = false
            };

            return(marsh.Unmarshal <T>(marsh.Marshal(obj)));
        }
        /// <summary>
        /// 패킷을 전송한다.
        /// </summary>
        /// <param name="packet">전송할 패킷</param>
        public override void Send(IPacket packet)
        {
            string message = Marshaller.Marshal(packet);

            cLogger.Information("Send Message: {0}\r\n", message);
            ArraySegment <byte> binaryData = cConvertUtil.ConvertStringToBinary(message);

            Context.WebSocket.SendAsync(binaryData, WebSocketMessageType.Text, true, CancellationToken.None);
        }
Пример #7
0
        /// <summary>
        /// Tests specified field combination.
        /// </summary>
        private static void TestChangingFieldOrderAndPresence(params int[][] fieldSequences)
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(CustomFieldOrder)));

            foreach (var fields in fieldSequences)
            {
                CustomFieldOrder.Fields = fields;
                marsh.Marshal(new CustomFieldOrder());
            }
        }
        /// <summary>
        /// Gets the binary object.
        /// </summary>
        private static IBinaryObject GetBinaryObject(int id, string name, int raw)
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(Foo)));

            var bytes = marsh.Marshal(new Foo {
                Id = id, Name = name, Raw = raw
            });

            return(marsh.Unmarshal <IBinaryObject>(bytes, BinaryMode.ForceBinary));
        }
        /// <exception cref="System.Exception"/>
        internal static string GetMarshalledAppInfo(ApplicationSubmissionContextInfo appInfo
                                                    )
        {
            StringWriter writer  = new StringWriter();
            JAXBContext  context = JAXBContext.NewInstance(typeof(ApplicationSubmissionContextInfo
                                                                  ));
            Marshaller m = context.CreateMarshaller();

            m.Marshal(appInfo, writer);
            return(writer.ToString());
        }
Пример #10
0
        public void TestMultithreadedRandomFields()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(RandomFieldOrder)));
            var obj   = new RandomFieldOrder();

            TestUtils.RunMultiThreaded(() =>
            {
                var bytes = marsh.Marshal(obj);

                marsh.Unmarshal <RandomFieldOrder>(bytes);
            }, Environment.ProcessorCount, 5);
        }
Пример #11
0
        public void TestRandomFieldOrderAndPresence()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(RandomFieldOrder)));
            var obj   = new RandomFieldOrder();

            for (var i = 0; i < 1000; i++)
            {
                var bytes = marsh.Marshal(obj);

                marsh.Unmarshal <RandomFieldOrder>(bytes);
            }
        }
Пример #12
0
        public static void SendStopMessage()
        {
            var      msg   = new Message("Client", "MyInfoImpl:InfoImpl!TurnOff");
            var      req   = new Requestor("Client");
            var      bytes = Marshaller.Marshal(msg);
            IAddress dest  = new Entry(Address, ActivatorPort);

            bytes = req.deliver_and_wait_feedback(dest, bytes);
            var answer = Marshaller.Unmarshal(bytes);

            Console.WriteLine(bool.Parse(answer.Data) ? "The server is still on!!!" : "The server is off");
        }
Пример #13
0
        public void TestStructure()
        {
            for (int i = 1; i <= RepeatCnt; i++)
            {
                Console.WriteLine(">>> Iteration started: " + i);

                // 1. Generate and shuffle objects.
                IList <BranchedType> objs = new List <BranchedType>();

                for (int j = 0; j < 6 * ObjectsPerMode; j++)
                {
                    objs.Add(new BranchedType((j % 6) + 1));
                }

                objs = IgniteUtils.Shuffle(objs);

                // 2. Create new marshaller.
                BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration(typeof(BranchedType));

                BinaryConfiguration cfg = new BinaryConfiguration
                {
                    TypeConfigurations = new List <BinaryTypeConfiguration> {
                        typeCfg
                    }
                };

                Marshaller marsh = new Marshaller(cfg);

                // 3. Marshal all data and ensure deserialized object is fine.
                foreach (BranchedType obj in objs)
                {
                    Console.WriteLine(">>> Write object [mode=" + obj.mode + ']');

                    byte[] data = marsh.Marshal(obj);

                    BranchedType other = marsh.Unmarshal <BranchedType>(data);

                    Assert.IsTrue(obj.Equals(other));
                }

                Console.WriteLine();

                // 4. Ensure that all fields are recorded.
                var desc = marsh.GetDescriptor(typeof(BranchedType));

                CollectionAssert.AreEquivalent(new[] { "mode", "f2", "f3", "f4", "f5", "f6", "f7", "f8" },
                                               desc.WriterTypeStructure.FieldTypes.Keys);
            }
        }
Пример #14
0
        public void TestNestedRaw()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(RawContainer), typeof(RawNested)));

            var obj = new RawContainer {
                Int = 3, Raw = new RawNested {
                    Int = 5
                }
            };

            var res = marsh.Unmarshal <RawContainer>(marsh.Marshal(obj));

            Assert.AreEqual(obj.Int, res.Int);
            Assert.AreEqual(0, res.Raw.Int);  // Int is not written and can't be read.
        }
Пример #15
0
        /// <summary>
        /// Serializes and deserializes back an object.
        /// </summary>
        public static T SerializeDeserialize <T>(T obj, bool raw = false)
        {
            var cfg = new BinaryConfiguration
            {
                Serializer = raw ? new BinaryReflectiveSerializer {
                    RawMode = true
                } : null
            };

            var marsh = new Marshaller(cfg)
            {
                CompactFooter = false
            };

            return(marsh.Unmarshal <T>(marsh.Marshal(obj)));
        }
Пример #16
0
        public System.String get_road_info(System.Int32 param0)
        {
            var msgData = param0 + ":get_road_info";
            //scrie in mesaj toti parametri
            var      msg   = new Message("InfoClientProxy", msgData);
            var      bytes = Marshaller.Marshal(msg);
            IAddress dest  = new Entry("127.0.0.1", _portNumber);

            //asteapta rezutatul
            bytes = _req.deliver_and_wait_feedback(dest, bytes);
            //despacheteaza rezultatul
            var answer = Marshaller.Unmarshal(bytes);
            //rezultatul este convertiti la tipul care este returnat de metoda curenta
            var result = answer.Data;

            return(result);
        }
Пример #17
0
        public void TestStructure()
        {
            for (int i = 1; i <= RepeatCnt; i++)
            {
                Console.WriteLine(">>> Iteration started: " + i);

                // 1. Generate and shuffle objects.
                IList<BranchedType> objs = new List<BranchedType>();

                for (int j = 0; j < 6 * ObjectsPerMode; j++)
                    objs.Add(new BranchedType((j%6) + 1));

                objs = IgniteUtils.Shuffle(objs);

                // 2. Create new marshaller.
                BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration(typeof(BranchedType));

                BinaryConfiguration cfg = new BinaryConfiguration
                {
                    TypeConfigurations = new List<BinaryTypeConfiguration> { typeCfg }
                };

                Marshaller marsh = new Marshaller(cfg);

                // 3. Marshal all data and ensure deserialized object is fine.
                foreach (BranchedType obj in objs)
                {
                    Console.WriteLine(">>> Write object [mode=" + obj.mode + ']');

                    byte[] data = marsh.Marshal(obj);

                    BranchedType other = marsh.Unmarshal<BranchedType>(data);

                    Assert.IsTrue(obj.Equals(other));
                }
                
                Console.WriteLine();

                // 4. Ensure that all fields are recorded.
                var desc = marsh.GetDescriptor(typeof (BranchedType));

                CollectionAssert.AreEquivalent(new[] {"mode", "f2", "f3", "f4", "f5", "f6", "f7", "f8"},
                    desc.WriterTypeStructure.FieldTypes.Keys);
            }
        }
Пример #18
0
        /// <summary>
        /// Gets the binary object.
        /// </summary>
        private static IBinaryObject GetBinaryObject(int id, string name, int raw)
        {
            var marsh = new Marshaller(new BinaryConfiguration
            {
                TypeConfigurations = new[]
                {
                    new BinaryTypeConfiguration(typeof(Foo))
                    {
                        EqualityComparer = new BinaryArrayEqualityComparer()
                    }
                }
            });

            var bytes = marsh.Marshal(new Foo {
                Id = id, Name = name, Raw = raw
            });

            return(marsh.Unmarshal <IBinaryObject>(bytes, BinaryMode.ForceBinary));
        }
Пример #19
0
        public void TestNested()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(Container), typeof(Nested)));

            var obj = new Container
            {
                Foo    = 2,
                Bar    = 4,
                Nested = new Nested
                {
                    Baz = 3,
                    Qux = 5
                }
            };

            var res = marsh.Unmarshal <Container>(marsh.Marshal(obj));

            Assert.AreEqual(2, res.Foo);
            Assert.AreEqual(4, res.Bar);
            Assert.AreEqual(3, res.Nested.Baz);
            Assert.AreEqual(5, res.Nested.Qux);
        }
Пример #20
0
        public void TestWriteRead()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(ReadWriteAll)));

            marsh.Unmarshal <ReadWriteAll>(marsh.Marshal(new ReadWriteAll()));
        }
Пример #21
0
        public void TestWriteRead()
        {
            var marsh = new Marshaller(new BinaryConfiguration(typeof(ReadWriteAll)));

            marsh.Unmarshal<ReadWriteAll>(marsh.Marshal(new ReadWriteAll()));
        }
Пример #22
0
        public static object GetObjectReference(string name)
        {
            var      msg   = new Message("Client", name);
            var      req   = new Requestor("Client");
            var      bytes = Marshaller.Marshal(msg);
            IAddress dest  = new Entry(Address, PortNo);

            bytes = req.deliver_and_wait_feedback(dest, bytes);
            var answer = Marshaller.Unmarshal(bytes);

            Console.Write("Object reference with name: ");
            Console.Write(name);
            Console.Write(" was found at port number: ");
            Console.WriteLine(answer.Data);
            var portNumber = int.Parse(answer.Data);
            //se returneaza o referinta (o instanta a lui client side proxy de fapt)
            //la obiectul server care a fost inregistrat
            var arrOfStr          = msg.Data.Split(':');
            var theDestObjectName = arrOfStr[1];
            var aux           = theDestObjectName.IndexOf("Impl");
            var interfaceName = theDestObjectName.Substring(0, aux);
            var className     = interfaceName + "ClientProxy";

            //Activating the server
            var data = name + "!Check";

            msg    = new Message("Client", data);
            req    = new Requestor("Client");
            bytes  = Marshaller.Marshal(msg);
            dest   = new Entry(Address, ActivatorPort);
            bytes  = req.deliver_and_wait_feedback(dest, bytes);
            answer = Marshaller.Unmarshal(bytes);
            var    serverOn = bool.Parse(answer.Data);
            string ans;

            if (!serverOn)
            {
                ans    = "The server is off";
                data   = name + ":" + portNumber + "!TurnOn";
                msg    = new Message("Client", data);
                req    = new Requestor("Client");
                bytes  = Marshaller.Marshal(msg);
                dest   = new Entry(Address, ActivatorPort);
                bytes  = req.deliver_and_wait_feedback(dest, bytes);
                answer = Marshaller.Unmarshal(bytes);
                var ret = bool.Parse(answer.Data);
                Console.WriteLine(ans);
                Console.WriteLine(ret ? "The server is turned on" : "The server could not be turned on");
            }
            else
            {
                ans = "The server is on";
                Console.WriteLine(ans);
            }

            //Creating the proxy
            try
            {
                object proxy = (IClientProxy)ClientProxyGenerator.GenerateAndCompile(className, interfaceName, name, portNumber);
                return(proxy);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(null);
        }