writeFile Example

The main application:

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

#if DtDIS
#include <vl/disPduFactory.h>
#include <lgrDisMessageIoInfo.h>
#include <lgrDisRecordFile.h>
#include <vl/esPdu.h>
#include <lgrDisFileFormats.h>
#endif

#if DtHLA
#include <hlaMessageIOInfo.h>
#include <lgrHlaRecordFile.h>
#include <onlineFomDesc.h>
#include <vl/interaction.h>
#include <hlaMessageFactory.h>
#include <baseIntDesc.h>
#include <baseObjDesc.h>
#include <lgrHlaFileFormats.h>
#endif

#include <packet.h>
#include <messageBuffer.h>
#include <absoluteTime.h>
#include <loggerFileWriter.h>
#include <lgrPrint.h>
#include <cmdLine/cmdLine.h>
#include <lgrVersion.h>

using namespace MAKLogger;

#if DtDIS

void getMessages(DtMessageBuffer& messagesToWrite)
{
   // Normally this PDU would not be null.
   DtPdu* aPdu = 0;

   if(aPdu)
   {
      DtNetPacket* netPacket = reinterpret_cast<DtNetPacket*>(aPdu->packet());
      int packetSize = netPacket->DtLength;

      DtAbsoluteTime currentTime(0.0);
      DtPacket* packet = DtPacket::create(packetSize,currentTime,DtPacket::Type_Pdu);
      //  Copy PDU into buffer
      memcpy(packet->data(),netPacket,packetSize);

      messagesToWrite.add(packet);
   }
}


DtLoggerFileWriter* openFileForWriting( DtString filename )
{
   try
   {
      DtLgrDisFileFormats::registerFormats();

      // Ok lets open the file for playback.
      DtLgrDisRecordFile* file = DtLgrDisRecordFile::create(filename);

      // Necessary information (not really needed here but for completeness)
      DtLgrDisMessageIOInfo* ioInfo = DtLgrDisMessageIOInfo::create(DtMessageIOInfo::File,5);

      // Now construct a reader object to read the packets from the file.
      return DtLoggerFileWriter::create(ioInfo,file);
   }
   catch (DtException& ex)
   {
      DtString output = DtSerializeException(ex);
      DtInfo << output << std::endl;
      return 0;
   }

}

#endif

#if DtHLA

// A couple of global variables.
DtExerciseConn* theExerciseConn;
DtLgrOnlineFomDesc* theFom;

#if DtHLA_1516
typedef std::map<RTI::ObjectInstanceHandle,DtLgrHlaHandle> ObjectInstanceMap;
#else
typedef std::map<RTI::ObjectHandle,DtLgrHlaHandle> ObjectInstanceMap;
#endif
ObjectInstanceMap theObjectInstanceMap;
DtLgrHlaHandle theFreeId = 0;

DtPacket* encodeInteraction(const DtInteraction* interaction, const DtAbsoluteTime& time)
{
   DtLgrHandleValueReferenceMap map;

#if DtHLA_1516
   const DtBaseInterClassDesc* interDsc = 0;
   interDsc = theFom->interDesc(interaction->interactionClassHandle());
   if(!interDsc)
   {
      DtTHROW_NEW(DtInvalidFom,"Unknown interaction class handle.");
   }
   DtLgrValueMapFactory::convertRtiData(*theFom,*interDsc, interaction->phvm(),map);
   DtLgrHlaHandle classHandle = interDsc->handle();
   DtLgrValueReference tagRef((const char*)interaction->tag().data(),
      interaction->tag().size());
#else
   DtLgrValueMapFactory::convertRtiData(*theFom,interaction->phvps(),map);
   DtLgrHlaHandle classHandle = interaction->interactionClassHandle();
   DtLgrValueReference tagRef(interaction->tag().c_str(),interaction->tag().size());
#endif
   size_t size = DtLgrHlaMessageFactory::calculateInteractionSize(map,tagRef);
   DtPacket* packet = DtPacket::create(size,time,DtPacket::Type_Interaction);
   DtLgrHlaMessageFactory::encodeInteraction(*packet,classHandle, map,tagRef);
   return packet;
}

DtPacket* encodeUpdate(const DtStateMsg& msg, DtHlaObject* obj, const DtAbsoluteTime& time )
{
   DtLgrHandleValueReferenceMap mapRef;
   DtPacket* update = 0;

   ObjectInstanceMap::iterator handleIter = theObjectInstanceMap.find(msg.objectId());
   if(handleIter == theObjectInstanceMap.end())
   {
      theObjectInstanceMap[msg.objectId()] = ++theFreeId;
   }

#if DtHLA_1516
   DtLgrHlaHandle objectHandle = theObjectInstanceMap[msg.objectId()];
   DtLgrValueReference tagRef((const char*)msg.tag().data(),msg.tag().size());

   const DtBaseObjClassDesc* objDesc = theFom->objDesc(obj->classDesc().handle());
   if(!objDesc)
   {
      DtLgrPrinter::message(DtLgrPrinter::Warning,
         "Unable to find class handle of object instance.");
      return 0;
   }
   DtLgrValueMapFactory::convertRtiData(*theFom,*objDesc, msg.handleMap(),mapRef);
   update = DtLgrHlaMessageFactory::encodeUpdate(time,objectHandle,
      objDesc->handle(), mapRef,obj->name(),tagRef);

#else
   DtLgrHlaHandle objectHandle = msg.objectId();
   DtLgrValueReference tagRef(msg.tag().c_str(),msg.tag().size());
   DtLgrValueMapFactory::convertRtiData(&msg.handleMap(),mapRef);
   update = DtLgrHlaMessageFactory::encodeUpdate(time,objectHandle ,
      obj->objectClassHandle(), mapRef,obj->name(),tagRef);
#endif

   return update;
}

void getMessages(DtMessageBuffer& messagesToWrite)
{
   DtPacket* packet = 0;

   //Examples of how to get data from an online source.
   //DtInteraction* interaction = getInteraction();
   //DtPacket* packet = encodeInteraction(interaction,time);
   //packet = encodeUpdate(msg,obj,time);
   if(packet)
   {
      messagesToWrite.add(packet);
   }
}

DtLoggerFileWriter* openFileForWriting( DtString filename )
{
   try
   {
      DtLgrHlaFileFormats::registerFormats();

      //Get the FOM from the exercise connection.
      theFom = DtLgrOnlineFomDesc::create(theExerciseConn);

      // Ok lets open the file for playback.
      DtLgrHlaRecordFile* file = DtLgrHlaRecordFile::create(filename, *theFom);

      // Necessary information (not really needed here but for completeness)
      DtLgrHlaMessageIOInfo* ioInfo = DtLgrHlaMessageIOInfo::create(DtMessageIOInfo::File,theFom);

      // Now construct a reader object to read the packets from the file.
      return DtLoggerFileWriter::create(ioInfo,file);
   }
   catch (DtException& ex)
   {
      DtString output = DtSerializeException(ex);
      DtInfo << output << std::endl;
      return 0;
   }
}


#endif


void writeData( DtLoggerFileWriter* writer)
{

   // Create an empty message buffer.
   DtMessageBuffer aBufferOfMessages;

   // Get your messages.
   getMessages(aBufferOfMessages);

   //Output multiple messages more efficient then individual packets.
   writer->outputMessages(aBufferOfMessages);

   //Optional clear
   aBufferOfMessages.clear();
}


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

   DtString filename(filenameArg.getValue());

   DtLoggerFileWriter* writer = openFileForWriting(filename);

   if(writer)
   {
      writer->activate(0);
      writeData(writer);
      writer->deactivate();
      delete writer;
   }


   return 0;
}

// The application entry point
int main(int argc,char** argv)
{
   return writeFile(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)