Search code examples
clinuxsocketsdata-distribution-service

Creating Multiple Data Writers to multiple topics in DDS


I am working on OpenSplice DDS Community Editionin Linux 32-bit. Here I can Have Multiple Topics, where, Data Writers can Write into this topic and a Publishers will publish these topics. I tried modifying the source code available, where I am trying to get the output as shown below:

    Chatter 1: Hi there, I will send you 10 more exxpert messages.
    Chatter 1: Message no. 1
    Chatter 1: Message no. 2
    Chatter 1: Message no. 3
    Chatter 1: Message no. 4
    Chatter 1: Message no. 4
    Chatter 1: Message no. 5
    Chatter 1: Message no. 6
    Chatter 1: Message no. 7
    Chatter 1: Message no. 8
    Chatter 1: Message no. 9
    Chatter 1: Message no. 10

and the source code is as shown below:

/* Initialize the chat messages on Heap. */
msg = Chat_ChatMessage__alloc();
checkHandle(msg, "Chat_ChatMessage__alloc");
msg->userID = ownID;
msg->index = 0;
msg->content = DDS_string_alloc(MAX_MSG_LEN);
checkHandle(msg->content, "DDS_string_alloc");
if (ownID == TERMINATION_MESSAGE) {
    snprintf (msg->content, MAX_MSG_LEN, "Termination message.");
} else {
    snprintf (msg->content, MAX_MSG_LEN, "Hi there, I will send you %d more messages.", NUM_MSG);
}
printf("Writing message: %s\n", msg->content);

//Initialize the chat messages on Heap//

/* Register a chat message for this user (pre-allocating resources for it!!) */
userHandle = Chat_ChatMessageDataWriter_register_instance(talker, msg);

/* Write a message using the pre-generated instance handle. */
status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");

/* Register a chat message for this user (pre-allocating resources for it!!) */
userHandle = Chat_ChatMessageDataWriter_register_instance(chatterbox, msg);

/* Write a message using the pre-generated instance handle. */
status = Chat_ChatMessageDataWriter_write(chatterbox, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");

sleep (1); /* do not run so fast! */
/* Write any number of messages, re-using the existing string-buffer: no leak!!. */
for (i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) {
    msg->index = i;
    snprintf ( msg->content, MAX_MSG_LEN, "Message no. %d", msg->index);
    printf("Writing message: %s\n", msg->content);
    status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
    checkStatus(status, "Chat_ChatMessageDataWriter_write");
    sleep (1); /* do not run so fast! */
    }

 /* Write any number of messages, re-using the existing string-buffer: no leak!!. */
    for (i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) {
        msg->index = i;
        snprintf (msg->content, MAX_MSG_LEN, "Hi there, I will send you %d more exxpert_messages.", NUM_MSG);
        snprintf ( msg->content, MAX_MSG_LEN, "Message no. %d", msg->index);
        printf("Writing message: %s\n", msg->content);
        status = Chat_ChatMessageDataWriter_write(chatterbox, msg, userHandle);
        checkStatus(status, "Chat_ChatMessageDataWriter_write");
}

but my output for the above code is as follows:

    Writing message: Hi there, I will send you 10 more messages.
    Writing message: Message no. 1
    Error in Chat_ChatMessageDataWriter_write: DDS_RETCODE_PRECONDITION_NOT_MET

Solution

  • When you register an instance with a DataWriter, a value of the type InstanceHandle is returned, like in your code here:

    userHandle = Chat_ChatMessageDataWriter_register_instance(chatterbox, msg);
    

    You can only use that userHandle variable for a subsequent write() with the same DataWriter that you registered with. However, you try to do the following:

    status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
    

    Notice the different variables chatterbox and talker. The userHandle variable does not belong to the talker DataWriter, hence the failure with PRECONDITION_NOT_MET return value.