Exemplo n.º 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);
}
Exemplo n.º 2
0
void createParticipant(const char * partitiontName)
{
    g_domainParticipantFactory = DDS_DomainParticipantFactory_get_instance();
    checkHandle(g_domainParticipantFactory, "DDS_DomainParticipantFactory_get_instance");

    g_domainParticipant = DDS_DomainParticipantFactory_create_participant(g_domainParticipantFactory, g_domainId, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_ANY_STATUS);

    checkHandle(g_domainParticipant, "DDS_DomainParticipantFactory_create_participant");

    g_partitionName = partitiontName;
}
Exemplo n.º 3
0
/*
	Create a ROS 2 embedded node

		Internally a ROS 2 node consists of a domain participant created
		in a specific domain.
*/
void create_node(void)
{
	if (verbose)
		printf("create_node()\n");

	/* Create a DDS entity, name hardcoded for now */
	DDS_entity_name ("ROS2Embedded-Tinq-Nuttx");
	/* Create a domain participant */
	part = DDS_DomainParticipantFactory_create_participant (domain_id, NULL, NULL, 0);
	if (!part) {
		printf ("create_node() can't create participant!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("create_node() DDS Domain Participant created.\r\n");
}
Exemplo n.º 4
0
int
main (
    int argc,
    char *argv[]
    )
{
    DDS_ConditionSeq                        *conditionList;
    DDS_WaitSet                              w;
    DDS_Condition                            exp_condition;
    pong_handler                            *active_handler;

    DDS_DomainParticipantQos                 *dpQos;
    DDS_TopicQos                             *tQos;
    DDS_PublisherQos                         *pQos;
    DDS_DataWriterQos                        *dwQos;
    DDS_SubscriberQos                        *sQos;
    DDS_DataReaderQos                        *drQos;
    
    time_t                                   clock = time (NULL);
    DDS_Duration_t                           wait_timeout = {3,0};

    DDS_ReturnCode_t                         result;
    DDS_boolean                              finish_flag = FALSE;
    DDS_boolean                              timeout_flag = FALSE;
    DDS_boolean                              terminate = FALSE;

    int                                      imax = 1;
    int                                      i;
    unsigned int                             block;

    init_clock();
    /*
     * init timing statistics 
     */
    init_stats (&roundtrip,    "round_trip");
    init_stats (&write_access, "write_access");
    init_stats (&read_access,  "read_access");

    /*
     * Evaluate cmdline arguments
     */
    if (argc != 1) {
        if (argc != 6) {
            printf ("Invalid.....\n Usage: %s [blocks blocksize topic_id WRITE_PARTITION READ_PARTITION]\n", argv[0]);
            exit (1);
        }
        nof_blocks      = atoi (argv[1]);
        nof_cycles      = atoi (argv[2]);
        topic_id        = argv[3][0];
        write_partition = argv[4];
        read_partition  = argv[5];
    }

    /*
     * Create WaitSet
     */
    w = DDS_WaitSet__alloc ();
    /*
     * Initialize Qos variables
     */
    dpQos = DDS_DomainParticipantQos__alloc();
    tQos  = DDS_TopicQos__alloc();
    pQos  = DDS_PublisherQos__alloc();
    dwQos = DDS_DataWriterQos__alloc();
    sQos  = DDS_SubscriberQos__alloc();
    drQos = DDS_DataReaderQos__alloc();
    /*
     * Initialize condition list
     */
    conditionList = NULL;

    /*
     * Create participant
     */
    dpf = DDS_DomainParticipantFactory_get_instance ();
	dp = DDS_DomainParticipantFactory_create_participant (dpf, myDomain, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (dp == DDS_HANDLE_NIL) {
        printf ("%s PING: ERROR - Splice Daemon not running", argv[0]);
        exit (1);
    }

    /* 
     * Create PING publisher
     */
    DDS_DomainParticipant_get_default_publisher_qos (dp, pQos);
    pQos->partition.name._length = 1;
    pQos->partition.name._maximum = 1;
    pQos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    pQos->partition.name._buffer[0] = DDS_string_alloc (strlen(write_partition) + 1);
    strcpy (pQos->partition.name._buffer[0], write_partition);
    p = DDS_DomainParticipant_create_publisher (dp, pQos, NULL, DDS_STATUS_MASK_NONE);
    DDS_free (pQos);

    /*
     * Create PONG subscriber
     */
    DDS_DomainParticipant_get_default_subscriber_qos (dp, sQos);
    sQos->partition.name._length = 1;
    sQos->partition.name._maximum = 1;
    sQos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    sQos->partition.name._buffer[0] = DDS_string_alloc (strlen(read_partition) + 1);
    strcpy (sQos->partition.name._buffer[0], read_partition);
    s = DDS_DomainParticipant_create_subscriber (dp, sQos, NULL, DDS_STATUS_MASK_NONE);
    DDS_free (sQos);

    /*
     * PP_min_msg
     */

    /*  Create Topic */
    PP_min_dt = pingpong_PP_min_msgTypeSupport__alloc ();
    pingpong_PP_min_msgTypeSupport_register_type (PP_min_dt, dp, "pingpong::PP_min_msg");
    PP_min_topic = DDS_DomainParticipant_create_topic (dp, "PP_min_topic", "pingpong::PP_min_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_min_writer = DDS_Publisher_create_datawriter (p, PP_min_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_min_reader = DDS_Subscriber_create_datareader (s, PP_min_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_min_sc = DDS_DataReader_get_statuscondition (PP_min_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_min_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_min_sc);

    /*
     * PP_seq_msg
     */

    /*  Create Topic */
    PP_seq_dt = pingpong_PP_seq_msgTypeSupport__alloc ();
    pingpong_PP_seq_msgTypeSupport_register_type (PP_seq_dt, dp, "pingpong::PP_seq_msg");
    PP_seq_topic = DDS_DomainParticipant_create_topic (dp, "PP_seq_topic", "pingpong::PP_seq_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_seq_writer = DDS_Publisher_create_datawriter (p, PP_seq_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_seq_reader = DDS_Subscriber_create_datareader (s, PP_seq_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    
    /* Add datareader statuscondition to waitset */
    PP_seq_sc = DDS_DataReader_get_statuscondition (PP_seq_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_seq_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_seq_sc);
    
    /*
     * PP_string_msg
     */

    /*  Create Topic */
    PP_string_dt = pingpong_PP_string_msgTypeSupport__alloc ();
    pingpong_PP_string_msgTypeSupport_register_type (PP_string_dt, dp, "pingpong::PP_string_msg");
    PP_string_topic = DDS_DomainParticipant_create_topic (dp, "PP_string_topic", "pingpong::PP_string_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_string_writer = DDS_Publisher_create_datawriter (p, PP_string_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_string_reader = DDS_Subscriber_create_datareader (s, PP_string_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_string_sc = DDS_DataReader_get_statuscondition (PP_string_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_string_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_string_sc);
    
    /*
     * PP_fixed_msg
     */
    
    /*  Create Topic */
    PP_fixed_dt = pingpong_PP_fixed_msgTypeSupport__alloc ();
    pingpong_PP_fixed_msgTypeSupport_register_type (PP_fixed_dt, dp, "pingpong::PP_fixed_msg");
    PP_fixed_topic = DDS_DomainParticipant_create_topic (dp, "PP_fixed_topic", "pingpong::PP_fixed_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_fixed_writer = DDS_Publisher_create_datawriter (p, PP_fixed_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_fixed_reader = DDS_Subscriber_create_datareader (s, PP_fixed_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    
    /* Add datareader statuscondition to waitset */
    PP_fixed_sc = DDS_DataReader_get_statuscondition (PP_fixed_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_fixed_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_fixed_sc);
    
    /*
     * PP_array_msg
     */
    
    /*  Create Topic */
    PP_array_dt = pingpong_PP_array_msgTypeSupport__alloc ();
    pingpong_PP_array_msgTypeSupport_register_type (PP_array_dt, dp, "pingpong::PP_array_msg");
    PP_array_topic = DDS_DomainParticipant_create_topic (dp, "PP_array_topic", "pingpong::PP_array_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_array_writer = DDS_Publisher_create_datawriter (p, PP_array_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_array_reader = DDS_Subscriber_create_datareader (s, PP_array_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    
    /* Add datareader statuscondition to waitset */
    PP_array_sc = DDS_DataReader_get_statuscondition (PP_array_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_array_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_array_sc);

    /*
     * PP_quit_msg
     */
    
    /*  Create Topic */
    PP_quit_dt = pingpong_PP_quit_msgTypeSupport__alloc ();
    pingpong_PP_quit_msgTypeSupport_register_type (PP_quit_dt, dp, "pingpong::PP_quit_msg");
    PP_quit_topic = DDS_DomainParticipant_create_topic (dp, "PP_quit_topic", "pingpong::PP_quit_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_quit_writer = DDS_Publisher_create_datawriter (p, PP_quit_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    for (block = 0; block < nof_blocks ; block++) {
        while (!finish_flag) {
            /*
             * Send Initial message
             */
            timeout_flag = FALSE;
            
            switch(topic_id) {
                case 'm':
                    {
                        /* printf ("PING: sending initial ping_min\n"); */
                        pingpong_PP_min_msg *PPdata = pingpong_PP_min_msg__alloc ();
                        exp_condition = PP_min_sc;
                        active_handler = &PP_min_handler;
                        PPdata->count = 0;
                        PPdata->block = block;
                        preWriteTime = timeGet ();
                        result = pingpong_PP_min_msgDataWriter_write (PP_min_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                case 'q':
                    {
                        /* printf ("PING: sending initial ping_seq\n"); */
                        pingpong_PP_seq_msg *PPdata = pingpong_PP_seq_msg__alloc ();
                        exp_condition = PP_seq_sc;
                        active_handler = &PP_seq_handler;
                        PPdata->count = 0;
                        PPdata->block = block;
                        preWriteTime = timeGet ();
                        result = pingpong_PP_seq_msgDataWriter_write (PP_seq_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                case 's':
                    {
                        /* printf ("PING: sending initial ping_string\n"); */
                        pingpong_PP_string_msg *PPdata = pingpong_PP_string_msg__alloc ();
                        exp_condition = PP_string_sc;
                        active_handler = &PP_string_handler;
                        PPdata->count = 0;
                        PPdata->block = block;
                        PPdata->a_string = DDS_string_alloc (8);
                        strcpy (PPdata->a_string, "a_string");
                        preWriteTime = timeGet ();
                        result = pingpong_PP_string_msgDataWriter_write (PP_string_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                case 'f':
                    {
                        /* printf ("PING: sending initial ping_fixed\n"); */
                        pingpong_PP_fixed_msg *PPdata = pingpong_PP_fixed_msg__alloc ();
                        exp_condition = PP_fixed_sc;
                        active_handler = &PP_fixed_handler;
                        PPdata->count = 0;
                        PPdata->block = block;
                        PPdata->a_bstring = DDS_string_alloc (9);
                        strcpy (PPdata->a_bstring, "a_bstring");
                        preWriteTime = timeGet ();
                        result = pingpong_PP_fixed_msgDataWriter_write (PP_fixed_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                case 'a':
                    {
                        /* printf ("PING: sending initial ping_array\n"); */
                        pingpong_PP_array_msg *PPdata = pingpong_PP_array_msg__alloc ();
                        exp_condition = PP_array_sc;
                        active_handler = &PP_array_handler;
                        PPdata->count = 0;
                        PPdata->block = block;
                        preWriteTime = timeGet ();
                        result = pingpong_PP_array_msgDataWriter_write (PP_array_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                case 't':
                    {
                        /* printf ("PING: sending initial ping_quit\n"); */
                        pingpong_PP_quit_msg *PPdata = pingpong_PP_quit_msg__alloc();
                        PPdata->quit = TRUE;
                        terminate = TRUE;
                        finish_flag = TRUE;
                        preWriteTime = timeGet ();
                        result = pingpong_PP_quit_msgDataWriter_write (PP_quit_writer, PPdata, DDS_HANDLE_NIL);
                        postWriteTime = timeGet ();
                        DDS_free (PPdata);
                    }
                    break;
                default:
                    printf("Invalid topic-id\n");
                    exit(1);
            }

	    if (!terminate) {
                roundTripTime = preWriteTime;
                add_stats (&write_access, 1E6 * timeToReal (timeSub (postWriteTime, preWriteTime)));
            
                /*
                 * Wait for response, calculate timing, and send another data if not ready
                 */
                while (!(timeout_flag || finish_flag)) {
                    conditionList = DDS_ConditionSeq__alloc();
                    result = DDS_WaitSet_wait (w, conditionList, &wait_timeout);
                    if (conditionList) {
                        imax = conditionList->_length;
                        if (imax != 0) {
                            for (i = 0; i < imax; i++) {
                                if (conditionList->_buffer[i] == exp_condition) {
                                    finish_flag = active_handler (nof_cycles);
                                } else {
                                    printf ("PING: unexpected condition triggered: %x\n",
                                            (unsigned int)conditionList->_buffer[i]);
                                }
                            }
                        } else {
                            printf ("PING: TIMEOUT - message lost\n");
                            timeout_flag = TRUE;
                        }
                        DDS_free(conditionList);
                    } else {
                        printf ("PING: TIMEOUT - message lost\n");
                        timeout_flag = TRUE;
					}
                }
            }
        }
        if (!terminate) {
            finish_flag = FALSE;
            if (block == 0) {
                printf ("# PING PONG measurements (in us) \n");
                printf ("# Executed at: %s", ctime(&clock));
                printf ("#           Roundtrip time [us]             Write-access time [us]          Read-access time [us]\n");
                printf ("# Block     Count   mean    min    max      Count   mean    min    max      Count   mean    min    max\n");
            }
            printf ("%6d %10d %6.0f %6.0f %6.0f %10d %6.0f %6.0f %6.0f %10d %6.0f %6.0f %6.0f\n",
                block,
                roundtrip.count,
                roundtrip.average,
                roundtrip.min,
                roundtrip.max,
                write_access.count,
                write_access.average,
                write_access.min,
                write_access.max,
                read_access.count,
                read_access.average,
                read_access.min,
                read_access.max);
            fflush (NULL);
            init_stats (&write_access, "write_access");
            init_stats (&read_access,  "read_access");
            init_stats (&roundtrip,    "round_trip");
        }
    }
    result = DDS_Subscriber_delete_datareader (s, PP_min_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_min_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_seq_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_seq_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_string_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_string_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_fixed_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_fixed_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_array_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_array_writer);
    result = DDS_Publisher_delete_datawriter (p, PP_quit_writer);
    result = DDS_DomainParticipant_delete_subscriber (dp, s);
    result = DDS_DomainParticipant_delete_publisher (dp, p);
    result = DDS_DomainParticipant_delete_topic (dp, PP_min_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_seq_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_string_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_fixed_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_array_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_quit_topic);
    result = DDS_DomainParticipantFactory_delete_participant (dpf, dp);
    DDS_free (w);
    DDS_free (PP_min_dt);
    DDS_free (PP_seq_dt);
    DDS_free (PP_string_dt);
    DDS_free (PP_fixed_dt);
    DDS_free (PP_array_dt);
    DDS_free (PP_quit_dt);
    DDS_free (dpQos);
    DDS_free (tQos);
    DDS_free (dwQos);
    DDS_free (drQos);

    return 0;
}
Exemplo n.º 5
0
int main_subscriber (int argc, const char **argv)
{
	DDS_DataWriterQos 	wr_qos;
	DDS_DataReaderQos	rd_qos;
	DDS_ReturnCode_t	error;

	sprintf (user_name, ".pid.%u", getpid ());
	DDS_entity_name ("Technicolor Chatroom");

#ifdef DDS_SECURITY
	if (cert_path || key_path || engine_id)
		enable_security ();
#endif
	part = DDS_DomainParticipantFactory_create_participant (domain_id, NULL, NULL, 0);
	if (!part) {
		printf ("Can't create participant!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Domain Participant created.\r\n");

	ts = Vector3_type_new ();
	if (!ts) {
		printf ("Can't create vector3 message type!\r\n");
		exit (1);
	}
	error = DDS_DynamicTypeSupport_register_type (ts, part, "simple_msgs::dds_::Vector3_");
	if (error) {
		printf ("Can't register vector3 message type.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Topic type ('%s') registered.\r\n", "simple_msgs::dds_::Vector3_");

	topic = DDS_DomainParticipant_create_topic (part, "imu", "simple_msgs::dds_::Vector3_", NULL, NULL, 0);
	if (!topic) {
		printf ("Can't register vector3 message type.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Vector3 Topic created.\r\n");

	td = DDS_DomainParticipant_lookup_topicdescription (part, "imu");
	if (!td) {
		printf ("Can't get topicdescription.\r\n");
		exit (1);
	}
	pub = DDS_DomainParticipant_create_publisher (part, NULL, NULL, 0);
	if (!pub) {
		printf ("DDS_DomainParticipant_create_publisher () failed!\r\n");
		exit (1);
	}
	DDS_Publisher_get_default_datawriter_qos (pub, &wr_qos);
#ifdef TRANSIENT_LOCAL
	wr_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS;
#endif
#ifdef RELIABLE
	wr_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
#endif
#ifdef KEEP_ALL
	wr_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
	wr_qos.history.depth = DDS_LENGTH_UNLIMITED;
	wr_qos.resource_limits.max_samples_per_instance = HISTORY;
	wr_qos.resource_limits.max_instances = HISTORY * 10;
	wr_qos.resource_limits.max_samples = HISTORY * 4;
#else
	wr_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS;
	wr_qos.history.depth = HISTORY;
#endif
	/* Create a Data Writer. */
	dw = DDS_Publisher_create_datawriter (pub, topic, &wr_qos, NULL, 0);
	if (!dw) {
		printf ("Unable to create vector3 message writer.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Vector3 message writer created.\r\n");

	sub = DDS_DomainParticipant_create_subscriber (part, NULL, NULL, 0); 
	if (!sub) {
		printf ("DDS_DomainParticipant_create_subscriber () returned an error!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Subscriber created.\r\n");

	DDS_Subscriber_get_default_datareader_qos (sub, &rd_qos);
#ifdef TRANSIENT_LOCAL
	rd_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS;
#endif
#ifdef RELIABLE
	rd_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
#endif
#ifdef KEEP_ALL
	rd_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
	rd_qos.history.depth = DDS_LENGTH_UNLIMITED;
	rd_qos.resource_limits.max_samples_per_instance = HISTORY;
	rd_qos.resource_limits.max_instances = HISTORY * 10;
	rd_qos.resource_limits.max_samples = HISTORY * 4;
#else
	rd_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS;
	rd_qos.history.depth = HISTORY;
#endif
	dr = DDS_Subscriber_create_datareader (sub, td, &rd_qos,
#ifndef WAITSETS
			 &msg_listener, DDS_DATA_AVAILABLE_STATUS);
#else
			 NULL, 0);
#endif
	if (!dr) {
		printf ("DDS_DomainParticipant_create_datareader () returned an error!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Vector message reader created.\r\n");

#ifdef WAITSETS
	start_imu_reader (dr);
#endif

	// publisher thread
	//thread_create (rt2, dds_send_imu, dr);	

	// DDS Debug shell thread
	do_dds_shell (dw);

#ifdef WAITSETS
	stop_imu_reader (dr);
#endif
	DDS_Publisher_delete_datawriter (pub, dw);
	usleep (200000);
	error = DDS_DomainParticipant_delete_contained_entities (part);
	if (verbose)
		printf ("DDS Entities deleted (error = %u).\r\n", error);

	Vector3_type_free (ts);
	if (verbose)
		printf ("Vector3 Type deleted.\r\n");

	error = DDS_DomainParticipantFactory_delete_participant (part);
	if (verbose)
		printf ("DDS Participant deleted (error = %u).\r\n", error);

#ifdef DDS_SECURITY
	if (cert_path || key_path || engine_id)
		cleanup_security ();
#endif
	return (0);
}
Exemplo n.º 6
0
static void test_listener (void)
{
	DDS_DomainParticipant		p;
	DDS_Topic			t;
	DDS_TopicDescription		td;
	DDS_Subscriber			sub;
	DDS_DataReader			dr;
	DDS_DataReaderListener		*lp;
	DDS_ReturnCode_t		r;

	p = DDS_DomainParticipantFactory_create_participant (0, DDS_PARTICIPANT_QOS_DEFAULT, NULL, 0);
	fail_unless (p != NULL);

	r = register_HelloWorldData_type (p);
	fail_unless (r == DDS_RETCODE_OK);
	t = DDS_DomainParticipant_create_topic (p, "HelloWorld", TYPE_NAME, DDS_TOPIC_QOS_DEFAULT, NULL, 0);
	fail_unless (t != NULL);

	td = DDS_DomainParticipant_lookup_topicdescription (p, "HelloWorld");
	fail_unless (td != NULL);

	sub = DDS_DomainParticipant_create_subscriber (p, DDS_SUBSCRIBER_QOS_DEFAULT, NULL, 0);
	fail_unless (sub != NULL);

	v_printf (" - Test reader listener.\r\n");
	dr = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr != NULL);

	lp = DDS_DataReader_get_listener (dr);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == NULL &&
		     lp->on_liveliness_changed == NULL &&
		     lp->on_requested_deadline_missed == NULL &&
		     lp->on_requested_incompatible_qos == NULL &&
		     lp->on_data_available == NULL &&
		     lp->on_subscription_matched == NULL &&
		     lp->on_sample_lost == NULL);

	v_printf (" - Test specific reader listener updates.\r\n");
	r = DDS_DataReader_set_listener (dr, &r_listener,
			DDS_SAMPLE_REJECTED_STATUS |
			DDS_LIVELINESS_CHANGED_STATUS |
			DDS_REQUESTED_DEADLINE_MISSED_STATUS |
			DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS |
			DDS_DATA_AVAILABLE_STATUS |
			DDS_SUBSCRIPTION_MATCHED_STATUS |
			DDS_SAMPLE_LOST_STATUS);
	fail_unless (r == DDS_RETCODE_OK);

	lp = DDS_DataReader_get_listener (dr);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == sample_rejected &&
		     lp->on_liveliness_changed == liveliness_changed &&
		     lp->on_requested_deadline_missed == requested_deadline_missed &&
		     lp->on_requested_incompatible_qos == requested_inc_qos &&
		     lp->on_data_available == data_available &&
		     lp->on_subscription_matched == subscription_matched &&
		     lp->on_sample_lost == sample_lost &&
		     lp->cookie == (void *) 0x44556677);

	v_printf (" - Test default reader listener update.\r\n");
	r = DDS_DataReader_set_listener (dr, NULL, DDS_REQUESTED_DEADLINE_MISSED_STATUS);
	fail_unless (r == DDS_RETCODE_OK);
	lp = DDS_DataReader_get_listener (dr);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == NULL &&
		     lp->on_liveliness_changed == NULL &&
		     lp->on_requested_deadline_missed == NULL &&
		     lp->on_requested_incompatible_qos == NULL &&
		     lp->on_data_available == NULL &&
		     lp->on_subscription_matched == NULL &&
		     lp->on_sample_lost == NULL);
	r = DDS_Subscriber_delete_datareader (sub, dr);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_topic (p, t);
	fail_unless (r == DDS_RETCODE_OK);
	unregister_HelloWorldData_type (p);
	r = DDS_DomainParticipantFactory_delete_participant (p);
	fail_unless (r == DDS_RETCODE_OK);
}
Exemplo n.º 7
0
static void test_aux (void)
{
	DDS_DomainParticipant		np, p;
	DDS_Subscriber			sub;
	DDS_Topic			t;
	DDS_TopicDescription		td;
	DDS_DataReader			dr, dr0, dr1;
	DDS_StatusCondition		sc, sc2;
	DDS_StatusMask			sm;
	DDS_InstanceHandle_t		h, h2;
	DDS_DataReaderQos		drq;
	DDS_ReturnCode_t		r;

	v_printf (" - Test auxiliary subscriber functions.\r\n");
	p = DDS_DomainParticipantFactory_create_participant (1, DDS_PARTICIPANT_QOS_DEFAULT, NULL, 0);
	fail_unless (p != NULL);
	sub = DDS_DomainParticipant_create_subscriber (p, DDS_SUBSCRIBER_QOS_DEFAULT, NULL, 0);
	fail_unless (sub != NULL);

	sc = DDS_Subscriber_get_statuscondition (sub);
	fail_unless (sc != NULL);
	sc2 = DDS_Entity_get_statuscondition (sub);
	fail_unless (sc2 != NULL);
	
	sm = DDS_Subscriber_get_status_changes (sub);
	fail_unless (sm == 0);
	sm = DDS_Entity_get_status_changes (sub);
	fail_unless (sm == 0);
	/*dbg_printf ("(mask=%u)", sm);*/
	h = DDS_Subscriber_get_instance_handle (sub);
	fail_unless (h != 0);
	h2 = DDS_Entity_get_instance_handle (sub);
	fail_unless (h == h2);
	np = DDS_Subscriber_get_participant (sub);
	fail_unless (np == p);

	r = register_HelloWorldData_type (p);
	fail_unless (r == DDS_RETCODE_OK);

	t = DDS_DomainParticipant_create_topic (p, "HelloWorld", TYPE_NAME, DDS_TOPIC_QOS_DEFAULT, NULL, 0);
	fail_unless (t != NULL);
	td = DDS_DomainParticipant_lookup_topicdescription (p, "HelloWorld");
	fail_unless (td != NULL);
	dr0 = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr0 != NULL);

	dr = DDS_Subscriber_lookup_datareader (sub, "HelloWorld");
	fail_unless (dr == dr0);

	dr1 = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr1 != NULL);

	delay ();

	r = DDS_Subscriber_delete_contained_entities (sub);
	fail_unless (r == DDS_RETCODE_OK);

	v_printf (" - Test default child QoS of subscriber entities\r\n");
	r = DDS_Subscriber_get_default_datareader_qos (sub, &drq);
	fail_unless (r == DDS_RETCODE_OK);

	drq.ownership.kind = DDS_EXCLUSIVE_OWNERSHIP_QOS;
	r = DDS_Subscriber_set_default_datareader_qos (sub, &drq);
	fail_unless (r == DDS_RETCODE_OK);

	dr1 = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr1 != NULL);

	delay ();

	v_printf (" - Test coherency (not implemented).\r\n");
	r = DDS_Subscriber_begin_access (sub);
	fail_unless (r == DDS_RETCODE_UNSUPPORTED);

	r = DDS_Subscriber_end_access (sub);
	fail_unless (r == DDS_RETCODE_UNSUPPORTED);

	r = DDS_Subscriber_copy_from_topic_qos (sub, &drq, NULL);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_Subscriber_notify_datareaders (sub);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_PRECONDITION_NOT_MET);
	r = DDS_Subscriber_delete_datareader (sub, dr1);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DomainParticipant_delete_topic (p, t);	
	fail_unless (r == DDS_RETCODE_OK);
	unregister_HelloWorldData_type (p);

	r = DDS_DomainParticipantFactory_delete_participant (p);
	fail_unless (r == DDS_RETCODE_OK);
}
Exemplo n.º 8
0
int
OSPL_MAIN(int argc, const char *argv[])
{
    FILE *testDataFile;
    char testDataLine[128];
#ifdef _WIN32
    char* oldSearchPath;
    char* newSearchPath;
#endif
    DDS_DomainParticipantFactory domainParticipantFactory;
    DDS_DomainParticipant domainParticipant = DDS_OBJECT_NIL;
    os_result result = os_resultSuccess;

    /** A local configuration file is used : ospllog4cplugin.xml
     * and a DomainParticpant is created with this configuration */
    os_setenv("OSPL_URI", "file://ospllog4cplugin.xml");

#ifdef _WIN32
    /**
     * Iff the patform is windows, the plugin lib directory is added onto the
     * PATH so it can be loaded */
    oldSearchPath = os_getenv(OS_LIB_LOAD_PATH_VAR);
    if (oldSearchPath == NULL)
    {
        oldSearchPath = "";
    }
    newSearchPath = os_malloc(sizeof(".." "lib") + 2 + strlen(oldSearchPath));
    sprintf(newSearchPath, "%s%c%s%c%s", "..", OS_FILESEPCHAR, "lib", OS_PATHSEPCHAR, oldSearchPath);
    os_setenv(OS_LIB_LOAD_PATH_VAR, newSearchPath);
    os_free(newSearchPath);
#endif

    /**
     * A single process DomainParticipant is created. */
    domainParticipantFactory = DDS_DomainParticipantFactory_get_instance();
    if (domainParticipantFactory == NULL)
    {
        printf("Error: can't get the domain participant factory.\n");
        return -1;
    }
    domainParticipant = DDS_DomainParticipantFactory_create_participant(domainParticipantFactory,
                                                                       DDS_DOMAIN_ID_DEFAULT,
                                                                       DDS_PARTICIPANT_QOS_DEFAULT,
                                                                       NULL,
                                                                       DDS_STATUS_MASK_NONE);
    if (domainParticipant == NULL)
    {
        printf("Error: can't create the domain participant. Please check the configuration & the contents of any error logs.\n");
        return -1;
    }

    /** After a DomainParticipant has been created the log plug-in will have
     * been initialised and will be in use. Some test data is now loaded and
     * written as report messages. */
    testDataFile = fopen("testdata.txt", "r");
    if (testDataFile != NULL)
    {
        int i = 0;
        while (i < 1000 && result == os_resultSuccess)
        {
            if (fgets(testDataLine, sizeof(testDataLine), testDataFile) != NULL)
            {
                i++;
                testDataLine[strlen(testDataLine) - 1] = '\0';
                OS_REPORT_3 ((os_reportType) (i % (OS_REPAIRED - OS_DEBUG + 1)),
                             "OpenSplice log4c plug-in example",
                             i,
                             "Test data line %d: %s\t\t%s",
                             i, testDataLine,
                             os_reportTypeText[i % (OS_REPAIRED - OS_DEBUG + 1)]
                             );
            }
            else if (i == 0)
            {
                OS_REPORT(OS_FATAL, "OpenSplice log4c plug-in example", 0,
                                        "Data cannot be read from testsdata.txt.");
                result = os_resultFail;
            }
            else
            {
                fseek(testDataFile, 0, SEEK_SET);
            }
        }
        fclose(testDataFile);
    }

    DDS_DomainParticipantFactory_delete_participant(domainParticipantFactory,
                                                    domainParticipant);
    return result;
}
static int subscriber_main(int domainId, int sample_count, int sel_cft)
{
    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};
    DDS_ContentFilteredTopic *cft = NULL;
    /* If you want to modify datareader_qos programatically you
     * will need to use the following structure. */
    struct DDS_DataReaderQos datareader_qos = DDS_DataReaderQos_INITIALIZER;
    /* For this filter we only allow 1 parameter*/
    struct DDS_StringSeq parameters;
    const char* param_list[] = {"SOME_STRING"};

    /* 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");
        subscriber_shutdown(participant);
        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 = cftTypeSupport_get_type_name();
    retcode = cftTypeSupport_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 cft",
        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;
    }

    DDS_StringSeq_initialize(&parameters);
    DDS_StringSeq_set_maximum(&parameters, 1);
    /* Here we set the default filter using the param_list */
    DDS_StringSeq_from_array(&parameters, param_list, 1);
    
    if (sel_cft) {
        /* create_contentfilteredtopic_with_filter */
        cft = DDS_DomainParticipant_create_contentfilteredtopic_with_filter(
            participant,"ContentFilteredTopic", topic, "name MATCH %0",
            &parameters, DDS_STRINGMATCHFILTER_NAME);
        if (cft == NULL) {
            printf("create_contentfilteredtopic error\n");
            subscriber_shutdown(participant);
            return -1;
        }
    }

    /* Set up a data reader listener */
    reader_listener.on_requested_deadline_missed  =
        cftListener_on_requested_deadline_missed;
    reader_listener.on_requested_incompatible_qos =
        cftListener_on_requested_incompatible_qos;
    reader_listener.on_sample_rejected =
        cftListener_on_sample_rejected;
    reader_listener.on_liveliness_changed =
        cftListener_on_liveliness_changed;
    reader_listener.on_sample_lost =
        cftListener_on_sample_lost;
    reader_listener.on_subscription_matched =
        cftListener_on_subscription_matched;
    reader_listener.on_data_available =
        cftListener_on_data_available;

    /* To customize data reader QoS, use 
       the configuration file USER_QOS_PROFILES.xml */
    if (sel_cft) {
        printf("Using ContentFiltered Topic\n");
        reader = DDS_Subscriber_create_datareader(
            subscriber, DDS_ContentFilteredTopic_as_topicdescription(cft),
            &DDS_DATAREADER_QOS_DEFAULT, &reader_listener, DDS_STATUS_MASK_ALL);
    } else {
        printf("Using Normal Topic\n");
        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;
    }
        
    /* 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_datareader
     * calls above.
     */

    /*
    retcode = DDS_Subscriber_get_default_datareader_qos(subscriber,
        &datareader_qos);
    if (retcode != DDS_RETCODE_OK) {
        printf("get_default_datareader_qos error\n");
        return -1;
    }

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

    if (sel_cft) {
        printf("Using ContentFiltered Topic\n");
        reader = DDS_Subscriber_create_datareader(
            subscriber, DDS_ContentFilteredTopic_as_topicdescription(cft),
            &datareader_qos, &reader_listener, DDS_STATUS_MASK_ALL);
    } else {
        printf("Using Normal Topic\n");
        reader = DDS_Subscriber_create_datareader(
            subscriber, DDS_Topic_as_topicdescription(topic),
            &datareader_qos, &reader_listener, DDS_STATUS_MASK_ALL);
    }
    if (reader == NULL) {
        printf("create_datareader error\n");
        subscriber_shutdown(participant);
        return -1;
    }
*/
    /* Change the filter */
    if (sel_cft) {
        printf(">>> Now setting a new filter: name MATCH \"EVEN\"\n");
        retcode = DDS_ContentFilteredTopic_append_to_expression_parameter(
            cft, 0,"EVEN");
        if (retcode != DDS_RETCODE_OK) {
            printf("append_to_expression_parameter error\n");
            subscriber_shutdown(participant);
            return -1;
        }
    }

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

        if (sel_cft == 0) {
            continue;
        }
        if (count == 10) {
            printf("\n===========================\n");
            printf("Changing filter parameters\n");
            printf("Append 'ODD' filter\n");
            printf("===========================\n");
            retcode = DDS_ContentFilteredTopic_append_to_expression_parameter(
                cft, 0,"ODD");
            if (retcode != DDS_RETCODE_OK) {
                printf("append_to_expression_parameter error\n");
                subscriber_shutdown(participant);
                return -1;
            }
        } else if (count == 20) {
            printf("\n===========================\n");
            printf("Changing filter parameters\n");
            printf("Removing 'EVEN' filter \n");
            printf("===========================\n");

            retcode = DDS_ContentFilteredTopic_remove_from_expression_parameter(
                cft, 0,"EVEN");
            if (retcode != DDS_RETCODE_OK) {
                printf("append_to_expression_parameter error\n");
                subscriber_shutdown(participant);
                return -1;
            }
        }
    }

    /* Cleanup and delete all entities */ 
    return subscriber_shutdown(participant);
}
static int subscriber_main(int domainId, int sample_count) {
    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 };
    const char* param_list[] = { "2", "divides" };
    DDS_ContentFilteredTopic *cft = NULL;
    struct DDS_StringSeq parameters;
    struct DDS_ContentFilter custom_filter = DDS_ContentFilter_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");
        subscriber_shutdown(participant);
        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 = ccfTypeSupport_get_type_name();
    retcode = ccfTypeSupport_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 ccf",
            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;
    }

    /*------ Start changes for Custom_Content_Filter ------*/
    /* Create and register custom filter */
    custom_filter.compile = custom_filter_compile_function;
    custom_filter.evaluate = custom_filter_evaluate_function;
    custom_filter.finalize = custom_filter_finalize_function;
    custom_filter.filter_data = NULL;

    retcode = DDS_DomainParticipant_register_contentfilter(participant,
            "CustomFilter", &custom_filter);
    if (retcode != DDS_RETCODE_OK) {
        printf("Failed to register custom content filter");
        subscriber_shutdown(participant);
        return -1;
    }

    DDS_StringSeq_initialize(&parameters);
    DDS_StringSeq_set_maximum(&parameters, 2);
    DDS_StringSeq_from_array(&parameters, param_list, 2);

    cft = DDS_DomainParticipant_create_contentfilteredtopic_with_filter(
            participant, "ContentFilteredTopic", topic, "%0 %1 x", &parameters,
            "CustomFilter");
    if (cft == NULL) {
        printf("create_contentfilteredtopic error\n");
        subscriber_shutdown(participant);
        return -1;
    }

    printf("Filter: 2 divides x\n");

    /* Note that we pass 'ccf' rather than 'topic' to the datareader below */
    /*------ End changes for Custom_Content_Filter ------*/

    /* Set up a data reader listener */
    reader_listener.on_requested_deadline_missed =
            ccfListener_on_requested_deadline_missed;
    reader_listener.on_requested_incompatible_qos =
            ccfListener_on_requested_incompatible_qos;
    reader_listener.on_sample_rejected = ccfListener_on_sample_rejected;
    reader_listener.on_liveliness_changed = ccfListener_on_liveliness_changed;
    reader_listener.on_sample_lost = ccfListener_on_sample_lost;
    reader_listener.on_subscription_matched =
            ccfListener_on_subscription_matched;
    reader_listener.on_data_available = ccfListener_on_data_available;

    /*
     * NOTE THAT WE USE THE PREVIOUSLY CREATED CUSTOM FILTERED TOPIC TO READ
     * NEW SAMPLES
     */
    reader = DDS_Subscriber_create_datareader(subscriber,
            DDS_Topic_as_topicdescription(cft), &DDS_DATAREADER_QOS_DEFAULT,
            &reader_listener, DDS_STATUS_MASK_ALL);
    if (reader == NULL) {
        printf("create_datareader error\n");
        subscriber_shutdown(participant);
        return -1;
    }

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

        if (count == 10) {
            printf("changing filter parameters\n");
            printf("Filter: 15 greater-than x\n");
            DDS_String_free(DDS_StringSeq_get(&parameters, 0));
            DDS_String_free(DDS_StringSeq_get(&parameters, 1));
            *DDS_StringSeq_get_reference(&parameters, 0) = DDS_String_dup("15");
            *DDS_StringSeq_get_reference(&parameters, 1) = DDS_String_dup(
                    "greater-than");

            retcode = DDS_ContentFilteredTopic_set_expression_parameters(cft,
                    &parameters);
            if (retcode != DDS_RETCODE_OK) {
                printf("set_expression_parameters error\n");
                subscriber_shutdown(participant);
                return -1;
            }
        } else if (count == 20) {
            struct DDS_StringSeq oldParameters;
            printf("changing filter parameters\n");
            printf("Filter: 3 divides x\n");
            DDS_ContentFilteredTopic_get_expression_parameters(cft,
                    &oldParameters);
            DDS_String_free(DDS_StringSeq_get(&oldParameters, 0));
            *DDS_StringSeq_get_reference(&oldParameters, 0) = DDS_String_dup(
                    "3");
            *DDS_StringSeq_get_reference(&oldParameters, 1) = DDS_String_dup(
                    "divides");

            retcode = DDS_ContentFilteredTopic_set_expression_parameters(cft,
                    &oldParameters);
            if (retcode != DDS_RETCODE_OK) {
                printf("set_expression_parameters error\n");
                subscriber_shutdown(participant);
                return -1;
            }
        }
        NDDS_Utility_sleep(&poll_period);
    }

    /* Cleanup and delete all entities */
    return subscriber_shutdown(participant);
}
Exemplo n.º 11
0
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);
}
Exemplo n.º 12
0
Arquivo: main.c Projeto: bq/qeo-core
int main (int argc, const char *argv [])
{
	DDS_DomainParticipant	part;
	unsigned		sw, sr, dw, dr, i;
	int			error;

	DDS_entity_name ("Technicolor Interop test");

	printf ("Test 1: dynamic type first, then static.\r\n");
	printf ("----------------------------------------\r\n");

	/* Create a domain participant. */
	part = DDS_DomainParticipantFactory_create_participant (
						domain_id, NULL, NULL, 0);
	if (!part)
		fatal ("DDS_DomainParticipantFactory_create_participant () failed!");

	printf ("DDS Domain Participant created.\r\n");

	/* Register the dynamic shape topic type. */
	register_dynamic_type (part);
	printf ("DDS dynamic Topic type registered.\r\n");

	/* Register the static topic type. */
	register_static_type (part);
	printf ("DDS static Topic type registered.\r\n");

	dw = dynamic_writer_create ("Square");
	dr = dynamic_reader_create ("Square");
	sw = static_writer_create ("Square");
	sr = static_reader_create ("Square");
	for (i = 0; i < 10; i++) {
		dynamic_writer_write (dw, "Red", i * 9 + 5, i * 11 + 5);
		static_writer_write (sw, "Yellow", i * 10, i * 12);
		usleep (100000);
	}
	dynamic_writer_delete (dw);
	dynamic_reader_delete (dr);
	static_writer_delete (sw);
	static_reader_delete (sr);

	unregister_dynamic_type ();
	unregister_static_type ();

	error = DDS_DomainParticipant_delete_contained_entities (part);
	if (error)
		fatal ("DDS_DomainParticipant_delete_contained_entities () failed (%s)!", DDS_error (error));

	printf ("DDS Entities deleted\r\n");

	error = DDS_DomainParticipantFactory_delete_participant (part);
	if (error)
		fatal ("DDS_DomainParticipantFactory_delete_participant () failed (%s)!", DDS_error (error));

	printf ("DDS Participant deleted\r\n\r\n");

	printf ("Test 2: static type first, then dynamic.\r\n");
	printf ("----------------------------------------\r\n");

	/* Create a domain participant. */
	part = DDS_DomainParticipantFactory_create_participant (
						domain_id, NULL, NULL, 0);
	if (!part)
		fatal ("DDS_DomainParticipantFactory_create_participant () failed!");

	printf ("DDS Domain Participant created.\r\n");

	/* Register the static topic type. */
	register_static_type (part);
	printf ("DDS static Topic type registered.\r\n");

	/* Register the dynamic shape topic type. */
	register_dynamic_type (part);
	printf ("DDS dynamic Topic type registered.\r\n");

	sw = static_writer_create ("Circle");
	sr = static_reader_create ("Circle");
	dw = dynamic_writer_create ("Circle");
	dr = dynamic_reader_create ("Circle");
	for (i = 0; i < 10; i++) {
		static_writer_write (sw, "Green", i * 11, i * 13);
		dynamic_writer_write (dw, "Magenta", i * 10 + 5, i * 12 + 5);
		usleep (100000);
	}
	static_writer_delete (sw);
	static_reader_delete (sr);
	dynamic_writer_delete (dw);
	dynamic_reader_delete (dr);
	unregister_static_type ();
	unregister_dynamic_type ();
	error = DDS_DomainParticipant_delete_contained_entities (part);
	if (error)
		fatal ("DDS_DomainParticipant_delete_contained_entities () failed (%s)!", DDS_error (error));

	printf ("DDS Entities deleted\r\n");

	error = DDS_DomainParticipantFactory_delete_participant (part);
	if (error)
		fatal ("DDS_DomainParticipantFactory_delete_participant () failed (%s)!", DDS_error (error));

	printf ("DDS Participant deleted\r\n\r\n");

	return (0);
}
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);
}
Exemplo n.º 14
0
int
main (
    int argc,
    char *argv[])
{
    /* Generic DDS entities */
    DDS_DomainParticipantFactory    dpf;
    DDS_DomainParticipant           participant;
    DDS_Topic                       chatMessageTopic;
    DDS_Topic                       nameServiceTopic;
    DDS_MultiTopic                  namedMessageTopic;
    DDS_Subscriber                  chatSubscriber;

    /* Type-specific DDS entities */
    Chat_ChatMessageTypeSupport     chatMessageTS;
    Chat_NameServiceTypeSupport     nameServiceTS;
    Chat_NamedMessageTypeSupport    namedMessageTS;
    Chat_NamedMessageDataReader     chatAdmin;
    DDS_sequence_Chat_NamedMessage  *msgSeq;
    DDS_SampleInfoSeq               *infoSeq;

    /* QosPolicy holders */
    DDS_TopicQos                    *reliable_topic_qos;
    DDS_TopicQos                    *setting_topic_qos;
    DDS_SubscriberQos               *sub_qos;
    DDS_StringSeq                   *parameterList;

    /* DDS Identifiers */
    DDS_DomainId_t                  domain = NULL;
    DDS_ReturnCode_t                status;

    /* Others */
    DDS_unsigned_long               i;
    DDS_boolean                     terminated = FALSE;
    char *                          partitionName;
    char *                          chatMessageTypeName = NULL;
    char *                          nameServiceTypeName = NULL;
    char *                          namedMessageTypeName = NULL;
    
    /* Options: MessageBoard [ownID] */
    /* Messages having owner ownID will be ignored */
    parameterList = DDS_StringSeq__alloc();
    checkHandle(parameterList, "DDS_StringSeq__alloc");
    parameterList->_length = 1;
    parameterList->_maximum = 1;
    parameterList->_buffer = DDS_StringSeq_allocbuf(1);
    checkHandle(parameterList->_buffer, "DDS_StringSeq_allocbuf");
    
    if (argc > 1) {
        parameterList->_buffer[0] = DDS_string_alloc ( strlen(argv[1]) );
        checkHandle(parameterList->_buffer[0], "DDS_string_alloc");
        strcpy (parameterList->_buffer[0], argv[1]);
    }
    else
    {
        parameterList->_buffer[0] = DDS_string_alloc(1);
        checkHandle(parameterList->_buffer[0], "DDS_string_alloc");
        strcpy (parameterList->_buffer[0], "0");
    }
      
    /* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings. */
    dpf = DDS_DomainParticipantFactory_get_instance ();
    checkHandle(dpf, "DDS_DomainParticipantFactory_get_instance");
    participant = DDS_DomainParticipantFactory_create_participant (
        dpf, 
        domain, 
        DDS_PARTICIPANT_QOS_DEFAULT, 
        NULL, 
        DDS_STATUS_MASK_NONE);
    checkHandle(participant, "DDS_DomainParticipantFactory_create_participant");  

    /* Register the required datatype for ChatMessage. */
    chatMessageTS = Chat_ChatMessageTypeSupport__alloc();
    checkHandle(chatMessageTS, "Chat_ChatMessageTypeSupport__alloc");
    chatMessageTypeName = Chat_ChatMessageTypeSupport_get_type_name(chatMessageTS);
    status = Chat_ChatMessageTypeSupport_register_type(
        chatMessageTS, 
        participant, 
        chatMessageTypeName);
    checkStatus(status, "Chat_ChatMessageTypeSupport_register_type");
    
    /* Register the required datatype for NameService. */
    nameServiceTS = Chat_NameServiceTypeSupport__alloc();
    checkHandle(nameServiceTS, "Chat_NameServiceTypeSupport__alloc");
    nameServiceTypeName = Chat_NameServiceTypeSupport_get_type_name(nameServiceTS);
    status = Chat_NameServiceTypeSupport_register_type(
        nameServiceTS, 
        participant, 
        nameServiceTypeName);
    checkStatus(status, "Chat_NameServiceTypeSupport_register_type");
    
    /* Register the required datatype for NamedMessage. */
    namedMessageTS = Chat_NamedMessageTypeSupport__alloc();
    checkHandle(namedMessageTS, "Chat_NamedMessageTypeSupport__alloc");
    namedMessageTypeName = Chat_NamedMessageTypeSupport_get_type_name(namedMessageTS);
    status = Chat_NamedMessageTypeSupport_register_type(
        namedMessageTS, 
        participant, 
        namedMessageTypeName);
    checkStatus(status, "Chat_NamedMessageTypeSupport_register_type");
    
    /* Set the ReliabilityQosPolicy to RELIABLE. */
    reliable_topic_qos = DDS_TopicQos__alloc();
    checkHandle(reliable_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    reliable_topic_qos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
    
    /* Make the tailored QoS the new default. */
    status = DDS_DomainParticipant_set_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_set_default_topic_qos");

    /* Use the changed policy when defining the ChatMessage topic */
    chatMessageTopic = DDS_DomainParticipant_create_topic( 
        participant, 
        "Chat_ChatMessage", 
        chatMessageTypeName, 
        reliable_topic_qos, 
        NULL, 
        DDS_STATUS_MASK_NONE);
    checkHandle(chatMessageTopic, "DDS_DomainParticipant_create_topic (ChatMessage)");
    
    /* Set the DurabilityQosPolicy to TRANSIENT. */
    setting_topic_qos = DDS_TopicQos__alloc();
    checkHandle(setting_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, setting_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    setting_topic_qos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS;

    /* Create the NameService Topic. */
    nameServiceTopic = DDS_DomainParticipant_create_topic( 
        participant, 
        "Chat_NameService", 
        nameServiceTypeName, 
        setting_topic_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(nameServiceTopic, "DDS_DomainParticipant_create_topic");
    
    /* Create a multitopic that substitutes the userID with its corresponding userName. */
    namedMessageTopic = DDS_DomainParticipant_create_simulated_multitopic(
        participant, 
        "Chat_NamedMessage", 
        namedMessageTypeName, 
        "SELECT userID, name AS userName, index, content "
            "FROM Chat_NameService NATURAL JOIN Chat_ChatMessage WHERE userID <> %0",
        parameterList);
    checkHandle(namedMessageTopic, "DDS_DomainParticipant_simulate_multitopic");

    /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */
    partitionName = "ChatRoom";
    sub_qos = DDS_SubscriberQos__alloc();
    checkHandle(sub_qos, "DDS_SubscriberQos__alloc");
    status = DDS_DomainParticipant_get_default_subscriber_qos (participant, sub_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_subscriber_qos");
    sub_qos->partition.name._length = 1;
    sub_qos->partition.name._maximum = 1;
    sub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    checkHandle(sub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf");
    sub_qos->partition.name._buffer[0] = DDS_string_alloc ( strlen(partitionName) );
    checkHandle(sub_qos->partition.name._buffer[0], "DDS_string_alloc");
    strcpy (sub_qos->partition.name._buffer[0], partitionName);

    /* Create a Subscriber for the MessageBoard application. */
    chatSubscriber = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE);
    checkHandle(chatSubscriber, "DDS_DomainParticipant_create_subscriber");
    
    /* Create a DataReader for the NamedMessage Topic (using the appropriate QoS). */
    chatAdmin = DDS_Subscriber_create_datareader( 
        chatSubscriber, 
        namedMessageTopic, 
        DDS_DATAREADER_QOS_USE_TOPIC_QOS, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(chatAdmin, "DDS_Subscriber_create_datareader");
    
    /* Print a message that the MessageBoard has opened. */
    printf("MessageBoard has opened: send a ChatMessage with userID = -1 to close it....\n\n");

    /* Allocate the sequence holders for the DataReader */
    msgSeq = DDS_sequence_Chat_NamedMessage__alloc();
    checkHandle(msgSeq, "DDS_sequence_Chat_NamedMessage__alloc");
    infoSeq = DDS_SampleInfoSeq__alloc();
    checkHandle(infoSeq, "DDS_SampleInfoSeq__alloc");

    while (!terminated) {
        /* Note: using read does not remove the samples from 
           unregistered instances from the DataReader. This means 
           that the DataRase would use more and more resources. 
           That's why we use take here instead. */

        status = Chat_NamedMessageDataReader_take( 
            chatAdmin, 
            msgSeq, 
            infoSeq, 
            DDS_LENGTH_UNLIMITED, 
            DDS_ANY_SAMPLE_STATE, 
            DDS_ANY_VIEW_STATE, 
            DDS_ALIVE_INSTANCE_STATE );
        checkStatus(status, "Chat_NamedMessageDataReader_take");

        for (i = 0; i < msgSeq->_length; i++) {
            Chat_NamedMessage *msg = &(msgSeq->_buffer[i]);
            if (msg->userID == TERMINATION_MESSAGE) {
                printf("Termination message received: exiting...\n");
                terminated = TRUE;
            } else {
                printf ("%s: %s\n", msg->userName, msg->content);
            }
        }

        status = Chat_NamedMessageDataReader_return_loan(chatAdmin, msgSeq, infoSeq);
        checkStatus(status, "Chat_ChatMessageDataReader_return_loan");
        
        /* Sleep for some amount of time, as not to consume too much CPU cycles. */
        Sleep(100);
    }

    /* Remove the DataReader */
    status = DDS_Subscriber_delete_datareader(chatSubscriber, chatAdmin);
    checkStatus(status, "DDS_Subscriber_delete_datareader");
    
    /* Remove the Subscriber. */
    status = DDS_DomainParticipant_delete_subscriber(participant, chatSubscriber);
    checkStatus(status, "DDS_DomainParticipant_delete_subscriber");
    
    /* Remove the Topics. */
    status = DDS_DomainParticipant_delete_simulated_multitopic(participant, namedMessageTopic);
    checkStatus(status, "DDS_DomainParticipant_delete_simulated_multitopic");
    
    status = DDS_DomainParticipant_delete_topic(participant, nameServiceTopic);
    checkStatus(status, "DDS_DomainParticipant_delete_topic (nameServiceTopic)");
    
    status = DDS_DomainParticipant_delete_topic(participant, chatMessageTopic);
    checkStatus(status, "DDS_DomainParticipant_delete_topic (chatMessageTopic)");
    
    /* De-allocate the QoS policies. */
    DDS_free(reliable_topic_qos);
    DDS_free(setting_topic_qos);
    DDS_free(sub_qos);  // Note that DDS_free recursively de-allocates all indirections as well!!

    /* De-allocate the type-names and TypeSupport objects. */
    DDS_free(namedMessageTypeName);
    DDS_free(nameServiceTypeName);
    DDS_free(chatMessageTypeName);
    DDS_free(namedMessageTS);
    DDS_free(nameServiceTS);
    DDS_free(chatMessageTS);
    
    /* Remove the DomainParticipant. */
    status = DDS_DomainParticipantFactory_delete_participant(dpf, participant);
    checkStatus(status, "DDS_DomainParticipantFactory_delete_participant");

    return 0;
}
static int subscriber_main(int domainId, int sample_count)
{
    DDS_DomainParticipant *participant = NULL;
    DDS_Subscriber *subscriber = NULL;

    DDS_Topic *topic1 = NULL;
    DDS_Topic *topic2 = NULL;
    DDS_Topic *topic3 = NULL;

    struct DDS_SubscriberListener subscriber_listener =
            DDS_SubscriberListener_INITIALIZER;

    DDS_DataReader *reader1 = NULL;
    DDS_DataReader *reader2 = NULL;
    DDS_DataReader *reader3 = NULL;

    DDS_ReturnCode_t retcode;
    const char *type_name = NULL;
    int count = 0;
    struct DDS_Duration_t poll_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");
        subscriber_shutdown(participant);
        return -1;
    }

    /* Set up Subscriber listener and establish on_data_on_readers callback */
    /* Set up a data reader listener */
    subscriber_listener.on_data_on_readers  =
            ordered_groupListener_on_data_on_readers;

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

    /* Register the type before creating the 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);
        subscriber_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");
        subscriber_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");
        subscriber_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");
        subscriber_shutdown(participant);
        return -1;
    }

    /* DATAREADERS */

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

    reader2 = DDS_Subscriber_create_datareader(
            subscriber, DDS_Topic_as_topicdescription(topic2),
            &DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (reader2 == NULL) {
        printf("create_datareader error\n");
        subscriber_shutdown(participant);
        return -1;
    }

    reader3 = DDS_Subscriber_create_datareader(
            subscriber, DDS_Topic_as_topicdescription(topic3),
            &DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (reader3 == NULL) {
        printf("create_datareader error\n");
        subscriber_shutdown(participant);
        return -1;
    }

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

    /* Cleanup and delete all entities */ 
    return subscriber_shutdown(participant);
}
Exemplo n.º 16
0
int dds_chat_main(int argc, char *argv[])
#endif
{
	DDS_DataWriterQos 	wr_qos;
	DDS_DataReaderQos	rd_qos;
	DDS_ReturnCode_t	error;	
	struct in_addr addr;

	/* Configure the network */
	/* Set up our host address */
	addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_IPADDR);
	netlib_sethostaddr("eth0", &addr);

	/* Set up the default router address */
	addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_DRIPADDR);
	netlib_setdraddr("eth0", &addr);

	/* Setup the subnet mask */
	addr.s_addr = HTONL(CONFIG_EXAMPLES_UDP_NETMASK);
	netlib_setnetmask("eth0", &addr);

  	/* Start the application */
  	printf("Network configured, starting DDS chat:\n");

	sprintf (user_name, ".pid.%u", getpid ());
	do_switches (argc, argv);
	if (verbose > 1)
		DDS_Log_stdio (1);

	DDS_entity_name ("ROS 2.0 embedded");

#ifdef DDS_SECURITY
	if (cert_path || key_path || engine_id)
		enable_security ();
#endif
	part = DDS_DomainParticipantFactory_create_participant (domain_id, NULL, NULL, 0);
	if (!part) {
		printf ("Can't create participant!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Domain Participant created.\r\n");

	ts = ChatMsg_type_new ();
	if (!ts) {
		printf ("Can't create chat message type!\r\n");
		exit (1);
	}
	error = DDS_DynamicTypeSupport_register_type (ts, part, "ChatMsg");
	if (error) {
		printf ("Can't register chat message type.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Topic type ('%s') registered.\r\n", "ChatMsg");

	topic = DDS_DomainParticipant_create_topic (part, "Chat", "ChatMsg", NULL, NULL, 0);
	if (!topic) {
		printf ("Can't register chat message type.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS ChatMsg Topic created.\r\n");

	td = DDS_DomainParticipant_lookup_topicdescription (part, "Chat");
	if (!td) {
		printf ("Can't get topicdescription.\r\n");
		exit (1);
	}
	pub = DDS_DomainParticipant_create_publisher (part, NULL, NULL, 0);
	if (!pub) {
		printf ("DDS_DomainParticipant_create_publisher () failed!\r\n");
		exit (1);
	}
	DDS_Publisher_get_default_datawriter_qos (pub, &wr_qos);
#ifdef TRANSIENT_LOCAL
	wr_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS;
#endif
#ifdef RELIABLE
	wr_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
#endif
#ifdef KEEP_ALL
	wr_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
	wr_qos.history.depth = DDS_LENGTH_UNLIMITED;
	wr_qos.resource_limits.max_samples_per_instance = HISTORY;
	wr_qos.resource_limits.max_instances = HISTORY * 10;
	wr_qos.resource_limits.max_samples = HISTORY * 4;
#else
	wr_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS;
	wr_qos.history.depth = HISTORY;
#endif
	/* Create a Data Writer. */
	dw = DDS_Publisher_create_datawriter (pub, topic, &wr_qos, NULL, 0);
	if (!dw) {
		printf ("Unable to create chat message writer.\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Chat message writer created.\r\n");

	sub = DDS_DomainParticipant_create_subscriber (part, NULL, NULL, 0); 
	if (!sub) {
		printf ("DDS_DomainParticipant_create_subscriber () returned an error!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Subscriber created.\r\n");

	DDS_Subscriber_get_default_datareader_qos (sub, &rd_qos);
#ifdef TRANSIENT_LOCAL
	rd_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS;
#endif
#ifdef RELIABLE
	rd_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
#endif
#ifdef KEEP_ALL
	rd_qos.history.kind = DDS_KEEP_ALL_HISTORY_QOS;
	rd_qos.history.depth = DDS_LENGTH_UNLIMITED;
	rd_qos.resource_limits.max_samples_per_instance = HISTORY;
	rd_qos.resource_limits.max_instances = HISTORY * 10;
	rd_qos.resource_limits.max_samples = HISTORY * 4;
#else
	rd_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS;
	rd_qos.history.depth = HISTORY;
#endif
	dr = DDS_Subscriber_create_datareader (sub, td, &rd_qos,
#ifndef WAITSETS
			 &msg_listener, DDS_DATA_AVAILABLE_STATUS);
#else
			 NULL, 0);
#endif
	if (!dr) {
		printf ("DDS_DomainParticipant_create_datareader () returned an error!\r\n");
		exit (1);
	}
	if (verbose)
		printf ("DDS Chat message reader created.\r\n");

#ifdef WAITSETS
	start_chat_reader (dr);
#endif
	do_chat (dw);

#ifdef WAITSETS
	stop_chat_reader (dr);
#endif
	DDS_Publisher_delete_datawriter (pub, dw);
	usleep (200000);
	error = DDS_DomainParticipant_delete_contained_entities (part);
	if (verbose)
		printf ("DDS Entities deleted (error = %u).\r\n", error);

	ChatMsg_type_free (ts);
	if (verbose)
		printf ("Chat Type deleted.\r\n");

	error = DDS_DomainParticipantFactory_delete_participant (part);
	if (verbose)
		printf ("DDS Participant deleted (error = %u).\r\n", error);

#ifdef DDS_SECURITY
	if (cert_path || key_path || engine_id)
		cleanup_security ();
#endif
	return (0);
}
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);
}
Exemplo n.º 19
0
int main (int argc, char ** argv)
#endif
{
    /* Generic DDS entities */
    DDS_DomainParticipantFactory    dpf;
    DDS_DomainParticipant           participant;
    DDS_Topic                       chatMessageTopic;
    DDS_Topic                       nameServiceTopic;
    DDS_Publisher                   chatPublisher;

    /* QosPolicy holders */
    DDS_TopicQos                    *reliable_topic_qos;
    DDS_TopicQos                    *setting_topic_qos;
    DDS_PublisherQos                *pub_qos;
    DDS_DataWriterQos               *dw_qos;

    /* DDS Identifiers */
    DDS_DomainId_t                  domain = NULL;
    DDS_InstanceHandle_t            userHandle;
    DDS_ReturnCode_t                status;

    /* Type-specific DDS entities */
    Chat_ChatMessageTypeSupport     chatMessageTS;
    Chat_NameServiceTypeSupport     nameServiceTS;
    Chat_ChatMessageDataWriter      talker;
    Chat_NameServiceDataWriter      nameServer;

    /* Sample definitions */
    Chat_ChatMessage                *msg;   /* Example on Heap */
    Chat_NameService                ns;     /* Example on Stack */
    
    /* Others */
    int                             ownID = 1;
    int                             i;
    char                            *chatMessageTypeName = NULL;
    char                            *nameServiceTypeName = NULL;
    char                            *chatterName = NULL;
    char                            *partitionName = NULL;
        
#ifdef INTEGRITY
#ifdef CHATTER_QUIT
    ownID = -1;
#else
    ownID = 1;
#endif
    chatterName = "dds_user";
#else
    /* Options: Chatter [ownID [name]] */
    if (argc > 1) {
        sscanf(argv[1], "%d", &ownID);
        if (argc > 2) {
            chatterName = argv[2];
        }
    }
#endif

    /* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings). */
    dpf = DDS_DomainParticipantFactory_get_instance ();
    checkHandle(dpf, "DDS_DomainParticipantFactory_get_instance");
    participant = DDS_DomainParticipantFactory_create_participant (
        dpf, 
        domain, 
        DDS_PARTICIPANT_QOS_DEFAULT, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(participant, "DDS_DomainParticipantFactory_create_participant");  

    /* Register the required datatype for ChatMessage. */
    chatMessageTS = Chat_ChatMessageTypeSupport__alloc();
    checkHandle(chatMessageTS, "Chat_ChatMessageTypeSupport__alloc");
    chatMessageTypeName = Chat_ChatMessageTypeSupport_get_type_name(chatMessageTS);
    status = Chat_ChatMessageTypeSupport_register_type(
        chatMessageTS, 
        participant, 
        chatMessageTypeName);
    checkStatus(status, "Chat_ChatMessageTypeSupport_register_type");
    
    /* Register the required datatype for NameService. */
    nameServiceTS = Chat_NameServiceTypeSupport__alloc();
    checkHandle(nameServiceTS, "Chat_NameServiceTypeSupport__alloc");
    nameServiceTypeName = Chat_NameServiceTypeSupport_get_type_name(nameServiceTS);
    status = Chat_NameServiceTypeSupport_register_type(
        nameServiceTS, 
        participant, 
        nameServiceTypeName);
    checkStatus(status, "Chat_NameServiceTypeSupport_register_type");
    
    /* Set the ReliabilityQosPolicy to RELIABLE. */
    reliable_topic_qos = DDS_TopicQos__alloc();
    checkHandle(reliable_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    reliable_topic_qos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
    
    /* Make the tailored QoS the new default. */
    status = DDS_DomainParticipant_set_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_set_default_topic_qos");

    /* Use the changed policy when defining the ChatMessage topic */
    chatMessageTopic = DDS_DomainParticipant_create_topic(
        participant, 
        "Chat_ChatMessage", 
        chatMessageTypeName, 
        reliable_topic_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(chatMessageTopic, "DDS_DomainParticipant_create_topic (ChatMessage)");
    
    /* Set the DurabilityQosPolicy to TRANSIENT. */
    setting_topic_qos = DDS_TopicQos__alloc();
    checkHandle(setting_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, setting_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    setting_topic_qos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS;

    /* Create the NameService Topic. */
    nameServiceTopic = DDS_DomainParticipant_create_topic( 
        participant, 
        "Chat_NameService", 
        nameServiceTypeName, 
        setting_topic_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(nameServiceTopic, "DDS_DomainParticipant_create_topic");

    /* Adapt the default PublisherQos to write into the "ChatRoom" Partition. */
    partitionName = "ChatRoom";
    pub_qos = DDS_PublisherQos__alloc();
    checkHandle(pub_qos, "DDS_PublisherQos__alloc");
    status = DDS_DomainParticipant_get_default_publisher_qos (participant, pub_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_publisher_qos");
    pub_qos->partition.name._length = 1;
    pub_qos->partition.name._maximum = 1;
    pub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    checkHandle(pub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf");
    pub_qos->partition.name._buffer[0] = DDS_string_alloc ( strlen(partitionName) );
    checkHandle(pub_qos->partition.name._buffer[0], "DDS_string_alloc");
    strcpy (pub_qos->partition.name._buffer[0], partitionName);

    /* Create a Publisher for the chatter application. */
    chatPublisher = DDS_DomainParticipant_create_publisher(participant, pub_qos, NULL, DDS_STATUS_MASK_NONE);
    checkHandle(chatPublisher, "DDS_DomainParticipant_create_publisher");
    
    /* Create a DataWriter for the ChatMessage Topic (using the appropriate QoS). */
    talker = DDS_Publisher_create_datawriter( 
        chatPublisher, 
        chatMessageTopic, 
        DDS_DATAWRITER_QOS_USE_TOPIC_QOS, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(talker, "DDS_Publisher_create_datawriter (chatMessage)");
    
    /* Create a DataWriter for the NameService Topic (using the appropriate QoS). */
    dw_qos = DDS_DataWriterQos__alloc();
    checkHandle(dw_qos, "DDS_DataWriterQos__alloc");
    status = DDS_Publisher_get_default_datawriter_qos (chatPublisher, dw_qos);
    checkStatus(status, "DDS_Publisher_get_default_datawriter_qos");
    status = DDS_Publisher_copy_from_topic_qos(chatPublisher, dw_qos, setting_topic_qos);
    checkStatus(status, "DDS_Publisher_copy_from_topic_qos");
    dw_qos->writer_data_lifecycle.autodispose_unregistered_instances = FALSE;
    nameServer = DDS_Publisher_create_datawriter( 
        chatPublisher, 
        nameServiceTopic, 
        dw_qos,
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(nameServer, "DDS_Publisher_create_datawriter (NameService)");

    /* Initialize the NameServer attributes located on stack. */
    ns.userID = ownID;
    ns.name = DDS_string_alloc(Chat_MAX_NAME+1);
    checkHandle(ns.name, "DDS_string_alloc");
    if (chatterName) {
        strncpy (ns.name, chatterName, Chat_MAX_NAME + 1);
    } else {
        snprintf(ns.name, Chat_MAX_NAME+1, "Chatter %d", ownID);
    }

    /* Write the user-information into the system (registering the instance implicitly). */
    status = Chat_NameServiceDataWriter_write(nameServer, &ns, DDS_HANDLE_NIL);
    checkStatus(status, "Chat_ChatMessageDataWriter_write");
    
    /* 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);

    /* 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");

    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! */
    }

    /* Leave the room by disposing and unregistering the message instance. */
    status = Chat_ChatMessageDataWriter_dispose(talker, msg, userHandle);
    checkStatus(status, "Chat_ChatMessageDataWriter_dispose");
    status = Chat_ChatMessageDataWriter_unregister_instance(talker, msg, userHandle);
    checkStatus(status, "Chat_ChatMessageDataWriter_unregister_instance");

    /* Also unregister our name. */
    status = Chat_NameServiceDataWriter_unregister_instance(nameServer, &ns, DDS_HANDLE_NIL);
    checkStatus(status, "Chat_NameServiceDataWriter_unregister_instance");

    /* Release the data-samples. */
    DDS_free(ns.name); /* ns allocated on stack: explicit de-allocation of indirections!! */
    DDS_free(msg);     /* msg allocated on heap: implicit de-allocation of indirections!! */

    /* Remove the DataWriters */
    status = DDS_Publisher_delete_datawriter(chatPublisher, talker);
    checkStatus(status, "DDS_Publisher_delete_datawriter (talker)");
    
    status = DDS_Publisher_delete_datawriter(chatPublisher, nameServer);
    checkStatus(status, "DDS_Publisher_delete_datawriter (nameServer)");
    
    /* Remove the Publisher. */
    status = DDS_DomainParticipant_delete_publisher(participant, chatPublisher);
    checkStatus(status, "DDS_DomainParticipant_delete_publisher");
    
    /* Remove the Topics. */
    status = DDS_DomainParticipant_delete_topic(participant, nameServiceTopic);
    checkStatus(status, "DDS_DomainParticipant_delete_topic (nameServiceTopic)");
    
    status = DDS_DomainParticipant_delete_topic(participant, chatMessageTopic);
    checkStatus(status, "DDS_DomainParticipant_delete_topic (chatMessageTopic)");
    
    /* De-allocate the QoS policies. */
    DDS_free(reliable_topic_qos);
    DDS_free(setting_topic_qos);
    DDS_free(pub_qos);  /* Note that DDS_free recursively de-allocates all indirections as well!! */

    /* De-allocate the type-names and TypeSupport objects. */
    DDS_free(nameServiceTypeName);
    DDS_free(chatMessageTypeName);
    DDS_free(nameServiceTS);
    DDS_free(chatMessageTS);
    
    /* Remove the DomainParticipant. */
    status = DDS_DomainParticipantFactory_delete_participant(dpf, participant);
    checkStatus(status, "DDS_DomainParticipantFactory_delete_participant");

    printf("Completed chatter example.\n");
    fflush(stdout);
    return 0;
}
Exemplo n.º 20
0
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);
}
Exemplo n.º 21
0
int
main (
    int argc,
    char *argv[])
{
    DDS_DomainId_t                             myDomain           = DDS_OBJECT_NIL;
    DDS_DomainParticipantFactory               dpf                = DDS_OBJECT_NIL;
    DDS_DomainParticipant                      dp                 = DDS_OBJECT_NIL;
    DDS_Publisher                              p                  = DDS_OBJECT_NIL;
    DDS_Subscriber                             s                  = DDS_OBJECT_NIL;

    pingpong_PP_min_msgDataWriter              PP_min_writer      = DDS_OBJECT_NIL;
    pingpong_PP_seq_msgDataWriter              PP_seq_writer      = DDS_OBJECT_NIL;
    pingpong_PP_string_msgDataWriter           PP_string_writer   = DDS_OBJECT_NIL;
    pingpong_PP_fixed_msgDataWriter            PP_fixed_writer    = DDS_OBJECT_NIL;
    pingpong_PP_array_msgDataWriter            PP_array_writer    = DDS_OBJECT_NIL;
    pingpong_PP_bseq_msgDataWriter      PP_bseq_writer    = DDS_OBJECT_NIL;

    pingpong_PP_min_msgDataReader              PP_min_reader      = DDS_OBJECT_NIL;
    pingpong_PP_seq_msgDataReader              PP_seq_reader      = DDS_OBJECT_NIL;
    pingpong_PP_string_msgDataReader           PP_string_reader   = DDS_OBJECT_NIL;
    pingpong_PP_fixed_msgDataReader            PP_fixed_reader    = DDS_OBJECT_NIL;
    pingpong_PP_array_msgDataReader            PP_array_reader    = DDS_OBJECT_NIL;
    pingpong_PP_bseq_msgDataReader      PP_bseq_reader    = DDS_OBJECT_NIL;
    pingpong_PP_quit_msgDataReader             PP_quit_reader     = DDS_OBJECT_NIL;

    pingpong_PP_min_msgTypeSupport             PP_min_dt;
    pingpong_PP_seq_msgTypeSupport             PP_seq_dt;
    pingpong_PP_string_msgTypeSupport          PP_string_dt;
    pingpong_PP_fixed_msgTypeSupport           PP_fixed_dt;
    pingpong_PP_array_msgTypeSupport           PP_array_dt;
    pingpong_PP_bseq_msgTypeSupport     PP_bseq_dt;
    pingpong_PP_quit_msgTypeSupport            PP_quit_dt;

    DDS_sequence_pingpong_PP_min_msg           PP_min_dataList    = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_seq_msg           PP_seq_dataList    = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_string_msg        PP_string_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_fixed_msg         PP_fixed_dataList  = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_array_msg         PP_array_dataList  = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_bseq_msg   PP_bseq_dataList  = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_pingpong_PP_quit_msg          PP_quit_dataList   = { 0, 0, DDS_OBJECT_NIL, FALSE };

    DDS_StatusCondition                        PP_min_sc;
    DDS_StatusCondition                        PP_seq_sc;
    DDS_StatusCondition                        PP_string_sc;
    DDS_StatusCondition                        PP_fixed_sc;
    DDS_StatusCondition                        PP_array_sc;
    DDS_StatusCondition                        PP_bseq_sc;
    DDS_StatusCondition                        PP_quit_sc;

    DDS_Topic                                  PP_min_topic       = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_seq_topic       = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_string_topic    = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_fixed_topic     = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_array_topic     = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_bseq_topic     = DDS_OBJECT_NIL;
    DDS_Topic                                  PP_quit_topic      = DDS_OBJECT_NIL;

    DDS_ConditionSeq                          *conditionList;
    DDS_SampleInfoSeq                          infoList           = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_WaitSet                                w;

    DDS_DomainParticipantQos                  *dpQos;
    DDS_TopicQos                              *tQos;
    DDS_PublisherQos                          *pQos;
    DDS_DataWriterQos                         *dwQos;
    DDS_SubscriberQos                         *sQos;
    DDS_DataReaderQos                         *drQos;

    DDS_boolean                                terminate = FALSE;

    DDS_ReturnCode_t                           result;
    int                                        i;
    int                                        imax;
    int                                        j;
    int                                        jmax;

    printf ("Starting pong example\n");
    fflush(stdout);
    /*
     * Evaluate cmdline arguments
     */

#ifdef INTEGRITY
    read_partition = "PongRead";
    write_partition = "PongWrite";
#else
    if (argc != 1) {
        if (argc != 3) {
            printf ("Invalid.....\n Usage: %s [READ_PARTITION WRITE_PARTITION]\n", argv[0]);
            exit (1);
        }
        read_partition  = argv[1];
        write_partition = argv[2];
    }
#endif

    /*
     * Create WaitSet
     */
    w = DDS_WaitSet__alloc ();
    /*
     * Initialize Qos variables
     */ 
    dpQos = DDS_DomainParticipantQos__alloc();
    tQos  = DDS_TopicQos__alloc();
    pQos  = DDS_PublisherQos__alloc();
    dwQos = DDS_DataWriterQos__alloc();
    sQos  = DDS_SubscriberQos__alloc();
    drQos = DDS_DataReaderQos__alloc();
    /*
     * Initialize condition list
     */
    conditionList = NULL;

    /*
     * Create participant
     */
    dpf = DDS_DomainParticipantFactory_get_instance ();
    dp = DDS_DomainParticipantFactory_create_participant (dpf, myDomain, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);
    if (dp == DDS_OBJECT_NIL) {
        printf ("%s PONG: ERROR - Splice Daemon not running\n", argv[0]);
        exit(1);
    }

    /* 
     * Create PONG publisher
     */
    DDS_DomainParticipant_get_default_publisher_qos (dp, pQos);
    pQos->partition.name._length = 1;
    pQos->partition.name._maximum = 1;
    pQos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    pQos->partition.name._buffer[0] = DDS_string_alloc (strlen (write_partition) + 1);
    strcpy (pQos->partition.name._buffer[0], write_partition);
    p = DDS_DomainParticipant_create_publisher (dp, pQos, NULL, DDS_STATUS_MASK_NONE);
    DDS_free (pQos);

    /*
     * Create PING subscriber
     */
    DDS_DomainParticipant_get_default_subscriber_qos (dp, sQos);
    sQos->partition.name._length = 1;
    sQos->partition.name._maximum = 1;
    sQos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    sQos->partition.name._buffer[0] = DDS_string_alloc (strlen (read_partition) + 1);
    strcpy (sQos->partition.name._buffer[0], read_partition);
    s = DDS_DomainParticipant_create_subscriber (dp, sQos, NULL, DDS_STATUS_MASK_NONE);
    DDS_free (sQos);

    /*
     * PP_min_msg
     */
    
    /* Create Topic */
    PP_min_dt = pingpong_PP_min_msgTypeSupport__alloc ();
    pingpong_PP_min_msgTypeSupport_register_type (PP_min_dt, dp, "pingpong::PP_min_msg");
    PP_min_topic = DDS_DomainParticipant_create_topic (dp, "PP_min_topic", "pingpong::PP_min_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_min_writer = DDS_Publisher_create_datawriter (p, PP_min_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_min_reader = DDS_Subscriber_create_datareader (s, PP_min_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_min_sc = DDS_DataReader_get_statuscondition (PP_min_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_min_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_min_sc);

    /*
     * PP_seq_msg
     */

    /*  Create Topic */
    PP_seq_dt = pingpong_PP_seq_msgTypeSupport__alloc ();
    pingpong_PP_seq_msgTypeSupport_register_type (PP_seq_dt, dp, "pingpong::PP_seq_msg");
    PP_seq_topic = DDS_DomainParticipant_create_topic (dp, "PP_seq_topic", "pingpong::PP_seq_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_seq_writer = DDS_Publisher_create_datawriter (p, PP_seq_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_seq_reader = DDS_Subscriber_create_datareader (s, PP_seq_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_seq_sc = DDS_DataReader_get_statuscondition (PP_seq_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_seq_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_seq_sc);

    /*
     * PP_string_msg
     */

    /*  Create Topic */
    PP_string_dt = pingpong_PP_string_msgTypeSupport__alloc ();
    pingpong_PP_string_msgTypeSupport_register_type (PP_string_dt, dp, "pingpong::PP_string_msg");
    PP_string_topic = DDS_DomainParticipant_create_topic (dp, "PP_string_topic", "pingpong::PP_string_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_string_writer = DDS_Publisher_create_datawriter (p, PP_string_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_string_reader = DDS_Subscriber_create_datareader (s, PP_string_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_string_sc = DDS_DataReader_get_statuscondition (PP_string_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_string_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_string_sc);

    /*
     * PP_fixed_msg
     */
    
    /*  Create Topic */
    PP_fixed_dt = pingpong_PP_fixed_msgTypeSupport__alloc ();
    pingpong_PP_fixed_msgTypeSupport_register_type (PP_fixed_dt, dp, "pingpong::PP_fixed_msg");
    PP_fixed_topic = DDS_DomainParticipant_create_topic (dp, "PP_fixed_topic", "pingpong::PP_fixed_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_fixed_writer = DDS_Publisher_create_datawriter (p, PP_fixed_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_fixed_reader = DDS_Subscriber_create_datareader (s, PP_fixed_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_fixed_sc = DDS_DataReader_get_statuscondition (PP_fixed_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_fixed_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_fixed_sc);

    /*
     * PP_array_msg
     */

    /*  Create Topic */
    PP_array_dt = pingpong_PP_array_msgTypeSupport__alloc ();
    pingpong_PP_array_msgTypeSupport_register_type (PP_array_dt, dp, "pingpong::PP_array_msg");
    PP_array_topic = DDS_DomainParticipant_create_topic (dp, "PP_array_topic", "pingpong::PP_array_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_array_writer = DDS_Publisher_create_datawriter (p, PP_array_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_array_reader = DDS_Subscriber_create_datareader (s, PP_array_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_array_sc = DDS_DataReader_get_statuscondition (PP_array_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_array_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_array_sc);

    /*
     * PP_bseq_msg
     */

    /*  Create Topic */
    PP_bseq_dt = pingpong_PP_bseq_msgTypeSupport__alloc ();
    pingpong_PP_bseq_msgTypeSupport_register_type (PP_bseq_dt, dp, "pingpong::PP_bseq_msg");
    PP_bseq_topic = DDS_DomainParticipant_create_topic (dp, "PP_bseq_topic", "pingpong::PP_bseq_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datawriter */
    PP_bseq_writer = DDS_Publisher_create_datawriter (p, PP_bseq_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_bseq_reader = DDS_Subscriber_create_datareader (s, PP_bseq_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_bseq_sc = DDS_DataReader_get_statuscondition (PP_bseq_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_bseq_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_bseq_sc);

    /*
     * PP_quit_msg
     */
    
    /*  Create Topic */
    PP_quit_dt = pingpong_PP_quit_msgTypeSupport__alloc ();
    pingpong_PP_quit_msgTypeSupport_register_type (PP_quit_dt, dp, "pingpong::PP_quit_msg");
    PP_quit_topic = DDS_DomainParticipant_create_topic (dp, "PP_quit_topic", "pingpong::PP_quit_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Create datareader */
    PP_quit_reader = DDS_Subscriber_create_datareader (s, PP_quit_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE);

    /* Add datareader statuscondition to waitset */
    PP_quit_sc = DDS_DataReader_get_statuscondition (PP_quit_reader);
    DDS_StatusCondition_set_enabled_statuses (PP_quit_sc, DDS_DATA_AVAILABLE_STATUS);
    result = DDS_WaitSet_attach_condition (w, PP_quit_sc);

    while (!terminate) {
	DDS_Duration_t wait_timeout = DDS_DURATION_INFINITE;

        /* printf ("PONG: waiting for PING\n"); */
        conditionList = DDS_ConditionSeq__alloc();
        result = DDS_WaitSet_wait (w, conditionList, &wait_timeout);

        if (result == DDS_RETCODE_OK) {
            imax = conditionList->_length;
            for (i = 0; i < imax; i++) {
                if (conditionList->_buffer[i] == PP_min_sc) {
    		    /* printf ("PONG: PING_min arrived\n"); */
                    result = pingpong_PP_min_msgDataReader_take (PP_min_reader, &PP_min_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_min_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_min_msgDataWriter_write (PP_min_writer, &PP_min_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_min_msgDataReader_return_loan (PP_min_reader, &PP_min_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_min triggered, but no data available\n");
                    }
                } else if (conditionList->_buffer[i] == PP_seq_sc) {
    		    /* printf ("PONG: PING_seq arrived\n"); */
                    result = pingpong_PP_seq_msgDataReader_take (PP_seq_reader, &PP_seq_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_seq_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_seq_msgDataWriter_write (PP_seq_writer, &PP_seq_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_seq_msgDataReader_return_loan (PP_seq_reader, &PP_seq_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_seq triggered, but no data available\n");
                    }
                } else if (conditionList->_buffer[i] == PP_string_sc) {
    		    /* printf ("PONG: PING_string arrived\n"); */
                    result = pingpong_PP_string_msgDataReader_take (PP_string_reader, &PP_string_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_string_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_string_msgDataWriter_write (PP_string_writer, &PP_string_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_string_msgDataReader_return_loan (PP_string_reader, &PP_string_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_string triggered, but no data available\n");
                        exit(1);
                    }
                } else if (conditionList->_buffer[i] == PP_fixed_sc) {
    		    /* printf ("PONG: PING_fixed arrived\n"); */
                    result = pingpong_PP_fixed_msgDataReader_take (PP_fixed_reader, &PP_fixed_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_fixed_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_fixed_msgDataWriter_write (PP_fixed_writer, &PP_fixed_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_fixed_msgDataReader_return_loan (PP_fixed_reader, &PP_fixed_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_fixed triggered, but no data available\n");
                    }
                } else if (conditionList->_buffer[i] == PP_array_sc) {
    		    /* printf ("PONG: PING_array arrived\n"); */
                    result = pingpong_PP_array_msgDataReader_take (PP_array_reader, &PP_array_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_array_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_array_msgDataWriter_write (PP_array_writer, &PP_array_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_array_msgDataReader_return_loan (PP_array_reader, &PP_array_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_array triggered, but no data available\n");
                    }
                } else if (conditionList->_buffer[i] == PP_bseq_sc) {
    		    /* printf ("PONG: PING_bseq arrived\n"); */
                    result = pingpong_PP_bseq_msgDataReader_take (PP_bseq_reader, &PP_bseq_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_bseq_dataList._length;
                    if (jmax != 0) {
                        for (j = 0; j < jmax; j++) {
                            if (infoList._buffer[j].valid_data) {
                                result = pingpong_PP_bseq_msgDataWriter_write (PP_bseq_writer, &PP_bseq_dataList._buffer[j], DDS_HANDLE_NIL);
                            }
                        }
                        result = pingpong_PP_bseq_msgDataReader_return_loan (PP_bseq_reader, &PP_bseq_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_bseq triggered, but no data available\n");
                    }
                } else if (conditionList->_buffer[i] == PP_quit_sc) {
    		    /* printf ("PONG: PING_quit arrived\n"); */
                    result = pingpong_PP_quit_msgDataReader_take (PP_quit_reader, &PP_quit_dataList, &infoList, DDS_LENGTH_UNLIMITED,
                                 DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
                    jmax = PP_quit_dataList._length;
                    if (jmax != 0) {
                        if (PP_quit_dataList._buffer[0].quit) {
                            terminate = TRUE;
                        }
                        result = pingpong_PP_quit_msgDataReader_return_loan (PP_quit_reader, &PP_quit_dataList, &infoList);
                    } else {
                        printf ("PONG: PING_quit triggered, but no data available\n");
                    }
                } else {
                    printf ("PONG: unknown condition triggered: %lx\n", (unsigned long)conditionList->_buffer[i]);
                }
            }
        } else {
            if (result == DDS_RETCODE_ALREADY_DELETED) {
                terminate = TRUE;
            }
            printf ("PONG: wait returned not ok: %d\n", result);
        }
        DDS_free(conditionList);
    }

    result = DDS_Subscriber_delete_datareader (s, PP_min_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_min_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_seq_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_seq_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_string_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_string_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_fixed_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_fixed_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_array_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_array_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_bseq_reader);
    result = DDS_Publisher_delete_datawriter (p, PP_bseq_writer);
    result = DDS_Subscriber_delete_datareader (s, PP_quit_reader);
    result = DDS_DomainParticipant_delete_subscriber (dp, s);
    result = DDS_DomainParticipant_delete_publisher (dp, p);
    result = DDS_DomainParticipant_delete_topic (dp, PP_min_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_seq_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_string_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_fixed_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_array_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_bseq_topic);
    result = DDS_DomainParticipant_delete_topic (dp, PP_quit_topic);
    result = DDS_DomainParticipantFactory_delete_participant (dpf, dp);
    DDS_free (w);
    DDS_free (PP_min_dt);
    DDS_free (PP_seq_dt);
    DDS_free (PP_string_dt);
    DDS_free (PP_fixed_dt);
    DDS_free (PP_array_dt);
    DDS_free (PP_bseq_dt);
    DDS_free (PP_quit_dt);
    DDS_free (dpQos);
    DDS_free (tQos);
    DDS_free (dwQos);
    DDS_free (drQos);

    printf ("Completed pong example\n");
    fflush(stdout);
    return 0;
}
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);
}
static int subscriber_main(int domainId, int sample_count) {
    DDS_DomainParticipant *participant = NULL;
    DDS_Subscriber *subscriber[MAX_SUBSCRIBERS] = { 0 };
    DDS_Topic *topic = NULL;
    DDS_DataReader *reader[MAX_SUBSCRIBERS] = { 0 };
    DDS_ReturnCode_t retcode;
    const char *type_name = NULL;
    int count = 0;
    struct DDS_Duration_t poll_period = { 4, 0 };
    orderedDataReader *ordered_reader[MAX_SUBSCRIBERS] = { 0 };
    int i;
    struct DDS_SubscriberQos subscriber_qos = DDS_SubscriberQos_INITIALIZER;
    struct DDS_DataReaderQos datareader_qos = DDS_DataReaderQos_INITIALIZER;
    char* profile_name[] = { "ordered_Profile_subscriber_instance",
                             "ordered_Profile_subscriber_topic"
                           };

    /* 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");
        subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
        return -1;
    }

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

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

    /* Start changes for ordered_presentation */
    /* Create two subscriber to illustrate different presentation QoS
     * This is a publisher/subscriber level QoS, so we have to do it
     * here instead of just making two datareaders
     */
    /* Subscriber[0], reader[0] and ordered_reader[0] is getting
     * the profile "ordered_Profile_subscriber_instance"
     */

    for (i = 0; i < MAX_SUBSCRIBERS; ++i) {
        /* Subscriber[1], reader[1] and ordered_reader[1] is getting
         * the profile "ordered_Profile_subscriber_topic"
         */
        printf("Subscriber %d using %s\n", i, profile_name[i]);

        subscriber[i] = DDS_DomainParticipant_create_subscriber_with_profile(
                            participant, "ordered_Library", profile_name[i],
                            NULL /* Listener */, DDS_STATUS_MASK_NONE);
        if (subscriber[i] == NULL) {
            printf("create_subscriber%d error\n", i);
            subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
            return -1;
        }

        reader[i] = DDS_Subscriber_create_datareader_with_profile(subscriber[i],
                    DDS_Topic_as_topicdescription(topic), "ordered_Library",
                    profile_name[i], NULL, DDS_STATUS_MASK_NONE);
        if (reader[i] == NULL) {
            printf("create_datareader%d error\n", i);
            subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
            return -1;
        }

        ordered_reader[i] = orderedDataReader_narrow(reader[i]);
        if (ordered_reader[i] == NULL) {
            printf("DataReader%d narrow error\n", i);
            return -1;
        }
    }

    /* If you want to change the Publisher's QoS programmatically rather than
     * using the XML file, you will need to add the following lines to your
     * code and comment out the above 'for' loop.
     */
    /*
        for (i = 0; i < MAX_SUBSCRIBERS; ++i) {

    */      /* Get default subscriber QoS to customize */
    /*      retcode = DDS_DomainParticipant_get_default_subscriber_qos(participant,
                    &subscriber_qos);
            if (retcode != DDS_RETCODE_OK) {
                printf("get_default_subscriber_qos error\n");
                return -1;
            }

    */      /* Set ordered_access = TRUE for both subscribers */
    /*      subscriber_qos.presentation.ordered_access = DDS_BOOLEAN_TRUE;

    */      /* We are assuming there are only 2 subscribers. Otherwise you
         * will need to modify the following conditions.
         */
    /*      if (i == 0) {
                printf("Subscriber 0 using Instance access scope\n");
                subscriber_qos.presentation.access_scope =
                        DDS_INSTANCE_PRESENTATION_QOS;
            } else {
                printf("Subscriber 1 using Topic access scope\n");
                subscriber_qos.presentation.access_scope =
                        DDS_TOPIC_PRESENTATION_QOS;
            }

    */      /* To create subscriber with default QoS, use DDS_SUBSCRIBER_QOS_DEFAULT
           instead of subscriber_qos */
    /*      subscriber[i] = DDS_DomainParticipant_create_subscriber(participant,
                    &subscriber_qos, NULL, DDS_STATUS_MASK_NONE);
            if (subscriber == NULL) {
                printf("create_subscriber error\n");
                subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
                return -1;
            }

    */      /* No listener needed, but we do need to increase history depth */

    /* Get default datareader QoS to customize */
    /*        retcode = DDS_Subscriber_get_default_datareader_qos(subscriber[i],
                    &datareader_qos);
            if (retcode != DDS_RETCODE_OK) {
                printf("get_default_datareader_qos error\n");
                return -1;
            }

            datareader_qos.history.depth = 10;

    */      /* To create datareader with default QoS, use DDS_DATAREADER_QOS_DEFAULT
         instead of datareader_qos */
    /*        reader[i] = DDS_Subscriber_create_datareader(subscriber[i],
                    DDS_Topic_as_topicdescription(topic), &datareader_qos, NULL,
                    DDS_STATUS_MASK_NONE);
            if (reader == NULL) {
                printf("create_datareader error\n");
                subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
                return -1;
            }

            ordered_reader[i] = orderedDataReader_narrow(reader[i]);
            if (ordered_reader[i] == NULL) {
                printf("DataReader narrow error\n");
                return -1;
            }
        }
    */
    /* Main loop */
    for (count = 0; (sample_count == 0) || (count < sample_count); ++count) {
        printf("ordered subscriber sleeping for %d sec...\n", poll_period.sec);
        NDDS_Utility_sleep(&poll_period);
        poll_data(ordered_reader, MAX_SUBSCRIBERS);
    }

    /* Cleanup and delete all entities */
    return subscriber_shutdown(participant, &subscriber_qos, &datareader_qos);
}
Exemplo n.º 24
0
int main (int argc, char *argv [])
{
	int domain_id = 128;
	DDS_ReturnCode_t error;

	do_switches (argc, argv);

#ifdef DDS_DEBUG
	DDS_Debug_start ();
#endif
	
	DDS_set_generate_callback (calculate_member_id);
		
	sem_init(&_sync, 0, 0);

	part = DDS_DomainParticipantFactory_create_participant (domain_id, NULL, NULL, 0);
	if (!part)
		fatal ("DDS_DomainParticipantFactory_create_participant () failed!");

	if (tsm_type == 0) {
		error = register_HelloWorldData_type (part);
		if (error) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("DDS_DomainParticipant_register_type ('HelloWorldData') failed (%s)!", DDS_error (error));
		}

		sm = DDS_INCONSISTENT_TOPIC_STATUS;
		topic = DDS_DomainParticipant_create_topic (part, "HelloWorld", "HelloWorldData",
							    NULL, NULL, sm);
		if (!topic) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("DDS_DomainParticipant_create_topic ('HelloWorld') failed!");
		}
		
		topic_desc = DDS_DomainParticipant_lookup_topicdescription (part, "HelloWorld");
		if (!topic_desc) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("Unable to create topic description for 'HelloWorld'!");
		}
	} else {
		_tsm_types[0].flags |= TSMFLAG_KEY;
		error = register_TestType_type (part);
		if (error) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("DDS_DomainParticipant_register_type ('HelloWorldData') failed (%s)!", DDS_error (error));
		}

		sm = DDS_INCONSISTENT_TOPIC_STATUS;
		topic = DDS_DomainParticipant_create_topic (part, _tsm_types->name, _tsm_types->name,
							    NULL, NULL, sm);
		if (!topic) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("DDS_DomainParticipant_create_topic ('%s') failed!", _tsm_types->name);
		}
		
		topic_desc = DDS_DomainParticipant_lookup_topicdescription (part, _tsm_types->name);
		if (!topic_desc) {
			DDS_DomainParticipantFactory_delete_participant (part);
			fatal ("Unable to create topic description for '%s'!", _tsm_types->name);
		}
	}

	if (test == 3) {
		/* interop test */
		make_state_reader ();
		make_state_writer (key);
	} else {	
		if (reader)
			make_state_reader ();
		if (writer)
			make_state_writer (key);
	}
	
	if (reader)
		remove_state_reader ();
	if (writer)
		remove_state_writer ();

	if (test == 3) {
		remove_state_reader ();
		remove_state_writer ();
	}

	error = DDS_DomainParticipant_delete_topic (part, topic);
	if (error)
		fatal ("DDS_DomainParticipant_delete_topic () failed (%s)!", DDS_error (error));

	free_HelloWorldData_type (part);
	
	error = DDS_DomainParticipant_delete_contained_entities (part);
	if (error)
		fatal ("DDS_DomainParticipant_delete_contained_entities () failed (%s)!", DDS_error (error));

	error = DDS_DomainParticipantFactory_delete_participant (part);
	if (error)
		fatal ("DDS_DomainParticipantFactory_delete_participant () failed (%s)!", DDS_error (error));

	return (0);

}
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;
    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);
}
Exemplo n.º 27
0
static void test_qos (int enabled)
{
	DDS_DomainParticipantFactoryQos	qos;
	DDS_DomainParticipant		p;
	DDS_Publisher			pub;
	DDS_Subscriber			sub;
	DDS_DataWriter			dw;
	DDS_DataReader			dr;
	DDS_Topic			t;
	DDS_TopicDescription		td;
	DDS_DataReaderQos		rq, refrq;
	DDS_ReturnCode_t		r;
	static unsigned char		data [] = { 0x77, 0x88, 0xaa, 0xbb, 0xcc, 0xdd };
	unsigned char			d2 [sizeof (data) + 1];
	unsigned			n;
	int				err;

	v_printf (" - Set factory QoS to %sabled.\r\n", (enabled) ? "en" : "dis");
	r = DDS_DomainParticipantFactory_get_qos (&qos);
	fail_unless (r == DDS_RETCODE_OK);
	qos.entity_factory.autoenable_created_entities = enabled;
	r = DDS_DomainParticipantFactory_set_qos (&qos);
	fail_unless (r == DDS_RETCODE_OK);

	p = DDS_DomainParticipantFactory_create_participant (0, DDS_PARTICIPANT_QOS_DEFAULT, NULL, 0);
	fail_unless (p != NULL);
	r = register_HelloWorldData_type (p);
	fail_unless (r == DDS_RETCODE_OK);

	t = DDS_DomainParticipant_create_topic (p, "HelloWorld", TYPE_NAME, DDS_TOPIC_QOS_DEFAULT, NULL, 0);
	fail_unless (t != NULL);

	td = DDS_DomainParticipant_lookup_topicdescription (p, "HelloWorld");
	fail_unless (td != NULL);

	sub = DDS_DomainParticipant_create_subscriber (p, NULL, NULL, 0);
	fail_unless (sub != NULL);

	DDS_DataReaderQos__init (&refrq);
	err = dds_seq_from_array (&refrq.user_data, data, sizeof (data));
	fail_unless (err == 0);

	v_printf (" - Create datareader with specific QoS parameters.\r\n");
	dr = DDS_Subscriber_create_datareader (sub, td, &refrq, NULL, 0);
	fail_unless (dr != NULL);

	memset (&rq, 0, sizeof (rq));
	r = DDS_DataReader_get_qos (dr, &rq);
	n = dds_seq_to_array (&rq.user_data, d2, sizeof (d2));
	fail_unless (r == DDS_RETCODE_OK &&
		     DDS_SEQ_LENGTH (refrq.user_data.value) == sizeof (data));
	n = dds_seq_to_array (&rq.user_data, d2, sizeof (d2));
	fail_unless (n == sizeof (data) && !memcmp (data, d2, sizeof (data)));
	DDS_DataReaderQos__clear (&rq);
	r = DDS_Subscriber_delete_datareader (sub, dr);
	fail_unless (r == DDS_RETCODE_OK);

	v_printf (" - Create datareader with default QoS parameters.\r\n");
	dr = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr != NULL);
	pub = DDS_DomainParticipant_create_publisher (p, DDS_PUBLISHER_QOS_DEFAULT, NULL, 0);
	fail_unless (sub != NULL);
	dw = DDS_Publisher_create_datawriter (pub, t, DDS_DATAWRITER_QOS_DEFAULT, NULL, 0);
	fail_unless (dw != NULL);

	v_printf (" - Update datareader QoS parameters.\r\n");
	r = DDS_DataReader_get_qos (dr, &rq);
	fail_unless (r == DDS_RETCODE_OK);
	DDS_DataReaderQos__clear (&refrq);
	fail_unless (r == DDS_RETCODE_OK && !memcmp (&rq, &refrq, sizeof (refrq)));
	err = dds_seq_from_array (&refrq.user_data, data, sizeof (data));
	fail_unless (err == 0);

	r = DDS_DataReader_set_qos (dr, &refrq);
	fail_unless (r == DDS_RETCODE_OK);

	delay ();
	r = DDS_DataReader_get_qos (dr, &rq);
	fail_unless (r == DDS_RETCODE_OK &&
		     DDS_SEQ_LENGTH (rq.user_data.value) == sizeof (data));
	n = dds_seq_to_array (&rq.user_data, d2, sizeof (d2));
	fail_unless (n == sizeof (data) && !memcmp (data, d2, sizeof (data)));

	DDS_DataReaderQos__clear (&refrq);
	DDS_DataReaderQos__clear (&rq);

	delay ();
	if (!enabled) {
		v_printf (" - Enable all entities.\r\n");
		r = DDS_DomainParticipant_enable (p);
		fail_unless (r == DDS_RETCODE_OK);
		r = DDS_Topic_enable (t);
		fail_unless (r == DDS_RETCODE_OK);
		r = DDS_Publisher_enable (pub);
		fail_unless (r == DDS_RETCODE_OK);
		r = DDS_Subscriber_enable (sub);
		fail_unless (r == DDS_RETCODE_OK);
		r = DDS_DataWriter_enable (dw);
		fail_unless (r == DDS_RETCODE_OK);
		r = DDS_DataReader_enable (dr);
		fail_unless (r == DDS_RETCODE_OK);
		sleep (1);
	}
	r = DDS_Publisher_delete_datawriter (pub, dw);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_Subscriber_delete_datareader (sub, dr);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_publisher (p, pub);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_topic (p, t);
	fail_unless (r == DDS_RETCODE_OK);

	unregister_HelloWorldData_type (p);
	r = DDS_DomainParticipantFactory_delete_participant (p);
	fail_unless (r == DDS_RETCODE_OK);
}
Exemplo n.º 28
0
int main (int argc, char ** argv)
#endif
{
    /* Generic DDS entities */
    DDS_DomainParticipant           participant;
    DDS_Topic                       chatMessageTopic;
    DDS_Topic                       nameServiceTopic;
    DDS_Subscriber                  chatSubscriber;
    DDS_QueryCondition              singleUser;
    DDS_ReadCondition               newUser;
    DDS_StatusCondition             leftUser;
    DDS_GuardCondition              guard;
    DDS_WaitSet                     userLoadWS;
    DDS_LivelinessChangedStatus     livChangStatus;

    /* QosPolicy holders */
    DDS_TopicQos                    *setting_topic_qos;
    DDS_TopicQos                    *reliable_topic_qos;
    DDS_SubscriberQos               *sub_qos;
    DDS_DataReaderQos               *message_qos;

    /* DDS Identifiers */
    DDS_DomainId_t                  domain = NULL;
    DDS_ReturnCode_t                status;
    DDS_ConditionSeq                *guardList = NULL;
    DDS_Duration_t                  timeout = DDS_DURATION_INFINITE;

    /* Type-specific DDS entities */
    Chat_ChatMessageTypeSupport     chatMessageTS;
    Chat_NameServiceTypeSupport     nameServiceTS;
    Chat_NameServiceDataReader      nameServer;
    Chat_ChatMessageDataReader      loadAdmin;
    DDS_sequence_Chat_ChatMessage   msgList = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_sequence_Chat_NameService   nsList = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_SampleInfoSeq               infoSeq    = { 0, 0, DDS_OBJECT_NIL, FALSE };
    DDS_SampleInfoSeq               infoSeq2   = { 0, 0, DDS_OBJECT_NIL, FALSE };

    /* Others */
    DDS_StringSeq                   args;
    int                             closed = 0;
    DDS_unsigned_long               i, j;
    DDS_long                        prevCount = 0;
    char                            *partitionName;
    char                            *chatMessageTypeName = NULL;
    char                            *nameServiceTypeName = NULL;
    pthread_t                       tid;
    pthread_attr_t                  tattr;

    printf("Starting UserLoad example.\n");
    fflush(stdout);
    /* Create a DomainParticipant (using the 'TheParticipantFactory' convenience macro). */
    participant = DDS_DomainParticipantFactory_create_participant (
        DDS_TheParticipantFactory, 
        domain, 
        DDS_PARTICIPANT_QOS_DEFAULT, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(participant, "DDS_DomainParticipantFactory_create_participant");  

    /* Register the required datatype for ChatMessage. */
    chatMessageTS = Chat_ChatMessageTypeSupport__alloc();
    checkHandle(chatMessageTS, "Chat_ChatMessageTypeSupport__alloc");
    chatMessageTypeName = Chat_ChatMessageTypeSupport_get_type_name(chatMessageTS);
    status = Chat_ChatMessageTypeSupport_register_type(
        chatMessageTS, 
        participant, 
        chatMessageTypeName);
    checkStatus(status, "Chat_ChatMessageTypeSupport_register_type");
    
    /* Register the required datatype for NameService. */
    nameServiceTS = Chat_NameServiceTypeSupport__alloc();
    checkHandle(nameServiceTS, "Chat_NameServiceTypeSupport__alloc");
    nameServiceTypeName = Chat_NameServiceTypeSupport_get_type_name(nameServiceTS);
    status = Chat_NameServiceTypeSupport_register_type(
        nameServiceTS, 
        participant, 
        nameServiceTypeName);
    checkStatus(status, "Chat_NameServiceTypeSupport_register_type");

    /* Set the ReliabilityQosPolicy to RELIABLE. */
    reliable_topic_qos = DDS_TopicQos__alloc();
    checkHandle(reliable_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    reliable_topic_qos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS;
    
    /* Make the tailored QoS the new default. */
    status = DDS_DomainParticipant_set_default_topic_qos(participant, reliable_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_set_default_topic_qos");

    /* Use the changed policy when defining the ChatMessage topic */
    chatMessageTopic = DDS_DomainParticipant_create_topic( 
        participant, 
        "Chat_ChatMessage", 
        chatMessageTypeName, 
        reliable_topic_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(chatMessageTopic, "DDS_DomainParticipant_create_topic (ChatMessage)");
    
    /* Set the DurabilityQosPolicy to TRANSIENT. */
    setting_topic_qos = DDS_TopicQos__alloc();
    checkHandle(setting_topic_qos, "DDS_TopicQos__alloc");
    status = DDS_DomainParticipant_get_default_topic_qos(participant, setting_topic_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos");
    setting_topic_qos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS;

    /* Create the NameService Topic. */
    nameServiceTopic = DDS_DomainParticipant_create_topic( 
        participant, 
        "Chat_NameService", 
        nameServiceTypeName, 
        setting_topic_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(nameServiceTopic, "DDS_DomainParticipant_create_topic");

    /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */
    partitionName = "ChatRoom";
    sub_qos = DDS_SubscriberQos__alloc();
    checkHandle(sub_qos, "DDS_SubscriberQos__alloc");
    status = DDS_DomainParticipant_get_default_subscriber_qos (participant, sub_qos);
    checkStatus(status, "DDS_DomainParticipant_get_default_subscriber_qos");
    sub_qos->partition.name._length = 1;
    sub_qos->partition.name._maximum = 1;
    sub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1);
    checkHandle(sub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf");
    sub_qos->partition.name._buffer[0] = DDS_string_alloc (strlen(partitionName) + 1);
    checkHandle(sub_qos->partition.name._buffer[0], "DDS_string_alloc");
    strcpy (sub_qos->partition.name._buffer[0], partitionName);

    /* Create a Subscriber for the UserLoad application. */
    chatSubscriber = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE);
    checkHandle(chatSubscriber, "DDS_DomainParticipant_create_subscriber");
    
    /* Create a DataReader for the NameService Topic (using the appropriate QoS). */
    nameServer = DDS_Subscriber_create_datareader( 
        chatSubscriber, 
        nameServiceTopic, 
        DDS_DATAREADER_QOS_USE_TOPIC_QOS, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(nameServer, "DDS_Subscriber_create_datareader (NameService)");

    /* Adapt the DataReaderQos for the ChatMessageDataReader to keep track of all messages. */
    message_qos = DDS_DataReaderQos__alloc();
    checkHandle(message_qos, "DDS_DataReaderQos__alloc");
    status = DDS_Subscriber_get_default_datareader_qos(chatSubscriber, message_qos);
    checkStatus(status, "DDS_Subscriber_get_default_datareader_qos");
    status = DDS_Subscriber_copy_from_topic_qos(chatSubscriber, message_qos, reliable_topic_qos);
    checkStatus(status, "DDS_Subscriber_copy_from_topic_qos");
    message_qos->history.kind = DDS_KEEP_ALL_HISTORY_QOS;

    /* Create a DataReader for the ChatMessage Topic (using the appropriate QoS). */
    loadAdmin = DDS_Subscriber_create_datareader( 
        chatSubscriber, 
        chatMessageTopic, 
        message_qos, 
        NULL,
        DDS_STATUS_MASK_NONE);
    checkHandle(loadAdmin, "DDS_Subscriber_create_datareader (ChatMessage)");
    
    /* Initialize the Query Arguments. */
    args._length = 1;
    args._maximum = 1;
    args._buffer = DDS_StringSeq_allocbuf(1);
    checkHandle(args._buffer, "DDS_StringSeq_allocbuf");
    args._buffer[0] = DDS_string_alloc (12);  /* Enough for maximum size numbers. */
    checkHandle(args._buffer[0], "DDS_string_alloc");
    sprintf(args._buffer[0], "%d", 0);
    
    /* Create a QueryCondition that will contain all messages with userID=ownID */
    singleUser = DDS_DataReader_create_querycondition( 
        loadAdmin, 
        DDS_ANY_SAMPLE_STATE, 
        DDS_ANY_VIEW_STATE, 
        DDS_ANY_INSTANCE_STATE, 
        "userID=%0", 
        &args);
    checkHandle(singleUser, "DDS_DataReader_create_querycondition (singleUser Query)");
    
    /* Create a ReadCondition that will contain new users only */
    newUser = DDS_DataReader_create_readcondition( 
        nameServer, 
        DDS_NOT_READ_SAMPLE_STATE, 
        DDS_NEW_VIEW_STATE, 
        DDS_ALIVE_INSTANCE_STATE);
    checkHandle(newUser, "DDS_DataReader_create_readcondition (newUser)");

    /* Obtain a StatusCondition that triggers only when a Writer changes Liveliness */
    leftUser = DDS_DataReader_get_statuscondition(loadAdmin);
    checkHandle(leftUser, "DDS_DataReader_get_statuscondition");
    status = DDS_StatusCondition_set_enabled_statuses(leftUser, DDS_LIVELINESS_CHANGED_STATUS);
    checkStatus(status, "DDS_StatusCondition_set_enabled_statuses");

    /* Create a bare guard which will be used to close the room */
    escape = DDS_GuardCondition__alloc();
    checkHandle(escape, "DDS_GuardCondition__alloc");

    /* Create a waitset and add the ReadConditions */
    userLoadWS = DDS_WaitSet__alloc();
    checkHandle(userLoadWS, "DDS_WaitSet__alloc");
    status = DDS_WaitSet_attach_condition(userLoadWS, newUser);
    checkStatus(status, "DDS_WaitSet_attach_condition (newUser)");
    status = DDS_WaitSet_attach_condition(userLoadWS, leftUser);
    checkStatus(status, "DDS_WaitSet_attach_condition (leftUser)");
    status = DDS_WaitSet_attach_condition(userLoadWS, escape);
    checkStatus(status, "DDS_WaitSet_attach_condition (escape)");
    
    /* Initialize and pre-allocate the GuardList used to obtain the triggered Conditions. */
    guardList = DDS_ConditionSeq__alloc();
    checkHandle(guardList, "DDS_ConditionSeq__alloc");
    guardList->_maximum = 3;
    guardList->_length = 0;
    guardList->_buffer = DDS_ConditionSeq_allocbuf(3);
    checkHandle(guardList->_buffer, "DDS_ConditionSeq_allocbuf");
    
    /* Remove all known Users that are not currently active. */
    status = Chat_NameServiceDataReader_take(
        nameServer,
        &nsList, 
        &infoSeq, 
        DDS_LENGTH_UNLIMITED, 
        DDS_ANY_SAMPLE_STATE, 
        DDS_ANY_VIEW_STATE, 
        DDS_NOT_ALIVE_INSTANCE_STATE);
    checkStatus(status, "Chat_NameServiceDataReader_take");
    status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq);
    checkStatus(status, "Chat_NameServiceDataReader_return_loan");
    
    /* Start the sleeper thread. */
    pthread_attr_init(&tattr);
    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
    pthread_create (&tid, &tattr, delayedEscape, NULL);
    pthread_attr_destroy(&tattr);

    while (!closed) {
        /* Wait until at least one of the Conditions in the waitset triggers. */
        status = DDS_WaitSet_wait(userLoadWS, guardList, &timeout);
        checkStatus(status, "DDS_WaitSet_wait");

        /* Walk over all guards to display information */
        for (i = 0; i < guardList->_length; i++) {
            guard = guardList->_buffer[i];
            if (guard == newUser) {
                /* The newUser ReadCondition contains data */
                status = Chat_NameServiceDataReader_read_w_condition( 
                    nameServer, 
                    &nsList, 
                    &infoSeq, 
                    DDS_LENGTH_UNLIMITED, 
                    newUser);
                checkStatus(status, "Chat_NameServiceDataReader_read_w_condition");
                
                for (j = 0; j < nsList._length; j++) {
                    printf ("New user: %s\n", nsList._buffer[j].name);
                }
                status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq);
                checkStatus(status, "Chat_NameServiceDataReader_return_loan");

            } else if (guard == leftUser) {
                /* Some liveliness has changed (either a DataWriter joined or a DataWriter left) */
                status = DDS_DataReader_get_liveliness_changed_status(loadAdmin, &livChangStatus);
                checkStatus(status, "DDS_DataReader_get_liveliness_changed_status");
                if (livChangStatus.alive_count < prevCount) {
                    /* A user has left the ChatRoom, since a DataWriter lost its liveliness */
                    /* Take the effected users so tey will not appear in the list later on. */
                    status = Chat_NameServiceDataReader_take( 
                        nameServer, 
                        &nsList, 
                        &infoSeq, 
                        DDS_LENGTH_UNLIMITED, 
                        DDS_ANY_SAMPLE_STATE, 
                        DDS_ANY_VIEW_STATE, 
                        DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE);
                    checkStatus(status, "Chat_NameServiceDataReader_take");
    
                    for (j = 0; j < nsList._length; j++) {
                        /* re-apply query arguments */
                        sprintf(args._buffer[0], "%d", nsList._buffer[j].userID);
                        status = DDS_QueryCondition_set_query_parameters(singleUser, &args);
                        checkStatus(status, "DDS_QueryCondition_set_query_parameters");
    
                        /* Read this users history */
                        status = Chat_ChatMessageDataReader_take_w_condition( 
                            loadAdmin, 
                            &msgList, 
                            &infoSeq2, 
                            DDS_LENGTH_UNLIMITED, 
                            singleUser);
                        checkStatus(status, "Chat_ChatMessageDataReader_take_w_condition");
                        
                        /* Display the user and his history */
                        printf (
                            "Departed user %s has sent %d messages\n", 
                            nsList._buffer[j].name,
                            msgList._length);
                        status = Chat_ChatMessageDataReader_return_loan(loadAdmin, &msgList, &infoSeq2);
                        checkStatus(status, "Chat_ChatMessageDataReader_return_loan");
                    }
                    status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq);
                    checkStatus(status, "Chat_NameServiceDataReader_return_loan");
                }
                prevCount = livChangStatus.alive_count;

            } else if (guard == escape) {
                printf ("UserLoad has terminated.\n");
                closed = 1;
            }
            else
            {
                assert(0);
            };
        } /* for */
    } /* while (!closed) */

    /* Remove all Conditions from the WaitSet. */
    status = DDS_WaitSet_detach_condition(userLoadWS, escape);
    checkStatus(status, "DDS_WaitSet_detach_condition (escape)");
    status = DDS_WaitSet_detach_condition(userLoadWS, leftUser);
    checkStatus(status, "DDS_WaitSet_detach_condition (leftUser)");
    status = DDS_WaitSet_detach_condition(userLoadWS, newUser);
    checkStatus(status, "DDS_WaitSet_detach_condition (newUser)");

    /* Free all resources */
    DDS_free(guardList);
    DDS_free(args._buffer);
    DDS_free(userLoadWS);
    DDS_free(escape);
    DDS_free(setting_topic_qos);
    DDS_free(reliable_topic_qos);
    DDS_free(nameServiceTypeName);
    DDS_free(chatMessageTypeName);
    DDS_free(nameServiceTS);
    DDS_free(chatMessageTS);    
    status = DDS_DomainParticipant_delete_contained_entities(participant);
    checkStatus(status, "DDS_DomainParticipant_delete_contained_entities");
    status = DDS_DomainParticipantFactory_delete_participant(
        DDS_TheParticipantFactory, 
        participant);
    checkStatus(status, "DDS_DomainParticipantFactory_delete_participant");
    
    return 0;
}
Exemplo n.º 29
0
static void test_aux (void)
{
	DDS_DomainParticipant		p;
	DDS_Topic			t;
	DDS_TopicDescription		td;
	DDS_Subscriber			sub, nsub;
	DDS_DataReader			dr;
	DDS_StatusCondition		sc, sc2;
	DDS_StatusMask 			sm;
	DDS_InstanceHandle_t		h, h2;
	DDS_Duration_t			w;
	DDS_LivelinessChangedStatus	lcs;
	DDS_RequestedDeadlineMissedStatus dms;
	DDS_RequestedIncompatibleQosStatus iqs;
	DDS_SampleLostStatus		sls;
	DDS_SampleRejectedStatus	srs;
	DDS_SubscriptionMatchedStatus	sms;
	DDS_InstanceHandleSeq		handles;
	DDS_ReturnCode_t		r;

	p = DDS_DomainParticipantFactory_create_participant (0, DDS_PARTICIPANT_QOS_DEFAULT, NULL, 0);
	fail_unless (p != NULL);

	r = register_HelloWorldData_type (p);
	fail_unless (r == DDS_RETCODE_OK);
	t = DDS_DomainParticipant_create_topic (p, "HelloWorld", TYPE_NAME, DDS_TOPIC_QOS_DEFAULT, NULL, 0);
	fail_unless (t != NULL);

	td = DDS_DomainParticipant_lookup_topicdescription (p, "HelloWorld");
	fail_unless (td != NULL);

	sub = DDS_DomainParticipant_create_subscriber (p, DDS_SUBSCRIBER_QOS_DEFAULT, NULL, 0);
	fail_unless (sub != NULL);

	v_printf (" - Test reader auxiliary functions.\r\n");
	dr = DDS_Subscriber_create_datareader (sub, td, DDS_DATAREADER_QOS_DEFAULT, NULL, 0);
	fail_unless (dr != NULL);

	sc = DDS_DataReader_get_statuscondition (dr);
	fail_unless (sc != NULL);
	
	sc2 = DDS_Entity_get_statuscondition (dr);
	fail_unless (sc2 == sc);
	
	sm = DDS_DataReader_get_status_changes (dr);
	fail_unless (sm == 0);

	sm = DDS_Entity_get_status_changes (dr);
	fail_unless (sm == 0);

	h = DDS_Entity_get_instance_handle (dr);
	fail_unless (h != 0);

	h2 = DDS_DataReader_get_instance_handle (dr);
	fail_unless (h == h2);

	r = DDS_DataReader_get_liveliness_changed_status (dr, &lcs);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DataReader_get_requested_deadline_missed_status (dr, &dms);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DataReader_get_requested_incompatible_qos_status (dr, &iqs);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DataReader_get_sample_lost_status (dr, &sls);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DataReader_get_sample_rejected_status (dr, &srs);
	fail_unless (r == DDS_RETCODE_OK);

	r = DDS_DataReader_get_subscription_matched_status (dr, &sms);
	fail_unless (r == DDS_RETCODE_OK);

	td = DDS_DataReader_get_topicdescription (dr);
	fail_unless ((DDS_Topic) td == t);

	nsub = DDS_DataReader_get_subscriber (dr);
	fail_unless (nsub == sub);

	w.sec = 2;
	w.nanosec = 0;
	r = DDS_DataReader_wait_for_historical_data (dr, &w);
	fail_unless (r == DDS_RETCODE_OK);

	DDS_InstanceHandleSeq__init (&handles);
	r = DDS_DataReader_get_matched_publications (dr, &handles);
	fail_unless (r == DDS_RETCODE_OK);
	DDS_InstanceHandleSeq__clear (&handles);

	r = DDS_Subscriber_delete_datareader (sub, dr);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipant_delete_topic (p, t);
	fail_unless (r == DDS_RETCODE_OK);
	unregister_HelloWorldData_type (p);
	r = DDS_DomainParticipantFactory_delete_participant (p);
	fail_unless (r == DDS_RETCODE_OK);
}
Exemplo n.º 30
0
static void test_listener (void)
{
	DDS_DomainParticipant		p;
	DDS_Subscriber			sub;
	DDS_SubscriberListener		*lp;
	DDS_ReturnCode_t		r;

	p = DDS_DomainParticipantFactory_create_participant (1, DDS_PARTICIPANT_QOS_DEFAULT, NULL, 0);
	fail_unless (p != NULL);

	v_printf (" - Test subscriber listener.\r\n");
	sub = DDS_DomainParticipant_create_subscriber (p, DDS_SUBSCRIBER_QOS_DEFAULT, NULL, 0);
	fail_unless (sub != NULL);
	lp = DDS_Subscriber_get_listener (sub);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == NULL && 
		     lp->on_requested_incompatible_qos == NULL &&
		     lp->on_sample_lost == NULL &&
		     lp->on_subscription_matched == NULL &&
		     lp->on_data_available == NULL &&
		     lp->on_liveliness_changed == NULL &&
		     lp->on_data_on_readers == NULL &&
		     lp->on_requested_deadline_missed == NULL);
	r = DDS_Subscriber_set_listener (sub, &sub_listener,
			DDS_SAMPLE_REJECTED_STATUS |
			DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS |
			DDS_SAMPLE_LOST_STATUS |
			DDS_SUBSCRIPTION_MATCHED_STATUS |
			DDS_DATA_AVAILABLE_STATUS |
			DDS_LIVELINESS_CHANGED_STATUS |
			DDS_DATA_ON_READERS_STATUS |
			DDS_REQUESTED_DEADLINE_MISSED_STATUS);
	fail_unless (r == DDS_RETCODE_OK);

	v_printf (" - Test specific subscriber listener updates.\r\n");
	lp = DDS_Subscriber_get_listener (sub);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == sample_rejected && 
		     lp->on_requested_incompatible_qos == requested_incompatible_qos &&
		     lp->on_sample_lost == sample_lost &&
		     lp->on_subscription_matched == subscription_matched &&
		     lp->on_data_available == data_available &&
		     lp->on_liveliness_changed == liveliness_changed &&
		     lp->on_data_on_readers == data_on_readers &&
		     lp->on_requested_deadline_missed == requested_deadline_missed);

	v_printf (" - Test default subscriber listener update.\r\n");
	r = DDS_Subscriber_set_listener (sub, NULL, DDS_REQUESTED_DEADLINE_MISSED_STATUS);
	fail_unless (r == DDS_RETCODE_OK);
	lp = DDS_Subscriber_get_listener (sub);
	fail_unless (lp != NULL &&
		     lp->on_sample_rejected == NULL && 
		     lp->on_requested_incompatible_qos == NULL &&
		     lp->on_sample_lost == NULL &&
		     lp->on_subscription_matched == NULL &&
		     lp->on_data_available == NULL &&
		     lp->on_liveliness_changed == NULL &&
		     lp->on_data_on_readers == NULL &&
		     lp->on_requested_deadline_missed == NULL);
	r = DDS_DomainParticipant_delete_subscriber (p, sub);
	fail_unless (r == DDS_RETCODE_OK);
	r = DDS_DomainParticipantFactory_delete_participant (p);
	fail_unless (r == DDS_RETCODE_OK);
}