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

The main application:

/*******************************************************************************
** Copyright (c) 1992-2010 VT MAK
** 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 <vlutil/vlMiniDumper.h>
#include <iostream>
#include <cmdLine/cmdLine.h>
using namespace MAKLogger;
// Hard coded paths for files. Relative to the bin directories.
#if DtDIS
const char defaultFileName[] = "..\\logger_tapes\\makland2016-DIS.lgr";
#elif DtHLA_1516_EVOLVED
const char defaultFileName[] = "..\\logger_tapes\\makland2016-HLAe-RPR20.lgr";
#elif DtHLA_1516
const char defaultFileName[] = "..\\logger_tapes\\makland2016-HLA516-RPR20.lgr";
#else
const char defaultFileName[] = "..\\logger_tapes\\makland2016-HLA13-RPR20.lgr";
#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.
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)
{
DtString output = DtSerializeException(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)
{
DtString output = DtSerializeException(ex);
DtInfo << output << std::endl;
return 0;
}
}
// Add the specific parameters for the tape.
void setConfigParameters(DtExerciseConnInitializer& 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)
{
DtString output = DtSerializeException(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());
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]);
fmFilename.stripPath();
fomModNames[fmIndex] = fmFilename;
}
exConnInit.setFomModules(fomModNames);
}
if (!configData.mim().isEmpty())
{
DtFilename mim(configData.mim());
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 readFile(int argc,char** argv)
{
DtCmdLine::CmdLine cmdLine("readFile","Example of how to read a logger file.",LOGGER_VERSION_STRING);
DtCmdLine::UnlabeledMultiArg<DtString> filenameArg("Filename","The name of the logger file to read.","string");
cmdLine.add(&filenameArg);
cmdLine.parse(argc,argv);
std::vector<DtString> values = filenameArg.getValue();
if(values.size() == 0)
{
values.push_back(defaultFileName);
}
DtFilename filename(values.front());
if (filename.isEmpty())
{
filename = defaultFileName;
}
#if DtDIS
// DIS initialization
#else
// HLA initialization
DtLgrHlaFileFormats::registerFormats();
#endif
if (!fileTypeMatches(filename))
{
DtInfo << "Unable to process file (" <<
filename.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
DtExerciseConnInitializer exConnInit;
setConfigParameters(exConnInit, filename);
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(filename);
if(!reader)
{
DtInfo << "Unable to open file: " << filename << " for reading." << std::endl;
return 1;
}
else
{
DtInfo << "Opened file: " << filename << " for reading." << std::endl;
}
//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
// 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(buffer);
// If no more messages break out.
if(buffer.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(buffer);
#endif
//Iterate through all packets in the buffer.
DtMessageBuffer::PacketList::iterator packetIterator;
for(packetIterator = buffer.begin(); packetIterator != buffer.end();++packetIterator)
{
DtPacket* packet = *packetIterator;
static int packetCount = 0;
DtInfo <<
"***********************************************" << std::endl <<
"* Packet #" << ++packetCount << " Type = " << packet->type() <<
" at Time = " << packet->time().toString(2) << std::endl;
readPacket(packet);
// Break out if q or enter was pressed.
if (keyCheck("q\r"))
{
quit = true;
break;
}
}
buffer.clear();
}
delete reader;
#if DtHLA
delete theMessageState;
delete theExerciseConn;
#endif
return 0;
}
// The application entry point
int main(int argc,char** argv)
{
DtINIT_MINIDUMPER( "ReadFile" );
return readFile(argc,argv);
}

Document ID: Generated on Tue Aug 1 09:00:26 EDT 2017 from SVN revision 179128
Copyright © 2017 VT MÄK. All Rights Reserved (www.mak.com)