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; }
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); }
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; }
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); }
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); }
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 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; }
int OSPL_MAIN (int argc, const char *argv[]) { DDS_Publisher message_Publisher; DDS_DataWriter message_DataWriter; WaitSetData_Msg* message_Sample; const DDS_char firstHello[] = "First Hello"; DDS_unsigned_long firstHelloLength = sizeof(firstHello) - 1; const DDS_char helloAgain[] = "Hello again"; DDS_unsigned_long helloAgainLength = sizeof(helloAgain) - 1; os_time os_delay300ms = { 0, 300000000 }; printf("\n Starting WaitSetDataPublisher..."); // Create DDS DomainParticipant printf("\n create Participant..."); createParticipant("WaitSet example"); // Register the Topic's type in the DDS Domain. g_MessageTypeSupport = WaitSetData_MsgTypeSupport__alloc(); checkHandle(g_MessageTypeSupport, "WaitSetData_MsgTypeSupport__alloc"); registerMessageType(g_MessageTypeSupport); // Create the Topic's in the DDS Domain. g_MessageTypeName = WaitSetData_MsgTypeSupport_get_type_name(g_MessageTypeSupport); g_MessageTopic = createTopic("WaitSetData_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 = WaitSetData_Msg__alloc(); message_Sample->userID = 1; message_Sample->message = DDS_string_alloc(firstHelloLength); strncpy(message_Sample->message, firstHello, firstHelloLength); printf("\n=== [WaitSetDataPublisher] writing a message containing :"); printf("\n userID : %d", message_Sample->userID); printf("\n Message : \"%s\"", message_Sample->message); g_status = WaitSetData_MsgDataWriter_write(message_DataWriter, message_Sample, DDS_HANDLE_NIL); checkStatus(g_status, "WaitSetData_MsgDataWriter_write"); DDS_free(message_Sample->message); // Let the time for the new StatusCondition to be handled by the Subscriber. os_nanoSleep(os_delay300ms); message_Sample->message = DDS_string_alloc(helloAgainLength); strncpy(message_Sample->message, helloAgain, helloAgainLength); printf("\n=== [WaitSetDataPublisher] writing a message containing :"); printf("\n userID : %d", message_Sample->userID); printf("\n Message : \"%s\"", message_Sample->message); g_status = WaitSetData_MsgDataWriter_write(message_DataWriter, message_Sample, DDS_HANDLE_NIL); checkStatus(g_status, "WaitSetData_MsgDataWriter_write"); // This time, no need to free message_Sample->message as it will be freed by DDS_free(message_Sample); // Let the time for the new StatusCondition to be handled by the Subscriber. os_nanoSleep(os_delay300ms); printf("\n=== [WaitSetDataPublisher] Exiting.\n\n"); // Cleanup DDS deleteDataWriter(message_Publisher, message_DataWriter); // Let the time for the new StatusCondition to be handled by the Subscriber. os_nanoSleep(os_delay300ms); deletePublisher(message_Publisher); deleteTopic(g_MessageTopic); deleteParticipant(); // Cleanup C allocations DDS_free(message_Sample); return 0; }
int main ( int argc, char *argv[]) { DDS_DomainId_t myDomain = DDS_OBJECT_NIL; DDS_DomainParticipantFactory dpf = DDS_OBJECT_NIL; DDS_DomainParticipant dp = DDS_OBJECT_NIL; DDS_Publisher p = DDS_OBJECT_NIL; DDS_Subscriber s = DDS_OBJECT_NIL; pingpong_PP_min_msgDataWriter PP_min_writer = DDS_OBJECT_NIL; pingpong_PP_seq_msgDataWriter PP_seq_writer = DDS_OBJECT_NIL; pingpong_PP_string_msgDataWriter PP_string_writer = DDS_OBJECT_NIL; pingpong_PP_fixed_msgDataWriter PP_fixed_writer = DDS_OBJECT_NIL; pingpong_PP_array_msgDataWriter PP_array_writer = DDS_OBJECT_NIL; pingpong_PP_bseq_msgDataWriter PP_bseq_writer = DDS_OBJECT_NIL; pingpong_PP_min_msgDataReader PP_min_reader = DDS_OBJECT_NIL; pingpong_PP_seq_msgDataReader PP_seq_reader = DDS_OBJECT_NIL; pingpong_PP_string_msgDataReader PP_string_reader = DDS_OBJECT_NIL; pingpong_PP_fixed_msgDataReader PP_fixed_reader = DDS_OBJECT_NIL; pingpong_PP_array_msgDataReader PP_array_reader = DDS_OBJECT_NIL; pingpong_PP_bseq_msgDataReader PP_bseq_reader = DDS_OBJECT_NIL; pingpong_PP_quit_msgDataReader PP_quit_reader = DDS_OBJECT_NIL; pingpong_PP_min_msgTypeSupport PP_min_dt; pingpong_PP_seq_msgTypeSupport PP_seq_dt; pingpong_PP_string_msgTypeSupport PP_string_dt; pingpong_PP_fixed_msgTypeSupport PP_fixed_dt; pingpong_PP_array_msgTypeSupport PP_array_dt; pingpong_PP_bseq_msgTypeSupport PP_bseq_dt; pingpong_PP_quit_msgTypeSupport PP_quit_dt; DDS_sequence_pingpong_PP_min_msg PP_min_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_seq_msg PP_seq_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_string_msg PP_string_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_fixed_msg PP_fixed_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_array_msg PP_array_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_bseq_msg PP_bseq_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_pingpong_PP_quit_msg PP_quit_dataList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_StatusCondition PP_min_sc; DDS_StatusCondition PP_seq_sc; DDS_StatusCondition PP_string_sc; DDS_StatusCondition PP_fixed_sc; DDS_StatusCondition PP_array_sc; DDS_StatusCondition PP_bseq_sc; DDS_StatusCondition PP_quit_sc; DDS_Topic PP_min_topic = DDS_OBJECT_NIL; DDS_Topic PP_seq_topic = DDS_OBJECT_NIL; DDS_Topic PP_string_topic = DDS_OBJECT_NIL; DDS_Topic PP_fixed_topic = DDS_OBJECT_NIL; DDS_Topic PP_array_topic = DDS_OBJECT_NIL; DDS_Topic PP_bseq_topic = DDS_OBJECT_NIL; DDS_Topic PP_quit_topic = DDS_OBJECT_NIL; DDS_ConditionSeq *conditionList; DDS_SampleInfoSeq infoList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_WaitSet w; DDS_DomainParticipantQos *dpQos; DDS_TopicQos *tQos; DDS_PublisherQos *pQos; DDS_DataWriterQos *dwQos; DDS_SubscriberQos *sQos; DDS_DataReaderQos *drQos; DDS_boolean terminate = FALSE; DDS_ReturnCode_t result; int i; int imax; int j; int jmax; printf ("Starting pong example\n"); fflush(stdout); /* * Evaluate cmdline arguments */ #ifdef INTEGRITY read_partition = "PongRead"; write_partition = "PongWrite"; #else if (argc != 1) { if (argc != 3) { printf ("Invalid.....\n Usage: %s [READ_PARTITION WRITE_PARTITION]\n", argv[0]); exit (1); } read_partition = argv[1]; write_partition = argv[2]; } #endif /* * Create WaitSet */ w = DDS_WaitSet__alloc (); /* * Initialize Qos variables */ dpQos = DDS_DomainParticipantQos__alloc(); tQos = DDS_TopicQos__alloc(); pQos = DDS_PublisherQos__alloc(); dwQos = DDS_DataWriterQos__alloc(); sQos = DDS_SubscriberQos__alloc(); drQos = DDS_DataReaderQos__alloc(); /* * Initialize condition list */ conditionList = NULL; /* * Create participant */ dpf = DDS_DomainParticipantFactory_get_instance (); dp = DDS_DomainParticipantFactory_create_participant (dpf, myDomain, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); if (dp == DDS_OBJECT_NIL) { printf ("%s PONG: ERROR - Splice Daemon not running\n", argv[0]); exit(1); } /* * Create PONG publisher */ DDS_DomainParticipant_get_default_publisher_qos (dp, pQos); pQos->partition.name._length = 1; pQos->partition.name._maximum = 1; pQos->partition.name._buffer = DDS_StringSeq_allocbuf (1); pQos->partition.name._buffer[0] = DDS_string_alloc (strlen (write_partition) + 1); strcpy (pQos->partition.name._buffer[0], write_partition); p = DDS_DomainParticipant_create_publisher (dp, pQos, NULL, DDS_STATUS_MASK_NONE); DDS_free (pQos); /* * Create PING subscriber */ DDS_DomainParticipant_get_default_subscriber_qos (dp, sQos); sQos->partition.name._length = 1; sQos->partition.name._maximum = 1; sQos->partition.name._buffer = DDS_StringSeq_allocbuf (1); sQos->partition.name._buffer[0] = DDS_string_alloc (strlen (read_partition) + 1); strcpy (sQos->partition.name._buffer[0], read_partition); s = DDS_DomainParticipant_create_subscriber (dp, sQos, NULL, DDS_STATUS_MASK_NONE); DDS_free (sQos); /* * PP_min_msg */ /* Create Topic */ PP_min_dt = pingpong_PP_min_msgTypeSupport__alloc (); pingpong_PP_min_msgTypeSupport_register_type (PP_min_dt, dp, "pingpong::PP_min_msg"); PP_min_topic = DDS_DomainParticipant_create_topic (dp, "PP_min_topic", "pingpong::PP_min_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_min_writer = DDS_Publisher_create_datawriter (p, PP_min_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_min_reader = DDS_Subscriber_create_datareader (s, PP_min_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_min_sc = DDS_DataReader_get_statuscondition (PP_min_reader); DDS_StatusCondition_set_enabled_statuses (PP_min_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_min_sc); /* * PP_seq_msg */ /* Create Topic */ PP_seq_dt = pingpong_PP_seq_msgTypeSupport__alloc (); pingpong_PP_seq_msgTypeSupport_register_type (PP_seq_dt, dp, "pingpong::PP_seq_msg"); PP_seq_topic = DDS_DomainParticipant_create_topic (dp, "PP_seq_topic", "pingpong::PP_seq_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_seq_writer = DDS_Publisher_create_datawriter (p, PP_seq_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_seq_reader = DDS_Subscriber_create_datareader (s, PP_seq_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_seq_sc = DDS_DataReader_get_statuscondition (PP_seq_reader); DDS_StatusCondition_set_enabled_statuses (PP_seq_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_seq_sc); /* * PP_string_msg */ /* Create Topic */ PP_string_dt = pingpong_PP_string_msgTypeSupport__alloc (); pingpong_PP_string_msgTypeSupport_register_type (PP_string_dt, dp, "pingpong::PP_string_msg"); PP_string_topic = DDS_DomainParticipant_create_topic (dp, "PP_string_topic", "pingpong::PP_string_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_string_writer = DDS_Publisher_create_datawriter (p, PP_string_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_string_reader = DDS_Subscriber_create_datareader (s, PP_string_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_string_sc = DDS_DataReader_get_statuscondition (PP_string_reader); DDS_StatusCondition_set_enabled_statuses (PP_string_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_string_sc); /* * PP_fixed_msg */ /* Create Topic */ PP_fixed_dt = pingpong_PP_fixed_msgTypeSupport__alloc (); pingpong_PP_fixed_msgTypeSupport_register_type (PP_fixed_dt, dp, "pingpong::PP_fixed_msg"); PP_fixed_topic = DDS_DomainParticipant_create_topic (dp, "PP_fixed_topic", "pingpong::PP_fixed_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_fixed_writer = DDS_Publisher_create_datawriter (p, PP_fixed_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_fixed_reader = DDS_Subscriber_create_datareader (s, PP_fixed_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_fixed_sc = DDS_DataReader_get_statuscondition (PP_fixed_reader); DDS_StatusCondition_set_enabled_statuses (PP_fixed_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_fixed_sc); /* * PP_array_msg */ /* Create Topic */ PP_array_dt = pingpong_PP_array_msgTypeSupport__alloc (); pingpong_PP_array_msgTypeSupport_register_type (PP_array_dt, dp, "pingpong::PP_array_msg"); PP_array_topic = DDS_DomainParticipant_create_topic (dp, "PP_array_topic", "pingpong::PP_array_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_array_writer = DDS_Publisher_create_datawriter (p, PP_array_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_array_reader = DDS_Subscriber_create_datareader (s, PP_array_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_array_sc = DDS_DataReader_get_statuscondition (PP_array_reader); DDS_StatusCondition_set_enabled_statuses (PP_array_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_array_sc); /* * PP_bseq_msg */ /* Create Topic */ PP_bseq_dt = pingpong_PP_bseq_msgTypeSupport__alloc (); pingpong_PP_bseq_msgTypeSupport_register_type (PP_bseq_dt, dp, "pingpong::PP_bseq_msg"); PP_bseq_topic = DDS_DomainParticipant_create_topic (dp, "PP_bseq_topic", "pingpong::PP_bseq_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datawriter */ PP_bseq_writer = DDS_Publisher_create_datawriter (p, PP_bseq_topic, DDS_DATAWRITER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_bseq_reader = DDS_Subscriber_create_datareader (s, PP_bseq_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_bseq_sc = DDS_DataReader_get_statuscondition (PP_bseq_reader); DDS_StatusCondition_set_enabled_statuses (PP_bseq_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_bseq_sc); /* * PP_quit_msg */ /* Create Topic */ PP_quit_dt = pingpong_PP_quit_msgTypeSupport__alloc (); pingpong_PP_quit_msgTypeSupport_register_type (PP_quit_dt, dp, "pingpong::PP_quit_msg"); PP_quit_topic = DDS_DomainParticipant_create_topic (dp, "PP_quit_topic", "pingpong::PP_quit_msg", DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Create datareader */ PP_quit_reader = DDS_Subscriber_create_datareader (s, PP_quit_topic, DDS_DATAREADER_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); /* Add datareader statuscondition to waitset */ PP_quit_sc = DDS_DataReader_get_statuscondition (PP_quit_reader); DDS_StatusCondition_set_enabled_statuses (PP_quit_sc, DDS_DATA_AVAILABLE_STATUS); result = DDS_WaitSet_attach_condition (w, PP_quit_sc); while (!terminate) { DDS_Duration_t wait_timeout = DDS_DURATION_INFINITE; /* printf ("PONG: waiting for PING\n"); */ conditionList = DDS_ConditionSeq__alloc(); result = DDS_WaitSet_wait (w, conditionList, &wait_timeout); if (result == DDS_RETCODE_OK) { imax = conditionList->_length; for (i = 0; i < imax; i++) { if (conditionList->_buffer[i] == PP_min_sc) { /* printf ("PONG: PING_min arrived\n"); */ result = pingpong_PP_min_msgDataReader_take (PP_min_reader, &PP_min_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_min_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_min_msgDataWriter_write (PP_min_writer, &PP_min_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_min_msgDataReader_return_loan (PP_min_reader, &PP_min_dataList, &infoList); } else { printf ("PONG: PING_min triggered, but no data available\n"); } } else if (conditionList->_buffer[i] == PP_seq_sc) { /* printf ("PONG: PING_seq arrived\n"); */ result = pingpong_PP_seq_msgDataReader_take (PP_seq_reader, &PP_seq_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_seq_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_seq_msgDataWriter_write (PP_seq_writer, &PP_seq_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_seq_msgDataReader_return_loan (PP_seq_reader, &PP_seq_dataList, &infoList); } else { printf ("PONG: PING_seq triggered, but no data available\n"); } } else if (conditionList->_buffer[i] == PP_string_sc) { /* printf ("PONG: PING_string arrived\n"); */ result = pingpong_PP_string_msgDataReader_take (PP_string_reader, &PP_string_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_string_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_string_msgDataWriter_write (PP_string_writer, &PP_string_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_string_msgDataReader_return_loan (PP_string_reader, &PP_string_dataList, &infoList); } else { printf ("PONG: PING_string triggered, but no data available\n"); exit(1); } } else if (conditionList->_buffer[i] == PP_fixed_sc) { /* printf ("PONG: PING_fixed arrived\n"); */ result = pingpong_PP_fixed_msgDataReader_take (PP_fixed_reader, &PP_fixed_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_fixed_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_fixed_msgDataWriter_write (PP_fixed_writer, &PP_fixed_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_fixed_msgDataReader_return_loan (PP_fixed_reader, &PP_fixed_dataList, &infoList); } else { printf ("PONG: PING_fixed triggered, but no data available\n"); } } else if (conditionList->_buffer[i] == PP_array_sc) { /* printf ("PONG: PING_array arrived\n"); */ result = pingpong_PP_array_msgDataReader_take (PP_array_reader, &PP_array_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_array_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_array_msgDataWriter_write (PP_array_writer, &PP_array_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_array_msgDataReader_return_loan (PP_array_reader, &PP_array_dataList, &infoList); } else { printf ("PONG: PING_array triggered, but no data available\n"); } } else if (conditionList->_buffer[i] == PP_bseq_sc) { /* printf ("PONG: PING_bseq arrived\n"); */ result = pingpong_PP_bseq_msgDataReader_take (PP_bseq_reader, &PP_bseq_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_bseq_dataList._length; if (jmax != 0) { for (j = 0; j < jmax; j++) { if (infoList._buffer[j].valid_data) { result = pingpong_PP_bseq_msgDataWriter_write (PP_bseq_writer, &PP_bseq_dataList._buffer[j], DDS_HANDLE_NIL); } } result = pingpong_PP_bseq_msgDataReader_return_loan (PP_bseq_reader, &PP_bseq_dataList, &infoList); } else { printf ("PONG: PING_bseq triggered, but no data available\n"); } } else if (conditionList->_buffer[i] == PP_quit_sc) { /* printf ("PONG: PING_quit arrived\n"); */ result = pingpong_PP_quit_msgDataReader_take (PP_quit_reader, &PP_quit_dataList, &infoList, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); jmax = PP_quit_dataList._length; if (jmax != 0) { if (PP_quit_dataList._buffer[0].quit) { terminate = TRUE; } result = pingpong_PP_quit_msgDataReader_return_loan (PP_quit_reader, &PP_quit_dataList, &infoList); } else { printf ("PONG: PING_quit triggered, but no data available\n"); } } else { printf ("PONG: unknown condition triggered: %lx\n", (unsigned long)conditionList->_buffer[i]); } } } else { if (result == DDS_RETCODE_ALREADY_DELETED) { terminate = TRUE; } printf ("PONG: wait returned not ok: %d\n", result); } DDS_free(conditionList); } result = DDS_Subscriber_delete_datareader (s, PP_min_reader); result = DDS_Publisher_delete_datawriter (p, PP_min_writer); result = DDS_Subscriber_delete_datareader (s, PP_seq_reader); result = DDS_Publisher_delete_datawriter (p, PP_seq_writer); result = DDS_Subscriber_delete_datareader (s, PP_string_reader); result = DDS_Publisher_delete_datawriter (p, PP_string_writer); result = DDS_Subscriber_delete_datareader (s, PP_fixed_reader); result = DDS_Publisher_delete_datawriter (p, PP_fixed_writer); result = DDS_Subscriber_delete_datareader (s, PP_array_reader); result = DDS_Publisher_delete_datawriter (p, PP_array_writer); result = DDS_Subscriber_delete_datareader (s, PP_bseq_reader); result = DDS_Publisher_delete_datawriter (p, PP_bseq_writer); result = DDS_Subscriber_delete_datareader (s, PP_quit_reader); result = DDS_DomainParticipant_delete_subscriber (dp, s); result = DDS_DomainParticipant_delete_publisher (dp, p); result = DDS_DomainParticipant_delete_topic (dp, PP_min_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_seq_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_string_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_fixed_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_array_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_bseq_topic); result = DDS_DomainParticipant_delete_topic (dp, PP_quit_topic); result = DDS_DomainParticipantFactory_delete_participant (dpf, dp); DDS_free (w); DDS_free (PP_min_dt); DDS_free (PP_seq_dt); DDS_free (PP_string_dt); DDS_free (PP_fixed_dt); DDS_free (PP_array_dt); DDS_free (PP_bseq_dt); DDS_free (PP_quit_dt); DDS_free (dpQos); DDS_free (tQos); DDS_free (dwQos); DDS_free (drQos); printf ("Completed pong example\n"); fflush(stdout); return 0; }
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 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 userHandles[10]; DDS_sequence_DurabilityData_Msg* instances = DDS_sequence_DurabilityData_Msg__alloc(); DDS_boolean isTransient; DDS_boolean isPersistent; DDS_boolean isAutodisposeTrue; DDS_boolean isAutodisposeFalse; DDS_boolean isAutomated; os_time delay_10s = { 10, 0 }; if( argc < 4 ) { usage(); } isTransient = (strcmp(argv[1], "transient") == 0) ? TRUE : FALSE; isPersistent = (strcmp(argv[1], "persistent") == 0) ? TRUE : FALSE; isAutodisposeTrue = (strcmp(argv[2], "true") == 0) ? TRUE : FALSE; isAutodisposeFalse = (strcmp(argv[2], "false") == 0) ? TRUE : FALSE; if( !((isTransient || isPersistent) && (isAutodisposeTrue || isAutodisposeFalse)) ) { usage(); } isAutomated = (strcmp(argv[3], "true") == 0) ? TRUE : FALSE; g_durability_kind = (char*) argv[1]; g_autodispose_unregistered_instances = isAutodisposeTrue ? (DDS_boolean) TRUE : (DDS_boolean) FALSE; // 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 Publisher's in the DDS Domain. createPublisher(); // Request a Writer from the the Publisher. createWriter(); instances->_buffer = DDS_sequence_DurabilityData_Msg_allocbuf(10); for( x = 0; x < 10 ; x++ ) { instances->_buffer[x].id = x; instances->_buffer[x].content = DDS_string_alloc(1); snprintf(instances->_buffer[x].content, 2, "%d", x); userHandles[x] = DurabilityData_MsgDataWriter_register_instance(g_DataWriter, &instances->_buffer[x]); printf("\n%s", instances->_buffer[x].content); g_status = DurabilityData_MsgDataWriter_write(g_DataWriter, &instances->_buffer[x], userHandles[x]); checkStatus(g_status, "DurabilityData_MsgDataWriter_write"); } if( isAutomated == FALSE ) { int c = 0; printf("\n Type 'E' + Enter to exit:\n"); do { c = getchar(); printf("\n You Typed %c", c); } while( c != (int) 'E' ); printf("\n Exiting.\n\n"); } else { printf( "\n === sleeping 10s..." );; os_nanoSleep(delay_10s); } // Cleanup DDS from the created Entities. deleteDataWriter(); deletePublisher(); deleteTopic(); deleteParticipant(); // Cleanup C allocations, recursively freeing the allocated structures and sequences using the OpenSplice API. DDS_free(instances); // Print out an empty line, just to let behind a clean new line for the shell.. printf("\n"); return 0; }
DDS_MultiTopic DDS_DomainParticipant_create_simulated_multitopic ( DDS_DomainParticipant participant, const DDS_char *name, const DDS_char *type_name, const DDS_char *subscription_expression, const DDS_StringSeq *expression_parameters ) { /* Type-specific DDS entities */ static Chat_ChatMessageDataReader chatMessageDR; static Chat_NameServiceDataReader nameServiceDR; static Chat_NamedMessageDataWriter namedMessageDW; /* Query related stuff */ static DDS_QueryCondition nameFinder; static DDS_StringSeq *nameFinderParams; /* QosPolicy holders */ DDS_TopicQos *namedMessageQos; DDS_SubscriberQos *sub_qos; DDS_PublisherQos *pub_qos; /* Others */ const char *partitionName = "ChatRoom"; const char *nameFinderExpr; struct MsgListenerState *listener_state; DDS_Duration_t infiniteTimeOut = DDS_DURATION_INFINITE; DDS_ReturnCode_t status; /* Lookup both components that constitute the multi-topic. */ chatMessageTopic = DDS_DomainParticipant_find_topic( participant, "Chat_ChatMessage", &infiniteTimeOut); checkHandle(chatMessageTopic, "DDS_DomainParticipant_find_topic (Chat_ChatMessage)"); nameServiceTopic = DDS_DomainParticipant_find_topic( participant, "Chat_NameService", &infiniteTimeOut); checkHandle(nameServiceTopic, "DDS_DomainParticipant_find_topic (Chat_NameService)"); /* Create a ContentFilteredTopic to filter out our own ChatMessages. */ filteredMessageTopic = DDS_DomainParticipant_create_contentfilteredtopic( participant, "Chat_FilteredMessage", chatMessageTopic, "userID <> %0", expression_parameters); checkHandle(filteredMessageTopic, "DDS_DomainParticipant_create_contentfilteredtopic"); /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */ sub_qos = DDS_SubscriberQos__alloc(); checkHandle(sub_qos, "DDS_SubscriberQos__alloc"); status = DDS_DomainParticipant_get_default_subscriber_qos (participant, sub_qos); checkStatus(status, "DDS_DomainParticipant_get_default_subscriber_qos"); sub_qos->partition.name._length = 1; sub_qos->partition.name._maximum = 1; sub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1); checkHandle(sub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf"); sub_qos->partition.name._buffer[0] = DDS_string_alloc ( strlen(partitionName) ); checkHandle(sub_qos->partition.name._buffer[0], "DDS_string_alloc"); strcpy (sub_qos->partition.name._buffer[0], partitionName); /* Create a private Subscriber for the multitopic simulator. */ multiSub = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(multiSub, "DDS_DomainParticipant_create_subscriber (for multitopic)"); /* Create a DataReader for the FilteredMessage Topic (using the appropriate QoS). */ chatMessageDR = DDS_Subscriber_create_datareader( multiSub, filteredMessageTopic, DDS_DATAREADER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(chatMessageDR, "DDS_Subscriber_create_datareader (ChatMessage)"); /* Create a DataReader for the nameService Topic (using the appropriate QoS). */ nameServiceDR = DDS_Subscriber_create_datareader( multiSub, nameServiceTopic, DDS_DATAREADER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(nameServiceDR, "DDS_Subscriber_create_datareader (NameService)"); /* Define the SQL expression (using a parameterized value). */ nameFinderExpr = "userID = %0"; /* Allocate and assign the query parameters. */ nameFinderParams = DDS_StringSeq__alloc(); checkHandle(nameFinderParams, "DDS_StringSeq__alloc"); nameFinderParams->_length = 1; nameFinderParams->_maximum = 1; nameFinderParams->_buffer = DDS_StringSeq_allocbuf (1); checkHandle(nameFinderParams->_buffer, "DDS_StringSeq_allocbuf"); nameFinderParams->_buffer[0] = DDS_string_alloc ( MAX_INT_LENGTH ); checkHandle(nameFinderParams->_buffer[0], "DDS_string_alloc"); strcpy(nameFinderParams->_buffer[0], "0"); DDS_sequence_set_release(nameFinderParams, TRUE); /* Create a QueryCondition to only read corresponding nameService information by key-value. */ nameFinder = DDS_DataReader_create_querycondition( nameServiceDR, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE, nameFinderExpr, nameFinderParams); checkHandle(nameFinder, "DDS_DataReader_create_querycondition (nameFinder)"); /* Create the Topic that simulates the multi-topic (use Qos from chatMessage).*/ namedMessageQos = DDS_TopicQos__alloc(); checkHandle(namedMessageQos, "DDS_TopicQos__alloc"); status = DDS_Topic_get_qos(chatMessageTopic, namedMessageQos); checkStatus(status, "DDS_Topic_get_qos"); /* Create the NamedMessage Topic whose samples simulate the MultiTopic */ namedMessageTopic = DDS_DomainParticipant_create_topic( participant, "Chat_NamedMessage", type_name, namedMessageQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(namedMessageTopic, "DDS_DomainParticipant_create_topic (NamedMessage)"); /* Adapt the default PublisherQos to write into the "ChatRoom" Partition. */ pub_qos = DDS_PublisherQos__alloc(); checkHandle(pub_qos, "DDS_PublisherQos__alloc"); status = DDS_DomainParticipant_get_default_publisher_qos (participant, pub_qos); checkStatus(status, "DDS_DomainParticipant_get_default_publisher_qos"); pub_qos->partition.name._length = 1; pub_qos->partition.name._maximum = 1; pub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1); checkHandle(pub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf"); pub_qos->partition.name._buffer[0] = DDS_string_alloc ( strlen(partitionName) ); checkHandle(pub_qos->partition.name._buffer[0], "DDS_string_alloc"); strcpy (pub_qos->partition.name._buffer[0], partitionName); /* Create a private Publisher for the multitopic simulator. */ multiPub = DDS_DomainParticipant_create_publisher(participant, pub_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(multiPub, "DDS_DomainParticipant_create_publisher (for multitopic)"); /* Create a DataWriter for the multitopic. */ namedMessageDW = DDS_Publisher_create_datawriter( multiPub, namedMessageTopic, DDS_DATAWRITER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(namedMessageDW, "DDS_Publisher_create_datawriter (NamedMessage)"); /* Allocate the DataReaderListener interface. */ msgListener = DDS_DataReaderListener__alloc(); checkHandle(msgListener, "DDS_DataReaderListener__alloc"); /* Fill the listener_data with pointers to all entities needed by the Listener implementation. */ listener_state = malloc(sizeof(struct MsgListenerState)); checkHandle(listener_state, "malloc"); listener_state->chatMessageDR = chatMessageDR; listener_state->nameServiceDR = nameServiceDR; listener_state->namedMessageDW = namedMessageDW; listener_state->nameFinder = nameFinder; listener_state->nameFinderParams = nameFinderParams; msgListener->listener_data = listener_state; /* Assign the function pointer attributes to their implementation functions. */ msgListener->on_data_available = (void (*)(void *, DDS_DataReader)) on_message_available; msgListener->on_requested_deadline_missed = NULL; msgListener->on_requested_incompatible_qos = NULL; msgListener->on_sample_rejected = NULL; msgListener->on_liveliness_changed = NULL; msgListener->on_subscription_matched = NULL; msgListener->on_sample_lost = NULL; /* Attach the DataReaderListener to the DataReader, only enabling the data_available event. */ status = DDS_DataReader_set_listener(chatMessageDR, msgListener, DDS_DATA_AVAILABLE_STATUS); checkStatus(status, "DDS_DataReader_set_listener"); /* Free up all resources that are no longer needed. */ DDS_free(namedMessageQos); DDS_free(sub_qos); DDS_free(pub_qos); /* Return the simulated Multitopic. */ return namedMessageTopic; }
/* Implementation for the callback function "on_data_available". */ void on_message_available ( void *listener_data, DDS_DataReader reader ) { struct MsgListenerState *listener_state; DDS_sequence_Chat_ChatMessage msgSeq = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_Chat_NameService nameSeq = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_SampleInfoSeq infoSeq1 = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_SampleInfoSeq infoSeq2 = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_ReturnCode_t status; DDS_unsigned_long i; DDS_long previous = 0x80000000; DDS_string userName = DDS_string_alloc(1); /* Obtain all entities mentioned in the listener state. */ listener_state = (struct MsgListenerState *) listener_data; /* Take all messages. */ status = Chat_ChatMessageDataReader_take( listener_state->chatMessageDR, &msgSeq, &infoSeq1, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE); checkStatus(status, "Chat_ChatMessageDataReader_take"); /* For each message, extract the key-field and find the corresponding name. */ for (i = 0; i < msgSeq._length; i++) { if (infoSeq1._buffer[i].valid_data) { Chat_NamedMessage joinedSample; /* Find the corresponding named message. */ if (msgSeq._buffer[i].userID != previous) { previous = msgSeq._buffer[i].userID; snprintf(listener_state->nameFinderParams->_buffer[0], MAX_INT_LENGTH, "%d", previous); status = DDS_QueryCondition_set_query_parameters( listener_state->nameFinder, listener_state->nameFinderParams); checkStatus(status, "DDS_QueryCondition_set_query_parameters"); status = Chat_NameServiceDataReader_read_w_condition( listener_state->nameServiceDR, &nameSeq, &infoSeq2, DDS_LENGTH_UNLIMITED, listener_state->nameFinder); checkStatus(status, "Chat_NameServiceDataReader_read_w_condition"); /* Extract Name (there should only be one result). */ DDS_free(userName); if (status == DDS_RETCODE_NO_DATA) { userName = DDS_string_alloc(40); checkHandle(userName, "DDS_string_alloc"); snprintf(userName, 40, "Name not found!! id = %d", previous); } else { userName = DDS_string_alloc( strlen(nameSeq._buffer[0].name) ); checkHandle(userName, "DDS_string_alloc"); strcpy(userName, nameSeq._buffer[0].name); } /* Release the name sample again. */ status = Chat_NameServiceDataReader_return_loan(listener_state->nameServiceDR, &nameSeq, &infoSeq2); checkStatus(status, "Chat_NameServiceDataReader_return_loan"); } /* Write merged Topic with both userName and userID. */ /* StringCopy not required since sample runs out of scope before string is released. */ joinedSample.userName = userName; joinedSample.userID = msgSeq._buffer[i].userID; joinedSample.index = msgSeq._buffer[i].index; joinedSample.content = msgSeq._buffer[i].content; status = Chat_NamedMessageDataWriter_write( listener_state->namedMessageDW, &joinedSample, DDS_HANDLE_NIL); checkStatus(status, "Chat_NamedMessageDataWriter_write"); } } status = Chat_ChatMessageDataReader_return_loan(listener_state->chatMessageDR, &msgSeq, &infoSeq1); checkStatus(status, "Chat_ChatMessageDataReader_return_loan"); }
int OSPL_MAIN (int argc, char *argv[]) { char* publisher_name; int i, sampleIndex, ownership_strength, nb_iteration; c_bool isStoppingSubscriber; float price; os_time os_delay200 = { 0, 200000000 }; os_time os_delay2000 = { 2, 0 }; DDS_Publisher OwnershipDataPublisher; DDS_DataWriter OwnershipDataDataWriter; OwnershipData_Stock* OwnershipDataSample; DDS_InstanceHandle_t userHandle; const char ticker [] = "MSFT"; DDS_unsigned_long stringLength = sizeof(ticker) - 1; // usage : Publisher.exe <publisher_name> <ownership_strength> <nb_iterations> if( argc < 5 ) { usage(); } printf("\n\n Starting OwnershipDataPublisher..."); printf("\n\n Parameters are:"); for( i = 1; i < argc ; ++i ) { printf("\n %d: %s", i, argv[i]); } publisher_name = argv[1]; ownership_strength = atoi(argv[2]); nb_iteration = atoi(argv[3]); isStoppingSubscriber = argv[4][0] == '1' ? TRUE : FALSE; createParticipant("Ownership example"); // Register Stock Topic's type in the DDS Domain. g_StockTypeSupport = (DDS_TypeSupport) OwnershipData_StockTypeSupport__alloc(); checkHandle(g_StockTypeSupport, "OwnershipData_StockTypeSupport__alloc"); registerStockType(g_StockTypeSupport); // Create Stock Topic in the DDS Domain. g_StockTypeName = OwnershipData_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. OwnershipDataPublisher = createPublisher(); // Request a Writer from the the Publisher. OwnershipDataDataWriter = createDataWriter(OwnershipDataPublisher, g_StockTopic, FALSE, ownership_strength); // Publish a Stock Sample reflecting the state of the Msg DataWriter. OwnershipDataSample = OwnershipData_Stock__alloc(); OwnershipDataSample->ticker = DDS_string_alloc(stringLength); snprintf(OwnershipDataSample->ticker, stringLength + 1, "%s", ticker); OwnershipDataSample->price = 0.0; OwnershipDataSample->publisher = DDS_string_dup(publisher_name); OwnershipDataSample->strength = ownership_strength; userHandle = OwnershipData_StockDataWriter_register_instance(OwnershipDataDataWriter, OwnershipDataSample); //Publisher publishes the prices in dollars printf("\n=== [Publisher] Publisher %d with strength : ", ownership_strength); printf("\n / sending %d prices...", nb_iteration); // The subscriber should display the prices sent by the publisher with the highest ownership strength price = 10.0f; for( sampleIndex = 0; sampleIndex < nb_iteration ; ++sampleIndex ) { OwnershipDataSample->price = price; writeStockSample(OwnershipDataDataWriter, userHandle, OwnershipDataSample); os_nanoSleep(os_delay200); price = price + 0.5f; } if( isStoppingSubscriber == TRUE ) { // This special price gives the end signal to the Subscriber: OwnershipDataSample->price = (DDS_float) -1.0f; writeStockSample(OwnershipDataDataWriter, userHandle, OwnershipDataSample); } // This to make sure the Subscriber will get all the Samples. os_nanoSleep(os_delay2000); OwnershipData_StockDataWriter_unregister_instance (OwnershipDataDataWriter, OwnershipDataSample, userHandle); // Cleanup DDS from the created Entities. deleteDataWriter(OwnershipDataPublisher, OwnershipDataDataWriter); deletePublisher(OwnershipDataPublisher); deleteTopic(g_StockTopic); deleteParticipant(); // Cleanup C allocations, // recursively freeing the allocated structures and sequences using the OpenSplice API. DDS_free(OwnershipDataSample); // 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) #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) #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; }
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[]) { DDS_DomainParticipantFactory dpf; DDS_DomainParticipant dp; DDS_DomainId_t domain = DDS_DOMAIN_ID_DEFAULT; DDS_ReturnCode_t status; Chat_ChatMessageTypeSupport chatMessageTS; DDS_Topic chatMessageTopic; char *chatMessageTypeName; /* Create a DomainParticipantFactory and a DomainParticipant */ /* (using Default QoS settings). */ dpf = DDS_DomainParticipantFactory_get_instance(); if (!dpf) { printf("Creating ParticipantFactory failed!!\n"); exit(-1); } printf("Created ParticipantFactory.\n"); dp = DDS_DomainParticipantFactory_create_participant ( dpf, domain, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); if (!dp) { printf("Creating Participant failed!!\n"); exit(-1); } printf("Created Participant.\n"); /* Register the required data type for ChatMessage. */ chatMessageTS = Chat_ChatMessageTypeSupport__alloc(); if (!chatMessageTS) { printf ("Allocating TypeSupport failed!!\n"); exit(-1); }; chatMessageTypeName = Chat_ChatMessageTypeSupport_get_type_name(chatMessageTS); status = Chat_ChatMessageTypeSupport_register_type( chatMessageTS, dp, chatMessageTypeName); if (status != DDS_RETCODE_OK) { printf("Registering data type failed. Status = %d\n", status); exit(-1); }; printf("Registered data type.\n"); /*Create the ChatMessage topic */ chatMessageTopic = DDS_DomainParticipant_create_topic( dp, "Chat_ChatMessage", chatMessageTypeName, DDS_TOPIC_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); if (!chatMessageTopic) { printf("Creating ChatMessage topic failed!!\n"); exit(-1); }; printf("Created ChatMessage topic.\n"); DDS_PublisherQos *pub_qos; DDS_DataWriterQos *dw_qos; DDS_Publisher chatPublisher; Chat_ChatMessageDataWriter talker; Chat_NameServiceDataWriter nameServer; char *partitionName = NULL; DDS_Topic nameServiceTopic; /* Adapt the default PublisherQos to write into the "ChatRoom" Partition. */ partitionName = "ChatRoom"; pub_qos = DDS_PublisherQos__alloc(); if (!pub_qos) { printf("Allocating PublisherQos failed!!\n"); exit(-1); } status = DDS_DomainParticipant_get_default_publisher_qos ( dp, pub_qos); if (status != DDS_RETCODE_OK) { printf("Getting default publisher qos failed!!\n"); exit(-1); } pub_qos->partition.name._length = 1; pub_qos->partition.name._maximum = 1; pub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1); if (!pub_qos->partition.name._buffer) { printf("Allocating partition name failed!!\n"); exit(-1); } pub_qos->partition.name._buffer[0] = DDS_string_alloc ( strlen(partitionName)); if (!pub_qos->partition.name._buffer[0]) { printf("Allocating partition name failed!!\n"); exit(-1); } strcpy (pub_qos->partition.name._buffer[0], partitionName); /* Create a Publisher for the chatter application. */ chatPublisher = DDS_DomainParticipant_create_publisher( dp, pub_qos, NULL, DDS_STATUS_MASK_NONE); if (!chatPublisher) { printf("Creating publisher failed!!\n"); exit(-1); } printf("Created publisher.\n"); /* 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); if (!talker) { printf("Creating datawriter failed!!\n"); exit(-1); } printf("Created datawriter.\n"); // Initialize message int ownID = 0; Chat_ChatMessage *msg; msg = Chat_ChatMessage__alloc(); //checkHandle(msg, "Chat_ChatMessage__alolc"); msg->userID = ownID; msg->index = 0; msg->content = DDS_string_alloc(MAX_MSG_LEN); //checkHandle(msg->content, "DDS_string_alloc"); snprintf(msg->content, MAX_MSG_LEN, "hello world"); // register a chat message DDS_InstanceHandle_t userHandle; userHandle = Chat_ChatMessageDataWriter_register_instance(talker, msg); Chat_NameService ns; ns.userID = ownID; ns.name = DDS_string_alloc(Chat_MAX_NAME+1); //checkHandle(ns.name, "DDS_string_alloc"); char *chatterName; if (chatterName) { strncpy(ns.name, chatterName, Chat_MAX_NAME + 1); } else { snprintf(ns.name, Chat_MAX_NAME+1, "Chatter %d", ownID); } // Write user information status = Chat_NameServiceDataWriter_write(nameServer, &ns, DDS_HANDLE_NIL); //checkStatus(status, "Chat_ChatMessageDataWriter_write"); // Write a message status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle); // checkStatus(status, "Chat_ChatMessageDataWriter_write"); // pause sleep(1); int i = 0; for (i = 1; i < 6; ++i) { msg->index = i; snprintf(msg->content, MAX_MSG_LEN, "Message number: %d", msg->index); status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle); // checkStatus(status, "Chat_ChatMessageDataWriter_write"); sleep(1); } /* Remove the DataWriters */ status = DDS_Publisher_delete_datawriter(chatPublisher, talker); if (status != DDS_RETCODE_OK) { printf("Deleting datawriter failed!!\n"); exit(-1); } printf("Deleted datawriter.\n"); /*status = DDS_Publisher_delete_datawriter( chatPublisher, nameServer); if (status != DDS_RETCODE_OK) { printf("Deleting datawriter (NameService) failed!!\n"); exit(-1); } printf("Deleted datawriter (NameService).\n");*/ /* Remove the Publisher. */ status = DDS_DomainParticipant_delete_publisher( dp, chatPublisher); if (status != DDS_RETCODE_OK) { printf("Deleting publisher failed!!\n"); exit(-1); } /* De-allocate the PublisherQoS holder. */ DDS_free(pub_qos); // Note that DDS_free recursively // de-allocates all indirections!! printf("Deleted publisher.\n"); /* Deleting the Topic. */ status = DDS_DomainParticipant_delete_topic( dp, chatMessageTopic); if (status != DDS_RETCODE_OK) { printf("Deleting topic failed. Status = %d\n", status); exit(-1); }; printf("Deleted ChatMessage topic.\n"); /* Deleting the DomainParticipant */ status = DDS_DomainParticipantFactory_delete_participant( dpf, dp); if (status != DDS_RETCODE_OK) { printf("Deleting participant failed. Status = %d\n", status); exit(-1); }; printf("Deleted Participant.\n"); /* Everything is fine, return normally. */ return 0; };