static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;

    /* Declaration of Topics */
    DDS_Topic *topic1 = NULL;
    DDS_Topic *topic2 = NULL;
    DDS_Topic *topic3 = NULL;

    /* Declaration of DataWriters */
    DDS_DataWriter *writer1 = NULL;
    DDS_DataWriter *writer2 = NULL;
    DDS_DataWriter *writer3 = NULL;

    ordered_groupDataWriter * ordered_group_writer1 = NULL;
    ordered_groupDataWriter * ordered_group_writer2 = NULL;
    ordered_groupDataWriter * ordered_group_writer3 = NULL;

    /* Declaration of instances */
    ordered_group *instance1 = NULL;
    ordered_group *instance2 = NULL;	
    ordered_group *instance3 = NULL;	

    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;  
    struct DDS_Duration_t send_period = {1,0};

    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
            DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
            NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
            participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register DATA TYPES. In this example, only one data type */
    /* Register type before creating topic */
    type_name = ordered_groupTypeSupport_get_type_name();
    retcode = ordered_groupTypeSupport_register_type(
            participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* TOPICS */

    /* To customize topic QoS, use 
       the configuration file USER_QOS_PROFILES.xml */   
    topic1 = DDS_DomainParticipant_create_topic(
            participant, "Topic1",
            type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (topic1 == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    topic2 = DDS_DomainParticipant_create_topic(
            participant, "Topic2",
            type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (topic2 == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    topic3 = DDS_DomainParticipant_create_topic(
            participant, "Topic3",
            type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (topic3 == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* DATAWRITERS */

    /* To customize data writer QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    writer1 = DDS_Publisher_create_datawriter(
            publisher, topic1,
            &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, 
            DDS_STATUS_MASK_NONE);
    if (writer1 == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    ordered_group_writer1 = ordered_groupDataWriter_narrow(writer1);
    if (ordered_group_writer1 == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    writer2 = DDS_Publisher_create_datawriter(
            publisher, topic2,
            &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, 
            DDS_STATUS_MASK_NONE);
    if (writer2 == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    ordered_group_writer2 = ordered_groupDataWriter_narrow(writer2);
    if (ordered_group_writer2 == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    writer3 = DDS_Publisher_create_datawriter(
            publisher, topic3,
            &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, 
            DDS_STATUS_MASK_NONE);
    if (writer3 == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    ordered_group_writer3 = ordered_groupDataWriter_narrow(writer3);
    if (ordered_group_writer3 == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }


    /* INSTANCES */

    /* Create data sample for writing */

    instance1 = ordered_groupTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);

    if (instance1 == NULL) {
        printf("ordered_groupTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    instance2 = ordered_groupTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);

    if (instance2 == NULL) {
        printf("ordered_groupTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    instance3 = ordered_groupTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);

    if (instance3 == NULL) {
        printf("ordered_groupTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
    /*
    instance_handle = ordered_groupDataWriter_register_instance(
        ordered_group_writer, instance);
     */

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        printf("Writing ordered_group, count %d\n", count);

        /* Modify the data to be sent and Write data */
        instance1->message = 
                "First sample, Topic 1 sent by DataWriter number 1";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer1, instance1, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        instance1->message = 
                "Second sample, Topic 1 sent by DataWriter number 1";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer1, instance1, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        instance2->message = 
                "First sample, Topic 2 sent by DataWriter number 2";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer2, instance2, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        instance2->message = 
                "Second sample, Topic 2 sent by DataWriter number 2";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer2, instance2, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        instance3->message = 
                "First sample, Topic 3 sent by DataWriter number 3";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer3, instance3, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        instance3->message = 
                "Second sample, Topic 3 sent by DataWriter number 3";

        retcode = ordered_groupDataWriter_write(
                ordered_group_writer3, instance3, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        NDDS_Utility_sleep(&send_period);
    }

    /*
    retcode = ordered_groupDataWriter_unregister_instance(
        ordered_group_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
     */

    /* Delete data sample */
    retcode = ordered_groupTypeSupport_delete_data_ex(instance1, 
            DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("ordered_groupTypeSupport_delete_data error %d\n", retcode);
    }

    /* Delete data sample */
    retcode = ordered_groupTypeSupport_delete_data_ex(instance2, 
            DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("ordered_groupTypeSupport_delete_data error %d\n", retcode);
    }

    /* Delete data sample */
    retcode = ordered_groupTypeSupport_delete_data_ex(instance3, 
            DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("ordered_groupTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    deadline_contentfilterDataWriter *deadline_contentfilter_writer = NULL;
    DDS_ReturnCode_t retcode;
    const char *type_name = NULL;
    int count = 0;  
    struct DDS_Duration_t deadline_period = {1, 500000000}; /* 1.5sec */
    struct DDS_Duration_t send_period = {1,0};
    deadline_contentfilter *instance0 = NULL;
    deadline_contentfilter *instance1 = NULL;
    DDS_InstanceHandle_t handle0 = DDS_HANDLE_NIL;
    DDS_InstanceHandle_t handle1 = DDS_HANDLE_NIL;
    /*struct DDS_DataWriterQos datawriter_qos = DDS_DataWriterQos_INITIALIZER;*/

    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
        NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
        participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = deadline_contentfilterTypeSupport_get_type_name();
    retcode = deadline_contentfilterTypeSupport_register_type(
        participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(
        participant, "Example deadline_contentfilter",
        type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }



    /* To customize data writer QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* If you want to change the DataWriter's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_datawriter call above.
     *
     * In this case, we set the deadline period to 1.5 seconds to trigger
     * a deadline if the DataWriter does not update often enough.
     */
    
    /* Start changes for Deadline */
/*    writer_listener.on_offered_deadline_missed =
        deadlineListener_on_offered_deadline_missed;

    retcode = DDS_Publisher_get_default_datawriter_qos(publisher,
                    &datawriter_qos);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_datawriter_qos error\n");
        return -1;
    }

*/    /* Set deadline QoS */
/*    datawriter_qos.deadline.period = deadline_period;

    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &datawriter_qos, &writer_listener, DDS_OFFERED_DEADLINE_MISSED_STATUS);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
*/
    /* End changes for Deadline */

   
    deadline_contentfilter_writer =
            deadline_contentfilterDataWriter_narrow(writer);
    if (deadline_contentfilter_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Start changes for Deadline */
    
    /* Create data sample for writing */
    instance0 =
            deadline_contentfilterTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    instance1 =
            deadline_contentfilterTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    if (instance0 == NULL || instance1 == NULL) {
        printf("deadlineTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Set keys -- we specify 'code' as the key field in the .idl */
    instance0->code = 0;
    instance1->code = 1;

    /* For data type that has key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */

    handle0 = deadline_contentfilterDataWriter_register_instance(
            deadline_contentfilter_writer, instance0);
    handle1 = deadline_contentfilterDataWriter_register_instance(
            deadline_contentfilter_writer, instance1);

    instance0->x = instance0->y = instance1->x = instance1->y = 0;

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {
        NDDS_Utility_sleep(&send_period);

        instance0->x++;
        instance0->y++;
        instance1->x++;
        instance1->y++;

        printf("Writing instance0, x = %d, y = %d\n", instance0->x,
                instance0->y);
        retcode = deadline_contentfilterDataWriter_write(
            deadline_contentfilter_writer, instance0, &handle0);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        if (count < 15) {
            printf("Writing instance1, x = %d, y = %d\n", instance1->x,
                    instance1->y);
            retcode = deadline_contentfilterDataWriter_write(
                deadline_contentfilter_writer, instance1, &handle1);
            if (retcode != DDS_RETCODE_OK) {
                printf("write error %d\n", retcode);
            }
        } else if (count == 15) {
            printf("Stopping writes to instance1\n");
        }
    }

    retcode = deadline_contentfilterDataWriter_unregister_instance(
        deadline_contentfilter_writer, instance0, &handle0);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
    retcode = deadline_contentfilterDataWriter_unregister_instance(
        deadline_contentfilter_writer, instance1, &handle1);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }

    /* Delete data sample */
    retcode = deadline_contentfilterTypeSupport_delete_data_ex(instance0,
            DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("deadline_contentfilterTypeSupport_delete_data_ex error %d\n",
                retcode);
    }
    retcode = deadline_contentfilterTypeSupport_delete_data_ex(instance1,
            DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("deadline_contentfilterTypeSupport_delete_data_ex error %d\n",
                retcode);
    }

    /* End changes for Deadline */
    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count) {
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    cfcDataWriter *cfc_writer = NULL;
    cfc *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;
    int i;
    int sample;
    struct DDS_Duration_t send_period = { 1, 0 };
    const char* cfc_name = "custom_flowcontroller";
    struct DDS_FlowControllerProperty_t custom_fcp;
    DDS_FlowController* cfc = NULL;
    struct DDS_DataWriterQos datawriter_qos = DDS_DataWriterQos_INITIALIZER;
    struct DDS_DomainParticipantQos participant_qos =
            DDS_DomainParticipantQos_INITIALIZER;

    participant = DDS_DomainParticipantFactory_create_participant(
            DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
            NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* Start changes for custom_flowcontroller */

    /* If you want to change the Participant's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_participant call above.
     */

    /* Get default participant QoS to customize */
/*    retcode = DDS_DomainParticipantFactory_get_default_participant_qos(
            DDS_TheParticipantFactory, &participant_qos);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_participant_qos error\n");
        return -1;
    }

*/    /* By default, data will be sent via shared memory _and_ UDPv4.  Because
     * the flowcontroller limits writes across all interfaces, this halves the
     * effective send rate.  To avoid this, we enable only the UDPv4 transport
     */
/*    participant_qos.transport_builtin.mask = DDS_TRANSPORTBUILTIN_UDPv4;

*/    /* To create participant with default QoS, use DDS_PARTICIPANT_QOS_DEFAULT
       instead of participant_qos */
/*    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &participant_qos,
        NULL, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
       publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }
*/
    /* End changes for Custom_Flowcontroller */

    /* To customize publisher QoS, use 
     the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(participant,
            &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* Register type before creating topic */
    type_name = cfcTypeSupport_get_type_name();
    retcode = cfcTypeSupport_register_type(participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* To customize topic QoS, use 
     the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(participant, "Example cfc",
            type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* To customize data writer QoS, use 
     the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(publisher, topic,
            &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* Start changes for custom_flowcontroller */
    
    /* If you want to change the Datawriter's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_datawriter call above.
     *
     * In this case we create the flowcontroller and the neccesary QoS
     * for the datawriter.
     */
    
/*    retcode = DDS_DomainParticipant_get_default_flowcontroller_property(
        participant, &custom_fcp);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_flowcontroller_property error \n");
        return -1;
    }

    
*/    /* Don't allow too many tokens to accumulate */
/*    custom_fcp.token_bucket.max_tokens = 
        custom_fcp.token_bucket.tokens_added_per_period = 2;
    custom_fcp.token_bucket.tokens_leaked_per_period = DDS_LENGTH_UNLIMITED;

*/    /* 100ms */
/*    custom_fcp.token_bucket.period.sec = 0;
    custom_fcp.token_bucket.period.nanosec = 100000000;

*/    /* The sample size is 1000, but the minimum bytes_per_token is 1024.
     * Furthermore, we want to allow some overhead.
     */
/*    custom_fcp.token_bucket.bytes_per_token = 1024;

*/    /* So, in summary, each token can be used to send about one message,
     * and we get 2 tokens every 100ms, so this limits transmissions to
     * about 20 messages per second.
     */
    /* Create flowcontroller and set properties */
/*    cfc = DDS_DomainParticipant_create_flowcontroller(
        participant, DDS_String_dup(cfc_name), &custom_fcp);
    if (cfc == NULL) {
        printf("create_flowcontroller error\n");
        return -1;
    }

*/    /* Get default datawriter QoS to customize */
/*    retcode = DDS_Publisher_get_default_datawriter_qos(publisher,
            &datawriter_qos);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_datawriter_qos error\n");
        return -1;
    }

*/    /* As an alternative to increasing history depth, we can just
     * set the qos to keep all samples
     */
/*    datawriter_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
*/
    /* Set flowcontroller for datawriter */
/*    datawriter_qos.publish_mode.kind = DDS_ASYNCHRONOUS_PUBLISH_MODE_QOS;
    datawriter_qos.publish_mode.flow_controller_name = DDS_String_dup(cfc_name);

*/    /* To create datawriter with default QoS, use DDS_DATAWRITER_QOS_DEFAULT
       instead of datawriter_qos */
/*    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
       publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }
*/
    /* End changes for Custom_Flowcontroller */

    cfc_writer = cfcDataWriter_narrow(writer);
    if (cfc_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* Create data sample for writing */

    instance = cfcTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);

    if (instance == NULL) {
        printf("cfcTypeSupport_create_data error\n");
        publisher_shutdown(participant, &participant_qos, &datawriter_qos);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
/*
    instance_handle = cfcDataWriter_register_instance(
        cfc_writer, instance);
*/

    /* Main loop */
    for (count = 0; (sample_count == 0) || (count < sample_count); ++count) {
        /* Changes for custom_flowcontroller */
        /* Simulate bursty writer */
        NDDS_Utility_sleep(&send_period);

        for (i = 0; i < 10; ++i) {
            sample = count * 10 + i;
            printf("Writing cfc, sample %d\n", sample);
            instance->x = sample;
            memset(instance->str, 1, 999);
            instance->str[999] = 0;
            retcode = cfcDataWriter_write(cfc_writer, instance,
                    &instance_handle);
            if (retcode != DDS_RETCODE_OK) {
                printf("write error %d\n", retcode);
            }
        }
    }

    /* This new sleep it is for let time to the subscriber to read all the
     * sent samples.
     */
    send_period.sec = 4;
    NDDS_Utility_sleep(&send_period);

/*
    retcode = cfcDataWriter_unregister_instance(
        cfc_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
*/

    /* Delete data sample */
    retcode = cfcTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("cfcTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant, &participant_qos, &datawriter_qos);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    asyncDataWriter *async_writer = NULL;
    async *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;
    struct DDS_Duration_t send_period = {0,100000000};
    /*    struct DDS_DataWriterQos datawriter_qos = DDS_DataWriterQos_INITIALIZER;*/


    /* To customize participant QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
                      DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
                      NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
                    participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
                    DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = asyncTypeSupport_get_type_name();
    retcode = asyncTypeSupport_register_type(
                  participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(
                participant, "Example async",
                type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
                DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize data writer QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(
                 publisher, topic,
                 &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* If you want to change the DataWriter's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_datawriter call above.
     *
     * In this case, we set the publish mode qos to asynchronous publish mode,
     * which enables asynchronous publishing.  We also set the flow controller
     * to be the fixed rate flow controller, and we increase the history depth.
     */
    /*
    /* Start changes for Asynchronous_Publication */

    /* Get default datawriter QoS to customize */
    /*    retcode = DDS_Publisher_get_default_datawriter_qos(publisher, &datawriter_qos);
        if (retcode != DDS_RETCODE_OK) {
            printf("get_default_datawriter_qos error\n");
            return -1;
        }

    */    /* Since samples are only being sent once per second, datawriter will need
       to keep them on queue.  History defaults to only keeping the last
       sample enqueued, so we increase that here.
    */
    /*    datawriter_qos.history.depth = 12;

    */    /* Set flowcontroller for datawriter */
    /*    datawriter_qos.publish_mode.kind = DDS_ASYNCHRONOUS_PUBLISH_MODE_QOS;
        datawriter_qos.publish_mode.flow_controller_name = DDS_FIXED_RATE_FLOW_CONTROLLER_NAME;

    */    /* To create datawriter with default QoS, use DDS_DATAWRITER_QOS_DEFAULT
       instead of datawriter_qos */

    /*    writer = DDS_Publisher_create_datawriter(
            publisher, topic,
            &datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
        if (writer == NULL) {
            printf("create_datawriter error\n");
            publisher_shutdown(participant);
            return -1;
        }
    */
    async_writer = asyncDataWriter_narrow(writer);
    if (async_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Create data sample for writing */

    instance = asyncTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);

    if (instance == NULL) {
        printf("asyncTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
    /*
        instance_handle = asyncDataWriter_register_instance(
            async_writer, instance);
    */

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        printf("Writing async, count %d\n", count);

        /* Modify the data to be written here */
        /* send count as data. */
        instance->x = count;

        /* Write data */
        retcode = asyncDataWriter_write(
                      async_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        NDDS_Utility_sleep(&send_period);
    }

    /* Changes for Asynchronous_Publication */
    /* Give time for publisher to send out last few samples */
    send_period.sec = 1;
    NDDS_Utility_sleep(&send_period);

    /*
        retcode = asyncDataWriter_unregister_instance(
            async_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("unregister instance error %d\n", retcode);
        }
    */

    /* Delete data sample */
    retcode = asyncTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("asyncTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    TreeDataWriter *Tree_writer = NULL;
    Tree *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;  
    struct DDS_Duration_t send_period = {4,0};

    /* To customize participant QoS, use 
    the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
        NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use 
    the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
        participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = TreeTypeSupport_get_type_name();
    retcode = TreeTypeSupport_register_type(
        participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use 
    the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(
        participant, "Example Tree",
        type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize data writer QoS, use 
    the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    Tree_writer = TreeDataWriter_narrow(writer);
    if (Tree_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Create data sample for writing */
    instance = TreeTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    if (instance == NULL) {
        printf("TreeTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
    written multiple times, initialize the key here
    and register the keyed instance prior to writing */
    /*
    instance_handle = TreeDataWriter_register_instance(
        Tree_writer, instance);
    */

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        printf("Writing Tree, count %d\n", count);

        /* Modify the data to be written here */
        fill_tree(instance, 1, count, count);

        /* Write data */
        retcode = TreeDataWriter_write(
            Tree_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        NDDS_Utility_sleep(&send_period);
    }

    /*
    retcode = TreeDataWriter_unregister_instance(
        Tree_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
    */

    /* Delete data sample */
    retcode = TreeTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("TreeTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count) {
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    coherentDataWriter *coherent_writer = NULL;
    coherent *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;
    struct DDS_Duration_t send_period = { 1, 0 };
    struct DDS_PublisherQos publisher_qos = DDS_PublisherQos_INITIALIZER;
    struct DDS_DataWriterQos datawriter_qos = DDS_DataWriterQos_INITIALIZER;
    int mod = 0;

    /* To customize participant QoS, use
     the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
                      DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
                      NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* To customize publisher QoS, use
     the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(participant,
                &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
                DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* If you want to change the DataWriter's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_publisher call above.
     */

    /* Start changes for coherent_presentation */

    /* Get default publisher QoS to customize */
    /*    retcode = DDS_DomainParticipant_get_default_publisher_qos(participant,
                &publisher_qos);
        if (retcode != DDS_RETCODE_OK) {
            printf("get_default_publisher_qos error\n");
            return -1;
        }

    */    /* Topic access scope means that writes from a particular datawriter to
     * multiple instances will be viewed coherently.
     */
    /*    publisher_qos.presentation.access_scope = DDS_TOPIC_PRESENTATION_QOS;
        publisher_qos.presentation.coherent_access = DDS_BOOLEAN_TRUE;

    */    /* To customize publisher QoS, use
     the configuration file USER_QOS_PROFILES.xml */
    /*    publisher = DDS_DomainParticipant_create_publisher(participant,
                &publisher_qos, NULL, DDS_STATUS_MASK_NONE);
        if (publisher == NULL) {
            printf("create_publisher error\n");
            publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
            return -1;
        }

    */    /* End changes for coherent_presentation */

    /* Register type before creating topic */
    type_name = coherentTypeSupport_get_type_name();
    retcode = coherentTypeSupport_register_type(participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* To customize topic QoS, use
     the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(participant, "Example coherent",
            type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    writer = DDS_Publisher_create_datawriter(publisher, topic,
             &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */,
             DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* If you want to change the DataWriter's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the create_datawriter call above.
     */
    /* Start changes for coherent_presentation */

    /* Get default datawriter QoS to customize */
    /*    retcode = DDS_Publisher_get_default_datawriter_qos(publisher,
                &datawriter_qos);
        if (retcode != DDS_RETCODE_OK) {
            printf("get_default_datawriter_qos error\n");
            return -1;
        }

        datawriter_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
        datawriter_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
        datawriter_qos.protocol.rtps_reliable_writer.heartbeats_per_max_samples = 0;

    */    /* To customize data writer QoS, use
     the configuration file USER_QOS_PROFILES.xml */

    /*    writer = DDS_Publisher_create_datawriter(publisher, topic,
                &datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
        if (writer == NULL) {
            printf("create_datawriter error\n");
            publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
            return -1;
        }

    */    /* End changes for coherent_presentation */

    coherent_writer = coherentDataWriter_narrow(writer);
    if (coherent_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* Create data sample for writing */

    instance = coherentTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    if (instance == NULL) {
        printf("coherentTypeSupport_create_data error\n");
        publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
        return -1;
    }

    /* Start changes for coherent_presentation */

    /* For a data type that has a key, if the same instance is going to be
     written multiple times, initialize the key here
     and register the keyed instance prior to writing */

    instance->id = 0;
    instance_handle = coherentDataWriter_register_instance(coherent_writer,
                      instance);

    DDS_Publisher_begin_coherent_changes(publisher);
    printf("Begin Coherent Changes\n");

    /* Main loop */
    for (count = 0; (sample_count == 0) || (count < sample_count); ++count) {
        NDDS_Utility_sleep(&send_period);

        mod = count % 7;
        if (mod == 6) {
            DDS_Publisher_begin_coherent_changes(publisher);
            printf("Begin Coherent Changes\n");
            continue;
        }

        /* Choose a random value for each field */
        instance->field = 'a' + mod;
        instance->value = (int) (rand() / (RAND_MAX / 10.0));

        printf("  Updating instance, %c->%d\n", instance->field,
               instance->value);

        /* Write data */
        retcode = coherentDataWriter_write(coherent_writer, instance,
                                           &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        /* Begin a new change group after sending 6 samples */
        if (mod == 5) {
            DDS_Publisher_end_coherent_changes(publisher);
            printf("End Coherent Changes\n\n");
        }

    }

    retcode = coherentDataWriter_unregister_instance(coherent_writer, instance,
              &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }

    /* Delete data sample */
    retcode = coherentTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("coherentTypeSupport_delete_data error %d\n", retcode);
    }

    /* End changes for coherent_presentation */

    /* Cleanup and delete delete all entities */
    return publisher_shutdown(participant, &publisher_qos, &datawriter_qos);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    queryconditionDataWriter *querycondition_writer = NULL;
    querycondition *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;  
    /* Send a new sample every second */
    struct DDS_Duration_t send_period = {1,0};

	/* Set up some unique IDs that can be used for the data type */
	char *myIds[] = {"GUID1", "GUID2", "GUID3"};


    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
        NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
        participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = queryconditionTypeSupport_get_type_name();
    retcode = queryconditionTypeSupport_register_type(
        participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(
        participant, "Example querycondition",
        type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize data writer QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    querycondition_writer = queryconditionDataWriter_narrow(writer);
    if (querycondition_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Create data sample for writing */

    instance = queryconditionTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    
    if (instance == NULL) {
        printf("queryconditionTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
/*
    instance_handle = queryconditionDataWriter_register_instance(
        querycondition_writer, instance);
*/
    /* Initialize random seed before entering the loop */
    srand(time(NULL));

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        /* Modify the data to be written here */

		/* Set id to be one of the three options */
		sprintf(instance->id, "%s", myIds[count%3]);

        /* Set x to a random number between 0 and 9 */
        instance->value = (int)(rand()/(RAND_MAX/10.0));

		printf("Writing querycondition, count %d, id = %s, value = %d\n", count, 
			instance->id,
			instance->value);

        /* Write data */
        retcode = queryconditionDataWriter_write(
            querycondition_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        NDDS_Utility_sleep(&send_period);
    }

/*
    retcode = queryconditionDataWriter_unregister_instance(
        querycondition_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
*/

    /* Delete data sample */
    retcode = queryconditionTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("queryconditionTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    cftDataWriter *cft_writer = NULL;
    cft *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;
    /* Send a new sample every second */
    struct DDS_Duration_t send_period = {1,0};
    /* We need this structure in case we want to change the datawriter_qos
     * programmatically.*/
    struct DDS_DataWriterQos datawriter_qos = DDS_DataWriterQos_INITIALIZER;

    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
        NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(
        participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = cftTypeSupport_get_type_name();
    retcode = cftTypeSupport_register_type(
        participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(
        participant, "Example cft",
        type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize data writer QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }



    /* If you want to set the reliability and history QoS settings
     * programmatically rather than using the XML, you will need to add
     * the following lines to your code and comment out the create_datawriter
     * call above.
     */

    /*
    retcode = DDS_Publisher_get_default_datawriter_qos(publisher,
            &datawriter_qos);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_datawriter_qos error\n");
        return -1;
    }

    datawriter_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
    datawriter_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS;
    datawriter_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS;
    datawriter_qos.history.depth = 20;

    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    */

    cft_writer = cftDataWriter_narrow(writer);
    if (cft_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Create data sample for writing */

    instance = cftTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    
    if (instance == NULL) {
        printf("cftTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
/*
    instance_handle = cftDataWriter_register_instance(
        cft_writer, instance);
*/

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        /* Modify the data to be written here */

        /* Our purpose is to increment x every time we send a sample and to
         * reset the x counter to 0 every time we send 10 samples (x=0,1,..,9).
         * Using the value of count, we can get set x to the appropriate value
         * applying % 10 operation to it.
         */
        instance->count = count;
        instance->x = count%10;

        printf("Writing cft, count %d\tx=%d\n", instance->count, instance->x);

        /* Write data */
        retcode = cftDataWriter_write(
            cft_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }

        NDDS_Utility_sleep(&send_period);
    }

/*
    retcode = cftDataWriter_unregister_instance(
        cft_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
*/

    /* Delete data sample */
    retcode = cftTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("cftTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    listenersDataWriter *listeners_writer = NULL;
    listeners *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    
    struct DDS_DataWriterListener writer_listener = 
	DDS_DataWriterListener_INITIALIZER;

    DDS_Topic *inconsistent_topic = NULL;
    DDS_DataWriter *inconsistent_topic_writer = NULL;
    const char *inconsistent_topic_type_name = NULL;
    msgDataWriter *msg_writer = NULL;

    int count = 0;  
    struct DDS_Duration_t send_period = {1,0};
    struct DDS_Duration_t sleep_period = {2,0};
    
    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(DDS_TheParticipantFactory, 
								  domainId, 
								  &DDS_PARTICIPANT_QOS_DEFAULT,
								  NULL /* listener */, 
								  DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
	printf("create_participant error\n");
	publisher_shutdown(participant);
	return -1;
    }

    /* To customize publisher QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    publisher = DDS_DomainParticipant_create_publisher(participant, 
						       &DDS_PUBLISHER_QOS_DEFAULT, 
						       NULL /* listener */,
						       DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
	printf("create_publisher error\n");
	publisher_shutdown(participant);
	return -1;
    }

    /* Create ande Delete Inconsistent Topic 
     * ---------------------------------------------------------------
     * Here we create an inconsistent topic to trigger the subscriber 
     * application's callback. 
     * The inconsistent topic is created with the topic name used in 
     * the Subscriber application, but with a different data type -- 
     * the msg data type defined in partitions.idl.
     * Once it is created, we sleep to ensure the applications discover
     * each other and delete the Data Writer and Topic.
     */
    
    /* First we register the msg type -- we name it
     * inconsistent_topic_type_name to avoid confusion. 
     */
    printf("Creating Inconsistent Topic...  \n");

    inconsistent_topic_type_name = msgTypeSupport_get_type_name();
    retcode = msgTypeSupport_register_type(participant, 
					   inconsistent_topic_type_name);
    if (retcode != DDS_RETCODE_OK) {
	printf("register_type error %d\n", retcode);
	publisher_shutdown(participant);
	return -1;
    }

    inconsistent_topic = 
	DDS_DomainParticipant_create_topic(participant,
					   "Example listeners",
					   inconsistent_topic_type_name, 
					   &DDS_TOPIC_QOS_DEFAULT, 
					   NULL,
					   DDS_STATUS_MASK_NONE);
    if (inconsistent_topic == NULL) {
	printf("create_topic error (inconsistent topic)\n");
	publisher_shutdown(participant);
	return -1;
    }
    
    /* We have to associate a writer to the topic, as Topic information is not
     * actually propagated until the creation of an associated writer.
     */
    inconsistent_topic_writer = DDS_Publisher_create_datawriter(publisher,
								inconsistent_topic, 
								&DDS_DATAWRITER_QOS_DEFAULT, 
								NULL /* listener */,
								DDS_STATUS_MASK_NONE);
    if (inconsistent_topic_writer == NULL) {
	printf("create_datawriter error\n");
	publisher_shutdown(participant);
	return -1;
    }

    msg_writer = msgDataWriter_narrow(inconsistent_topic_writer);
    if (msg_writer == NULL) {
	printf("DataWriter narrow error\n");
	publisher_shutdown(participant);
	return -1;
    }
    
    /* Sleep to leave time for applications to discover each other */
    NDDS_Utility_sleep(&sleep_period);

    /* Before creating the "consistent" topic, we delete the Data Writer and the
     * inconsistent topic.
     */
    retcode = DDS_Publisher_delete_datawriter(publisher, inconsistent_topic_writer);
    if (retcode != DDS_RETCODE_OK) {
        printf("delete_datawriter error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    retcode = DDS_DomainParticipant_delete_topic(participant, inconsistent_topic);
    if (retcode != DDS_RETCODE_OK) {
        printf("delete_topic error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    printf("... Deleted Inconsistent Topic\n\n");

    /* Create Consistent Topic 
     * -----------------------------------------------------------------
     * Once we have created the inconsistent topic with the wrong type, 
     * we create a topic with the right type name -- listeners -- that we 
     * will use to publish data. 
     */

    /* Register type before creating topic */
    type_name = listenersTypeSupport_get_type_name();
    retcode = listenersTypeSupport_register_type(participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
	printf("register_type error %d\n", retcode);
	publisher_shutdown(participant);
	return -1;
    }

    /* To customize topic QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    topic = DDS_DomainParticipant_create_topic(participant, 
					       "Example listeners",
					       type_name, 
					       &DDS_TOPIC_QOS_DEFAULT, 
					       NULL /* listener */,
					       DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
	printf("create_topic error\n");
	publisher_shutdown(participant);
	return -1;
    }

       

    /* We will use the Data Writer Listener defined above to print
     * a message when some of events are triggered in the DataWriter. 
     * To do that, first we have to pass the writer_listener and then
     * we have to enable all status in the status mask.
     */

    /* Set up participant listener */
    writer_listener.on_offered_deadline_missed =
	DataWriterListener_on_offered_deadline_missed;
    writer_listener.on_liveliness_lost =
	DataWriterListener_on_liveliness_lost;
    writer_listener.on_offered_incompatible_qos =
	DataWriterListener_on_offered_incompatible_qos;
    writer_listener.on_publication_matched =
	DataWriterListener_on_publication_matched;
    writer_listener.on_reliable_writer_cache_changed = 
	DataWriterListener_on_reliable_writer_cache_changed;
    writer_listener.on_reliable_reader_activity_changed = 
	DataWriterListener_on_reliable_reader_activity_changed;

    writer = DDS_Publisher_create_datawriter(publisher,
					     topic,
					     &DDS_DATAWRITER_QOS_DEFAULT, 
					     &writer_listener  /* listener */, 
					     DDS_STATUS_MASK_ALL /* enable all statuses */);
    if (writer == NULL) {
	printf("create_datawriter error\n");
	publisher_shutdown(participant);
	return -1;
    }
    listeners_writer = listenersDataWriter_narrow(writer);
    if (listeners_writer == NULL) {
	printf("DataWriter narrow error\n");
	publisher_shutdown(participant);
	return -1;
    }

    /* Create data sample for writing */
    instance = listenersTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    

    if (instance == NULL) {
	printf("listenersTypeSupport_create_data error\n");
	publisher_shutdown(participant);
	return -1;
    }

    /* For a data type that has a key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
    /*
      instance_handle = listenersDataWriter_register_instance(
      listeners_writer, instance);
    */

    printf("Publishing data using Consinstent Topic... \n");
    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

	printf("Writing listeners, count %d\n", count);

	/* Modify the data to be written here */
        instance->x = count;

	/* Write data */
	retcode = listenersDataWriter_write(
					    listeners_writer, instance, &instance_handle);
	if (retcode != DDS_RETCODE_OK) {
	    printf("write error %d\n", retcode);
	}

	NDDS_Utility_sleep(&send_period);
    }

    /*
      retcode = listenersDataWriter_unregister_instance(
      listeners_writer, instance, &instance_handle);
      if (retcode != DDS_RETCODE_OK) {
      printf("unregister instance error %d\n", retcode);
      }
    */

    /* Delete data sample */
    retcode = listenersTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
	printf("listenersTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}
static int publisher_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Publisher *publisher = NULL;
    DDS_Topic *topic = NULL;
    DDS_DataWriter *writer = NULL;
    numbersDataWriter *numbers_writer = NULL;
    numbers *instance = NULL;
    DDS_ReturnCode_t retcode;
    DDS_InstanceHandle_t instance_handle = DDS_HANDLE_NIL;
    const char *type_name = NULL;
    int count = 0;  
    struct DDS_Duration_t send_period = {4,0};

    /* To customize participant QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    participant = DDS_DomainParticipantFactory_create_participant(
        DDS_TheParticipantFactory, domainId, &DDS_PARTICIPANT_QOS_DEFAULT,
        NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (participant == NULL) {
        printf("create_participant error\n");
        publisher_shutdown(participant);
        return -1;
    }
    /* If you want to change the DomainParticipant's QoS programmatically  
     * rather than using the XML file, you will need to add the following lines 
     * to your code and comment out the create_participant call above.
     *
     * In this case, we set the transport settings in the XML by default, but
	 * in the numbers_common_create_participant call, we set up the transport
	 * properties either using the Properties QoS or with the transport 
	 * property objects.
     */

    /*participant = numbers_common_create_participant(domainId);
    if (participant == NULL) {
        publisher_shutdown(participant);
        return -1;
    }*/

    if (numbers_common_verify_qos(participant) != 0) {
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize publisher QoS, use
       DDS_DomainParticipant_get_default_publisher_qos() */
    publisher = DDS_DomainParticipant_create_publisher(
        participant, &DDS_PUBLISHER_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (publisher == NULL) {
        printf("create_publisher error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Register type before creating topic */
    type_name = numbersTypeSupport_get_type_name();
    retcode = numbersTypeSupport_register_type(
        participant, type_name);
    if (retcode != DDS_RETCODE_OK) {
        printf("register_type error %d\n", retcode);
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize topic QoS, use
       DDS_DomainParticipant_get_default_topic_qos() */
    topic = DDS_DomainParticipant_create_topic(
        participant, "Example numbers",
        type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
        DDS_STATUS_MASK_NONE);
    if (topic == NULL) {
        printf("create_topic error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* To customize data writer QoS, use
       DDS_Publisher_get_default_datawriter_qos() */
    writer = DDS_Publisher_create_datawriter(
        publisher, topic,
        &DDS_DATAWRITER_QOS_DEFAULT, NULL /* listener */, DDS_STATUS_MASK_NONE);
    if (writer == NULL) {
        printf("create_datawriter error\n");
        publisher_shutdown(participant);
        return -1;
    }
    numbers_writer = numbersDataWriter_narrow(writer);
    if (numbers_writer == NULL) {
        printf("DataWriter narrow error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* Create data sample for writing */
    instance = numbersTypeSupport_create_data_ex(DDS_BOOLEAN_TRUE);
    if (instance == NULL) {
        printf("numbersTypeSupport_create_data error\n");
        publisher_shutdown(participant);
        return -1;
    }

    /* For data type that has key, if the same instance is going to be
       written multiple times, initialize the key here
       and register the keyed instance prior to writing */
/*
    instance_handle = numbersDataWriter_register_instance(
        numbers_writer, instance);
*/
	instance->number = 1000;
	instance->halfNumber = (float)(instance->number)/2;

    /* Main loop */
    for (count=0; (sample_count == 0) || (count < sample_count); ++count) {

        printf("Writing numbers, count %d\n", count);

        /* Modify the data to be written here */
        

        /* Write data */
        retcode = numbersDataWriter_write(
            numbers_writer, instance, &instance_handle);
        if (retcode != DDS_RETCODE_OK) {
            printf("write error %d\n", retcode);
        }
		
	instance->number = instance->halfNumber;
	instance->halfNumber = (float)(instance->number)/2;

        NDDS_Utility_sleep(&send_period);
    }

/*
    retcode = numbersDataWriter_unregister_instance(
        numbers_writer, instance, &instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("unregister instance error %d\n", retcode);
    }
*/

    /* Delete data sample */
    retcode = numbersTypeSupport_delete_data_ex(instance, DDS_BOOLEAN_TRUE);
    if (retcode != DDS_RETCODE_OK) {
        printf("numbersTypeSupport_delete_data error %d\n", retcode);
    }

    /* Cleanup and delete delete all entities */         
    return publisher_shutdown(participant);
}