Problems with getting member infos from DynamicData(Java)

2 posts / 0 new
Last post
Offline
Last seen: 10 years 10 months ago
Joined: 10/10/2012
Posts: 24
Problems with getting member infos from DynamicData(Java)

Hello,

I want to iterate over DynamicData members and retrieve inormation about them (member name, kind and value). To keep it simple, I use the following type code for testing:

struct Example {
	short value1;
	short value2;
};

When I receive a topic sample (data) of this type code on a dynamic data listener, I can simply retrieve the values:

System.out.println("value1 = " + data.get_short(null, 1) + "\n value2 = " + data.get_short(null, 2))

This method is working, but since I don't know how the type code looks like beforehand, I would like to do it differently. My idea is to iterate over the members (let's not talk about nested members for now):

//iteration needs to start at 1
for(int i=1; i<=data.get_member_count(); i++){
        try {
            System.out.println("    Member " + i + " is named " + data.get_type().member_name(i) + " ,has type " + data.get_member_type(null, i).kind().name() +" and value " + data.get_short(null, i));
        } catch (BadKind | Bounds e) {
            e.printStackTrace();
	}
}

For instance, the original values are value1=9 and value2=3. If I use this for-loop, strange things happen to the sample apparently. During the first iteration the output is:

Member 1 is named value2 ,has type TK_SHORT and value 9

So, suddenly member 1 is value2, but the actual value is still 9, which belongs to value1, originally. I don't understand why this happens.

Then, on the second iteration of the loop, I get an exception:

com.rti.dds.infrastructure.Bounds at com.rti.dds.typecode.TypeCode.member_name(Unknown Source)

What may cause this behaviour and how can I fix it?

Best regards,

Arthur

P.S.: I've noticed that this error occurs due to the use of data.get_type().member_name(i). Is there another way to get the member name (through TypeCodes maybe)?

Gerardo Pardo's picture
Offline
Last seen: 3 weeks 3 days ago
Joined: 06/02/2010
Posts: 602

Hi Arthur,

The problem is that the operation on the DynamicData object, that is: data.get_member_type(null, memberId) and data.get_short(null, memberId) take a memberIdas a parameter which unless explicitly defined in the IDL take values from 1 (for the first member) to data.get_member_count() (for the last one).

On the other hand, the operations on the TypeCode, that is: data.get_type().member_name(memberIndex) take as parameter a memberIndex which goes from 0 (for the first one) to data.get_member_count()-1 (for the last one).

Thus the correct iteration would be:

//iteration needs to start at 1
for (int i=1; i<=data.get_member_count(); i++){
        try {
            System.out.println("    Member Id " + i + " is named " + data.get_type().member_name(i-1) + " ,has type " + data.get_member_type(null, i).kind().name() +" and value " + data.get_short(null, i));
        } catch (BadKind | Bounds e) {
            e.printStackTrace();
    }
}

Gerardo