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); }
DDS_DataReader createDataReader(DDS_Subscriber subscriber, DDS_Topic topic) { DDS_DataReader dataReader; DDS_TopicQos* topicQos; DDS_DataReaderQos* dataReaderQos; // Create a DataWriter for this Topic (using the appropriate QoS). dataReaderQos = DDS_DataReaderQos__alloc(); checkHandle(dataReaderQos, "DDS_DataReaderQos__alloc"); topicQos = DDS_TopicQos__alloc(); checkHandle(topicQos, "DDS_TopicQos__alloc"); g_status = DDS_Topic_get_qos(topic, topicQos); checkStatus(g_status, "DDS_Topic_get_qos"); g_status = DDS_Subscriber_get_default_datareader_qos(subscriber, dataReaderQos); checkStatus(g_status, "DDS_Subscriber_get_default_datareader_qos"); g_status = DDS_Subscriber_copy_from_topic_qos(subscriber, dataReaderQos, topicQos); checkStatus(g_status, "DDS_Publisher_copy_from_topic_qos"); dataReader = DDS_Subscriber_create_datareader(subscriber, topic, dataReaderQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(dataReader, "DDS_Subscriber_create_datareader"); DDS_free(dataReaderQos); DDS_free(topicQos); return dataReader; }
DDS_Topic createTopic(const char *topicName, const char *typeName) { DDS_Topic topic; const char* messageFirstPart; char* message; int messageFirstPartLength, topicNameLength; DDS_TopicQos* topicQos = DDS_TopicQos__alloc(); checkHandle(topicQos, "DDS_TopicQos__alloc"); g_status = DDS_DomainParticipant_get_default_topic_qos(g_domainParticipant, topicQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_topic_qos"); topicQos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; topicQos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS; // Set the history Policy topicQos->history.kind = DDS_KEEP_LAST_HISTORY_QOS; topicQos->history.depth = 2; // Use the changed policy when defining the Ownership topic topic = DDS_DomainParticipant_create_topic(g_domainParticipant, topicName, typeName, topicQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(topic, "DDS::DomainParticipant::create_topic ()"); //Format error message messageFirstPart = "DDS_DomainParticipant_create_topic"; messageFirstPartLength = strlen(messageFirstPart); topicNameLength = strlen(topicName); message = (char*) DDS_string_alloc(messageFirstPartLength + topicNameLength); snprintf(message, messageFirstPartLength + topicNameLength + 1, "%s %s", messageFirstPart, topicName); checkHandle(topic, message); DDS_free(message); DDS_free(topicQos); return topic; }
int OSPL_MAIN (int argc, const char *argv[]) { DDS_Publisher message_Publisher; DDS_DataWriter message_DataWriter; ListenerData_Msg* message_Sample; const DDS_char listener [] = "Hello World"; os_time delay_2s = { 2, 0 }; DDS_unsigned_long listenerLength = sizeof(listener) - 1; printf("\n Starting ListenerPublisher..."); // Create DDS DomainParticipant printf("\n create Participant..."); 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 Publisher's in the DDS Domain. message_Publisher = createPublisher(); // Request a Writer from the the Publisher. message_DataWriter = createDataWriter(message_Publisher, g_MessageTopic); message_Sample = ListenerData_Msg__alloc(); message_Sample->userID = 1; message_Sample->message = DDS_string_alloc(listenerLength); strncpy(message_Sample->message, listener, listenerLength); printf("\n=== [ListenerPublisher] writing a message containing :"); printf("\n userID : %d", message_Sample->userID); printf("\n Message : \"%s\"", message_Sample->message); g_status = ListenerData_MsgDataWriter_write(message_DataWriter, message_Sample, DDS_HANDLE_NIL); checkStatus(g_status, "ListenerData_MsgDataWriter_write"); os_nanoSleep(delay_2s); // Cleanup DDS deleteDataWriter(message_Publisher, message_DataWriter); deletePublisher(message_Publisher); deleteTopic(g_MessageTopic); deleteParticipant(); // Cleanup C allocations DDS_free(message_Sample); printf("\n=== [ListenerPublisher] Exiting.\n\n"); return 0; }
DDS_ReturnCode_t DDS_DomainParticipant_delete_simulated_multitopic( DDS_DomainParticipant participant, DDS_TopicDescription smt ) { DDS_ReturnCode_t status; struct MsgListenerState *listener_state; /* Obtain all entities mentioned in the listener state. */ listener_state = (struct MsgListenerState *) msgListener->listener_data; /* Remove the DataWriter */ status = DDS_Publisher_delete_datawriter(multiPub, listener_state->namedMessageDW); checkStatus(status, "DDS_Publisher_delete_datawriter"); /* Remove the Publisher. */ status = DDS_DomainParticipant_delete_publisher(participant, multiPub); checkStatus(status, "DDS_DomainParticipant_delete_publisher"); /* Remove the QueryCondition and its parameters. */ DDS_free(listener_state->nameFinderParams); status = DDS_DataReader_delete_readcondition( listener_state->nameServiceDR, listener_state->nameFinder); checkStatus(status, "DDS_DataReader_delete_readcondition"); /* Remove the DataReaders. */ status = DDS_Subscriber_delete_datareader(multiSub, listener_state->nameServiceDR); checkStatus(status, "DDS_Subscriber_delete_datareader"); status = DDS_Subscriber_delete_datareader(multiSub, listener_state->chatMessageDR); checkStatus(status, "DDS_Subscriber_delete_datareader"); /* Remove the DataReaderListener and its state. */ free(listener_state); DDS_free(msgListener); /* Remove the Subscriber. */ status = DDS_DomainParticipant_delete_subscriber(participant, multiSub); checkStatus(status, "DDS_DomainParticipant_delete_subscriber"); /* Remove the ContentFilteredTopic. */ status = DDS_DomainParticipant_delete_contentfilteredtopic(participant, filteredMessageTopic); checkStatus(status, "DDS_DomainParticipant_delete_contentfilteredtopic"); /* Remove all other topics. */ status = DDS_DomainParticipant_delete_topic(participant, namedMessageTopic); checkStatus(status, "DDS_DomainParticipant_delete_topic (namedMessageTopic)"); 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)"); return status; }
void createTopic(const char *topicName, const char *typeName) { const char* messageFirstPart; char* message; int messageFirstPartLength, topicNameLength; g_TopicQos = DDS_TopicQos__alloc(); checkHandle(g_TopicQos, "DDS_TopicQos__alloc"); g_status = DDS_DomainParticipant_get_default_topic_qos(g_domainParticipant, g_TopicQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_topic_qos"); g_TopicQos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; if( strcmp(g_durability_kind, "transient") == 0 ) { g_TopicQos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS; } else if( strcmp(g_durability_kind, "persistent") == 0 ) { g_TopicQos->durability.kind = DDS_PERSISTENT_DURABILITY_QOS; } // Use the changed policy when defining the DurabilityData topic. g_Topic = DDS_DomainParticipant_create_topic(g_domainParticipant, topicName, typeName, g_TopicQos, NULL, DDS_ANY_STATUS); //Format error message messageFirstPart = "DDS_DomainParticipant_create_topic "; messageFirstPartLength = strlen(messageFirstPart); topicNameLength = strlen(topicName); message = (char*) DDS_string_alloc(messageFirstPartLength + topicNameLength); snprintf(message, messageFirstPartLength + topicNameLength + 1, "%s%s", messageFirstPart, topicName); checkHandle(g_Topic, message); DDS_free(message); }
DDS_QueryCondition createQueryCondition(DDS_DataReader dataReader, DDS_char *query_string, DDS_char* queryParameter) { DDS_StringSeq* query_parameters = DDS_StringSeq__alloc(); DDS_QueryCondition queryCondition; query_parameters->_length = 1; query_parameters->_maximum = 1; query_parameters->_release = TRUE; query_parameters->_buffer = DDS_StringSeq_allocbuf (1); checkHandle(query_parameters->_buffer, "DDS_StringSeq_allocbuf"); query_parameters->_buffer[0] = DDS_string_dup (queryParameter); checkHandle(query_parameters->_buffer[0], "DDS_string_dup"); queryCondition = DDS_DataReader_create_querycondition(dataReader, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE, query_string, query_parameters); checkHandle(queryCondition, "DDS_DataReader_create_querycondition"); DDS_free(query_parameters); return queryCondition; }
void registerMessageType(DDS_TypeSupport typeSupport) { char* typeName = WaitSetData_MsgTypeSupport_get_type_name(typeSupport); g_status = WaitSetData_MsgTypeSupport_register_type(typeSupport, g_domainParticipant, typeName); checkStatus(g_status, "WaitSetData_MsgTypeSupport_register_type"); DDS_free(typeName); }
void registerType(DDS_TypeSupport typeSupport) { char* typeName = (char*) DurabilityData_MsgTypeSupport_get_type_name(typeSupport); g_status = DurabilityData_MsgTypeSupport_register_type(typeSupport, g_domainParticipant, typeName); checkStatus(g_status, "DurabilityData_MsgTypeSupport_register_type"); DDS_free(typeName); }
DDS_DataWriter createDataWriter(DDS_Publisher publisher, DDS_Topic topic) { DDS_DataWriter dataWriter; DDS_TopicQos *topicQos = DDS_TopicQos__alloc(); DDS_DataWriterQos *dataWriterQos = DDS_DataWriterQos__alloc(); checkHandle(dataWriterQos, "DDS_DataWriterQos__alloc"); g_status = DDS_Publisher_get_default_datawriter_qos(publisher, dataWriterQos); checkStatus(g_status, "DDS_Publisher_get_default_datawriter_qos"); g_status = DDS_Topic_get_qos(topic, topicQos); checkStatus(g_status, "DDS_Topic_get_qos"); g_status = DDS_Publisher_copy_from_topic_qos(publisher, dataWriterQos, topicQos); checkStatus(g_status, "DDS_Publisher_copy_from_topic_qos"); dataWriterQos->writer_data_lifecycle.autodispose_unregistered_instances = TRUE; dataWriter = DDS_Publisher_create_datawriter(publisher, topic, dataWriterQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(dataWriter, "DDS_Publisher_create_datawriter"); DDS_free(dataWriterQos); DDS_free(topicQos); return dataWriter; }
void createPublisher() { DDS_PublisherQos* publisherQos = DDS_PublisherQos__alloc(); checkHandle(publisherQos, "DDS_PublisherQos__alloc"); g_status = DDS_DomainParticipant_get_default_publisher_qos(g_domainParticipant, publisherQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_publisher_qos"); publisherQos->partition.name._length = 1; publisherQos->partition.name._maximum = 1; publisherQos->partition.name._buffer = DDS_StringSeq_allocbuf(1); checkHandle(publisherQos->partition.name._buffer, "DDS_StringSeq_allocbuf"); publisherQos->partition.name._buffer[0] = DDS_string_alloc(strlen(g_partitionName)); checkHandle(publisherQos->partition.name._buffer[0], "DDS_string_alloc"); strcpy(publisherQos->partition.name._buffer[0], g_partitionName); /* Create a Publisher for the application. */ g_Publisher = DDS_DomainParticipant_create_publisher(g_domainParticipant, publisherQos, NULL, DDS_ANY_STATUS); checkHandle(g_Publisher, "DDS_DomainParticipant_create_publisher"); DDS_free(publisherQos); }
DDS_Publisher createPublisher(char* publisherName) { DDS_Publisher publisher; DDS_PublisherQos* publisherQos = DDS_PublisherQos__alloc(); checkHandle(publisherQos, "DDS_PublisherQos__alloc"); g_status = DDS_DomainParticipant_get_default_publisher_qos(g_domainParticipant, publisherQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_publisher_qos"); publisherQos->partition.name._length = 1; publisherQos->partition.name._maximum = 1; publisherQos->partition.name._buffer = DDS_StringSeq_allocbuf(1); checkHandle(publisherQos->partition.name._buffer, "DDS_StringSeq_allocbuf"); publisherQos->partition.name._buffer[0] = DDS_string_dup(g_partitionName); checkHandle(publisherQos->partition.name._buffer[0], "DDS_string_dup"); /* Create a Publisher for the application. */ publisher = DDS_DomainParticipant_create_publisher(g_domainParticipant, publisherQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(publisher, "DDS_DomainParticipant_create_publisher"); DDS_free(publisherQos); return publisher; }
void createSubscriber() { // Adapt the default SubscriberQos to read from the Partition with the given name. DDS_SubscriberQos* subscriberQos = DDS_SubscriberQos__alloc(); checkHandle(subscriberQos, "DDS_SubscriberQos__alloc"); g_status = DDS_DomainParticipant_get_default_subscriber_qos(g_domainParticipant, subscriberQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_subscriber_qos"); subscriberQos->partition.name._length = 1; subscriberQos->partition.name._maximum = 1; subscriberQos->partition.name._buffer = DDS_StringSeq_allocbuf(1); checkHandle(subscriberQos->partition.name._buffer, "DDS_StringSeq_allocbuf"); subscriberQos->partition.name._buffer[0] = DDS_string_alloc(strlen(g_partitionName)); checkHandle(subscriberQos->partition.name._buffer[0], "DDS_string_alloc"); strcpy(subscriberQos->partition.name._buffer[0], g_partitionName); // Create a Subscriber for the MsgBoard application. g_Subscriber = DDS_DomainParticipant_create_subscriber(g_domainParticipant, subscriberQos, NULL, DDS_ANY_STATUS); checkHandle(g_Subscriber, "DDS_DomainParticipant_create_subscriber"); DDS_free(subscriberQos); }
DDS_Subscriber createSubscriber() { DDS_Subscriber subscriber; // Adapt the default SubscriberQos to read from the Partition with the given name. DDS_SubscriberQos* subscriberQos = DDS_SubscriberQos__alloc(); checkHandle(subscriberQos, "DDS_SubscriberQos__alloc"); g_status = DDS_DomainParticipant_get_default_subscriber_qos(g_domainParticipant, subscriberQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_subscriber_qos"); subscriberQos->partition.name._length = 1; subscriberQos->partition.name._maximum = 1; subscriberQos->partition.name._buffer = DDS_StringSeq_allocbuf(1); checkHandle(subscriberQos->partition.name._buffer, "DDS_StringSeq_allocbuf"); subscriberQos->partition.name._buffer[0] = DDS_string_dup(g_partitionName); checkHandle(subscriberQos->partition.name._buffer[0], "DDS_string_dup"); // Create a Subscriber for the MessageBoard application. subscriber = DDS_DomainParticipant_create_subscriber(g_domainParticipant, subscriberQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(subscriber, "DDS_DomainParticipant_create_subscriber"); DDS_free(subscriberQos); return subscriber; }
DDS_Topic createTopic(const char *topicName, const char *typeName) { DDS_Topic topic; const char* messageFirstPart; char* message; size_t messageFirstPartLength, topicNameLength; DDS_TopicQos* topicQos = DDS_TopicQos__alloc(); checkHandle(topicQos, "DDS_TopicQos__alloc"); g_status = DDS_DomainParticipant_get_default_topic_qos(g_domainParticipant, topicQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_topic_qos"); topicQos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; topicQos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS; // DeadlineQoSPolicy : period used to trigger the listener // (on_requested_deadline_missed) topicQos->deadline.period.nanosec = 0; topicQos->deadline.period.sec = 1; /* Use the changed policy when defining the Ownership topic */ topic = DDS_DomainParticipant_create_topic(g_domainParticipant, topicName, typeName, topicQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(topic, "DDS::DomainParticipant::create_topic ()"); //Format error message messageFirstPart = "DDS_DomainParticipant_create_topic"; messageFirstPartLength = strlen(messageFirstPart); topicNameLength = strlen(topicName); message = (char*) malloc(messageFirstPartLength + topicNameLength + 2); snprintf(message, messageFirstPartLength + topicNameLength + 1, "%s %s", messageFirstPart, topicName); checkHandle(topic, message); free(message); DDS_free(topicQos); return topic; }
int OSPL_MAIN (int argc, char *argv[]) { DDS_Subscriber QueryConditionDataSubscriber; DDS_DataReader QueryConditionDataDataReader; StockMarket_Stock* QueryConditionDataSample; DDS_sequence_StockMarket_Stock* msgList = DDS_sequence_StockMarket_Stock__alloc(); DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc(); c_bool isClosed = FALSE; unsigned long j; DDS_char* QueryConditionDataToSubscribe; DDS_char *query_string = "ticker=%0"; DDS_QueryCondition queryCondition; os_time delay_200ms = { 0, 200000000 }; int count = 0; // usage : QueryConditionSubscriber <topic's content filtering string> if( argc < 2 ) { usage(); } printf("\n\n Starting QueryConditionSubscriber..."); printf("\n\n Parameter is \"%s\"", argv[1]); QueryConditionDataToSubscribe = argv[1]; createParticipant("QueryCondition example"); // Register Stock Topic's type in the DDS Domain. g_StockTypeSupport = (DDS_TypeSupport) StockMarket_StockTypeSupport__alloc(); checkHandle(g_StockTypeSupport, "StockMarket_StockTypeSupport__alloc"); registerStockType(g_StockTypeSupport); // Create Stock Topic in the DDS Domain. g_StockTypeName = StockMarket_StockTypeSupport_get_type_name(g_StockTypeSupport); g_StockTopic = createTopic("StockTrackerExclusive", g_StockTypeName); DDS_free(g_StockTypeName); DDS_free(g_StockTypeSupport); // Create the Subscriber's in the DDS Domain. QueryConditionDataSubscriber = createSubscriber(); // Request a Reader from the the Subscriber. QueryConditionDataDataReader = createDataReader(QueryConditionDataSubscriber, g_StockTopic); // Create QueryCondition printf( "\n=== [QueryConditionSubscriber] Query : ticker = %s\n", QueryConditionDataToSubscribe ); queryCondition = createQueryCondition(QueryConditionDataDataReader, query_string, (DDS_char*) QueryConditionDataToSubscribe); printf( "\n=== [QueryConditionSubscriber] Ready..." ); do { g_status = StockMarket_StockDataReader_take_w_condition(QueryConditionDataDataReader, msgList, infoSeq, DDS_LENGTH_UNLIMITED, queryCondition); checkStatus(g_status, "StockMarket_StockDataReaderView_take"); if( msgList->_length > 0 ) { j = 0; do { QueryConditionDataSample = &msgList->_buffer[j]; // When is this supposed to happen? // Needs comment... if( infoSeq->_buffer[j].valid_data ) { printf("\n\n %s: %f", QueryConditionDataSample->ticker, QueryConditionDataSample->price); if( QueryConditionDataSample->price == (DDS_float) -1.0f ) { printf("\n ===[QueryConditionSubscriber] QueryConditionDataSample->price == -1.0f "); isClosed = TRUE; } } } while( ++j < msgList->_length ); g_status = StockMarket_StockDataReader_return_loan(QueryConditionDataDataReader, msgList, infoSeq); checkStatus(g_status, "StockMarket_StockDataReaderView_return_loan"); } os_nanoSleep(delay_200ms); ++count; } while( isClosed == FALSE && count < 1500 ); printf("\n\n=== [QueryConditionSubscriber] Market Closed"); // Cleanup DDS from the created Entities. deleteQueryCondition(QueryConditionDataDataReader, queryCondition); deleteDataReader(QueryConditionDataSubscriber, QueryConditionDataDataReader); deleteSubscriber(QueryConditionDataSubscriber); deleteTopic(g_StockTopic); deleteParticipant(); // Cleanup C allocations, // recursively freeing the allocated structures and sequences using the OpenSplice API. DDS_free(msgList); DDS_free(infoSeq); // Print out an empty line, just to let behind a clean new line for the shell.. printf("\n\r"); return 0; }
int main(int argc, char *argv[]) { char* publisher_name; int i, sampleIndex, nb_iteration; c_bool isStoppingSubscriber; float price; char* geTicker = "GE"; int geTickerLength = strlen(geTicker); char* msftTicker = "MSFT"; int msftTickerLength = strlen(msftTicker); os_time os_delay100 = { 0, 100000000 }; os_time os_delay2000 = { 2, 0 }; DDS_Publisher QueryConditionDataPublisher; DDS_DataWriter QueryConditionDataDataWriter; QueryConditionData_Stock* geStockSample; QueryConditionData_Stock* msftStockSample; DDS_InstanceHandle_t geInstanceHandle; DDS_InstanceHandle_t msftInstancetHandle; createParticipant("QueryCondition example"); // Register Stock Topic's type in the DDS Domain. g_StockTypeSupport = (DDS_TypeSupport) QueryConditionData_StockTypeSupport__alloc(); checkHandle(g_StockTypeSupport, "QueryConditionData_StockTypeSupport__alloc"); registerStockType(g_StockTypeSupport); // Create Stock Topic in the DDS Domain. g_StockTypeName = (char*) QueryConditionData_StockTypeSupport_get_type_name(g_StockTypeSupport); g_StockTopic = createTopic("StockTrackerExclusive", g_StockTypeName); DDS_free(g_StockTypeName); DDS_free(g_StockTypeSupport); // Create the Publisher's in the DDS Domain. QueryConditionDataPublisher = createPublisher(); // Request a Writer from the the Publisher. QueryConditionDataDataWriter = createDataWriter(QueryConditionDataPublisher, g_StockTopic, FALSE); // Publish a Stock Sample reflecting the state of the Msg DataWriter. geStockSample = QueryConditionData_Stock__alloc(); msftStockSample = QueryConditionData_Stock__alloc(); msftStockSample->ticker = DDS_string_alloc(msftTickerLength); snprintf(msftStockSample->ticker, msftTickerLength + 1, "%s", msftTicker); msftStockSample->price = 12.00f; geStockSample->ticker = DDS_string_alloc(geTickerLength); snprintf(geStockSample->ticker, geTickerLength + 1, "%s", geTicker); geStockSample->price = 25.00f; geInstanceHandle = QueryConditionData_StockDataWriter_register_instance(QueryConditionDataDataWriter, geStockSample); msftInstancetHandle = QueryConditionData_StockDataWriter_register_instance(QueryConditionDataDataWriter, msftStockSample); nb_iteration = 20; // The subscriber should display the prices sent by the publisher with the highest ownership strength for( sampleIndex = 0; sampleIndex < nb_iteration ; ++sampleIndex ) { printf("GE : %.1f MSFT : %.1f\n", geStockSample->price, msftStockSample->price); writeStockSample(QueryConditionDataDataWriter, geInstanceHandle, geStockSample); writeStockSample(QueryConditionDataDataWriter, msftInstancetHandle, msftStockSample); geStockSample->price += 0.5; msftStockSample->price += 1.5; os_nanoSleep(os_delay100); } // This special price gives the end signal to the Subscriber: // signal to terminate geStockSample->price = -1.0f; msftStockSample->price = -1.0f; writeStockSample(QueryConditionDataDataWriter, geInstanceHandle, geStockSample); writeStockSample(QueryConditionDataDataWriter, msftInstancetHandle, msftStockSample); // This to make sure the Subscriber will get all the Samples. os_nanoSleep(os_delay2000); printf("Market Closed"); QueryConditionData_StockDataWriter_unregister_instance (QueryConditionDataDataWriter, geStockSample, geInstanceHandle); QueryConditionData_StockDataWriter_unregister_instance (QueryConditionDataDataWriter, msftStockSample, msftInstancetHandle); // Cleanup DDS from the created Entities. deleteDataWriter(QueryConditionDataPublisher, QueryConditionDataDataWriter); deletePublisher(QueryConditionDataPublisher); deleteTopic(g_StockTopic); deleteParticipant(); // Cleanup C allocations, // recursively freeing the allocated structures and sequences using the OpenSplice API. DDS_free(geStockSample); DDS_free(msftStockSample); // Print out an empty line, just to let behind a clean new line for the shell.. printf("\n\r"); return 0; }
int main(int argc, char *argv[]) { char* partition_name = "ContentFilteredTopic example"; DDS_Subscriber contentFilteredTopicDataSubscriber; DDS_DataReader contentFilteredTopicDataDataReader; ContentFilteredTopicData_Stock* contentFilteredTopicDataSample; DDS_InstanceHandle_t userHandle; DDS_sequence_ContentFilteredTopicData_Stock* msgList = DDS_sequence_ContentFilteredTopicData_Stock__alloc(); DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc(); c_bool isClosed = FALSE; unsigned long j, userInput, timeOut; const char *filterValueToSubscribe; DDS_char* contentFilteredStockTopicName = "MyStockTopic"; DDS_ContentFilteredTopic contentFilteredTopic; int filter_expressionLength; const char* filter_expressionPrefix = "ticker = '"; const char* filter_expressionSuffix = "'"; DDS_char *filter_expression; const DDS_StringSeq* filter_parameters = DDS_StringSeq__alloc(); os_time delay_200ms = { 0, 200000000 }; int count = 0; // usage : ContentFilteredTopicDataSubscriber <topic's content filtering string> if( argc < 2 ) { usage(); } printf("=== ContentFilteredTopicDataSubscriber"); // Removed to comply with expected results //printf("\n\n Parameter is \"%s\"", argv[1]); filterValueToSubscribe = argv[1]; createParticipant( partition_name ); // Register Stock Topic's type in the DDS Domain. g_StockTypeSupport = (DDS_TypeSupport) ContentFilteredTopicData_StockTypeSupport__alloc(); checkHandle(g_StockTypeSupport, "ContentFilteredTopicData_StockTypeSupport__alloc"); registerStockType(g_StockTypeSupport); // Create Stock Topic in the DDS Domain. g_StockTypeName = (char*) ContentFilteredTopicData_StockTypeSupport_get_type_name(g_StockTypeSupport); g_StockTopic = createTopic("StockTrackerExclusive", g_StockTypeName); DDS_free(g_StockTypeName); DDS_free(g_StockTypeSupport); // Create the Subscriber's in the DDS Domain. contentFilteredTopicDataSubscriber = createSubscriber(); // create subscription filter filter_expressionLength = strlen(filter_expressionPrefix); filter_expressionLength += strlen(filterValueToSubscribe); filter_expressionLength += strlen(filter_expressionSuffix); filter_expression = DDS_string_alloc(filter_expressionLength); snprintf(filter_expression, filter_expressionLength + 1, "%s%s%s", filter_expressionPrefix, filterValueToSubscribe, filter_expressionSuffix); // create ContentFilteredTopic to retrieve only the samples with the given Stock. contentFilteredTopic = createContentFilteredTopic(contentFilteredStockTopicName, g_StockTopic, filter_expression, filter_parameters); // create Filtered DataReader printf( "\n=== [ContentFilteredTopicDataSubscriber] Subscription filter : %s", filter_expression ); // Request a Reader from the the Subscriber. contentFilteredTopicDataDataReader = createContentFilteredDataReader(contentFilteredTopicDataSubscriber, contentFilteredTopic); printf( "\n=== [ContentFilteredTopicDataSubscriber] Ready ..." ); do { g_status = ContentFilteredTopicData_StockDataReader_take(contentFilteredTopicDataDataReader, msgList, infoSeq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); checkStatus(g_status, "ContentFilteredTopicData_StockDataReaderView_take"); if( msgList->_length > 0 ) { j = 0; do { contentFilteredTopicDataSample = &msgList->_buffer[j]; if( infoSeq->_buffer[j].valid_data ) { if( contentFilteredTopicDataSample->price != (DDS_float) -1.0f ) { int floatWidth = ( ((DDS_float) contentFilteredTopicDataSample->price) - ((long) contentFilteredTopicDataSample->price) ) ? 1 : 0; printf("\n=== [ContentFilteredTopicDataSubscriber] receives stockQuote : (%s, %.*f)", contentFilteredTopicDataSample->ticker, floatWidth, contentFilteredTopicDataSample->price); } else { isClosed = TRUE; } } } while( ++j < msgList->_length ); g_status = ContentFilteredTopicData_StockDataReader_return_loan(contentFilteredTopicDataDataReader, msgList, infoSeq); checkStatus(g_status, "ContentFilteredTopicData_StockDataReader_return_loan"); if(isClosed == FALSE) { os_nanoSleep(delay_200ms); ++count; } } } while( isClosed == FALSE && count < 1500); // We dont want the example to run indefinitely printf("\n=== [ContentFilteredTopicDataSubscriber] Market Closed\n"); // Cleanup DDS from the created Entities. deleteDataReader(contentFilteredTopicDataSubscriber, contentFilteredTopicDataDataReader); deleteSubscriber(contentFilteredTopicDataSubscriber); deleteContentFilteredTopic(contentFilteredTopic); deleteTopic(g_StockTopic); deleteParticipant(); // Cleanup C allocations, // recursively freeing the allocated structures and sequences using the OpenSplice API. DDS__free(msgList); DDS__free(infoSeq); // Print out an empty line, just to let behind a clean new line for the shell.. // Removed to comply with expected results //printf("\n\r"); return 0; }
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; }
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; }
main(int argc, const char *argv[]) { int x, i; DDS_InstanceHandle_t userHandle; DDS_Publisher message_Publisher; DDS_DataWriter message_DataWriter; ListenerData_Msg* message_Sample; DDS_char* listener = "Hello World"; int listenerLength; printf("\n Starting ListenerPublisher..."); // Create DDS DomainParticipant printf("\n create Participant..."); 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 = (char*) ListenerData_MsgTypeSupport_get_type_name(g_MessageTypeSupport); g_MessageTopic = createTopic("ListenerData_Msg", g_MessageTypeName); DDS_free(g_MessageTypeName); DDS_free(g_MessageTypeSupport); // Create the Publisher's in the DDS Domain. message_Publisher = createPublisher(); // Request a Writer from the the Publisher. message_DataWriter = createDataWriter(message_Publisher, g_MessageTopic); message_Sample = ListenerData_Msg__alloc(); userHandle = ListenerData_MsgDataWriter_register_instance(message_DataWriter, message_Sample); message_Sample->userID = 1; listenerLength = strlen(listener); message_Sample->message = DDS_string_alloc(listenerLength); strcpy(message_Sample->message, listener); printf("\n=== [ListenerPublisher] writing a message containing :"); printf("\n userID : %d", message_Sample->userID); printf("\n Message : \"%s\"", message_Sample->message); g_status = ListenerData_MsgDataWriter_write(message_DataWriter, message_Sample, 0); checkStatus(g_status, "ListenerData_MsgDataWriter_write"); ListenerData_MsgDataWriter_unregister_instance (message_DataWriter, message_Sample, userHandle); // Cleanup DDS deleteDataWriter(message_Publisher, message_DataWriter); deletePublisher(message_Publisher); deleteTopic(g_MessageTopic); deleteParticipant(); // Cleanup C allocations // Recursively free the instances sequence using the OpenSplice API. DDS_free(message_Sample); printf("\n=== [ListenerPublisher] Exiting.\n\n"); return 0; }
int dds_writer_create ( dds_entity_t pp_or_pub, dds_entity_t * writer, dds_entity_t topic, const dds_qos_t * qos, const dds_writerlistener_t * listener) { DDS_ReturnCode_t result = DDS_RETCODE_OK; dds_entity_t publisher = NULL; struct DataWriterInfo *info; struct DDS_DataWriterListener dpl; struct DDS_DataWriterListener *lp = NULL; DDS_StatusMask mask = (listener) ? DDS_STATUS_MASK_ANY : DDS_STATUS_MASK_NONE; DDS_DataWriterQos *wQos; bool ownPublisher = false; DDS_REPORT_STACK(); if (!writer) { result = DDS_RETCODE_BAD_PARAMETER; DDS_REPORT(result, "Writer parameter is NULL."); } if (!topic) { result = DDS_RETCODE_BAD_PARAMETER; DDS_REPORT(result, "Topic parameter is NULL."); } if (result == DDS_RETCODE_OK) { if (!pp_or_pub) { result = dds_publisher_create(NULL, &publisher, qos, NULL); ownPublisher = true; } else { if (DDS_Entity_get_kind(pp_or_pub) == DDS_ENTITY_KIND_DOMAINPARTICIPANT) { result = dds_publisher_create(pp_or_pub, &publisher, qos, NULL); ownPublisher = true; } else { publisher = pp_or_pub; } } } if (result == DDS_RETCODE_OK) { info = dds_datawriter_info_new(); if (!info) { result = DDS_RETCODE_ERROR; DDS_REPORT(result, "Failed to create writer info."); } } if (result == DDS_RETCODE_OK) { *writer = NULL; info->ownPublisher = ownPublisher; if (listener) { info->listener = os_malloc(sizeof(dds_writerlistener_t)); *info->listener = *listener; lp = &dpl; dds_datawriter_listener_init(&dpl, info); } if (qos) { wQos = DDS_DataWriterQos__alloc(); result = DDS_Publisher_get_default_datawriter_qos(publisher, wQos); if (result == DDS_RETCODE_OK) { dds_qos_to_writer_qos(wQos, qos); *writer = DDS_Publisher_create_datawriter(publisher, topic, wQos, lp, mask); } DDS_free(wQos); } else { *writer = DDS_Publisher_create_datawriter(publisher, topic, DDS_DATAWRITER_QOS_USE_TOPIC_QOS, lp, mask); } if (*writer) { result = DDS_Entity_set_user_data(*writer, (DDS_EntityUserData)info); } else { result = dds_report_get_error_code(); } DDS_Entity_release_user_data((DDS_EntityUserData)info); } DDS_REPORT_FLUSH(pp_or_pub, result != DDS_RETCODE_OK); return DDS_ERRNO(result, DDS_MOD_WRITER, DDS_ERR_Mx); }
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; }
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; }
int main(int argc, char *argv[]) { DDS_sequence_HelloWorldData_Msg* message_seq = DDS_sequence_HelloWorldData_Msg__alloc(); DDS_SampleInfoSeq* message_infoSeq = DDS_SampleInfoSeq__alloc(); DDS_Subscriber message_Subscriber; DDS_DataReader message_DataReader; c_bool isClosed = FALSE; int count = 0; os_time os_delay200 = { 0, 200000000 }; // Create DDS DomainParticipant createParticipant("HelloWorld example"); // Register the Topic's type in the DDS Domain. g_MessageTypeSupport = HelloWorldData_MsgTypeSupport__alloc(); checkHandle(g_MessageTypeSupport, "HelloWorldData_MsgTypeSupport__alloc"); registerMessageType(g_MessageTypeSupport); // Create the Topic's in the DDS Domain. g_MessageTypeName = (char*) HelloWorldData_MsgTypeSupport_get_type_name(g_MessageTypeSupport); g_MessageTopic = createTopic("HelloWorldData_Msg", g_MessageTypeName); DDS_free(g_MessageTypeName); DDS_free(g_MessageTypeSupport); // Create the Subscriber's in the DDS Domain. message_Subscriber = createSubscriber("HelloWorld"); // Request a Reader from the the Subscriber. message_DataReader = createDataReader(message_Subscriber, g_MessageTopic); printf("\n=== [Subscriber] Ready ..."); do { g_status = HelloWorldData_MsgDataReader_take( message_DataReader, message_seq, message_infoSeq, 1, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE ); checkStatus(g_status, "HelloWorldData_MsgDataReader_take"); if( message_seq->_length > 0 && message_infoSeq->_buffer[0].valid_data ) { isClosed = TRUE; printf("\n=== [Subscriber] message received :" ); printf( "\n userID : %d", message_seq->_buffer[0].userID ); printf( "\n Message : \"%s\"\n", message_seq->_buffer[0].message ); HelloWorldData_MsgDataReader_return_loan (message_DataReader, message_seq, message_infoSeq); } if(isClosed == FALSE) { os_nanoSleep(os_delay200); ++count; } } while( isClosed == FALSE && count < 1500 ); os_nanoSleep(os_delay200); // 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); return 0; }
int main(int argc, char *argv[]) { DDS_sequence_LifecycleData_Msg* msgSeq = DDS_sequence_LifecycleData_Msg__alloc(); DDS_SampleInfoSeq* msgInfoSeq = DDS_SampleInfoSeq__alloc(); DDS_Subscriber msgSubscriber; DDS_DataReader msgDataReader; unsigned long j, inputChar; os_time os_delay20ms = (os_time) {0, 20000000}; os_time os_delay200ms = (os_time) {0, 200000000}; // Force the output to be unbuffered. setbuf(stdout, (char *) 0); // First initialize the Topics and their DDS Entities // Create DDS DomainParticipant createParticipant("Lifecycle example"); //------------------ Msg topic --------------------// // Register Msg Topic's type in the DDS Domain. g_msgTypeSupport = LifecycleData_MsgTypeSupport__alloc(); checkHandle(g_msgTypeSupport, "LifecycleData_MsgTypeSupport__alloc"); registerMsgType(g_msgTypeSupport); // Create Msg Topic in the DDS Domain. g_msgTypeName = (char*) LifecycleData_MsgTypeSupport_get_type_name(g_msgTypeSupport); g_msgTopic = createTopic("Lifecycle_Msg", g_msgTypeName); DDS_free(g_msgTypeName); DDS_free(g_msgTypeSupport); // Create the Publisher's in the DDS Domain. msgSubscriber = createSubscriber(); // Request a DataReader from the the Subscriber. msgDataReader = createDataReader(msgSubscriber, g_msgTopic); //End initialization. printf("\n=== [Subscriber] Ready..."); msgInfoSeq->_length = 0; DDS_boolean closed = FALSE; int nbIter = 1; int nbIterMax = 100; while ((closed == FALSE) && (nbIter < nbIterMax)) { g_status = LifecycleData_MsgDataReader_read(msgDataReader, msgSeq, msgInfoSeq, 1, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); checkStatus(g_status, "LifecycleData_MsgDataReader_read"); if( msgInfoSeq->_length > 0 ) { if( msgInfoSeq->_buffer[0].valid_data == TRUE ) { printf("\n\n Message : %s", msgSeq->_buffer[0].message); printf("\n WriterStates : %s", msgSeq->_buffer[0].writerStates); printf("\n valid_data : %d", msgInfoSeq->_buffer[0].valid_data); printf("\n sample_state:%s-view_state:%s-instance_state:%s\n", sSampleState[getIndex(msgInfoSeq->_buffer[0].sample_state)], sViewState[getIndex(msgInfoSeq->_buffer[0].view_state)], sInstanceState[getIndex(msgInfoSeq->_buffer[0].instance_state)]); } closed = strcmp(msgSeq->_buffer[0].writerStates, "STOPPING_SUBSCRIBER") == 0 ? TRUE : FALSE; printf("=== closed=%d - nbIter %d\n", closed, nbIter); g_status = LifecycleData_MsgDataReader_return_loan(msgDataReader, msgSeq, msgInfoSeq); checkStatus(g_status, "LifecycleData_MsgDataReader_return_loan"); // Useless to harass the system, so we wait before to retry. os_nanoSleep(os_delay200ms); msgInfoSeq->_length = 0; nbIter++; } } printf("\n=== [Subscriber] stopping after %d iterations ..\n", nbIter); if (nbIter == nbIterMax) printf ("*** Error : max %d iterations reached", nbIterMax); // Cleanup DDS from the created Entities. deleteDataReader(msgSubscriber, msgDataReader); deleteSubscriber(msgSubscriber); deleteTopic(g_msgTopic); deleteParticipant(); // Cleanup C allocations, recursively freeing the allocated structures and sequences using the OpenSplice API. DDS__free(msgSeq); DDS__free(msgInfoSeq); return 0; }
main(int argc, const char *argv[]) { os_time os_delay_500ms = { 0, 500000000 }; os_time os_delay_200ms = { 0, 200000000 }; os_time os_delay_2ms = { 0, 2000000 }; int x, i; DDS_boolean autodispose_unregistered_instances = FALSE; DDS_Publisher writerStatePublisher; DDS_DataWriter writerStateWriter; DDS_Publisher msgPublisher; DDS_DataWriter msgWriter; DDS_DataWriter msgWriter_stopper; int messageLength; LifecycleData_Msg *sample_Msg; // Force the output to be unbuffered. setbuf(stdout, (char *) 0); printf("\n\n Starting LifecyclePublisher..."); if( argc < 3 ) { usage(); } if ((strcmp(argv[1], "false") != 0) && (strcmp(argv[1], "true") != 0) && (strcmp(argv[2], "dispose") != 0) && (strcmp(argv[2], "unregister") != 0) && (strcmp(argv[2], "stoppub") != 0)) { usage(); } autodispose_unregistered_instances = (strcmp(argv[1], "true") == 0); // First initialize the Topics and their DDS Entities // Create DDS DomainParticipant createParticipant("Lifecycle example"); //------------------ Msg topic --------------------// // Register Msg Topic's type in the DDS Domain. g_msgTypeSupport = LifecycleData_MsgTypeSupport__alloc(); checkHandle(g_msgTypeSupport, "LifecycleData_MsgTypeSupport__alloc"); registerMsgType(g_msgTypeSupport); // Create Msg Topic in the DDS Domain. g_msgTypeName = (char*) LifecycleData_MsgTypeSupport_get_type_name(g_msgTypeSupport); g_msgTopic = createTopic("Lifecycle_Msg", g_msgTypeName); DDS_free(g_msgTypeName); DDS_free(g_msgTypeSupport); // Create the Publisher's in the DDS Domain. msgPublisher = createPublisher(); // Request a Writer from the the Publisher. msgWriter = createDataWriter(msgPublisher, g_msgTopic, autodispose_unregistered_instances); msgWriter_stopper = createDataWriter(msgPublisher, g_msgTopic, autodispose_unregistered_instances); //End initialization. os_nanoSleep(os_delay_200ms); // Start publishing... printf("\n=== [Publisher] Ready..."); if (strcmp(argv[2], "dispose") == 0) { // Publish a Msg Sample and dispose the instance LifecycleData_Msg *sample_Msg = LifecycleData_Msg__alloc(); sample_Msg->userID = 1; messageLength = strlen("Lifecycle_1"); sample_Msg->message = DDS_string_alloc(messageLength); snprintf(sample_Msg->message, messageLength + 1, "%s", "Lifecycle_1"); messageLength = strlen("SAMPLE_SENT -> INSTANCE_DISPOSED -> DATAWRITER_DELETED"); sample_Msg->writerStates = DDS_string_alloc(messageLength); snprintf(sample_Msg->writerStates, messageLength + 1, "%s", "SAMPLE_SENT -> INSTANCE_DISPOSED -> DATAWRITER_DELETED"); printf("\n=== [Publisher] :"); printf("\n userID : %d", sample_Msg->userID); printf("\n Message : %s", sample_Msg->message); printf("\n writerStates : %s", sample_Msg->writerStates); g_status = LifecycleData_MsgDataWriter_write(msgWriter, sample_Msg, 0); checkStatus(g_status, "MsgDataWriter_write"); os_nanoSleep(os_delay_500ms); printf("\n=== [Publisher] : SAMPLE_SENT"); // Dispose instance g_status = LifecycleData_MsgDataWriter_dispose(msgWriter, sample_Msg, 0); checkStatus(g_status, "LifecycleData_MsgDataWriter_dispose"); printf("\n=== [Publisher] : INSTANCE_DISPOSED"); // OpenSplice will recursively free the content of the sample, // provided it has all been allocated through the DDS_xxxx_alloc API... DDS_free(sample_Msg); } else if (strcmp(argv[2], "unregister") == 0) { // Publish a Msg Sample and unregister the instance LifecycleData_Msg *sample_Msg = LifecycleData_Msg__alloc(); sample_Msg->userID = 1; messageLength = strlen("Lifecycle_2"); sample_Msg->message = DDS_string_alloc(messageLength); snprintf(sample_Msg->message, messageLength + 1, "%s", "Lifecycle_2"); messageLength = strlen("SAMPLE_SENT -> INSTANCE_UNREGISTERED -> DATAWRITER_DELETED"); sample_Msg->writerStates = DDS_string_alloc(messageLength); snprintf(sample_Msg->writerStates, messageLength + 1, "%s", "SAMPLE_SENT -> INSTANCE_UNREGISTERED -> DATAWRITER_DELETED"); printf("\n=== [Publisher] :"); printf("\n userID : %d", sample_Msg->userID); printf("\n Message : %s", sample_Msg->message); printf("\n writerStates : %s", sample_Msg->writerStates); g_status = LifecycleData_MsgDataWriter_write(msgWriter, sample_Msg, 0); checkStatus(g_status, "MsgDataWriter_write"); os_nanoSleep(os_delay_500ms); printf("\n=== [Publisher] : SAMPLE_SENT"); // Unregister instance : the auto_dispose_unregistered_instances flag // is currently ignored and the instance is never disposed automatically g_status = LifecycleData_MsgDataWriter_unregister_instance(msgWriter, sample_Msg, 0); checkStatus(g_status, "LifecycleData_MsgDataWriter_unregister_instance"); printf("\n=== [Publisher] : INSTANCE_UNREGISTERED"); // OpenSplice will recursively free the content of the sample, // provided it has all been allocated through the DDS_xxxx_alloc API... DDS_free(sample_Msg); } else if (strcmp(argv[2], "stoppub") == 0) { // Publish a Msg Sample LifecycleData_Msg *sample_Msg = LifecycleData_Msg__alloc(); sample_Msg->userID = 1; messageLength = strlen("Lifecycle_3"); sample_Msg->message = DDS_string_alloc(messageLength); snprintf(sample_Msg->message, messageLength + 1, "%s", "Lifecycle_3"); messageLength = strlen("SAMPLE_SENT -> DATAWRITER_DELETED"); sample_Msg->writerStates = DDS_string_alloc(messageLength); snprintf(sample_Msg->writerStates, messageLength + 1, "%s", "SAMPLE_SENT -> DATAWRITER_DELETED"); printf("\n=== [Publisher] :"); printf("\n userID : %d", sample_Msg->userID); printf("\n Message : %s", sample_Msg->message); printf("\n writerStates : %s", sample_Msg->writerStates); g_status = LifecycleData_MsgDataWriter_write(msgWriter, sample_Msg, 0); checkStatus(g_status, "MsgDataWriter_write"); os_nanoSleep(os_delay_500ms); printf("\n=== [Publisher] : SAMPLE_SENT"); // OpenSplice will recursively free the content of the sample, // provided it has all been allocated through the DDS_xxxx_alloc API... DDS_free(sample_Msg); } // let the subscriber treat the previous writer state !!!! printf("\n=== [Publisher] waiting 500ms to let the subscriber treat the previous write state ..."); os_nanoSleep(os_delay_500ms); /* Remove the DataWriters */ deleteDataWriter(msgPublisher, &msgWriter); printf("\n=== [Publisher] : DATAWRITER_DELETED"); os_nanoSleep(os_delay_500ms); printf("\n=== [Publisher] : Sending a message to stop the subscriber"); /* send a Msg sample to stop the subscriber */ sample_Msg = LifecycleData_Msg__alloc(); sample_Msg->userID = 1; messageLength = strlen("Lifecycle_4"); sample_Msg->message = DDS_string_alloc(messageLength); snprintf(sample_Msg->message, messageLength + 1, "%s", "Lifecycle_4"); messageLength = strlen("STOPPING_SUBSCRIBER"); sample_Msg->writerStates = DDS_string_alloc(messageLength); snprintf(sample_Msg->writerStates, messageLength + 1, "%s", "STOPPING_SUBSCRIBER"); printf("\n=== [Publisher] :"); printf("\n userID : %d", sample_Msg->userID); printf("\n Message : %s", sample_Msg->message); printf("\n writerStates : %s\n", sample_Msg->writerStates); g_status = LifecycleData_MsgDataWriter_write(msgWriter_stopper, sample_Msg, 0); checkStatus(g_status, "MsgDataWriter_write"); os_nanoSleep(os_delay_500ms); // OpenSplice will recursively free the content of the sample, // provided it has all been allocated through the DDS_xxxx_alloc API... DDS_free(sample_Msg); /* Remove the DataWriter */ deleteDataWriter(msgPublisher, &msgWriter_stopper); // Cleanup DDS from the created Entities. deletePublisher(&msgPublisher); deleteTopic(g_msgTopic); deleteParticipant(); return 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; }
int main(int argc, char *argv[]) { c_bool isTransient, isPersistent; c_bool isClosed = FALSE; unsigned long i, j; os_time os_delay2000 = { 2, 0 }; DDS_sequence_DurabilityData_Msg* DurabilityData_Msg_Seq = DDS_sequence_DurabilityData_Msg__alloc(); DDS_SampleInfoSeq* DurabilityData_infoSeq = DDS_SampleInfoSeq__alloc(); if( argc < 2 ) { usage(); } isTransient = (strcmp(argv[1], "transient") == 0) ? TRUE : FALSE; isPersistent = (strcmp(argv[1], "persistent") == 0) ? TRUE : FALSE; if( ! (isTransient || isPersistent) ) { usage(); } g_durability_kind = (char*) argv[1]; // Create DDS DomainParticipant createParticipant("Durability example"); // Register the Topic's type in the DDS Domain. g_MsgTypeSupport = DurabilityData_MsgTypeSupport__alloc(); checkHandle(g_MsgTypeSupport, "DurabilityData_MsgTypeSupport__alloc"); registerType(g_MsgTypeSupport); // Create the Topic's in the DDS Domain. g_MsgTypeName = (char*) DurabilityData_MsgTypeSupport_get_type_name(g_MsgTypeSupport); createTopic("DurabilityData_Msg", g_MsgTypeName); DDS_free(g_MsgTypeName); DDS_free(g_MsgTypeSupport); // Create the Subscriber's in the DDS Domain. createSubscriber(); // Request a Reader from the the Subscriber. createReader(); printf("=== [Subscriber] Ready ..."); // Added a max iteration threshold in order to avoid looping infinitely. // This is in the case of "persistent" + auto_dispose == TRUE, // if the user tries to use persistence feature, it won't succeed: // with this setting value, the Instance is still deleted upon the delete of the Writer, // even though the persistent setting has been passed on both processes. i = 0; do { g_status = DurabilityData_MsgDataReader_take( g_DataReader, DurabilityData_Msg_Seq, DurabilityData_infoSeq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE ); checkStatus(g_status, "DurabilityData_MsgDataReader_take"); if( DurabilityData_Msg_Seq->_length > 0 ) { j = 0; do { if( DurabilityData_infoSeq->_buffer[j].valid_data ) { printf("\n%s", DurabilityData_Msg_Seq->_buffer[j].content); if( strcmp(DurabilityData_Msg_Seq->_buffer[j].content, "9") == 0 ) { isClosed = TRUE; } } } while( ++j < DurabilityData_Msg_Seq->_length ); DurabilityData_MsgDataReader_return_loan (g_DataReader, DurabilityData_Msg_Seq, DurabilityData_infoSeq); } os_nanoSleep(os_delay2000); } while( isClosed == FALSE && i++ < 5); // Cleanup DDS from the created Entities. deleteDataReader(); deleteSubscriber(); deleteTopic(); deleteParticipant(); // Cleanup C allocations, recursively freeing the allocated structures and sequences using the OpenSplice API. DDS_free(DurabilityData_Msg_Seq); DDS_free(DurabilityData_infoSeq); // Print out an empty line, just to let behind a clean new line for the shell.. printf("\n"); return 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; }