Пример #1
0
int main(int argc, char *argv[])
{
   c_bool isTransient, isPersistent;
   c_bool isClosed = FALSE;
   unsigned long i, j;
   os_time os_delay2000 = { 2, 0 };

   DDS_sequence_DurabilityData_Msg* DurabilityData_Msg_Seq = DDS_sequence_DurabilityData_Msg__alloc();
   DDS_SampleInfoSeq* DurabilityData_infoSeq = DDS_SampleInfoSeq__alloc();

   if( argc < 2 )
   {
      usage();
   }

   isTransient = (strcmp(argv[1], "transient") == 0) ? TRUE : FALSE;
   isPersistent = (strcmp(argv[1], "persistent") == 0) ? TRUE : FALSE;
   if( ! (isTransient || isPersistent) )
   {
      usage();
   }

   g_durability_kind = (char*) argv[1];

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

   // Register the Topic's type in the DDS Domain.
   g_MsgTypeSupport = DurabilityData_MsgTypeSupport__alloc();
   checkHandle(g_MsgTypeSupport, "DurabilityData_MsgTypeSupport__alloc");
   registerType(g_MsgTypeSupport);
   // Create the Topic's in the DDS Domain.
   g_MsgTypeName = (char*) DurabilityData_MsgTypeSupport_get_type_name(g_MsgTypeSupport);
   createTopic("DurabilityData_Msg", g_MsgTypeName);
   DDS_free(g_MsgTypeName);
   DDS_free(g_MsgTypeSupport);

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

   // Request a Reader from the the Subscriber.
   createReader();

   printf("=== [Subscriber] Ready ...");

   // Added a max iteration threshold in order to avoid looping infinitely.
   // This is in the case of "persistent" + auto_dispose == TRUE,
   // if the user tries to use persistence feature, it won't succeed:
   // with this setting value, the Instance is still deleted upon the delete of the Writer,
   // even though the persistent setting has been passed on both processes.
   i = 0;
   do
   {
      g_status = DurabilityData_MsgDataReader_take(
          g_DataReader,
          DurabilityData_Msg_Seq,
          DurabilityData_infoSeq,
          DDS_LENGTH_UNLIMITED,
          DDS_ANY_SAMPLE_STATE,
          DDS_ANY_VIEW_STATE,
          DDS_ANY_INSTANCE_STATE );

      checkStatus(g_status, "DurabilityData_MsgDataReader_take");

      if( DurabilityData_Msg_Seq->_length > 0 )
      {
         j = 0;
         do
         {
            if( DurabilityData_infoSeq->_buffer[j].valid_data )
            {
               printf("\n%s", DurabilityData_Msg_Seq->_buffer[j].content);
               if( strcmp(DurabilityData_Msg_Seq->_buffer[j].content, "9") == 0 )
               {
                  isClosed = TRUE;
               }
            }
         }
         while( ++j < DurabilityData_Msg_Seq->_length );

         DurabilityData_MsgDataReader_return_loan (g_DataReader, DurabilityData_Msg_Seq, DurabilityData_infoSeq);
      }
      os_nanoSleep(os_delay2000);
   }
   while( isClosed == FALSE && i++ < 5);

   // Cleanup DDS from the created Entities.
   deleteDataReader();
   deleteSubscriber();
   deleteTopic();
   deleteParticipant();

   // Cleanup C allocations, recursively freeing the allocated structures and sequences using the OpenSplice API.
   DDS_free(DurabilityData_Msg_Seq);
   DDS_free(DurabilityData_infoSeq);

   // Print out an empty line, just to let behind a clean new line for the shell..
   printf("\n");
   return 0;
}
int OSPL_MAIN (int argc, char *argv[])
{
   DDS_Subscriber QueryConditionDataSubscriber;
   DDS_DataReader QueryConditionDataDataReader;
   StockMarket_Stock* QueryConditionDataSample;
   DDS_sequence_StockMarket_Stock* msgList = DDS_sequence_StockMarket_Stock__alloc();
   DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc();
   c_bool isClosed = FALSE;
   unsigned long j;
   DDS_char* QueryConditionDataToSubscribe;
   DDS_char *query_string = "ticker=%0";
   DDS_QueryCondition queryCondition;
   os_time delay_200ms = { 0, 200000000 };
   int count = 0;

   // usage : QueryConditionSubscriber <topic's content filtering string>
   if( argc < 2 )
   {
      usage();
   }
   printf("\n\n Starting QueryConditionSubscriber...");
   printf("\n\n Parameter is \"%s\"", argv[1]);
   QueryConditionDataToSubscribe = argv[1];

   createParticipant("QueryCondition example");

   // Register Stock Topic's type in the DDS Domain.
   g_StockTypeSupport = (DDS_TypeSupport) StockMarket_StockTypeSupport__alloc();
   checkHandle(g_StockTypeSupport, "StockMarket_StockTypeSupport__alloc");
   registerStockType(g_StockTypeSupport);
   // Create Stock Topic in the DDS Domain.
   g_StockTypeName = StockMarket_StockTypeSupport_get_type_name(g_StockTypeSupport);
   g_StockTopic = createTopic("StockTrackerExclusive", g_StockTypeName);
   DDS_free(g_StockTypeName);
   DDS_free(g_StockTypeSupport);

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

   // Request a Reader from the the Subscriber.
   QueryConditionDataDataReader = createDataReader(QueryConditionDataSubscriber, g_StockTopic);

   // Create QueryCondition
   printf( "\n=== [QueryConditionSubscriber] Query : ticker = %s\n", QueryConditionDataToSubscribe );

   queryCondition = createQueryCondition(QueryConditionDataDataReader, query_string, (DDS_char*) QueryConditionDataToSubscribe);


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

   do
   {
      g_status = StockMarket_StockDataReader_take_w_condition(QueryConditionDataDataReader,
               msgList,
               infoSeq,
               DDS_LENGTH_UNLIMITED,
               queryCondition);
      checkStatus(g_status, "StockMarket_StockDataReaderView_take");
      if( msgList->_length > 0 )
      {
         j = 0;
         do
         {
            QueryConditionDataSample = &msgList->_buffer[j];
            // When is this supposed to happen?
            // Needs comment...
            if( infoSeq->_buffer[j].valid_data )
            {
               printf("\n\n %s: %f", QueryConditionDataSample->ticker, QueryConditionDataSample->price);
               if( QueryConditionDataSample->price == (DDS_float) -1.0f )
               {
                  printf("\n ===[QueryConditionSubscriber] QueryConditionDataSample->price == -1.0f ");
                  isClosed = TRUE;
               }
            }
         }
         while( ++j < msgList->_length );

         g_status = StockMarket_StockDataReader_return_loan(QueryConditionDataDataReader, msgList, infoSeq);
         checkStatus(g_status, "StockMarket_StockDataReaderView_return_loan");
      }
      os_nanoSleep(delay_200ms);
      ++count;
   }
   while( isClosed == FALSE && count < 1500 );

   printf("\n\n=== [QueryConditionSubscriber] Market Closed");

   // Cleanup DDS from the created Entities.

   deleteQueryCondition(QueryConditionDataDataReader, queryCondition);
   deleteDataReader(QueryConditionDataSubscriber, QueryConditionDataDataReader);
   deleteSubscriber(QueryConditionDataSubscriber);
   deleteTopic(g_StockTopic);
   deleteParticipant();

   // Cleanup C allocations,
   // recursively freeing the allocated structures and sequences using the OpenSplice API.
   DDS_free(msgList);
   DDS_free(infoSeq);

   // Print out an empty line, just to let behind a clean new line for the shell..
   printf("\n\r");
   return 0;
}
Пример #3
0
int main(int argc, char *argv[])
{
   DDS_sequence_HelloWorldData_Msg* message_seq = DDS_sequence_HelloWorldData_Msg__alloc();
   DDS_SampleInfoSeq* message_infoSeq = DDS_SampleInfoSeq__alloc();
   DDS_Subscriber message_Subscriber;
   DDS_DataReader message_DataReader;
   c_bool isClosed = FALSE;
   int count = 0;
   os_time os_delay200 = { 0, 200000000 };

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

   // Register the Topic's type in the DDS Domain.
   g_MessageTypeSupport = HelloWorldData_MsgTypeSupport__alloc();
   checkHandle(g_MessageTypeSupport, "HelloWorldData_MsgTypeSupport__alloc");
   registerMessageType(g_MessageTypeSupport);
   // Create the Topic's in the DDS Domain.
   g_MessageTypeName = (char*) HelloWorldData_MsgTypeSupport_get_type_name(g_MessageTypeSupport);
   g_MessageTopic = createTopic("HelloWorldData_Msg", g_MessageTypeName);
   DDS_free(g_MessageTypeName);
   DDS_free(g_MessageTypeSupport);

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

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

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

   do
   {
      g_status = HelloWorldData_MsgDataReader_take(
          message_DataReader,
          message_seq,
          message_infoSeq,
          1,
          DDS_ANY_SAMPLE_STATE,
          DDS_ANY_VIEW_STATE,
          DDS_ANY_INSTANCE_STATE );
      checkStatus(g_status, "HelloWorldData_MsgDataReader_take");

      if( message_seq->_length > 0 && message_infoSeq->_buffer[0].valid_data )
      {
         isClosed = TRUE;
         printf("\n=== [Subscriber] message received :" );
         printf( "\n    userID  : %d", message_seq->_buffer[0].userID );
         printf( "\n    Message : \"%s\"\n", message_seq->_buffer[0].message );
         HelloWorldData_MsgDataReader_return_loan (message_DataReader, message_seq, message_infoSeq);
      }

      if(isClosed == FALSE)
      {
         os_nanoSleep(os_delay200);
         ++count;
      }
   }
   while( isClosed == FALSE && count < 1500 );

   os_nanoSleep(os_delay200);

   // Cleanup DDS from the created Entities.
   deleteDataReader(message_Subscriber, message_DataReader);
   deleteSubscriber(message_Subscriber);
   deleteTopic(g_MessageTopic);
   deleteParticipant();


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

   return 0;
}
int main(int argc, char *argv[])
{
   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;
}
Пример #5
0
int main(int argc, char *argv[])
{

   DDS_sequence_LifecycleData_Msg* msgSeq = DDS_sequence_LifecycleData_Msg__alloc();
   DDS_SampleInfoSeq* msgInfoSeq = DDS_SampleInfoSeq__alloc();
   DDS_Subscriber msgSubscriber;
   DDS_DataReader msgDataReader;

   unsigned long j, inputChar;
   os_time os_delay20ms = (os_time)  {0, 20000000};
   os_time os_delay200ms = (os_time) {0, 200000000};

   // Force the output to be unbuffered.
   setbuf(stdout, (char *) 0);

   // First initialize the Topics and their DDS Entities

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

   //------------------ Msg topic --------------------//
   // Register Msg Topic's type in the DDS Domain.
   g_msgTypeSupport = LifecycleData_MsgTypeSupport__alloc();
   checkHandle(g_msgTypeSupport, "LifecycleData_MsgTypeSupport__alloc");
   registerMsgType(g_msgTypeSupport);
   // Create Msg Topic in the DDS Domain.
   g_msgTypeName = (char*) LifecycleData_MsgTypeSupport_get_type_name(g_msgTypeSupport);
   g_msgTopic = createTopic("Lifecycle_Msg", g_msgTypeName);
   DDS_free(g_msgTypeName);
   DDS_free(g_msgTypeSupport);

   // Create the Publisher's in the DDS Domain.
   msgSubscriber = createSubscriber();

   // Request a DataReader from the the Subscriber.
   msgDataReader = createDataReader(msgSubscriber, g_msgTopic);
   //End initialization.

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

   msgInfoSeq->_length = 0;
   DDS_boolean closed = FALSE;
   int nbIter = 1;
   int nbIterMax = 100;
   while ((closed == FALSE) && (nbIter < nbIterMax))
   {
      g_status = LifecycleData_MsgDataReader_read(msgDataReader, msgSeq, msgInfoSeq, 1, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
      checkStatus(g_status, "LifecycleData_MsgDataReader_read");

      if( msgInfoSeq->_length > 0 )
      {
         if( msgInfoSeq->_buffer[0].valid_data == TRUE )
         {
            printf("\n\n Message        : %s", msgSeq->_buffer[0].message);
            printf("\n WriterStates : %s", msgSeq->_buffer[0].writerStates);
            printf("\n valid_data     : %d", msgInfoSeq->_buffer[0].valid_data);
            printf("\n sample_state:%s-view_state:%s-instance_state:%s\n", 
	   		       sSampleState[getIndex(msgInfoSeq->_buffer[0].sample_state)],
			       sViewState[getIndex(msgInfoSeq->_buffer[0].view_state)],
			       sInstanceState[getIndex(msgInfoSeq->_buffer[0].instance_state)]);   
         }
         closed = strcmp(msgSeq->_buffer[0].writerStates, "STOPPING_SUBSCRIBER") == 0 ? TRUE : FALSE;
         printf("=== closed=%d - nbIter %d\n", closed, nbIter);
         g_status = LifecycleData_MsgDataReader_return_loan(msgDataReader, msgSeq, msgInfoSeq);
         checkStatus(g_status, "LifecycleData_MsgDataReader_return_loan");
         // Useless to harass the system, so we wait before to retry.
         os_nanoSleep(os_delay200ms);
         msgInfoSeq->_length = 0;
         nbIter++;
      }
   }
   printf("\n=== [Subscriber] stopping after %d iterations ..\n", nbIter);
   if (nbIter == nbIterMax) printf ("*** Error : max %d iterations reached", nbIterMax);
   // Cleanup DDS from the created Entities.
   deleteDataReader(msgSubscriber, msgDataReader);
   deleteSubscriber(msgSubscriber);
   deleteTopic(g_msgTopic);
   deleteParticipant();

   // Cleanup C allocations, recursively freeing the allocated structures and sequences using the OpenSplice API.
   DDS__free(msgSeq);
   DDS__free(msgInfoSeq);

   return 0;
}
Пример #6
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;
}
Пример #7
0
int OSPL_MAIN (int argc, char *argv[])
{
   DDS_sequence_WaitSetData_Msg* message_seq = DDS_sequence_WaitSetData_Msg__alloc();
   DDS_SampleInfoSeq* message_infoSeq = DDS_SampleInfoSeq__alloc();
   DDS_Subscriber message_Subscriber;
   DDS_DataReader message_DataReader;
   DDS_char* query_parameter = "Hello again";
   DDS_char *query_string = "message=%0";
   DDS_ReadCondition readCondition;
   DDS_QueryCondition queryCondition;
   DDS_StatusCondition statusCondition;
   DDS_WaitSet waitSet;
   DDS_GuardCondition escape;
   DDS_LivelinessChangedStatus livelinessChangedStatus;
   DDS_ConditionSeq *guardList = NULL;
   DDS_Duration_t timeout = { 20, 0 };
   int count = 0;

   DDS_long previousLivelinessCount;
   c_bool isClosed = FALSE;
   c_bool isEscaped = FALSE;
   c_bool writerLeft = FALSE;
   unsigned long i, j;

   // Create DDS DomainParticipant
   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 Subscriber's in the DDS Domain.
   message_Subscriber = createSubscriber();

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

   // 1- Create a ReadCondition that will contain new Msg only.
   readCondition = DDS_DataReader_create_readcondition(message_DataReader, DDS_NOT_READ_SAMPLE_STATE, DDS_NEW_VIEW_STATE, DDS_ALIVE_INSTANCE_STATE);
   checkHandle(readCondition, "DDS_DataReader_create_readcondition (newUser)");

   // 2- Create QueryCondition.
   queryCondition = createQueryCondition(message_DataReader, query_string, (DDS_char*) query_parameter);
   printf("=== [WaitSetDataSubscriber] Query : message = \"%s\"", query_parameter);

   // 3- Obtain a StatusCondition associated to a Writer
   // and that triggers only when the Writer changes Liveliness.
   statusCondition = DDS_DataReader_get_statuscondition(message_DataReader);
   checkHandle(statusCondition, "DDS_DataReader_get_statuscondition");
   g_status = DDS_StatusCondition_set_enabled_statuses(statusCondition, DDS_LIVELINESS_CHANGED_STATUS);
   checkStatus(g_status, "DDS_StatusCondition_set_enabled_statuses");

   // 4- Create a GuardCondition which will be used to close the subscriber.
   escape = DDS_GuardCondition__alloc();
   checkHandle(escape, "DDS_GuardCondition__alloc");

   // Create a waitset and add the 4 Conditions created above :
   // ReadCondition, QueryCondition, StatusCondition, GuardCondition.
   waitSet = DDS_WaitSet__alloc();
   checkHandle(waitSet, "DDS_WaitSet__alloc");
   g_status = DDS_WaitSet_attach_condition(waitSet, readCondition);
   checkStatus(g_status, "DDS_WaitSet_attach_condition (readCondition)");
   g_status = DDS_WaitSet_attach_condition(waitSet, queryCondition);
   checkStatus(g_status, "DDS_WaitSet_attach_condition (queryCondition)");
   g_status = DDS_WaitSet_attach_condition(waitSet, statusCondition);
   checkStatus(g_status, "DDS_WaitSet_attach_condition (statusCondition)");
   g_status = DDS_WaitSet_attach_condition(waitSet, escape);
   checkStatus(g_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 = 4;
   guardList->_length = 0;
   guardList->_buffer = DDS_ConditionSeq_allocbuf(4);
   checkHandle(guardList->_buffer, "DDS_ConditionSeq_allocbuf");

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

   // Used to store the livelinessChangedStatus.alive_count value (StatusCondition).
   previousLivelinessCount = 0;
   do
   {
      // Wait until at least one of the Conditions in the waitset triggers.
      g_status = DDS_WaitSet_wait(waitSet, guardList, &timeout);

      if (g_status == DDS_RETCODE_OK) {
         i = 0;
         do
         {
            if( guardList->_buffer[i] == statusCondition )
            {
               // StatusCondition triggered.
               // Some liveliness has changed (either a DataWriter joined or a DataWriter left).
               // Check whether a new Writer appeared.
               g_status = DDS_DataReader_get_liveliness_changed_status(message_DataReader, &livelinessChangedStatus);
               checkStatus(g_status, "DDS_DataReader_get_liveliness_changed_status");

               if( livelinessChangedStatus.alive_count > previousLivelinessCount )
               {
                  // a DataWriter joined.
                  printf("\n\n!!! a MsgWriter joined");
                  g_status = DDS_StatusCondition_set_enabled_statuses(statusCondition, DDS_LIVELINESS_CHANGED_STATUS);
                  checkStatus(g_status, "DDS_StatusCondition_set_enabled_statuses");
               }
               else if( livelinessChangedStatus.alive_count < previousLivelinessCount )
               {
                  // A MsgWriter lost its liveliness.
                  printf("\n\n!!! a MsgWriter lost its liveliness");
                  g_status = DDS_StatusCondition_set_enabled_statuses(statusCondition, DDS_LIVELINESS_CHANGED_STATUS);
                  checkStatus(g_status, "DDS_StatusCondition_set_enabled_statuses");
                  writerLeft = TRUE;
                  printf("\n === Triggering escape condition");
                  g_status = DDS_GuardCondition_set_trigger_value(escape, TRUE);
                  checkStatus(g_status, "DDS_GuardCondition_set_trigger_value");
               }
               else if ( livelinessChangedStatus.alive_count == previousLivelinessCount )
               {
                  // The status has changed, though alive_count is still zero,
                  // this implies that both events have occurred,
                  // and that the value is back to zero already.
                  printf("\n\n!!! a MsgWriter joined\n");
                  printf("\n\n!!! a MsgWriter lost its liveliness\n");
                  writerLeft = TRUE;
                  printf("\n === Triggering escape condition");
                  g_status = DDS_GuardCondition_set_trigger_value(escape, TRUE);
                  checkStatus(g_status, "DDS_GuardCondition_set_trigger_value");

                  // NB: we do not need to activate this Condition anymore;
                  // both events occurred.
               }
               // NB: Here we record the count a in order to compare it next time this condition triggers (?).
               previousLivelinessCount = livelinessChangedStatus.alive_count;

            }
            else if( guardList->_buffer[i] == readCondition )
            {
               /* The newMsg ReadCondition contains data */
               g_status = WaitSetData_MsgDataReader_read_w_condition(message_DataReader, message_seq, message_infoSeq, DDS_LENGTH_UNLIMITED, readCondition);
               checkStatus(g_status, "WaitSetData_MsgDataReader_read_w_condition");

               for( j = 0; j < message_seq->_length ; j++ )
               {
                  printf("\n    --- New message received ---");
                  if( message_infoSeq->_buffer[j].valid_data == TRUE )
                  {
                     printf("\n    userID  : %d", message_seq->_buffer[j].userID);
                     printf("\n    Message : \"%s\"", message_seq->_buffer[j].message);
                  }
                  else
                  {
                     printf("\n    Data is invalid!");
                  }
               }
               fflush(stdout);
               g_status = WaitSetData_MsgDataReader_return_loan(message_DataReader, message_seq, message_infoSeq);
               checkStatus(g_status, "WaitSetData_MsgDataReader_return_loan");
            }
            else if( guardList->_buffer[i] == queryCondition )
            {
               /* The queryCond QueryCondition contains data  */
               g_status = WaitSetData_MsgDataReader_take_w_condition(message_DataReader, message_seq, message_infoSeq, DDS_LENGTH_UNLIMITED, queryCondition);
               checkStatus(g_status, "WaitSetData_MsgDataReader_take_w_condition");

               for( j = 0; j < message_seq->_length ; j++ )
               {
                  printf("\n\n    --- message received (with QueryCOndition on message field) ---");
                  if( message_infoSeq->_buffer[j].valid_data == TRUE )
                  {
                     printf("\n    userID  : %d", message_seq->_buffer[j].userID);
                     printf("\n    Message : \"%s\"", message_seq->_buffer[j].message);
                  }
                  else
                  {
                     printf("\n    Data is invalid!");
                  }
               }
               fflush(stdout);
               g_status = WaitSetData_MsgDataReader_return_loan(message_DataReader, message_seq, message_infoSeq);
               checkStatus(g_status, "WaitSetData_MsgDataReader_return_loan");
            }
            // This checks whether the Timeout Thread has resumed waiting.
            else if( guardList->_buffer[i] == escape )
            {
               printf("\n escape condition triggered! - count = %d\n ", count);
               isEscaped = TRUE;
               g_status = DDS_GuardCondition_set_trigger_value(escape, FALSE);
               checkStatus(g_status, "DDS_GuardCondition_set_trigger_value");
            }
         }
         while( ++i < guardList->_length );
      }
      else if (g_status != DDS_RETCODE_TIMEOUT) {
        // DDS_RETCODE_TIMEOUT is considered as an error
        // only after it has occurred count times
        checkStatus(g_status, "DDS_WaitSet_wait");
      } else {
        printf("!!! [INFO] WaitSet timedout - count = %d\n ", count);
      }

      ++count;
      isClosed = isEscaped && writerLeft;

   }   while( isClosed == FALSE && count < 20);
   if (count >= 20)  {
      printf( "*** Error : Timed out - count = %d ***\n", count);
   }
   /* Remove all Conditions from the WaitSet. */
   g_status = DDS_WaitSet_detach_condition(waitSet, escape);
   checkStatus(g_status, "DDS_WaitSet_detach_condition (escape)");
   g_status = DDS_WaitSet_detach_condition(waitSet, statusCondition);
   checkStatus(g_status, "DDS_WaitSet_detach_condition (statusCondition)");
   g_status = DDS_WaitSet_detach_condition(waitSet, queryCondition);
   checkStatus(g_status, "DDS_WaitSet_detach_condition (queryCondition)");
   g_status = DDS_WaitSet_detach_condition(waitSet, readCondition);
   checkStatus(g_status, "DDS_WaitSet_detach_condition (readCondition)");

   // Cleanup DDS from the created Entities.
   deleteQueryCondition(message_DataReader, readCondition);
   deleteQueryCondition(message_DataReader, queryCondition);
   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(guardList);
   DDS_free(message_seq);
   DDS_free(message_infoSeq);
   DDS_free(waitSet);
   DDS_free(escape);

   printf("\n");
   return 0;
}
int main(int argc, char *argv[])
{
   DDS_Subscriber OwnershipDataSubscriber;
   DDS_DataReader OwnershipDataDataReader;
   OwnershipData_Stock* OwnershipDataSample;
   DDS_InstanceHandle_t userHandle;
   DDS_sequence_OwnershipData_Stock* msgList = DDS_sequence_OwnershipData_Stock__alloc();
   DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc();
   c_bool isClosed;
   int count = 0;
   unsigned long j, userInput, timeOut;
   os_time os_delay200 = { 0, 200000000 };

   printf("\n\n Starting LifecycleSubscriber...");

   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 = (char*) OwnershipData_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.
   OwnershipDataSubscriber = createSubscriber();

   // Request a Reader from the the Subscriber.
   OwnershipDataDataReader = createDataReader(OwnershipDataSubscriber, g_StockTopic);

   printf("\n\n ===[OwnershipDataSubscriber] Ready...");
   printf("\n\n    Ticker   Price   Publisher   ownership strength");
   isClosed = FALSE;

   do
   {
      g_status = OwnershipData_StockDataReader_take(OwnershipDataDataReader, msgList, infoSeq, 1, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
      checkStatus(g_status, "OwnershipData_StockDataReaderView_take");
      if( msgList->_length > 0 )
      {
         j = 0;
         do
         {
            OwnershipDataSample = &msgList->_buffer[j];
            // When is this supposed to happen?
            // Needs comment...
            if( infoSeq->_buffer[j].valid_data )
            {
               printf("\n   %s %8.1f    %s        %d", OwnershipDataSample->ticker, OwnershipDataSample->price, OwnershipDataSample->publisher, OwnershipDataSample->strength);
               if( OwnershipDataSample->price == (DDS_float) -1.0f )
               {
                  printf("\n ===[OwnershipDataSubscriber] OwnershipDataSample->price == -1.0f ");
                  isClosed = TRUE;
               }
            }
         }
         while( ++j < msgList->_length );

         g_status = OwnershipData_StockDataReader_return_loan(OwnershipDataDataReader, msgList, infoSeq);
         checkStatus(g_status, "OwnershipData_StockDataReaderView_return_loan");
      }

      os_nanoSleep(os_delay200);
      ++count;
   }
   while( isClosed == FALSE && count < 1500 );

   printf("\n\n ===[OwnershipDataSubscriber] Market closed");

   // Cleanup DDS from the created Entities.
   deleteDataReader(OwnershipDataSubscriber, OwnershipDataDataReader);
   deleteSubscriber(OwnershipDataSubscriber);
   deleteTopic(g_StockTopic);
   deleteParticipant();

   // Cleanup C allocations,
   // recursively freeing the allocated structures and sequences using the OpenSplice API.
   DDS__free(msgList);
   DDS__free(infoSeq);

   // Print out an empty line, just to let behind a clean new line for the shell..
   printf("\n\r");
   return 0;
}