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

Table of Contents

Overview

The offsetFile example demonstrates how to read packets from a MAK Logger tape file (.lgr), apply a time offset to each packet, and write the modified packets to a new tape file. Each protocol build handles its matching DIS or HLA logger format and validates the input file's format before processing. Packet content is preserved while timestamps are adjusted.

Usage

Command line:

offsetFile [-o <offsetSeconds>] <readFile.lgr> <writeFile.lgr>

Parameters:

Examples:

# DIS build
offsetFileDIS.exe -o 5.0 ..\logger_tapes\bombMission2025-DIS.lgr ..\logger_tapes\adjusted.lgr
# HLA 4 build
offsetFileHLA4.exe -o 2.5 ..\logger_tapes\bombMission2026-HLA4-NETN.lgr

Build and Run

Behavior Details

Code Structure

Defaults

If filenames are omitted:

Notes

Source Code

/*******************************************************************************
** Copyright (c) 1992-2026 MAK Technologies, Inc
** All rights reserved.
*******************************************************************************/
#if DtDIS
#include <vl/disPduFactory.h>
#endif
#if DtHLA
#include <vl/exerciseConn.h>
#include <vl/exerciseConnInitializerHLA.h>
#include <vl/interactionFactory.h>
#include <vl/interactionHLA.h>
#include <vlutil/vlRtiMismatchException.h>
#endif
#include <iostream>
#include <cmdLine/cmdLine.h>
using namespace MAKLogger;
// Hard coded paths for files. Relative to the bin directories.
#if DtDIS
const char defaultReadFileName[] = "..\\logger_tapes\\bombMission2026-DIS.lgr";
#elif DtHLA_4
const char defaultReadFileName[] = "..\\logger_tapes\\bombMission2026-HLA4-NETN.lgr";
//#elif DtHLA_1516_EVOLVED // no default logger tape provided for HLA 1516 Evolved protocol
//const char defaultReadFileName[] = "..\\logger_tapes\<lgr filename>";
//#elif DtHLA_1516 // no default logger tape provided for HLA 1516 protocol
//const char defaultReadFileName[] = "..\\logger_tapes\<lgr filename>";
//#else // no default logger tape provided for HLA 1.3 protocol
//const char defaultReadFileName[] = "..\\logger_tapes\<lgr filename>";
#endif
// Function for checking if any keys in the array are pressed.
bool keyCheck(const char* keysToCheck)
{
if(DtKbHit())
{
int c = DtGetCh();
return strchr(keysToCheck,c) != 0;
}
return false;
}
#if DtDIS
void readPacket( const DtPacket* packet)
{
// Necessary factory object.
static DtDisPduFactory factory;
// Check to see if packet is a PDU.
if(packet->type() == static_cast<int>(DtPacket::Type_Pdu))
{
DtPdu* Pdu = factory.createPdu (
reinterpret_cast<const DtNetPacket*>(packet->data()),
reinterpret_cast<DtBufferPtr>(const_cast<char*>(packet->data())));
Pdu->print();
delete Pdu;
}
}
DtLoggerFileReader* openFileForReading( const DtString& filename )
{
// Ok lets open the file for playback.
DtLgrDisPlaybackFile* file(0);
DtLgrDisMessageIOInfo* ioInfo(0);
try
{
// Will be processing the file sequentially, so don't need to pay performance penalty
// of caching file in RAM for faster random access
file = DtLgrDisPlaybackFile::create(filename, false);
// Necessary information (not really needed here but for completeness)
ioInfo = DtLgrDisMessageIOInfo::create(DtMessageIOInfo::File,5);
// Now construct a reader object to read the packets from the file.
return DtLoggerFileReader::create(ioInfo,file);
}
catch(DtException& ex)
{
DtInfo << output << std::endl;
return 0;
}
}
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)
{
DtInfo << output << std::endl;
return 0;
}
}
#endif
#if DtHLA
DtExerciseConn* theExerciseConn;
DtInteractionFactory* theInteractionFactory;
DtLgrOnlineFomDesc* theOnlineFom;
DtBaseFomDesc* theFileFom;
DtLgrHlaMessageState* theMessageState;
void readPacket( const DtPacket* packet)
{
DtConstLgrHlaMessage hlaMessage(packet);
if(hlaMessage.isInteraction())
{
DtInteraction* inter = hlaMessage.createInteraction(
theInteractionFactory,
*theOnlineFom);
inter->print();
}
}
DtLoggerFileReader* openFileForReading( const DtString& filename )
{
// Now opening the file for playback.
try
{
// Will be processing the file sequentially, so don't need to pay performance penalty
// of caching file in RAM for faster random access
DtLgrHlaPlaybackFile* file = DtLgrHlaPlaybackFile::create(filename, false);
// Necessary information.
DtLgrHlaMessageIOInfo* ioInfo = DtLgrHlaMessageIOInfo::create(
DtMessageIOInfo::File,DtBaseFomDesc::copy(*file->fom()));
// Now construct a reader object to read the packets from the file.
return DtLoggerFileReader::create(ioInfo,file);
}
catch(DtException& ex)
{
DtInfo << output << std::endl;
return 0;
}
}
DtLoggerFileWriter* openFileForWriting( DtString filename )
{
DtString description; // blank logger description
try
{
// Ok lets open the file for playback.
DtLgrHlaRecordFile* file = DtLgrHlaRecordFile::create(filename, description, *theOnlineFom);
// Necessary information (not really needed here but for completeness)
DtLgrHlaMessageIOInfo* ioInfo = DtLgrHlaMessageIOInfo::create(DtMessageIOInfo::File,theOnlineFom);
// Now construct a reader object to read the packets from the file.
return DtLoggerFileWriter::create(ioInfo,file);
}
catch (DtException& ex)
{
DtInfo << output << std::endl;
return 0;
}
}
// Add the specific parameters for the tape.
void setConfigParameters(DtVrlApplicationInitializer& exConnInit, const DtFilename& filename)
{
// Open the file and grab the config meta data
try
{
file = new DtLoggerPlaybackFile(DtString(filename.c_str()), false, true);
}
catch(DtException& ex)
{
DtInfo << output << std::endl;
}
const DtPacket* configPacket =
DtLgrHlaConfigData configData;
if(configPacket)
{
configData.deserialize(configPacket->data());
}
// Use the config meta data to set the exercise connection initializer
// that select things like the FOM version, FOM file name, etc.
if (!configData.fomVersion().isEmpty())
exConnInit.setRprFomVersion(configData.fomVersion().toDouble());
if (!configData.fomMapperName().isEmpty())
{
exConnInit.setFomMapperLibName(configData.fomMapperName());
if (!configData.fomMapperInitData().isEmpty())
{
exConnInit.setFomMapperInitData((void*)configData.fomMapperInitData().c_str());
}
}
if (!configData.emptyFomMapper().isEmpty() && configData.emptyFomMapper() != "0")
{
exConnInit.setFomMapper(new DtEmptyFomMapper());
}
exConnInit.setExecName(configData.federationName());
DtFilename fedFile(configData.fedFile().c_str());
fedFile.stripPath();
exConnInit.setFedFileName(fedFile);
#if DtHLA_1516_EVOLVED
std::vector<DtString> fomModNames;
configData.getFomModules(fomModNames);
int numberModules = fomModNames.size();
if (numberModules > 0)
{
for (int fmIndex=0; fmIndex < numberModules; ++fmIndex)
{
DtFilename fmFilename(fomModNames[fmIndex].c_str());
fmFilename.stripPath();
fomModNames[fmIndex] = fmFilename;
}
exConnInit.setFomModules(fomModNames);
}
if (!configData.mim().isEmpty())
{
DtFilename mim(configData.mim().c_str());
mim.stripPath();
exConnInit.setMimModule(mim);
}
#endif
delete file;
}
#endif
// Check that the file type matches one of the tape formats
bool fileTypeMatches(DtString& filename)
{
unsigned int magic = 0;
std::ifstream fs;
fs.open(filename.c_str(),std::ios::binary );
if(!fs.is_open() || !fs.good())
{
DtInfo << "Unable to open file: " << filename << "." << std::endl;
exit(1);
}
fs.read((char*)&magic,4);
magic = (magic >> 24) | ((magic >> 8) & 0xFF00) |
((magic << 8) & 0xFF0000) | (magic << 24);
// File formats must be registered for this check to work
#if DtDIS
return DtString("Unknown") != DtLgrDisFileFormats::versionString(magic);
#else
return DtString("Unknown") != DtLgrHlaFileFormats::versionString(magic);
#endif
}
int offsetFile(int argc,char** argv)
{
DtCmdLine::CmdLine cmdLine("offsetFile","Example of how to modify packets in a logger file.",LOGGER_VERSION_STRING);
DtCmdLine::ValueArg<double> offsetArg("o","Offset","The offset time to apply to each packet.",true,0.0, "double");
DtCmdLine::UnlabeledMultiArg<DtString> filenameArg("Filename","The name of the logger file to read followed by file to write.","string");
cmdLine.add(&offsetArg);
cmdLine.add(&filenameArg);
cmdLine.parse(argc,argv);
double offset = offsetArg.getValue();
std::vector<DtString> values = filenameArg.getValue();
if(values.size() < 1)
{
#ifdef DtHLA_4
values.push_back(defaultReadFileName);
#elif DtHLA_1516_EVOLVED
DtInfo << "Please provide a logger file using the HLA 1516 Evolved protocol to offset." << std::endl;
return 1;
#elif DtDIS
values.push_back(defaultReadFileName);
#elif DtHLA_1516
DtInfo << "Please provide a logger file using the HLA 1516 protocol to offset." << std::endl;
return 1;
#else
DtInfo << "Please provide a logger file using the HLA 1.3 protocol to offset." << std::endl;
return 1;
#endif
}
if (values.size() < 2)
{
values.push_back("");
}
DtFilename readFilename(values[0].c_str());
if (readFilename.isEmpty())
{
#ifdef DtHLA_4
readFilename = defaultReadFileName;
#elif DtHLA_1516_EVOLVED
DtInfo << "Please provide a logger file using the HLA 1516 Evolved protocol to offset." << std::endl;
return 1;
#elif DtDIS
readFilename = defaultReadFileName;
#elif DtHLA_1516
DtInfo << "Please provide a logger file using the HLA 1516 protocol to offset." << std::endl;
return 1;
#else
DtInfo << "Please provide a logger file using the HLA 1.3 protocol to offset." << std::endl;
return 1;
#endif
}
DtFilename writeFilename(values[1].c_str());
if (writeFilename.isEmpty())
{
writeFilename = DtFilename::stripExtension(readFilename);
writeFilename.stripExtension();
writeFilename += "-Offset";
writeFilename.setExtension("lgr");
}
#if DtDIS
// DIS initialization
DtLgrDisFileFormats::registerFormats();
#else
// HLA initialization
#endif
if (!fileTypeMatches(readFilename))
{
DtInfo << "Unable to process file (" <<
readFilename.c_str() << "): file format designator does not match, unknown or corrupted." << std::endl;
return 1;
}
#if DtHLA
// HLA initialization continued
// Extract the configuration data from the file
DtVrlApplicationInitializer exConnInit(argc, argv, "offsetFile");
setConfigParameters(exConnInit, readFilename);
try
{
theExerciseConn = new DtExerciseConn(exConnInit);
}
catch( DtVlRtiMismatchException )
{
DtFatal << "Mismatching RTI compiler Version. Please reconfigure your environment" << std::endl;
return -1;
}
theInteractionFactory = theExerciseConn->interactionFactory();
theOnlineFom = DtLgrOnlineFomDesc::create(theExerciseConn);
#endif
DtLoggerFileReader* reader = openFileForReading(readFilename);
if(!reader)
{
DtInfo << "Unable to open file: " << readFilename << " for reading." << std::endl;
return 1;
}
else
{
DtInfo << "Opened file: " << readFilename << " for reading." << std::endl;
}
DtLoggerFileWriter* writer = openFileForWriting(writeFilename);
if(!writer)
{
DtInfo << "Unable to open file: " << writeFilename << " for writing." << std::endl;
return 1;
}
else
{
DtInfo << "Opened file: " << writeFilename << " for writing." << std::endl;
}
if(writer)
{
writer->activate(0);
}
// HLA hook up state for handle mapping.
#if DtHLA
theMessageState = DtLgrHlaMessageState::create();
theMessageState->setInputInfo(reader->info());
theMessageState->setOutputInfo(DtLgrHlaMessageIOInfo::create(
DtMessageIOInfo::Network,theOnlineFom));
#endif
// Buffer to store the messages
DtMessageBuffer readBuffer;
DtMessageBuffer writeBuffer;
// Get the time of the last message in the file.
DtAbsoluteTime lastTime = reader->lastMessageTime();
// Tell the reader to read all messages up to and including the last time.
reader->startReadMessages(lastTime);
// Read until the end of the tape and the buffer is empty.
// The second case is required for all messages with the timestamp
// as the last time.
bool quit = false;
while(!quit && reader->lastReadTime() != lastTime)
{
// Break out if q or enter was pressed.
if (keyCheck("q\r"))
{
quit = true;
break;
}
// Read a set of packets into the buffer
// The number read is determined by the internal buffer size of the reader.
reader->getNextMessages(readBuffer);
// If no more messages break out.
if(readBuffer.size() == 0) break;
#if DtHLA
// This is an important and necessary step to convert an offline hla packet
// to an online packet. This is required for using VR-Link to create
// interactions and such
theMessageState->modifyMessages(readBuffer);
#endif
bool displayTime = true;
// Iterate through all packets in the buffer.
DtMessageBuffer::PacketList::iterator packetIterator;
for(packetIterator = readBuffer.begin(); packetIterator != readBuffer.end();++packetIterator)
{
DtPacket* readPacket = *packetIterator;
DtPacket* writePacket = DtPacket::create(readPacket->buffer(), readPacket->time(), readPacket->type(), readPacket->entityTrackingId());
DtAbsoluteTime tm = writePacket->time() + offset;
if ( tm < 0.0 )
{
tm = 0.0;
}
if (displayTime)
{
DtInfo << writePacket->time().toString() << " => " << tm.toString() << std::endl;
displayTime = false;
}
writePacket->setTime( tm );
writeBuffer.add(writePacket);
// Break out if q or enter was pressed.
if (keyCheck("q\r"))
{
quit = true;
break;
}
}
// Output multiple messages more efficient then individual packets.
writer->outputMessages(writeBuffer);
readBuffer.clear();
writeBuffer.clear();
}
if(writer)
{
writer->deactivate();
delete writer;
}
delete reader;
#if DtHLA
delete theMessageState;
delete theExerciseConn;
#endif
return 0;
}
// The application entry point
int main(int argc,char** argv)
{
return offsetFile(argc,argv);
}

Document ID: Generated on Tue Nov 17 15:25:07 EST 2026 from SVN revision 298446
Copyright © 2026 MAK Technologies. All Rights Reserved (www.mak.com)