Dynamic data string arrays

14 posts / 0 new
Last post
Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46
Dynamic data string arrays

I am using java to write a dynamic data program with RTI DDS that needs to use string arrays. I however don't see any API to set a string array. What is the way you got about making a string array to be used with dynamic data?

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

Hi there,

I think you will have to 'bind complex member" first and then set the strings for each position in the array. Here you can find an example in C++ but the logic is the same!

Does this help? 

Best,
  Gianpiero

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

I was trying that example but was having trouble converting some of the C++ code to an exact java match, is there any example of this exact code in java that you know of?

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

Hello,

Here how the code looks like in java: 

/* create an empty dyanamic data object */
DynamicData array_of_string = new DynamicData(null,DynamicData.PROPERTY_DEFAULT);

/* instance is the main struct that contains our array of strings. 
 * we want to bind the empty dynamic data to the inner object that 
 * represent the array of string*/    
this.instance.bind_complex_member(
    array_of_string,
    "array_of_string_member",
    DynamicData.MEMBER_ID_UNSPECIFIED);

/* now we can set each string of the array */  
array_of_string.set_string(null,1, "Hello 1");
array_of_string.set_string(null,2, "Hello 2");
array_of_string.set_string(null,3, "Hello 3");
array_of_string.set_string(null,4, "Hello 4");

/* once we are done we can unbind */ 
this.instance.unbind_complex_member(array_of_string);

The type i used in the above example is a modified version of $NDDSHOME/example/java/HelloWorld_xml_dynamic/USER_QOS_PROFILES.xml:

<types>
    <const name="MAX_NAME_LEN" type="long" value="64"/>
    <const name="MAX_MSG_LEN" type="long" value="128"/>
    <struct name="HelloWorld">
        <member name="sender" type="string" key="true" stringMaxLength="MAX_NAME_LEN"/>
        <member name="message" type="string" stringMaxLength="MAX_MSG_LEN"/>
        <member name="count" type="long"/>
        <member name="array_of_string_member" stringMaxLength="MAX_MSG_LEN" type="string" arrayDimensions="4"/>
    </struct>
</types>

 

I put my modifed example here: https://community.rti.com/filedepot?cid=1&fid=31 

Best,
  Gianpiero

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

That you very much, now with my stuff I am trying to get work, I get the following error.

Exception in thread "main" com.rti.dds.infrastructure.RETCODE_BAD_PARAMETER
at com.rti.dds.util.Utilities.rethrow(Unknown Source)
at com.rti.dds.infrastructure.RETCODE_ERROR.check_return_codeI(Unknown Source)
at com.rti.dds.dynamicdata.DynamicData.bind_complex_member(Unknown Source)

DDS_DynamicData_bind_typeI:type not supported (primitive or string not allowed as a top-level type)

 

Not too sure why it is complaining about my top level type?

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

 

Do you have a reproducer you can share?

 

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

Reproducer? 

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

Sorry, can you share your code so I can 'reproduce' the error you are getting?

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

This will be kinda abstract but heres the part I am using when I get the error, using an idl that creates a string called data

DynamicData data = new DynamicData(typeCode,DynamicData.PROPERTY_DEFAULT);

DynamicData a = new DynamicData(null,DynamicData.PROPERTY_DEFAULT);
try
{
data.bind_complex_member(a,"data", DynamicData.MEMBER_ID_UNSPECIFIED);
}
catch(RETCODE_BAD_PARAMETER e)
{
e.printStackTrace();
}

 

"data.bind_complex_member(a,"data", DynamicData.MEMBER_ID_UNSPECIFIED);" is where the error gets thrown.

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

In the idl is "data" a string or an array of strings?

 

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

It is a string and not an array of strings, which i suspect could be causing an issue then.

Offline
Last seen: 4 years 9 months ago
Joined: 05/21/2014
Posts: 46

However the array of strings is variable, so how would that be expressed in the idl?

gianpiero's picture
Offline
Last seen: 11 months 2 weeks ago
Joined: 06/02/2010
Posts: 177

Hello,

Reguarding:

It is a string and not an array of strings, which i suspect could be causing an issue then.

If it is just a string you can simple call

            this.instance.set_string(
                    "sender",
                    DynamicData.MEMBER_ID_UNSPECIFIED,
                    "John Smith");

If you look at the example i shared here, in the HelloPublisher.java we do that on lines 84-87. 

Also, when you say:

However the array of strings is variable, so how would that be expressed in the idl?

what do you mean? Do you mean that the size of the array can be variable? If you use array all of them will be allocated. If you have need for 'dynamic' allocation I would suggest you use a sequence. Start here and then look at the user manual to learn more about them. 

Best,
  Gianpiero

Offline
Last seen: 7 years 2 months ago
Joined: 10/28/2017
Posts: 1

I am also facing the same problem..instead of array i have sequence..can u write code for sequence?