Esempio n. 1
0
//------------------------------------------------------------------------------
// Read a record
//------------------------------------------------------------------------------
const DataRecordHandle* NetInput::readRecordImp()
{
   // First pass?  Does the file need to be opened?
   if (firstPassFlg) {
      if ( !networkInitialized ) {
         initNetworks();
      }
      firstPassFlg = false;
   }


   DataRecordHandle* handle = 0;

   // When the file is open and ready ...
   if ( networkInitialized && netHandler->isConnected() ) {

      // ---
      // Try to read a message into 'ibuf'
      // ---
      unsigned int n = 0;
      n = netHandler->recvData( ibuf, MAX_INPUT_BUFFER_SIZE );

      // ---
      // If we've successfully read a message from the network
      // then parse it as a DataRecord and put it into a Handle.
      // ---
      if (n > 0) {
         // Parse the data record
         std::string wireFormat(ibuf, n);
         Pb::DataRecord* dataRecord = new Pb::DataRecord();
         bool ok = dataRecord->ParseFromString(wireFormat);

         if (ok) {
            // Create a handle for the data record (it now has ownership)
            handle = new DataRecordHandle(dataRecord);
         }

         else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
            std::cerr << "NetInput::readRecord() -- ParseFromString() error" << std::endl;
            delete dataRecord;
            dataRecord = 0;
         }

      }
   }  
   return handle;
}
Esempio n. 2
0
//------------------------------------------------------------------------------
// Close the data file
//------------------------------------------------------------------------------
void FileWriter::closeFile()
{
   if (isOpen()) {

      // ---
      // Write the EOD message, if it hasn't already been.
      if (!eodFlag) {
         eodFlag = true;

         // write something to signify don't read any more (e.g., last message)
         Pb::DataRecord* lastMsg = new Pb::DataRecord();

         // This will be the token representing the last message, but it can be
         // anything that is not one of the other event messages
         lastMsg->set_id(REID_END_OF_DATA);

         // Time is also required, although not used:
         Pb::Time* time = lastMsg->mutable_time();
         time->set_exec_time(0);
         time->set_sim_time(0);
         time->set_utc_time(0);

         // get a handle
         DataRecordHandle* handle = new DataRecordHandle(lastMsg);

         // write the message
         processRecordImp(handle);
         handle->unref();
         handle = nullptr;
      }

      // now close the file
      sout->close();
      fileOpened = false;
      fileFailed = false;

   }
}
//------------------------------------------------------------------------------
// Read a record
//------------------------------------------------------------------------------
const DataRecordHandle* FileReader::readRecordImp()
{
   DataRecordHandle* handle = nullptr;

   // First pass?  Does the file need to be opened?
   if (firstPassFlg) {
      if ( !isOpen() && !isFailed() ) {
         openFile();
      }
      firstPassFlg = false;
   }


   // When the file is open and ready ...
   if ( isOpen() && !isFailed() && !sin->eof() ) {

      // Number of bytes in the next serialized DataRecord
      unsigned int n = 0;

      // ---
      // Read the size of the next serialized DataRecord
      // ---
      char nbuff[8];
      sin->read(nbuff, 4);

      // Check for error or eof
      if ( sin->eof() || sin->fail() ) {
         fileFailed = sin->fail();
         if (fileFailed && isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
            std::cerr << "FileReader::readRecord() -- error reading data record size" << std::endl;
         }
      }

      // Ok then get the size of the message from the buffer
      else {
         nbuff[4] = '\0';
         n = std::atoi(nbuff);
      }


      // ---
      // Read the serialized DataRecord from the file, parse it as a DataRecord
      // and put it into a Handle.
      // ---
      if (n > 0) {

         // Read message into ibuf
         sin->read(ibuf, n);

         // Check for error or eof
         if ( sin->eof() || sin->fail() ) {
            if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
               std::cerr << "FileReader::readRecord() -- error reading data record" << std::endl;
            }
            fileFailed = true;
         }

         // Ok, create the DataRecord with handle
         else {

            // Parse the DataRecord
            std::string wireFormat(ibuf, n);
            Pb::DataRecord* dataRecord = new Pb::DataRecord();
            bool ok = dataRecord->ParseFromString(wireFormat);

            // Create a handle for the DataRecord (it now has ownership)
            if (ok) {
               handle = new DataRecordHandle(dataRecord);
            }

            // parsing error
            else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
               std::cerr << "FileReader::readRecord() -- ParseFromString() error" << std::endl;
               delete dataRecord;
               dataRecord = nullptr;
            }
         }

      }

   }

   return handle;
}