Example #1
2
int
dds_participant_create (
    dds_entity_t * pp,
    const dds_domainid_t domain,
    const dds_qos_t * qos,
    const dds_participantlistener_t * listener)
{
    DDS_ReturnCode_t result = DDS_RETCODE_OK;
    DDS_DomainParticipantFactory factory;
    struct DomainParticipantInfo *info;
    struct DDS_DomainParticipantListener dpl;
    struct DDS_DomainParticipantListener *lp = NULL;
    DDS_StatusMask mask = (listener) ? DDS_STATUS_MASK_ANY : 0;
    DDS_DomainParticipantQos *pQos;

    DDS_REPORT_STACK();

    if (pp) {
        info = dds_participant_info_new();

        if (listener) {
            info->listener = os_malloc(sizeof(dds_participantlistener_t));
            *info->listener = *listener;
            lp = &dpl;
            dds_participant_listener_init(&dpl, info);
        }

        factory = DDS_DomainParticipantFactory_get_instance();
        if (factory) {
            if (qos) {
                pQos = DDS_DomainParticipantQos__alloc();
                result = DDS_DomainParticipantFactory_get_default_participant_qos(factory, pQos);
                if (result == DDS_RETCODE_OK) {
                    dds_qos_to_participant_qos(pQos, qos);
                    *pp = DDS_DomainParticipantFactory_create_participant(factory, domain, pQos, lp, mask);
                }
                DDS_free(pQos);
            } else {
                *pp = DDS_DomainParticipantFactory_create_participant(factory, domain, DDS_PARTICIPANT_QOS_DEFAULT, lp, mask);
            }
            if (*pp) {
                result = DDS_Entity_set_user_data(*pp, (DDS_EntityUserData)info);
            } else {
                result = dds_report_get_error_code();
            }
        } else {
            result = dds_report_get_error_code();
        }
        DDS_Entity_release_user_data((DDS_EntityUserData)info);
    } else {
        result =  DDS_RETCODE_BAD_PARAMETER;
        DDS_REPORT(result, "Entity parameter is NULL.");
    }

    DDS_REPORT_FLUSH(NULL, result != DDS_RETCODE_OK);

    return DDS_ERRNO(result, DDS_MOD_KERNEL, DDS_ERR_Mx);
}
static int subscriber_main(int domain_id, int sample_count,
        char *participant_auth) {
    DDS_DomainParticipant *participant = NULL;
    DDS_Subscriber *subscriber = NULL;
    DDS_Topic *topic = NULL;
    struct DDS_DataReaderListener reader_listener =
            DDS_DataReaderListener_INITIALIZER;
    DDS_DataReader *reader = NULL;
    DDS_ReturnCode_t retcode;
    const char *type_name = NULL;
    int count = 0;
    struct DDS_Duration_t poll_period = { 1, 0 };
    struct DDS_DomainParticipantQos participant_qos =
            DDS_DomainParticipantQos_INITIALIZER;
    int len, max;

    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;
    }

    /* The maximum length for USER_DATA QoS field is set by default
       to 256 bytes. To increase it programmatically uncomment the
       following line of code. */
    /*
    participant_qos.resource_limits.participant_user_data_max_length = 1024;
    */

    /* We include the subscriber credentials into de USER_DATA QoS. */
    len = strlen(participant_auth) + 1;
    max = participant_qos.resource_limits.participant_user_data_max_length;

    if (len > max) {
        printf("error, participant user_data exceeds resource limits\n");
    } else {
        /*
         *  DDS_Octet is defined to be 8 bits.  If chars are not 8 bits
         *  on your system, this will not work.
         */
        DDS_OctetSeq_from_array(&participant_qos.user_data.value,
                (DDS_Octet*) (participant_auth), len);
    }

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

    /* Done!  All the listeners are installed, so we can enable the
     * participant now.
     */
    if (DDS_Entity_enable((DDS_Entity*) participant) != DDS_RETCODE_OK) {
        printf("***Error: Failed to Enable Participant\n");
        return -1;
    }

    /* To customize subscriber QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    subscriber = DDS_DomainParticipant_create_subscriber(participant,
            &DDS_SUBSCRIBER_QOS_DEFAULT, NULL /* listener */,
            DDS_STATUS_MASK_NONE);
    if (subscriber == NULL) {
        printf("create_subscriber error\n");
        subscriber_shutdown(participant);
        return -1;
    }

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

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

    /* Set up a data reader listener */
    reader_listener.on_requested_deadline_missed =
            msgListener_on_requested_deadline_missed;
    reader_listener.on_requested_incompatible_qos =
            msgListener_on_requested_incompatible_qos;
    reader_listener.on_sample_rejected = msgListener_on_sample_rejected;
    reader_listener.on_liveliness_changed = msgListener_on_liveliness_changed;
    reader_listener.on_sample_lost = msgListener_on_sample_lost;
    reader_listener.on_subscription_matched =
            msgListener_on_subscription_matched;
    reader_listener.on_data_available = msgListener_on_data_available;

    /* To customize data reader QoS, use
       the configuration file USER_QOS_PROFILES.xml */
    reader = DDS_Subscriber_create_datareader(subscriber,
            DDS_Topic_as_topicdescription(topic), &DDS_DATAREADER_QOS_DEFAULT,
            &reader_listener, DDS_STATUS_MASK_ALL);
    if (reader == NULL) {
        printf("create_datareader error\n");
        subscriber_shutdown(participant);
        return -1;
    }

    /* Main loop */
    for (count = 0; (sample_count == 0) || (count < sample_count); ++count) {
        /*
         printf("msg subscriber sleeping for %d sec...\n",
         poll_period.sec);
         */
        NDDS_Utility_sleep(&poll_period);
    }

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