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); }
DDS_Topic createTopic(const char *topicName, const char *typeName) { DDS_Topic topic; const char* messageFirstPart; char* message; size_t messageFirstPartLength, topicNameLength; DDS_TopicQos* topicQos = DDS_TopicQos__alloc(); checkHandle(topicQos, "DDS_TopicQos__alloc"); g_status = DDS_DomainParticipant_get_default_topic_qos(g_domainParticipant, topicQos); checkStatus(g_status, "DDS_DomainParticipant_get_default_topic_qos"); topicQos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; topicQos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS; // DeadlineQoSPolicy : period used to trigger the listener // (on_requested_deadline_missed) topicQos->deadline.period.nanosec = 0; topicQos->deadline.period.sec = 1; /* Use the changed policy when defining the Ownership topic */ topic = DDS_DomainParticipant_create_topic(g_domainParticipant, topicName, typeName, topicQos, NULL, DDS_STATUS_MASK_NONE); checkHandle(topic, "DDS::DomainParticipant::create_topic ()"); //Format error message messageFirstPart = "DDS_DomainParticipant_create_topic"; messageFirstPartLength = strlen(messageFirstPart); topicNameLength = strlen(topicName); message = (char*) malloc(messageFirstPartLength + topicNameLength + 2); snprintf(message, messageFirstPartLength + topicNameLength + 1, "%s %s", messageFirstPart, topicName); checkHandle(topic, message); free(message); DDS_free(topicQos); return topic; }
int main (int argc, char ** argv) #endif { /* Generic DDS entities */ DDS_DomainParticipant participant; DDS_Topic chatMessageTopic; DDS_Topic nameServiceTopic; DDS_Subscriber chatSubscriber; DDS_QueryCondition singleUser; DDS_ReadCondition newUser; DDS_StatusCondition leftUser; DDS_GuardCondition guard; DDS_WaitSet userLoadWS; DDS_LivelinessChangedStatus livChangStatus; /* QosPolicy holders */ DDS_TopicQos *setting_topic_qos; DDS_TopicQos *reliable_topic_qos; DDS_SubscriberQos *sub_qos; DDS_DataReaderQos *message_qos; /* DDS Identifiers */ DDS_DomainId_t domain = NULL; DDS_ReturnCode_t status; DDS_ConditionSeq *guardList = NULL; DDS_Duration_t timeout = DDS_DURATION_INFINITE; /* Type-specific DDS entities */ Chat_ChatMessageTypeSupport chatMessageTS; Chat_NameServiceTypeSupport nameServiceTS; Chat_NameServiceDataReader nameServer; Chat_ChatMessageDataReader loadAdmin; DDS_sequence_Chat_ChatMessage msgList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_sequence_Chat_NameService nsList = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_SampleInfoSeq infoSeq = { 0, 0, DDS_OBJECT_NIL, FALSE }; DDS_SampleInfoSeq infoSeq2 = { 0, 0, DDS_OBJECT_NIL, FALSE }; /* Others */ DDS_StringSeq args; int closed = 0; DDS_unsigned_long i, j; DDS_long prevCount = 0; char *partitionName; char *chatMessageTypeName = NULL; char *nameServiceTypeName = NULL; pthread_t tid; pthread_attr_t tattr; printf("Starting UserLoad example.\n"); fflush(stdout); /* Create a DomainParticipant (using the 'TheParticipantFactory' convenience macro). */ participant = DDS_DomainParticipantFactory_create_participant ( DDS_TheParticipantFactory, domain, DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE); checkHandle(participant, "DDS_DomainParticipantFactory_create_participant"); /* Register the required datatype for ChatMessage. */ chatMessageTS = Chat_ChatMessageTypeSupport__alloc(); checkHandle(chatMessageTS, "Chat_ChatMessageTypeSupport__alloc"); chatMessageTypeName = Chat_ChatMessageTypeSupport_get_type_name(chatMessageTS); status = Chat_ChatMessageTypeSupport_register_type( chatMessageTS, participant, chatMessageTypeName); checkStatus(status, "Chat_ChatMessageTypeSupport_register_type"); /* Register the required datatype for NameService. */ nameServiceTS = Chat_NameServiceTypeSupport__alloc(); checkHandle(nameServiceTS, "Chat_NameServiceTypeSupport__alloc"); nameServiceTypeName = Chat_NameServiceTypeSupport_get_type_name(nameServiceTS); status = Chat_NameServiceTypeSupport_register_type( nameServiceTS, participant, nameServiceTypeName); checkStatus(status, "Chat_NameServiceTypeSupport_register_type"); /* Set the ReliabilityQosPolicy to RELIABLE. */ reliable_topic_qos = DDS_TopicQos__alloc(); checkHandle(reliable_topic_qos, "DDS_TopicQos__alloc"); status = DDS_DomainParticipant_get_default_topic_qos(participant, reliable_topic_qos); checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos"); reliable_topic_qos->reliability.kind = DDS_RELIABLE_RELIABILITY_QOS; /* Make the tailored QoS the new default. */ status = DDS_DomainParticipant_set_default_topic_qos(participant, reliable_topic_qos); checkStatus(status, "DDS_DomainParticipant_set_default_topic_qos"); /* Use the changed policy when defining the ChatMessage topic */ chatMessageTopic = DDS_DomainParticipant_create_topic( participant, "Chat_ChatMessage", chatMessageTypeName, reliable_topic_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(chatMessageTopic, "DDS_DomainParticipant_create_topic (ChatMessage)"); /* Set the DurabilityQosPolicy to TRANSIENT. */ setting_topic_qos = DDS_TopicQos__alloc(); checkHandle(setting_topic_qos, "DDS_TopicQos__alloc"); status = DDS_DomainParticipant_get_default_topic_qos(participant, setting_topic_qos); checkStatus(status, "DDS_DomainParticipant_get_default_topic_qos"); setting_topic_qos->durability.kind = DDS_TRANSIENT_DURABILITY_QOS; /* Create the NameService Topic. */ nameServiceTopic = DDS_DomainParticipant_create_topic( participant, "Chat_NameService", nameServiceTypeName, setting_topic_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(nameServiceTopic, "DDS_DomainParticipant_create_topic"); /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */ partitionName = "ChatRoom"; sub_qos = DDS_SubscriberQos__alloc(); checkHandle(sub_qos, "DDS_SubscriberQos__alloc"); status = DDS_DomainParticipant_get_default_subscriber_qos (participant, sub_qos); checkStatus(status, "DDS_DomainParticipant_get_default_subscriber_qos"); sub_qos->partition.name._length = 1; sub_qos->partition.name._maximum = 1; sub_qos->partition.name._buffer = DDS_StringSeq_allocbuf (1); checkHandle(sub_qos->partition.name._buffer, "DDS_StringSeq_allocbuf"); sub_qos->partition.name._buffer[0] = DDS_string_alloc (strlen(partitionName) + 1); checkHandle(sub_qos->partition.name._buffer[0], "DDS_string_alloc"); strcpy (sub_qos->partition.name._buffer[0], partitionName); /* Create a Subscriber for the UserLoad application. */ chatSubscriber = DDS_DomainParticipant_create_subscriber(participant, sub_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(chatSubscriber, "DDS_DomainParticipant_create_subscriber"); /* Create a DataReader for the NameService Topic (using the appropriate QoS). */ nameServer = DDS_Subscriber_create_datareader( chatSubscriber, nameServiceTopic, DDS_DATAREADER_QOS_USE_TOPIC_QOS, NULL, DDS_STATUS_MASK_NONE); checkHandle(nameServer, "DDS_Subscriber_create_datareader (NameService)"); /* Adapt the DataReaderQos for the ChatMessageDataReader to keep track of all messages. */ message_qos = DDS_DataReaderQos__alloc(); checkHandle(message_qos, "DDS_DataReaderQos__alloc"); status = DDS_Subscriber_get_default_datareader_qos(chatSubscriber, message_qos); checkStatus(status, "DDS_Subscriber_get_default_datareader_qos"); status = DDS_Subscriber_copy_from_topic_qos(chatSubscriber, message_qos, reliable_topic_qos); checkStatus(status, "DDS_Subscriber_copy_from_topic_qos"); message_qos->history.kind = DDS_KEEP_ALL_HISTORY_QOS; /* Create a DataReader for the ChatMessage Topic (using the appropriate QoS). */ loadAdmin = DDS_Subscriber_create_datareader( chatSubscriber, chatMessageTopic, message_qos, NULL, DDS_STATUS_MASK_NONE); checkHandle(loadAdmin, "DDS_Subscriber_create_datareader (ChatMessage)"); /* Initialize the Query Arguments. */ args._length = 1; args._maximum = 1; args._buffer = DDS_StringSeq_allocbuf(1); checkHandle(args._buffer, "DDS_StringSeq_allocbuf"); args._buffer[0] = DDS_string_alloc (12); /* Enough for maximum size numbers. */ checkHandle(args._buffer[0], "DDS_string_alloc"); sprintf(args._buffer[0], "%d", 0); /* Create a QueryCondition that will contain all messages with userID=ownID */ singleUser = DDS_DataReader_create_querycondition( loadAdmin, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE, "userID=%0", &args); checkHandle(singleUser, "DDS_DataReader_create_querycondition (singleUser Query)"); /* Create a ReadCondition that will contain new users only */ newUser = DDS_DataReader_create_readcondition( nameServer, DDS_NOT_READ_SAMPLE_STATE, DDS_NEW_VIEW_STATE, DDS_ALIVE_INSTANCE_STATE); checkHandle(newUser, "DDS_DataReader_create_readcondition (newUser)"); /* Obtain a StatusCondition that triggers only when a Writer changes Liveliness */ leftUser = DDS_DataReader_get_statuscondition(loadAdmin); checkHandle(leftUser, "DDS_DataReader_get_statuscondition"); status = DDS_StatusCondition_set_enabled_statuses(leftUser, DDS_LIVELINESS_CHANGED_STATUS); checkStatus(status, "DDS_StatusCondition_set_enabled_statuses"); /* Create a bare guard which will be used to close the room */ escape = DDS_GuardCondition__alloc(); checkHandle(escape, "DDS_GuardCondition__alloc"); /* Create a waitset and add the ReadConditions */ userLoadWS = DDS_WaitSet__alloc(); checkHandle(userLoadWS, "DDS_WaitSet__alloc"); status = DDS_WaitSet_attach_condition(userLoadWS, newUser); checkStatus(status, "DDS_WaitSet_attach_condition (newUser)"); status = DDS_WaitSet_attach_condition(userLoadWS, leftUser); checkStatus(status, "DDS_WaitSet_attach_condition (leftUser)"); status = DDS_WaitSet_attach_condition(userLoadWS, escape); checkStatus(status, "DDS_WaitSet_attach_condition (escape)"); /* Initialize and pre-allocate the GuardList used to obtain the triggered Conditions. */ guardList = DDS_ConditionSeq__alloc(); checkHandle(guardList, "DDS_ConditionSeq__alloc"); guardList->_maximum = 3; guardList->_length = 0; guardList->_buffer = DDS_ConditionSeq_allocbuf(3); checkHandle(guardList->_buffer, "DDS_ConditionSeq_allocbuf"); /* Remove all known Users that are not currently active. */ status = Chat_NameServiceDataReader_take( nameServer, &nsList, &infoSeq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_NOT_ALIVE_INSTANCE_STATE); checkStatus(status, "Chat_NameServiceDataReader_take"); status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq); checkStatus(status, "Chat_NameServiceDataReader_return_loan"); /* Start the sleeper thread. */ pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); pthread_create (&tid, &tattr, delayedEscape, NULL); pthread_attr_destroy(&tattr); while (!closed) { /* Wait until at least one of the Conditions in the waitset triggers. */ status = DDS_WaitSet_wait(userLoadWS, guardList, &timeout); checkStatus(status, "DDS_WaitSet_wait"); /* Walk over all guards to display information */ for (i = 0; i < guardList->_length; i++) { guard = guardList->_buffer[i]; if (guard == newUser) { /* The newUser ReadCondition contains data */ status = Chat_NameServiceDataReader_read_w_condition( nameServer, &nsList, &infoSeq, DDS_LENGTH_UNLIMITED, newUser); checkStatus(status, "Chat_NameServiceDataReader_read_w_condition"); for (j = 0; j < nsList._length; j++) { printf ("New user: %s\n", nsList._buffer[j].name); } status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq); checkStatus(status, "Chat_NameServiceDataReader_return_loan"); } else if (guard == leftUser) { /* Some liveliness has changed (either a DataWriter joined or a DataWriter left) */ status = DDS_DataReader_get_liveliness_changed_status(loadAdmin, &livChangStatus); checkStatus(status, "DDS_DataReader_get_liveliness_changed_status"); if (livChangStatus.alive_count < prevCount) { /* A user has left the ChatRoom, since a DataWriter lost its liveliness */ /* Take the effected users so tey will not appear in the list later on. */ status = Chat_NameServiceDataReader_take( nameServer, &nsList, &infoSeq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_NOT_ALIVE_NO_WRITERS_INSTANCE_STATE); checkStatus(status, "Chat_NameServiceDataReader_take"); for (j = 0; j < nsList._length; j++) { /* re-apply query arguments */ sprintf(args._buffer[0], "%d", nsList._buffer[j].userID); status = DDS_QueryCondition_set_query_parameters(singleUser, &args); checkStatus(status, "DDS_QueryCondition_set_query_parameters"); /* Read this users history */ status = Chat_ChatMessageDataReader_take_w_condition( loadAdmin, &msgList, &infoSeq2, DDS_LENGTH_UNLIMITED, singleUser); checkStatus(status, "Chat_ChatMessageDataReader_take_w_condition"); /* Display the user and his history */ printf ( "Departed user %s has sent %d messages\n", nsList._buffer[j].name, msgList._length); status = Chat_ChatMessageDataReader_return_loan(loadAdmin, &msgList, &infoSeq2); checkStatus(status, "Chat_ChatMessageDataReader_return_loan"); } status = Chat_NameServiceDataReader_return_loan(nameServer, &nsList, &infoSeq); checkStatus(status, "Chat_NameServiceDataReader_return_loan"); } prevCount = livChangStatus.alive_count; } else if (guard == escape) { printf ("UserLoad has terminated.\n"); closed = 1; } else { assert(0); }; } /* for */ } /* while (!closed) */ /* Remove all Conditions from the WaitSet. */ status = DDS_WaitSet_detach_condition(userLoadWS, escape); checkStatus(status, "DDS_WaitSet_detach_condition (escape)"); status = DDS_WaitSet_detach_condition(userLoadWS, leftUser); checkStatus(status, "DDS_WaitSet_detach_condition (leftUser)"); status = DDS_WaitSet_detach_condition(userLoadWS, newUser); checkStatus(status, "DDS_WaitSet_detach_condition (newUser)"); /* Free all resources */ DDS_free(guardList); DDS_free(args._buffer); DDS_free(userLoadWS); DDS_free(escape); DDS_free(setting_topic_qos); DDS_free(reliable_topic_qos); DDS_free(nameServiceTypeName); DDS_free(chatMessageTypeName); DDS_free(nameServiceTS); DDS_free(chatMessageTS); status = DDS_DomainParticipant_delete_contained_entities(participant); checkStatus(status, "DDS_DomainParticipant_delete_contained_entities"); status = DDS_DomainParticipantFactory_delete_participant( DDS_TheParticipantFactory, participant); checkStatus(status, "DDS_DomainParticipantFactory_delete_participant"); return 0; }
int main (int argc, char ** argv) #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[]) { /* 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; }