예제 #1
0
int OSPL_MAIN (int argc, char *argv[])
{
   DDS_sequence_ListenerData_Msg* message_seq = DDS_sequence_ListenerData_Msg__alloc();
   DDS_SampleInfoSeq* message_infoSeq = DDS_SampleInfoSeq__alloc();
   DDS_Subscriber message_Subscriber;
   DDS_DataReader message_DataReader;
   DDS_WaitSet waitSet;
   DDS_ConditionSeq* guardList = NULL;
   struct DDS_DataReaderListener *message_Listener;
   DDS_Duration_t timeout = { 0, 200000000 };
   struct Listener_data* Listener_data;
   DDS_GuardCondition guardCondition = DDS_GuardCondition__alloc();
   int count = 0;
   DDS_StatusMask mask;

   // Create DDS DomainParticipant
   createParticipant("Listener example");

   // Register the Topic's type in the DDS Domain.
   g_MessageTypeSupport = ListenerData_MsgTypeSupport__alloc();
   checkHandle(g_MessageTypeSupport, "ListenerData_MsgTypeSupport__alloc");
   registerMessageType(g_MessageTypeSupport);
   // Create the Topic's in the DDS Domain.
   g_MessageTypeName = ListenerData_MsgTypeSupport_get_type_name(g_MessageTypeSupport);
   g_MessageTopic = createTopic("ListenerData_Msg", g_MessageTypeName);
   DDS_free(g_MessageTypeName);
   DDS_free(g_MessageTypeSupport);

   // Create the Subscriber's in the DDS Domain.
   message_Subscriber = createSubscriber();

   // Request a Reader from the the Subscriber.
   message_DataReader = createDataReader(message_Subscriber, g_MessageTopic);

   /* Allocate the DataReaderListener interface. */
   message_Listener = DDS_DataReaderListener__alloc();
   checkHandle(message_Listener, "DDS_DataReaderListener__alloc");

   Listener_data = malloc(sizeof(struct Listener_data));
   checkHandle(Listener_data, "malloc");
   Listener_data->guardCondition = &guardCondition;
   Listener_data->message_DataReader = &message_DataReader;
   Listener_data->isClosed = &isClosed;
   message_Listener->listener_data = Listener_data;
   message_Listener->on_data_available = on_data_available;
   message_Listener->on_requested_deadline_missed = on_requested_deadline_missed;

   mask =
            DDS_DATA_AVAILABLE_STATUS | DDS_REQUESTED_DEADLINE_MISSED_STATUS;
   g_status = DDS_DataReader_set_listener(message_DataReader, message_Listener, mask);
   checkStatus(g_status, "DDS_DataReader_set_listener");

   // WaitSet is used to avoid spinning in the loop below.
   waitSet = DDS_WaitSet__alloc();
   checkHandle(waitSet, "DDS_WaitSet__alloc");
   g_status = DDS_WaitSet_attach_condition(waitSet, guardCondition);
   checkStatus(g_status, "DDS_WaitSet_attach_condition (readCondition)");

   // Initialize and pre-allocate the GuardList used to obtain the triggered Conditions.
   guardList = DDS_ConditionSeq__alloc();
   checkHandle(guardList, "DDS_ConditionSeq__alloc");
   guardList->_maximum = 1;
   guardList->_length = 0;
   guardList->_buffer = DDS_ConditionSeq_allocbuf(1);
   checkHandle(guardList->_buffer, "DDS_ConditionSeq_allocbuf");

   printf("\n=== [ListenerDataSubscriber] Ready...");

   isClosed = FALSE;
   do
   {
      g_status = DDS_WaitSet_wait(waitSet, guardList, &timeout);
      if(g_status != DDS_RETCODE_TIMEOUT)
      {
          checkStatus(g_status, "DDS_WaitSet_wait");
      }
      else
      {
          printf("\n=== [ListenerDataSubscriber] (timeout)");
      }
      g_status = DDS_GuardCondition_set_trigger_value(guardCondition, FALSE);
      checkStatus(g_status, "DDS_GuardCondition_set_trigger_value");
      ++ count;
   }
   while( isClosed == FALSE && count < 1500 );

   printf("\n\n=== [ListenerDataSubscriber] isClosed");
   // Cleanup DDS from the created Entities.
   deleteDataReader(message_Subscriber, message_DataReader);
   deleteSubscriber(message_Subscriber);
   deleteTopic(g_MessageTopic);
   deleteParticipant();

   // Cleanup C allocations
   // Recursively free the instances sequence using the OpenSplice API.
   DDS_free(message_seq);
   DDS_free(message_infoSeq);
   free(Listener_data);

   // Print out an empty line, just to avoid the shell writing on the same line as our last output.
   printf("\n");
   return 0;
}
예제 #2
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;
}