VR-Link API Documentation for HLA 1516
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Interaction Example

If users want to create and send arbitrary data, this example demonstrates how to use data interactions to encode and decode your information.

/****************************************************************************
* Copyright (c) 2014 VT MAK
* All rights reserved.
****************************************************************************/
#include <iostream>
void receiveData( DtSetDataInteraction* inter, void* )
{
std::cout << "SetData Interaction from "
<< inter->senderId().string() << std::endl;
if ( inter->numFixedFields() >= 2 )
{
std::cout << " Mass: " << inter->datumValFloat32(DtFixed, 1) << std::endl;
std::cout << " Country: " << inter->datumValInt32(DtFixed, 2) << std::endl;
}
for ( int field = 1; field <= inter->numVarFields(); field++ )
{
std::cout << " " << inter->datumValByteArray(DtVar, field) << std::endl;
}
}
int main( int argc, char* argv[] )
{
// Used for error handling
DtINIT_MINIDUMPER( "SetData" );
try
{
// Create a connection to the exercise or federation execution.
DtVrlApplicationInitializer appInit(argc, argv, "VR-Link Talk");
#if DtDIS
// Change some defaults. Please note that this is DIS specific code.
// Notice how this is the only line of DIS specific code in this example,
// everything else is using the protocol independent API.
appInit.setUseAsynchIO(true);
appInit.setDisVersionToSend(7);
#else
// Set the default FED File, executable, and version
appInit.setFedFileName(DtDefaultRpr2Fom);
appInit.setExecName("MAK-RPR-2.0");
appInit.setRprFomVersion(2.0);
#endif
// Process the command line. This will actually set values in the
// DtVrlApplicationInitializer class and should be called before
// creating the DtExerciseConn instance.
appInit.parseCmdLine();
// The DtExerciseConn instance is perhaps the most important class
// in any VR-Link exercise. Think of it as the hub which holds
// everything else together. Technically this instance is what's
// responsible for connecting this federate to another federate via
// a network, or an RTI. After exConn is created, and if no error
// has occurred this simulator will be a live federate.
DtExerciseConn exConn(appInit);
DtSetDataInteraction::addCallback(&exConn, receiveData, 0);
DtEntityIdentifier receiverId = DtEntityIdentifier(3, 4, 2);
// Send a Data Interaction.
dataInter.setRequestId(4);
dataInter.setSenderId(senderId);
dataInter.setReceiverId(receiverId);
dataInter.setNumFixedFields(2);
dataInter.setDatumValFloat32(DtFixed, 1, 16.5);
dataInter.setDatumValInt32(DtFixed, 2, 300);
dataInter.setNumVarFields(3);
dataInter.setVarDataBytes(1, sizeof("Hi there"));
dataInter.setDatumParam(DtVar, 1, (DtDatumParam) 1122);
dataInter.setVarDataBytes(2, sizeof("world"));
dataInter.setDatumParam(DtVar, 2, (DtDatumParam) 2233);
dataInter.setVarDataBytes(3, sizeof("Jimmy was here"));
dataInter.setDatumParam(DtVar, 3, (DtDatumParam) 4567);
dataInter.setDatumValByteArray(DtVar, 1, "Hi there");
dataInter.setDatumValByteArray(DtVar, 2, "world");
dataInter.setDatumValByteArray(DtVar, 3, "Jimmy was here");
// Initialize VR-Link time.
DtClock* clock = exConn.clock();
// Main loop
DtTime dt = 1;
DtTime simTime = 0;
while (simTime <= 60.0)
{
// Tell VR-Link the current value of simulation time.
clock->setSimTime(simTime);
// Process any incoming messages.
exConn.drainInput();
exConn.sendStamped(dataInter);
simTime += dt;
// Wait till real time equals simulation time of next step.
DtSleep(simTime - clock->elapsedRealTime());
}
}
DtCATCH_AND_WARN(std::cout);
return 0;
}

Document ID: Generated on Tue Mar 1 02:56:17 EST 2016 from SVN revision 162687
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)