RTI Connext C API Version 7.3.0
HelloWorld_subscriber.c

RTI Connext Subscription Example

The unmodified subscription example generated by rtiddsgen (see the Code Generator User's Manual for more information).

HelloWorld_subscriber.c

/*
* (c) Copyright, Real-Time Innovations, 2012. All rights reserved.
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the software solely for use with RTI Connext DDS. Licensee may
* redistribute copies of the software provided that all such copies are subject
* to this license. The software is provided "as is", with no warranty of any
* type, including any warranty for fitness for any purpose. RTI is under no
* obligation to maintain or support the software. RTI shall not be liable for
* any incidental or consequential damages arising out of the use or inability
* to use the software.
*/
/* HelloWorld_subscriber.c
A subscription example
This file is derived from code automatically generated by the rtiddsgen
command:
rtiddsgen -language C -example <arch> HelloWorld.idl
Example subscription of type HelloWorld automatically generated by
'rtiddsgen'. To test it, follow these steps:
(1) Compile this file and the example publication.
(2) Start the subscription on the same domain used for RTI Connext
(3) Start the publication on the same domain used for RTI Connext
(4) [Optional] Specify the list of discovery initial peers and
multicast receive addresses via an environment variable or a file
(in the current working directory) called NDDS_DISCOVERY_PEERS.
You can run any number of publisher and subscriber programs, and can
add and remove them dynamically from the domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include "ndds/ndds_c.h"
#include "HelloWorld.h"
#include "HelloWorldSupport.h"
void HelloWorldListener_on_requested_deadline_missed(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_RequestedDeadlineMissedStatus *status)
{
}
void HelloWorldListener_on_requested_incompatible_qos(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_RequestedIncompatibleQosStatus *status)
{
}
void HelloWorldListener_on_sample_rejected(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_SampleRejectedStatus *status)
{
}
void HelloWorldListener_on_liveliness_changed(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_LivelinessChangedStatus *status)
{
}
void HelloWorldListener_on_sample_lost(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_SampleLostStatus *status)
{
}
void HelloWorldListener_on_subscription_matched(
void* listener_data,
DDS_DataReader* reader,
const struct DDS_SubscriptionMatchedStatus *status)
{
}
void HelloWorldListener_on_data_available(
void* listener_data,
DDS_DataReader* reader)
{
HelloWorldDataReader *HelloWorld_reader = NULL;
struct HelloWorldSeq data_seq = DDS_SEQUENCE_INITIALIZER;
int i;
HelloWorld_reader = HelloWorldDataReader_narrow(reader);
if (HelloWorld_reader == NULL) {
fprintf(stderr, "DataReader narrow error\n");
return;
}
retcode = HelloWorldDataReader_take(
HelloWorld_reader,
&data_seq, &info_seq, DDS_LENGTH_UNLIMITED,
if (retcode == DDS_RETCODE_NO_DATA) {
return;
} else if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "take error %d\n", retcode);
return;
}
for (i = 0; i < HelloWorldSeq_get_length(&data_seq); ++i) {
if (DDS_SampleInfoSeq_get_reference(&info_seq, i)->valid_data) {
printf("Received data\n");
HelloWorldTypeSupport_print_data(
HelloWorldSeq_get_reference(&data_seq, i));
}
}
retcode = HelloWorldDataReader_return_loan(
HelloWorld_reader,
&data_seq, &info_seq);
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "return loan error %d\n", retcode);
}
}
/* Delete all entities */
static int subscriber_shutdown(
DDS_DomainParticipant *participant)
{
int status = 0;
if (participant != NULL) {
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "delete_contained_entities error %d\n", retcode);
status = -1;
}
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "delete_participant error %d\n", retcode);
status = -1;
}
}
/* RTI Data Distribution Service provides the finalize_instance() method on
domain participant factory for users who want to release memory used
by the participant factory. Uncomment the following block of code for
clean destruction of the singleton. */
/*
retcode = DDS_DomainParticipantFactory_finalize_instance();
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "finalize_instance error %d\n", retcode);
status = -1;
}
*/
return status;
}
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_DataReader *reader = NULL;
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 */
NULL /* listener */, DDS_STATUS_MASK_NONE);
if (participant == NULL) {
fprintf(stderr, "create_participant error\n");
subscriber_shutdown(participant);
return -1;
}
/* To customize subscriber QoS, use
the configuration file USER_QOS_PROFILES.xml */
participant, &DDS_SUBSCRIBER_QOS_DEFAULT, NULL /* listener */,
if (subscriber == NULL) {
fprintf(stderr, "create_subscriber error\n");
subscriber_shutdown(participant);
return -1;
}
/* Register the type before creating the topic */
type_name = HelloWorldTypeSupport_get_type_name();
retcode = HelloWorldTypeSupport_register_type(participant, type_name);
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "register_type error %d\n", retcode);
subscriber_shutdown(participant);
return -1;
}
/* To customize topic QoS, use
the configuration file USER_QOS_PROFILES.xml */
participant, "Example HelloWorld",
type_name, &DDS_TOPIC_QOS_DEFAULT, NULL /* listener */,
if (topic == NULL) {
fprintf(stderr, "create_topic error\n");
subscriber_shutdown(participant);
return -1;
}
/* Set up a data reader listener */
reader_listener.on_requested_deadline_missed =
HelloWorldListener_on_requested_deadline_missed;
HelloWorldListener_on_requested_incompatible_qos;
reader_listener.on_sample_rejected =
HelloWorldListener_on_sample_rejected;
reader_listener.on_liveliness_changed =
HelloWorldListener_on_liveliness_changed;
reader_listener.on_sample_lost =
HelloWorldListener_on_sample_lost;
reader_listener.on_subscription_matched =
HelloWorldListener_on_subscription_matched;
reader_listener.on_data_available =
HelloWorldListener_on_data_available;
/* To customize data reader QoS, use
the configuration file USER_QOS_PROFILES.xml */
subscriber, DDS_Topic_as_topicdescription(topic),
if (reader == NULL) {
fprintf(stderr, "create_datareader error\n");
subscriber_shutdown(participant);
return -1;
}
/* Main loop */
for (count=0; (sample_count == 0) || (count < sample_count); ++count) {
printf("HelloWorld subscriber sleeping for %d sec...\n",
poll_period.sec);
NDDS_Utility_sleep(&poll_period);
}
/* Cleanup and delete all entities */
return subscriber_shutdown(participant);
}
int main(int argc, char *argv[])
{
int domain_id = 0;
int sample_count = 0; /* infinite loop */
if (argc >= 2) {
domain_id = atoi(argv[1]);
}
if (argc >= 3) {
sample_count = atoi(argv[2]);
}
/* Uncomment this to turn on additional logging
NDDS_Config_Logger_set_verbosity_by_category(
NDDS_Config_Logger_get_instance(),
NDDS_CONFIG_LOG_CATEGORY_API,
NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL);
*/
return subscriber_main(domain_id, sample_count);
}
#define DDS_TheParticipantFactory
Can be used as an alias for the singleton factory returned by the operation DDS_DomainParticipantFact...
Definition: domain.ifc:2860
const struct DDS_DomainParticipantQos DDS_PARTICIPANT_QOS_DEFAULT
Special value for creating a DomainParticipant with default QoS.
DDS_ReturnCode_t DDS_DomainParticipantFactory_delete_participant(DDS_DomainParticipantFactory *self, DDS_DomainParticipant *a_participant)
Deletes an existing DDS_DomainParticipant.
DDS_DomainParticipant * DDS_DomainParticipantFactory_create_participant(DDS_DomainParticipantFactory *self, DDS_DomainId_t domainId, const struct DDS_DomainParticipantQos *qos, const struct DDS_DomainParticipantListener *listener, DDS_StatusMask mask)
Creates a new DDS_DomainParticipant object.
struct DDS_DomainParticipantImpl DDS_DomainParticipant
<<interface>> Container for all DDS_DomainEntity objects.
Definition: infrastructure.ifc:9781
const struct DDS_SubscriberQos DDS_SUBSCRIBER_QOS_DEFAULT
Special value for creating a DDS_Subscriber with default QoS.
const struct DDS_TopicQos DDS_TOPIC_QOS_DEFAULT
Special value for creating a DDS_Topic with default QoS.
DDS_Subscriber * DDS_DomainParticipant_create_subscriber(DDS_DomainParticipant *self, const struct DDS_SubscriberQos *qos, const struct DDS_SubscriberListener *listener, DDS_StatusMask mask)
Creates a DDS_Subscriber with the desired QoS policies and attaches to it the specified DDS_Subscribe...
DDS_Topic * DDS_DomainParticipant_create_topic(DDS_DomainParticipant *self, const char *topic_name, const char *type_name, const struct DDS_TopicQos *qos, const struct DDS_TopicListener *listener, DDS_StatusMask mask)
Creates a DDS_Topic with the desired QoS policies and attaches to it the specified DDS_TopicListener.
DDS_ReturnCode_t DDS_DomainParticipant_delete_contained_entities(DDS_DomainParticipant *self)
Delete all the entities that were created by means of the "create" operations on the DDS_DomainPartic...
const DDS_InstanceStateMask DDS_ANY_INSTANCE_STATE
Any instance state ALIVE_INSTANCE_STATE | NOT_ALIVE_DISPOSED_INSTANCE_STATE | NOT_ALIVE_NO_WRITERS_IN...
struct DDS_DataReaderImpl DDS_DataReader
<<interface>> Allows the application to: (1) declare the data it wishes to receive (i....
Definition: subscription.ifc:230
#define DDS_DataReaderListener_INITIALIZER
Initializer for new DDS_DataReaderListener.
Definition: subscription.ifc:2243
const DDS_Long DDS_LENGTH_UNLIMITED
A special value indicating an unlimited quantity.
DDS_ReturnCode_t
Type for return codes.
Definition: infrastructure.ifc:1351
@ DDS_RETCODE_OK
Successful return.
Definition: infrastructure.ifc:1354
@ DDS_RETCODE_NO_DATA
Indicates a transient situation where the operation did not return any data but there is no inherent ...
Definition: infrastructure.ifc:1398
const DDS_SampleStateMask DDS_ANY_SAMPLE_STATE
Any sample state DDS_READ_SAMPLE_STATE | DDS_NOT_READ_SAMPLE_STATE.
#define DDS_SEQUENCE_INITIALIZER
An initializer for new sequence instances.
Definition: sequence.ifc:566
#define DDS_STATUS_MASK_NONE
No bits are set.
Definition: infrastructure.ifc:1425
#define DDS_STATUS_MASK_ALL
All bits are set.
Definition: infrastructure.ifc:1431
const struct DDS_DataReaderQos DDS_DATAREADER_QOS_DEFAULT
Special value for creating data reader with default QoS.
DDS_DataReader * DDS_Subscriber_create_datareader(DDS_Subscriber *self, DDS_TopicDescription *topic, const struct DDS_DataReaderQos *qos, const struct DDS_DataReaderListener *listener, DDS_StatusMask mask)
Creates a DDS_DataReader that will be attached and belong to the DDS_Subscriber.
struct DDS_SubscriberImpl DDS_Subscriber
<<interface>> A subscriber is the object responsible for actually receiving data from a subscription.
Definition: subscription.ifc:239
DDS_TopicDescription * DDS_Topic_as_topicdescription(DDS_Topic *topic)
Access a DDS_Topic's DDS_TopicDescription supertype instance.
struct DDS_TopicWrapperI DDS_Topic
<<interface>> The most basic description of the data to be published and subscribed.
Definition: topic.ifc:521
const DDS_ViewStateMask DDS_ANY_VIEW_STATE
Any view state DDS_NEW_VIEW_STATE | DDS_NOT_NEW_VIEW_STATE.
void NDDS_Utility_sleep(const struct DDS_Duration_t *durationIn)
Block the calling thread for the specified duration.
<<interface>> DDS_Listener for reader status.
Definition: subscription.ifc:2201
DDS_DataReaderListener_RequestedIncompatibleQosCallback on_requested_incompatible_qos
Handles the DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS communication status.
Definition: subscription.ifc:2215
DDS_DataReaderListener_SampleRejectedCallback on_sample_rejected
Handles the DDS_SAMPLE_REJECTED_STATUS communication status.
Definition: subscription.ifc:2219
DDS_DataReaderListener_SampleLostCallback on_sample_lost
Handles the DDS_SAMPLE_LOST_STATUS communication status.
Definition: subscription.ifc:2236
DDS_DataReaderListener_DataAvailableCallback on_data_available
Handle the DDS_DATA_AVAILABLE_STATUS communication status.
Definition: subscription.ifc:2228
DDS_DataReaderListener_LivelinessChangedCallback on_liveliness_changed
Handles the DDS_LIVELINESS_CHANGED_STATUS communication status.
Definition: subscription.ifc:2224
DDS_DataReaderListener_SubscriptionMatchedCallback on_subscription_matched
Handles the DDS_SUBSCRIPTION_MATCHED_STATUS communication status.
Definition: subscription.ifc:2232
DDS_DataReaderListener_RequestedDeadlineMissedCallback on_requested_deadline_missed
Handles the DDS_REQUESTED_DEADLINE_MISSED_STATUS communication status.
Definition: subscription.ifc:2210
Type for duration representation.
Definition: infrastructure.ifc:444
DDS_Long sec
seconds
Definition: infrastructure.ifc:447
DDS_LIVELINESS_CHANGED_STATUS
Definition: subscription.ifc:316
DDS_REQUESTED_DEADLINE_MISSED_STATUS
Definition: subscription.ifc:266
DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS
Definition: subscription.ifc:374
Declares IDL sequence < DDS_SampleInfo > .
Definition: subscription.ifc:1348
DDS_SAMPLE_LOST_STATUS
Definition: subscription.ifc:507
DDS_SAMPLE_REJECTED_STATUS
Definition: subscription.ifc:598
DDS_SUBSCRIPTION_MATCHED_STATUS
Definition: subscription.ifc:653