Exemplo n.º 1
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 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;
}