コード例 #1
0
        public object ReadBuiltInType(RosType rosType, Type destinationType)
        {
            var type  = BuiltInRosTypes.GetSerializationType(rosType);
            var value = ReadBuiltInType(type);

            if (type == destinationType)
            {
                return(value);
            }

            if (destinationType.IsEnum)
            {
                if (type == typeof(string))
                {
                    value = Enum.Parse(destinationType, (string)value);
                }
                else
                {
                    value = Enum.ToObject(destinationType, value);
                }

                return(value);
            }

            value = Convert.ChangeType(value, destinationType);
            return(value);
        }
コード例 #2
0
        public void Array_is_striped_from_type_name(string rosType, string expectedPackageName, string expectedTypeName)
        {
            var type = RosType.Parse(rosType);

            type.Should().NotBeNull();
            type.IsArray.Should().BeTrue();
            type.PackageName.Should().Be(expectedPackageName);
            type.TypeName.Should().Be(expectedTypeName);
        }
コード例 #3
0
        public void Tostring_returns_array_type_with_specifier(string rosType, string rosTypeWithoutArray)
        {
            var type = RosType.Parse(rosType);

            type.Should().NotBeNull();
            type.ToString().Should().Be(rosType);

            type.ToString("F").Should().Be(rosType);
            type.ToString("T").Should().Be(rosTypeWithoutArray);
        }
コード例 #4
0
        public void Can_parse_arrays(string rosType, bool isFixedSizeArray, int arraySize)
        {
            var type = RosType.Parse(rosType);

            type.Should().NotBeNull();
            type.IsArray.Should().BeTrue();
            type.IsFixedSizeArray.Should().Be(isFixedSizeArray);
            type.IsDynamicArray.Should().Be(!isFixedSizeArray);
            type.ArraySize.Should().Be(arraySize);
            type.ToString().Should().Be(rosType);
        }
コード例 #5
0
        public void Can_parse_built_in_types(string rosType)
        {
            var type = RosType.Parse(rosType);

            type.Should().NotBeNull();
            type.ToString().Should().Be(rosType);
            type.TypeName.Should().Be(rosType.TrimEnd('[', ']'));
            type.PackageName.Should().BeNull();
            type.IsBuiltIn.Should().BeTrue();
            type.IsFullQualified.Should().BeTrue();
        }
コード例 #6
0
        public void Header_can_be_defined_in_other_package_as_well()
        {
            var type = RosType.Parse("my_msgs/Header");

            type.Should().NotBeNull();
            type.IsHeaderType.Should().BeFalse();
            type.PackageName.Should().Be("my_msgs");
            type.TypeName.Should().Be("Header");
            type.ToString().Should().Be("my_msgs/Header");
            type.ToString("F").Should().Be("my_msgs/Header");
            type.ToString("T").Should().Be("my_msgs/Header");
            type.IsBuiltIn.Should().BeFalse();
        }
コード例 #7
0
        public void Can_parse_header(string headerType)
        {
            const string HeaderTypeName = "std_msgs/Header";

            var type = RosType.Parse(headerType);

            type.Should().NotBeNull();
            type.IsHeaderType.Should().BeTrue();
            type.PackageName.Should().Be("std_msgs");
            type.TypeName.Should().Be("Header");
            type.ToString().Should().Be(HeaderTypeName);
            type.ToString("F").Should().Be(HeaderTypeName);
            type.ToString("T").Should().Be(HeaderTypeName);
            type.IsBuiltIn.Should().BeFalse();
            type.IsArray.Should().BeFalse();
        }
コード例 #8
0
        public void WriteBuiltInType(RosType rosType, object value)
        {
            var type = BuiltInRosTypes.GetSerializationType(rosType);

            if (value != null)
            {
                var valueType = value.GetType();

                if (valueType != type)
                {
                    value = Convert.ChangeType(value, type);
                }
            }

            WriteBuiltInType(type, value);
        }
コード例 #9
0
        public void GenerateMessages_ShouldCallTransformTemplateToFileMethod()
        {
            //arrange
            string                        messageType              = "testType";
            string                        standardNamespace        = "testStandardNamespace";
            string                        messageNamespace         = "testNamespace";
            Mock <ProjectItem>            directoryProjectItemMock = new Mock <ProjectItem>();
            Mock <IMsgFile>               messageFileMock          = new Mock <IMsgFile>();
            IEnumerable <IMsgFile>        messageFileCollection    = new IMsgFile[] { messageFileMock.Object };
            Mock <ITextTemplatingSession> templatingSessionMock    = new Mock <ITextTemplatingSession>();

            RosType        testDependencyType = new RosType("testDepdendencyNamespace", "testDependencyType");
            ISet <RosType> dependencySet      = new HashSet <RosType>();

            dependencySet.Add(testDependencyType);

            ISet <MessageField> arrayFieldSet    = new HashSet <MessageField>();
            ISet <MessageField> constantFieldSet = new HashSet <MessageField>();
            ISet <MessageField> fieldSet         = new HashSet <MessageField>();

            messageFileMock.SetupGet(messageFile => messageFile.ArrayFieldSet).Returns(arrayFieldSet);
            messageFileMock.SetupGet(messageFile => messageFile.ConstantFieldSet).Returns(constantFieldSet);
            messageFileMock.SetupGet(messageFile => messageFile.FieldSet).Returns(fieldSet);

            _testClassPartialMock.CallBase = true;

            messageFileMock.SetupGet(messageFile => messageFile.Type).Returns(new RosType(messageNamespace, messageType));
            messageFileMock.SetupGet(messageFile => messageFile.DependencySet).Returns(dependencySet);
            _textTemplatingSessionHostMock.Setup(textTemplatingSessionHost => textTemplatingSessionHost.CreateSession())
            .Returns(templatingSessionMock.Object);
            _textTemplatingSessionHostMock.Setup(textTemplatingSessionHost => textTemplatingSessionHost.CreateSession())
            .Returns(templatingSessionMock.Object);
            _testClassPartialMock.Setup(testClass => testClass.TransformTemplateToFile(templatingSessionMock.Object,
                                                                                       directoryProjectItemMock.Object, ROS_MESSAGE_CODE_GENERATION_TEMPLATE_PATH,
                                                                                       ROS_MESSAGE_CODE_GENERATION_TEMPLATE_CONTENT, It.IsAny <string>())).Callback(() => { });

            //act
            _testClassPartialMock.Object.GenerateMessages(directoryProjectItemMock.Object, messageFileCollection, standardNamespace);

            //assert
            _textTemplatingSessionHostMock.Verify(sessionHost => sessionHost.CreateSession(), Times.Once);
            _testClassPartialMock.Verify(testClass =>
                                         testClass.TransformTemplateToFile(templatingSessionMock.Object, directoryProjectItemMock.Object, ROS_MESSAGE_CODE_GENERATION_TEMPLATE_PATH, ROS_MESSAGE_CODE_GENERATION_TEMPLATE_CONTENT, It.IsAny <string>()), Times.Once);
        }
コード例 #10
0
    public RosSubscriber(String nodeName,
                         String rosTopic,
                         int queueSize = 10)
    {
        // During instantiation, it is assumed that the RosManager instance is ready and connected
        name      = nodeName;
        RosTopic  = rosTopic;
        RosType   = typeof(T).ToString();
        RosType   = RosType.Substring(4, RosType.Length - 4).Replace(".", "/");
        QueueSize = queueSize;
        MsgQueue  = new Queue <T>();

#if !UNITY_EDITOR
        RosMessenger.Instance.Subscribe(RosTopic, RosType);
        buffer = RosMessenger.Instance.topicBuffer[RosTopic];

        Debug.Log("[" + name + "] Subscribed successfully, message type: " + RosType);
#endif
    }
コード例 #11
0
    public RosPublisher(String nodeName,
                        String rosTopic,
                        float rate    = 20,
                        int queueSize = 10)
    {
        connected = false;
        name      = nodeName;
        RosTopic  = rosTopic;
        RosType   = typeof(T).ToString();
        RosType   = RosType.Substring(4, RosType.Length - 4).Replace(".", "/");
        QueueSize = queueSize;
        SendQueue = new Queue <T>();

#if !UNITY_EDITOR
        RosMessenger.Instance.Advertise(RosTopic, RosType);
        Debug.Log("[" + name + "] Advertised successfully");
#endif
        connected = true;
    }
コード例 #12
0
        void Can_parse_not_packaged_type_name()
        {
            const string TypeName = "FooMsg";

            var type = RosType.Parse(TypeName);

            type.Should().NotBeNull();
            type.IsArray.Should().BeFalse();
            type.IsFixedSizeArray.Should().BeFalse();
            type.IsDynamicArray.Should().BeFalse();
            type.ArraySize.Should().Be(0);
            type.PackageName.Should().BeNull();
            type.TypeName.Should().Be("FooMsg");
            type.IsBuiltIn.Should().BeFalse();
            type.IsFullQualified.Should().BeFalse();
            type.IsHeaderType.Should().BeFalse();
            type.ToString().Should().Be(TypeName);
            type.ToString("T").Should().Be(TypeName);
            type.ToString("F").Should().Be(TypeName);
        }
コード例 #13
0
        public void CanWriteAndReadBuiltInTypes2(string rosType, object writeValue, object expectedValue)
        {
            var primitiveRosType = RosType.Parse(rosType);

            primitiveRosType.IsBuiltIn.Should().BeTrue();
            primitiveRosType.IsArray.Should().BeFalse();

            using (var s = new MemoryStream())
            {
                var writer = new RosBinaryWriter(s);
                var reader = new RosBinaryReader(s);

                writer.WriteBuiltInType(primitiveRosType, writeValue);

                s.Position = 0;

                var readValue = reader.ReadBuiltInType(primitiveRosType, expectedValue.GetType());

                readValue.Should().BeEquivalentTo(expectedValue);
                readValue.GetType().Should().Be(expectedValue.GetType());
            }
        }
コード例 #14
0
        public void Invalid_name_thrpws_exception(string name)
        {
            Action parse = () => RosType.Parse(name);

            parse.Should().Throw <FormatException>();
        }
コード例 #15
0
 private IntegrosActionTypeInfo(Type type)
 {
     Type    = type;
     RosType = RosType.Parse("integros_msgs/" + type.Name);
 }
コード例 #16
0
        private void SerializeArray(SerializationContext context, RosBinaryWriter writer, RosType rosType, Type memberType,
                                    object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var collection = value as ICollection;

            if (collection == null)
            {
                throw new InvalidCastException("Value does not implement System.ICollection.");
            }

            var elementCount = collection.Count;

            if (rosType.IsFixedSizeArray)
            {
                if (rosType.ArraySize != elementCount)
                {
                    throw new InvalidOperationException(
                              $"Expected array size of {rosType.ArraySize} but found array size of {elementCount}.");
                }
            }
            else
            {
                writer.Write(elementCount);
            }

            if (elementCount == 0)
            {
                return;
            }

            var elementType = GetGenericElementType(memberType);

            var index = -1;

            foreach (var item in collection)
            {
                index++;

                try
                {
                    SerializeValue(context, writer, rosType, elementType, item);
                }
                catch (Exception e)
                {
                    var indexString = $"[{index}]";
                    if (e is RosFieldSerializationException rosException)
                    {
                        rosException.AddLeadingRosIdentifier(indexString);
                        throw;
                    }
                    else
                    {
                        throw new RosFieldSerializationException(RosFieldSerializationException.SerializationOperation.Serialize, indexString, e);
                    }
                }
            }
        }
コード例 #17
0
        private void SerializeValue(SerializationContext context, RosBinaryWriter writer, RosType rosType, Type type,
                                    object value)
        {
            if (rosType.IsBuiltIn)
            {
                writer.WriteBuiltInType(rosType, value);
            }
            else
            {
                var typeInfo = context.MessageTypeRegistry.GetOrCreateMessageTypeInfo(type);
                IRosMessageFormatter formatter = this;

                // If this serializer cannot serialize the object search for serializer who can do it
                if (!CanSerialize(typeInfo))
                {
                    formatter = context.MessageFormatters.FindFormatterFor(typeInfo);

                    if (formatter == null)
                    {
                        throw new NotSupportedException($"No formatter for message {typeInfo} found.");
                    }
                }

                formatter.Serialize(context, writer, typeInfo, value);
            }
        }
コード例 #18
0
        private object DeserializeArray(SerializationContext context, RosBinaryReader reader, RosType rosType,
                                        Type memberType)
        {
            int length;

            if (rosType.IsFixedSizeArray)
            {
                length = rosType.ArraySize;
            }
            else
            {
                length = reader.ReadInt32();
            }

            var elementType = GetGenericElementType(memberType);
            var listType    = typeof(List <>).MakeGenericType(elementType);

            var list = (IList)Activator.CreateInstance(listType);

            for (var i = 0; i < length; i++)
            {
                try
                {
                    var item = DeserializeValue(context, reader, rosType, elementType);
                    list.Add(item);
                }
                catch (Exception e)
                {
                    var indexString = $"[{i}]";
                    if (e is RosFieldSerializationException rosException)
                    {
                        rosException.AddLeadingRosIdentifier(indexString);
                        throw;
                    }
                    else
                    {
                        throw new RosFieldSerializationException(RosFieldSerializationException.SerializationOperation.Deserialize, indexString, e);
                    }
                }
            }

            return(ConvertListToMemberType(list, memberType));
        }