static void remove_state_reader () { DDS_ReturnCode_t error; error = DDS_DataReader_delete_contained_entities (dr); if (error) fatal ("DDS_DataReader_delete_contained_entities() failed (%s)!", DDS_error (error)); DDS_DataReader_set_listener (dr, NULL, 0); error = DDS_Subscriber_delete_datareader (sub, dr); if (error) fatal ("DDS_Subscriber_delete_datareader() failed (%s)!", DDS_error (error)); }
static void make_state_reader () { DDS_DataReaderQos rd_qos; DDS_ReturnCode_t error; printf ("Create Reader\r\n"); /* === Create a subscriber === */ sub = DDS_DomainParticipant_create_subscriber (part, 0, NULL, 0); if (!sub) { fatal ("DDS_DomainParticipant_create_subscriber () returned an error!"); DDS_DomainParticipantFactory_delete_participant (part); } /* Test get_qos() fynctionality. */ if ((error = DDS_Subscriber_get_qos (sub, &sqos)) != DDS_RETCODE_OK) fatal ("DDS_Subscriber_get_qos () failed (%s)!", DDS_error (error)); /* Setup reader QoS parameters. */ DDS_Subscriber_get_default_datareader_qos (sub, &rd_qos); /* === create the reader === */ rd_qos.history.kind = DDS_KEEP_LAST_HISTORY_QOS; rd_qos.history.depth = 1; rd_qos.reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; rd_qos.durability.kind = DDS_TRANSIENT_LOCAL_DURABILITY_QOS; rd_qos.ownership.kind = DDS_EXCLUSIVE_OWNERSHIP_QOS; dr = DDS_Subscriber_create_datareader (sub, topic_desc, &rd_qos, NULL, sm); if (!dr) fatal ("DDS_DomainParticipant_create_datareader () returned an error!"); DDS_DataReaderListener *l = DDS_DataReader_get_listener(dr); if (NULL != l) { l->on_data_available = dr_listener_data_available; error = DDS_DataReader_set_listener(dr, l, DDS_DATA_AVAILABLE_STATUS); } error = DDS_DataReader_enable(dr); /* === end program === */ if (test == 3) return; if (!writer) while (1) sleep (1); }
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); }
int OSPL_MAIN (int argc, char *argv[]) { DDS_sequence_ListenerData_Msg* message_seq = DDS_sequence_ListenerData_Msg__alloc(); DDS_SampleInfoSeq* message_infoSeq = DDS_SampleInfoSeq__alloc(); DDS_Subscriber message_Subscriber; DDS_DataReader message_DataReader; DDS_WaitSet waitSet; DDS_ConditionSeq* guardList = NULL; struct DDS_DataReaderListener *message_Listener; DDS_Duration_t timeout = { 0, 200000000 }; struct Listener_data* Listener_data; DDS_GuardCondition guardCondition = DDS_GuardCondition__alloc(); int count = 0; DDS_StatusMask mask; // Create DDS DomainParticipant createParticipant("Listener example"); // Register the Topic's type in the DDS Domain. g_MessageTypeSupport = ListenerData_MsgTypeSupport__alloc(); checkHandle(g_MessageTypeSupport, "ListenerData_MsgTypeSupport__alloc"); registerMessageType(g_MessageTypeSupport); // Create the Topic's in the DDS Domain. g_MessageTypeName = ListenerData_MsgTypeSupport_get_type_name(g_MessageTypeSupport); g_MessageTopic = createTopic("ListenerData_Msg", g_MessageTypeName); DDS_free(g_MessageTypeName); DDS_free(g_MessageTypeSupport); // Create the Subscriber's in the DDS Domain. message_Subscriber = createSubscriber(); // Request a Reader from the the Subscriber. message_DataReader = createDataReader(message_Subscriber, g_MessageTopic); /* Allocate the DataReaderListener interface. */ message_Listener = DDS_DataReaderListener__alloc(); checkHandle(message_Listener, "DDS_DataReaderListener__alloc"); Listener_data = malloc(sizeof(struct Listener_data)); checkHandle(Listener_data, "malloc"); Listener_data->guardCondition = &guardCondition; Listener_data->message_DataReader = &message_DataReader; Listener_data->isClosed = &isClosed; message_Listener->listener_data = Listener_data; message_Listener->on_data_available = on_data_available; message_Listener->on_requested_deadline_missed = on_requested_deadline_missed; mask = DDS_DATA_AVAILABLE_STATUS | DDS_REQUESTED_DEADLINE_MISSED_STATUS; g_status = DDS_DataReader_set_listener(message_DataReader, message_Listener, mask); checkStatus(g_status, "DDS_DataReader_set_listener"); // WaitSet is used to avoid spinning in the loop below. waitSet = DDS_WaitSet__alloc(); checkHandle(waitSet, "DDS_WaitSet__alloc"); g_status = DDS_WaitSet_attach_condition(waitSet, guardCondition); checkStatus(g_status, "DDS_WaitSet_attach_condition (readCondition)"); // Initialize and pre-allocate the GuardList used to obtain the triggered Conditions. guardList = DDS_ConditionSeq__alloc(); checkHandle(guardList, "DDS_ConditionSeq__alloc"); guardList->_maximum = 1; guardList->_length = 0; guardList->_buffer = DDS_ConditionSeq_allocbuf(1); checkHandle(guardList->_buffer, "DDS_ConditionSeq_allocbuf"); printf("\n=== [ListenerDataSubscriber] Ready..."); isClosed = FALSE; do { g_status = DDS_WaitSet_wait(waitSet, guardList, &timeout); if(g_status != DDS_RETCODE_TIMEOUT) { checkStatus(g_status, "DDS_WaitSet_wait"); } else { printf("\n=== [ListenerDataSubscriber] (timeout)"); } g_status = DDS_GuardCondition_set_trigger_value(guardCondition, FALSE); checkStatus(g_status, "DDS_GuardCondition_set_trigger_value"); ++ count; } while( isClosed == FALSE && count < 1500 ); printf("\n\n=== [ListenerDataSubscriber] isClosed"); // Cleanup DDS from the created Entities. deleteDataReader(message_Subscriber, message_DataReader); deleteSubscriber(message_Subscriber); deleteTopic(g_MessageTopic); deleteParticipant(); // Cleanup C allocations // Recursively free the instances sequence using the OpenSplice API. DDS_free(message_seq); DDS_free(message_infoSeq); free(Listener_data); // Print out an empty line, just to avoid the shell writing on the same line as our last output. printf("\n"); return 0; }
DDS_MultiTopic DDS_DomainParticipant_create_simulated_multitopic ( DDS_DomainParticipant participant, const DDS_char *name, const DDS_char *type_name, const DDS_char *subscription_expression, const DDS_StringSeq *expression_parameters ) { /* Type-specific DDS entities */ static Chat_ChatMessageDataReader chatMessageDR; static Chat_NameServiceDataReader nameServiceDR; static Chat_NamedMessageDataWriter namedMessageDW; /* Query related stuff */ static DDS_QueryCondition nameFinder; static DDS_StringSeq *nameFinderParams; /* QosPolicy holders */ DDS_TopicQos *namedMessageQos; DDS_SubscriberQos *sub_qos; DDS_PublisherQos *pub_qos; /* Others */ const char *partitionName = "ChatRoom"; const char *nameFinderExpr; struct MsgListenerState *listener_state; DDS_Duration_t infiniteTimeOut = DDS_DURATION_INFINITE; DDS_ReturnCode_t status; /* Lookup both components that constitute the multi-topic. */ chatMessageTopic = DDS_DomainParticipant_find_topic( participant, "Chat_ChatMessage", &infiniteTimeOut); checkHandle(chatMessageTopic, "DDS_DomainParticipant_find_topic (Chat_ChatMessage)"); nameServiceTopic = DDS_DomainParticipant_find_topic( participant, "Chat_NameService", &infiniteTimeOut); checkHandle(nameServiceTopic, "DDS_DomainParticipant_find_topic (Chat_NameService)"); /* Create a ContentFilteredTopic to filter out our own ChatMessages. */ filteredMessageTopic = DDS_DomainParticipant_create_contentfilteredtopic( participant, "Chat_FilteredMessage", chatMessageTopic, "userID <> %0", expression_parameters); checkHandle(filteredMessageTopic, "DDS_DomainParticipant_create_contentfilteredtopic"); /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */ 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 private Subscriber for the multitopic simulator. */ multiSub = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(multiSub, "DDS_DomainParticipant_create_subscriber (for multitopic)"); /* Create a DataReader for the FilteredMessage Topic (using the appropriate QoS). */ chatMessageDR = DDS_Subscriber_create_datareader( multiSub, filteredMessageTopic, DDS_DATAREADER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(chatMessageDR, "DDS_Subscriber_create_datareader (ChatMessage)"); /* Create a DataReader for the nameService Topic (using the appropriate QoS). */ nameServiceDR = DDS_Subscriber_create_datareader( multiSub, nameServiceTopic, DDS_DATAREADER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(nameServiceDR, "DDS_Subscriber_create_datareader (NameService)"); /* Define the SQL expression (using a parameterized value). */ nameFinderExpr = "userID = %0"; /* Allocate and assign the query parameters. */ nameFinderParams = DDS_StringSeq__alloc(); checkHandle(nameFinderParams, "DDS_StringSeq__alloc"); nameFinderParams->_length = 1; nameFinderParams->_maximum = 1; nameFinderParams->_buffer = DDS_StringSeq_allocbuf (1); checkHandle(nameFinderParams->_buffer, "DDS_StringSeq_allocbuf"); nameFinderParams->_buffer[0] = DDS_string_alloc ( MAX_INT_LENGTH ); checkHandle(nameFinderParams->_buffer[0], "DDS_string_alloc"); strcpy(nameFinderParams->_buffer[0], "0"); DDS_sequence_set_release(nameFinderParams, TRUE); /* Create a QueryCondition to only read corresponding nameService information by key-value. */ nameFinder = DDS_DataReader_create_querycondition( nameServiceDR, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE, nameFinderExpr, nameFinderParams); checkHandle(nameFinder, "DDS_DataReader_create_querycondition (nameFinder)"); /* Create the Topic that simulates the multi-topic (use Qos from chatMessage).*/ namedMessageQos = DDS_TopicQos__alloc(); checkHandle(namedMessageQos, "DDS_TopicQos__alloc"); status = DDS_Topic_get_qos(chatMessageTopic, namedMessageQos); checkStatus(status, "DDS_Topic_get_qos"); /* Create the NamedMessage Topic whose samples simulate the MultiTopic */ namedMessageTopic = DDS_DomainParticipant_create_topic( participant, "Chat_NamedMessage", type_name, namedMessageQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(namedMessageTopic, "DDS_DomainParticipant_create_topic (NamedMessage)"); /* Adapt the default PublisherQos to write into the "ChatRoom" Partition. */ 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 private Publisher for the multitopic simulator. */ multiPub = DDS_DomainParticipant_create_publisher(participant, pub_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(multiPub, "DDS_DomainParticipant_create_publisher (for multitopic)"); /* Create a DataWriter for the multitopic. */ namedMessageDW = DDS_Publisher_create_datawriter( multiPub, namedMessageTopic, DDS_DATAWRITER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(namedMessageDW, "DDS_Publisher_create_datawriter (NamedMessage)"); /* Allocate the DataReaderListener interface. */ msgListener = DDS_DataReaderListener__alloc(); checkHandle(msgListener, "DDS_DataReaderListener__alloc"); /* Fill the listener_data with pointers to all entities needed by the Listener implementation. */ listener_state = malloc(sizeof(struct MsgListenerState)); checkHandle(listener_state, "malloc"); listener_state->chatMessageDR = chatMessageDR; listener_state->nameServiceDR = nameServiceDR; listener_state->namedMessageDW = namedMessageDW; listener_state->nameFinder = nameFinder; listener_state->nameFinderParams = nameFinderParams; msgListener->listener_data = listener_state; /* Assign the function pointer attributes to their implementation functions. */ msgListener->on_data_available = (void (*)(void *, DDS_DataReader)) on_message_available; msgListener->on_requested_deadline_missed = NULL; msgListener->on_requested_incompatible_qos = NULL; msgListener->on_sample_rejected = NULL; msgListener->on_liveliness_changed = NULL; msgListener->on_subscription_matched = NULL; msgListener->on_sample_lost = NULL; /* Attach the DataReaderListener to the DataReader, only enabling the data_available event. */ status = DDS_DataReader_set_listener(chatMessageDR, msgListener, DDS_DATA_AVAILABLE_STATUS); checkStatus(status, "DDS_DataReader_set_listener"); /* Free up all resources that are no longer needed. */ DDS_free(namedMessageQos); DDS_free(sub_qos); DDS_free(pub_qos); /* Return the simulated Multitopic. */ return namedMessageTopic; }
static int subscriber_main(int domainId, int sample_count) { DDS_DomainParticipant *participant = NULL; int count = 0; struct DDS_Duration_t poll_period = {4,0}; DDS_ReturnCode_t retcode; DDS_Subscriber *builtin_subscriber = NULL; DDS_PublicationBuiltinTopicDataDataReader *builtin_publication_datareader = NULL; /* Listener for the Built-in Publication Data Reader */ struct DDS_DataReaderListener builtin_publication_listener = DDS_DataReaderListener_INITIALIZER; /* If you want to change the type_code_max_serialized_length * programmatically (e.g., to 3070) rather than using the XML file, you * will need to add the following lines to your code and comment out the * create_participant call bellow. */ /* struct DDS_DomainParticipantQos participant_qos = DDS_DomainParticipantQos_INITIALIZER; 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; } participant_qos.resource_limits.type_code_max_serialized_length = 3070; participant = DDS_DomainParticipantFactory_create_participant( DDS_TheParticipantFactory, domainId, &participant_qos, NULL, DDS_STATUS_MASK_NONE); if (participant == NULL) { printf("create_participant error\n"); subscriber_shutdown(participant); return -1; } */ /* 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, DDS_STATUS_MASK_NONE); if (participant == NULL) { printf("create_participant error\n"); subscriber_shutdown(participant); return -1; } /* First get the built-in subscriber */ builtin_subscriber = DDS_DomainParticipant_get_builtin_subscriber(participant); if (builtin_subscriber == NULL) { printf("***Error: failed to create builtin subscriber\n"); return 0; } /* Then get the data reader for the built-in subscriber */ builtin_publication_datareader = (DDS_PublicationBuiltinTopicDataDataReader*) DDS_Subscriber_lookup_datareader(builtin_subscriber, DDS_PUBLICATION_TOPIC_NAME); if (builtin_publication_datareader == NULL) { printf("***Error: failed to create builtin publication data reader\n"); return 0; } /* Finally install the listener */ builtin_publication_listener.on_data_available = BuiltinPublicationListener_on_data_available; DDS_DataReader_set_listener((DDS_DataReader*)builtin_publication_datareader, &builtin_publication_listener, DDS_DATA_AVAILABLE_STATUS); /* Main loop */ for (count=0; (sample_count == 0) || (count < sample_count); ++count) { NDDS_Utility_sleep(&poll_period); } /* Cleanup and delete all entities */ return subscriber_shutdown(participant); }