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;
}
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;
}
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_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 OSPL_MAIN (int argc, char *argv[])
{
   DDS_Subscriber QueryConditionDataSubscriber;
   DDS_DataReader QueryConditionDataDataReader;
   StockMarket_Stock* QueryConditionDataSample;
   DDS_sequence_StockMarket_Stock* msgList = DDS_sequence_StockMarket_Stock__alloc();
   DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc();
   c_bool isClosed = FALSE;
   unsigned long j;
   DDS_char* QueryConditionDataToSubscribe;
   DDS_char *query_string = "ticker=%0";
   DDS_QueryCondition queryCondition;
   os_time delay_200ms = { 0, 200000000 };
   int count = 0;

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

   createParticipant("QueryCondition example");

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

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

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

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

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


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

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

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

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

   // Cleanup DDS from the created Entities.

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

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

   // Print out an empty line, just to let behind a clean new line for the shell..
   printf("\n\r");
   return 0;
}
int main(int argc, char *argv[])
{
   char* publisher_name;
   int i, sampleIndex, nb_iteration;
   c_bool isStoppingSubscriber;
   float price;
   char* geTicker = "GE";
   int geTickerLength = strlen(geTicker);
   char* msftTicker = "MSFT";
   int msftTickerLength = strlen(msftTicker);
   os_time os_delay100 = { 0, 100000000 };
   os_time os_delay2000 =  { 2, 0 };

   DDS_Publisher QueryConditionDataPublisher;
   DDS_DataWriter QueryConditionDataDataWriter;
   QueryConditionData_Stock* geStockSample;
   QueryConditionData_Stock* msftStockSample;
   DDS_InstanceHandle_t geInstanceHandle;
   DDS_InstanceHandle_t msftInstancetHandle;

   createParticipant("QueryCondition example");

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

   // Create the Publisher's in the DDS Domain.
   QueryConditionDataPublisher = createPublisher();

   // Request a Writer from the the Publisher.
   QueryConditionDataDataWriter = createDataWriter(QueryConditionDataPublisher, g_StockTopic, FALSE);

   // Publish a Stock Sample reflecting the state of the Msg DataWriter.
   geStockSample = QueryConditionData_Stock__alloc();
   msftStockSample = QueryConditionData_Stock__alloc();

   msftStockSample->ticker = DDS_string_alloc(msftTickerLength);
   snprintf(msftStockSample->ticker, msftTickerLength + 1, "%s", msftTicker);
   msftStockSample->price = 12.00f;

   geStockSample->ticker = DDS_string_alloc(geTickerLength);
   snprintf(geStockSample->ticker, geTickerLength + 1, "%s", geTicker);
   geStockSample->price = 25.00f;

   geInstanceHandle = QueryConditionData_StockDataWriter_register_instance(QueryConditionDataDataWriter, geStockSample);
   msftInstancetHandle = QueryConditionData_StockDataWriter_register_instance(QueryConditionDataDataWriter, msftStockSample);   

   nb_iteration = 20;
   // The subscriber should display the prices sent by the publisher with the highest ownership strength
   for( sampleIndex = 0; sampleIndex < nb_iteration ; ++sampleIndex )
   {
      printf("GE : %.1f MSFT : %.1f\n", geStockSample->price, msftStockSample->price);
      writeStockSample(QueryConditionDataDataWriter, geInstanceHandle, geStockSample);
      writeStockSample(QueryConditionDataDataWriter, msftInstancetHandle, msftStockSample);
      geStockSample->price += 0.5;
      msftStockSample->price += 1.5;

      os_nanoSleep(os_delay100);
   }

   // This special price gives the end signal to the Subscriber:
   // signal to terminate
   geStockSample->price =  -1.0f;
   msftStockSample->price =  -1.0f;
   writeStockSample(QueryConditionDataDataWriter, geInstanceHandle, geStockSample);
   writeStockSample(QueryConditionDataDataWriter, msftInstancetHandle, msftStockSample);

   // This to make sure the Subscriber will get all the Samples.
   os_nanoSleep(os_delay2000);
   printf("Market Closed");

   QueryConditionData_StockDataWriter_unregister_instance (QueryConditionDataDataWriter, geStockSample, geInstanceHandle);
   QueryConditionData_StockDataWriter_unregister_instance (QueryConditionDataDataWriter, msftStockSample, msftInstancetHandle);

   // Cleanup DDS from the created Entities.
   deleteDataWriter(QueryConditionDataPublisher, QueryConditionDataDataWriter);
   deletePublisher(QueryConditionDataPublisher);
   deleteTopic(g_StockTopic);
   deleteParticipant();

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

   // Print out an empty line, just to let behind a clean new line for the shell..
   printf("\n\r");
   return 0;
}
int main(int argc, char *argv[])
{
   char* partition_name = "ContentFilteredTopic example";
   DDS_Subscriber contentFilteredTopicDataSubscriber;
   DDS_DataReader contentFilteredTopicDataDataReader;
   ContentFilteredTopicData_Stock* contentFilteredTopicDataSample;
   DDS_InstanceHandle_t userHandle;
   DDS_sequence_ContentFilteredTopicData_Stock* msgList = DDS_sequence_ContentFilteredTopicData_Stock__alloc();
   DDS_SampleInfoSeq* infoSeq = DDS_SampleInfoSeq__alloc();
   c_bool isClosed = FALSE;
   unsigned long j, userInput, timeOut;
   const char *filterValueToSubscribe;
   DDS_char* contentFilteredStockTopicName = "MyStockTopic";
   DDS_ContentFilteredTopic contentFilteredTopic;
   int filter_expressionLength;
   const char* filter_expressionPrefix = "ticker = '";
   const char* filter_expressionSuffix = "'";
   DDS_char *filter_expression;
   const DDS_StringSeq* filter_parameters = DDS_StringSeq__alloc();
   os_time delay_200ms = { 0, 200000000 };
   int count = 0;

   // usage : ContentFilteredTopicDataSubscriber <topic's content filtering string>
   if( argc < 2 )
   {
      usage();
   }
   printf("=== ContentFilteredTopicDataSubscriber");
   // Removed to comply with expected results
   //printf("\n\n Parameter is \"%s\"", argv[1]);
   filterValueToSubscribe = argv[1];

   createParticipant( partition_name );

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

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

   // create subscription filter

   filter_expressionLength = strlen(filter_expressionPrefix);
   filter_expressionLength += strlen(filterValueToSubscribe);
   filter_expressionLength += strlen(filter_expressionSuffix);
   filter_expression = DDS_string_alloc(filter_expressionLength);
   snprintf(filter_expression, filter_expressionLength + 1, "%s%s%s", filter_expressionPrefix, filterValueToSubscribe, filter_expressionSuffix);

   // create ContentFilteredTopic to retrieve only the samples with the given Stock.
   contentFilteredTopic = createContentFilteredTopic(contentFilteredStockTopicName, g_StockTopic, filter_expression, filter_parameters);
   // create Filtered DataReader

   printf( "\n=== [ContentFilteredTopicDataSubscriber] Subscription filter : %s", filter_expression );

   // Request a Reader from the the Subscriber.
   contentFilteredTopicDataDataReader = createContentFilteredDataReader(contentFilteredTopicDataSubscriber, contentFilteredTopic);

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

   do
   {
      g_status = ContentFilteredTopicData_StockDataReader_take(contentFilteredTopicDataDataReader, msgList, infoSeq, DDS_LENGTH_UNLIMITED, DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
      checkStatus(g_status, "ContentFilteredTopicData_StockDataReaderView_take");
      if( msgList->_length > 0 )
      {
         j = 0;
         do
         {
            contentFilteredTopicDataSample = &msgList->_buffer[j];            
            if( infoSeq->_buffer[j].valid_data )
            {
               if( contentFilteredTopicDataSample->price != (DDS_float) -1.0f )
               {
            	   int floatWidth = ( ((DDS_float) contentFilteredTopicDataSample->price) - ((long) contentFilteredTopicDataSample->price) ) ? 1 : 0;
                   printf("\n=== [ContentFilteredTopicDataSubscriber] receives stockQuote :  (%s, %.*f)", contentFilteredTopicDataSample->ticker, floatWidth, contentFilteredTopicDataSample->price);
               }
               else
               {            	   
                  isClosed = TRUE;
               }
            }
         }
         while( ++j < msgList->_length );

         g_status = ContentFilteredTopicData_StockDataReader_return_loan(contentFilteredTopicDataDataReader, msgList, infoSeq);
         checkStatus(g_status, "ContentFilteredTopicData_StockDataReader_return_loan");

         if(isClosed == FALSE)
         {
            os_nanoSleep(delay_200ms);
            ++count;
         }
      }
   }
   while( isClosed == FALSE && count < 1500); // We dont want the example to run indefinitely

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

   // Cleanup DDS from the created Entities.
   deleteDataReader(contentFilteredTopicDataSubscriber, contentFilteredTopicDataDataReader);
   deleteSubscriber(contentFilteredTopicDataSubscriber);
   deleteContentFilteredTopic(contentFilteredTopic);
   deleteTopic(g_StockTopic);
   deleteParticipant();

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

   // Print out an empty line, just to let behind a clean new line for the shell..
   // Removed to comply with expected results
   //printf("\n\r");
   return 0;
}
Example #8
0
int main(int argc, char *argv[])
{

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

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

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

   // First initialize the Topics and their DDS Entities

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

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

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

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

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

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

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

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

   return 0;
}
main(int argc, const char *argv[])
{
   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 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;
}
Example #11
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;
}
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 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;
}
Example #15
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;
}