playFile Example

The playFile program creates a logger, opens a logger tape, and plays it back.

Periodic messages output the current tape time. If 'q' is pressed playback stops and the program exits\ immediately, otherwise it will continue to the end of the tape at which point it will exit.


The main application:

/*******************************************************************************
** Copyright (c) 1992-2010 VT MAK
** All rights reserved.
*******************************************************************************/

#include <absoluteTime.h>
#include <lgrPlaybackInterface.h>
#include <lgrPrint.h>
#include <lgrSimInterface.h>

#include <vlutil/vlProcessControl.h>
#include <vlutil/vlPrint.h>
#include <cmdLine/cmdLine.h>
#include <lgrVersion.h>

#if DtDIS
#include <vl/dExConnInit.h>
#include <disLogger.h>
#endif

#if DtHLA
#include <vl/hExConnInit.h>
#include <hlaLogger.h>
#endif

using namespace MAKLogger;

// Hard coded paths for files. Relative to the bin directories.
#if DtDIS
const char defaultFileName[] = "..\\logger_tapes\\maklanddemoDIS.lgr";
#elif DtHLA_1516_EVOLVED
const char defaultFileName[] = "..\\logger_tapes\\maklanddemoHLA1516e_vrlink_evolved_xml.lgr";
#elif DtHLA_1516
const char defaultFileName[] = "..\\logger_tapes\\maklanddemoHLA1516_vrlink_xml.lgr";
#else
const char defaultFileName[] = "..\\logger_tapes\\maklanddemoHLA13.lgr";
#endif

bool keyCheck(const char* keysToCheck)
{
   if(DtKbHit())
   {
      char c = DtGetCh();
      return strchr(keysToCheck,c) != 0;
   }
   return false;
}


int playFile(int argc, char** argv)
{
   DtCmdLine::CmdLine cmdLine("writeFile","Example of how to write a logger file.",LOGGER_VERSION_STRING);
   DtCmdLine::UnlabeledMultiArg<DtString> filenameArg("Filename","The name of the logger file to write.","string");
   cmdLine.add(&filenameArg);
   cmdLine.parse(argc,argv);

   std::vector<DtString> values = filenameArg.getValue();
   if(values.size() == 0)
   {
      values.push_back(defaultFileName);
   }
   DtString filename(values.front());

   try
   {

   // Create the logger based upon it's protocol. These helped sub classes
   // automatically register the protocol and provide additional utility
   // functions.
#if DtDIS
   // Create a DIS logger
   DtDisLogger* aLogger = DtDisLogger::create(true);
#endif

#if DtHLA
   // Create a HLA logger
   DtHlaLogger* aLogger = DtHlaLogger::create(true);
#endif

   // Initialize logger with default settings.
   DtExerciseConnInitializer exConnInit;
#if DtHLA
#if DtHLA_1516_EVOLVED
   exConnInit.setFedFileName("vr-link_evolved.xml");
#elif DtHLA_1516
   exConnInit.setFedFileName("vr-link.xml");
#else
   exConnInit.setFedFileName("vr-link.fed");
#endif
#endif
   aLogger->connect(exConnInit);

   DtLgrSimInterface* simInterface = DtLgrSimInterface::create(*aLogger);
   aLogger->tick();
   simInterface->update();
   if(!simInterface->isConnected())
   {
      DtInfo << "Unable to connect." << std::endl;
      return 1;
   }
   delete simInterface;

   // Create Playback interface for controlling playback.
   DtLgrPlaybackInterface* pbInterface = DtLgrPlaybackInterface::create(*aLogger);
   if(!pbInterface)
   {
      DtInfo << "Error: Unable to create playback interface." << std::endl;
      return 1;
   }

   // Tell the logger to open a file.
   pbInterface->openFile(filename);
   // Process Commands
   aLogger->tick();
   // Update interface
   pbInterface->update();

   // Check to see if file was loaded.
   if(!pbInterface->isFileLoaded())
   {
      DtInfo << "Unable to open file: " << filename << "." << std::endl;
      return 1;
   }

   // Tell the logger to play.
   pbInterface->play();
   // Process Commands
   aLogger->tick();
   // Update interface
   pbInterface->update();

   // Check to see if playback was started.
   if(!pbInterface->isPlaying())
   {
      DtInfo << "Unable to play file." << std::endl;
      return 1;
   }

   // The number of seconds between print outs.
   double printDelay = 1.0;
   double printLoopTotal = 0;

   // The time of a loop.
   double loopDelay = .1;

   DtInfo << "Playing file: " << filename << "." << std::endl;
   DtInfo << "Press 'q' or 'enter' to quit." << std::endl;

   // Execution loop.
   while(pbInterface->isPlaying())
   {
      // Tick logger
      aLogger->tick();
      // Update Interface
      pbInterface->update();

      if(printLoopTotal >= printDelay)
      {
         DtInfo << "Current Time = " << pbInterface->currentTime() << "." << std::endl;
         printLoopTotal = 0.0;
      }
      printLoopTotal += loopDelay;
      DtSleep(loopDelay);
      // Break out if q or enter was pressed.
      if(keyCheck("q\r")) break;
   }

   // Check to see if the logger is still playing.
   if(pbInterface->isPlaying())
   {
      DtInfo << "Stopping playback." << std::endl;
      pbInterface->stop();
      // Make the logger stop.
      aLogger->tick();
   }

   DtInfo << "Finished playing file." << std::endl;
   DtRestoreKbHit();

   delete pbInterface;
   delete aLogger;

   }
   catch (DtException& ex)
   {
      DtString output = DtSerializeException(ex);
      DtInfo << output << std::endl;
      return 0;
   }

   return 0;
}

int main(int argc, char** argv)
{
   return playFile(argc,argv);
}

Document ID: Generated on Tue Oct 11 19:41:03 EDT 2011 from SVN revision 107422
Copyright © 2005-2011 VT MÄK Inc. All Rights Reserved (www.mak.com)