Search code examples
javapublish-subscribedata-distribution-service

Passing Complex Structure from OpenDDS publisher


I have following idl structure. I want to publish it using opendds

#pragma DCPS_DATA_TYPE "B::CData"
#pragma DCPS_DATA_KEY "B::CData id"


module B { 

    struct Quote {
        string skit_name;
        string episode_name;     
        string line;
    };

    struct CData{
        long id;
        Quote payload;
    };
};

I have written publisher and subscriber in java. but while publishing and subscribing to above topic JVM crashes.

Any one has idea about this?

Below is the java code for public Topic

  public static void main(String[] args) {

    DomainParticipantFactory dpf =
    TheParticipantFactory.WithArgs(new StringSeqHolder(args));
    if (dpf == null) {
    System.err.println ("Domain Participant Factory not found");
    return;
    }
    final int DOMAIN_ID = 42;
    DomainParticipant dp = dpf.create_participant(DOMAIN_ID,
    PARTICIPANT_QOS_DEFAULT.get(), null, DEFAULT_STATUS_MASK.value);
    if (dp == null) {
    System.err.println ("Domain Participant creation failed");
    return;
    }


    CDataTypeSupportImpl servant = new CDataTypeSupportImpl();

    if (servant.register_type(dp, "") != RETCODE_OK.value) {
    System.err.println ("register_type failed");
    return;
    }


    Topic top = dp.create_topic("data",
    servant.get_type_name(),
    TOPIC_QOS_DEFAULT.get(), null,
    DEFAULT_STATUS_MASK.value);


    Publisher pub = dp.create_publisher(
    PUBLISHER_QOS_DEFAULT.get(),
    null,
    DEFAULT_STATUS_MASK.value);




    DataWriter dw = pub.create_datawriter(
    top, DATAWRITER_QOS_DEFAULT.get(), null, DEFAULT_STATUS_MASK.value);


    CDataDataWriter mdw = CDataDataWriterHelper.narrow(dw);
    CData cData=new CData();
    int handle = mdw.register(cData);

//    above statement crashes the jvm   

    int ret = mdw.write(msg, handle);

}

Solution

  • You can pass complex structure using openDDS

    you need to define complex structure idl like

    module B { 
      typedef struct Quote {
        string skit_name;
        string episode_name;     
        string line;
      } QuoteData;
    
      @topic
      struct CData {
        @key long id;
        QuoteData payload;
      };
    };
    

    while receiving data you need to pre allocate memory for your complex data type.In case of CData and QuoteData first we will allocate memory for QuoteData and then allocate memory for CData.

    In my case, I'm using openDDS in Java. openDDS in JAVA uses native libraries i.e.dll or lib which are generated after compiling openDDS and ACEWrappers.

    JVM was crashing because I haven't pre-allocated memory for complex object.I'm passing only CData object. When I first created QuoteData object and after that I have created CData object it works fine.