MAK Data Logger API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
writeFile Example

The main application:

/*******************************************************************************
** Copyright (c) 1992-2025 MAK Technologies, Inc
** All rights reserved.
*******************************************************************************/
#if DtDIS
#include <vl/disPduFactory.h>
#include <vl/entityStatePdu.h>
#endif
#if DtHLA
#include <vl/exerciseConn.h>
#include <vl/exerciseConnInitializerHLA.h>
#include <vl/interaction.h>
#include <vlutil/vlRtiMismatchException.h>
#endif
#include <lgrCore/packet.h>
#include <cmdLine/cmdLine.h>
#include <vlutil/vlMiniDumper.h>
#include "vlutil/vlLogFileInfo.h"
#include "lgrUtil/svnRev.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 )
{
DtString description;
try
{
// Ok lets open the file as if recording.
DtLgrDisRecordFile* file = DtLgrDisRecordFile::create(filename, description);
// Necessary information (not really needed here but for completeness)
DtLgrDisMessageIOInfo* ioInfo = DtLgrDisMessageIOInfo::create(DtMessageIOInfo::File,5);
// Now construct a writer object to write the packets to 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)
{
"Unknown interaction class handle.");
return 0;
}
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);
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)
{
"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 )
{
DtString description; // blank logger description
try
{
// Ok lets open the file for playback.
DtLgrHlaRecordFile* file = DtLgrHlaRecordFile::create(filename, description, *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);
#if !DtHLA
// DIS initialization
#else
// HLA initialization
DtExerciseConnInitializer exConnInit;
#if DtHLA
// Set the default FED File, executable, and version
exConnInit.setFedFileName( DtDefaultRpr2Fom );
exConnInit.setExecName( "MAK-RPR-2.0" );
exConnInit.setRprFomVersion( 2.0 );
exConnInit.setRprFomRevision( 2 );
#ifdef DtHLA_1516_EVOLVED
std::vector<DtString> fomModules;
std::vector<DtString> fomModuleFromCommandLine;
exConnInit.fomModules(fomModuleFromCommandLine);
fomModules.push_back("MAK-VRFExt-6_evolved.xml");
fomModules.push_back("MAK-DIGuy-7_evolved.xml");
fomModules.push_back("MAK-LgrControl-2_evolved.xml");
fomModules.push_back("MAK-VRFAggregate-3_evolved.xml");
fomModules.push_back("MAK-DynamicTerrain-2_evolved.xml");
fomModules.insert(fomModules.end(), fomModuleFromCommandLine.begin(), fomModuleFromCommandLine.end());
exConnInit.setFomModules(fomModules);
#endif
#endif
try
{
theExerciseConn = new DtExerciseConn(exConnInit);
}
catch( DtVlRtiMismatchException )
{
DtFatal << "Mismatching RTI compiler Version. Please reconfigure your environment" << std::endl;
return -1;
}
//Get the FOM from the exercise connection.
theFom = DtLgrOnlineFomDesc::create(theExerciseConn);
DtLgrHlaFileFormats::registerFormats();
#endif
DtString filename(filenameArg.getValue());
DtLoggerFileWriter* writer = openFileForWriting(filename);
if(writer)
{
writer->activate(0);
writeData(writer);
writer->deactivate();
delete writer;
}
#if DtHLA
DtDELETE(theExerciseConn);
#endif
return 0;
}
// The application entry point
int main(int argc,char** argv)
{
// Used for error handling
DtLogFileInfo::instance().setAppName("WriteFile");
DtLogFileInfo::instance().setProtocol(DT_PROTOCOL_SHORT_NAME);
DtLogFileInfo::instance().setSvnRev(SVN_REV);
DtLogFileInfo::instance().setVersion(LOGGER_VERSION_STRING);
DtINIT_MINIDUMPER(DtLogFileInfo::instance().getFullString().c_str());
return writeFile(argc,argv);
}

Document ID: Generated on Thu Dec 18 15:35:09 EST 2025 from SVN revision 282494
Copyright © 2024 MAK Technologies. All Rights Reserved (www.mak.com)