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

Table of Contents

The Data Interaction example demonstrates the following:

How to Run the Example

This example demonstrates how to use data interactions to encode and decode your information.

Usage

To run this example:

  1. Run two instances of the setData executable in the protocol of choice from the <VR-Link install>/bin64 directory.
  2. Observe that the new data is being sent/received over the network.

Sample Data

Data Interaction Example Code

/****************************************************************************
* Copyright (c) 1992-2025 MAK Technologies, Inc
* 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-One-2025" );
appInit.setRprFomVersion( 2.0 );
appInit.setRprFomRevision( 1 );
#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);
if (input.keybrdTick() == -1) break;
// 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 Thu Oct 16 00:12:25 EDT 2025 from SVN revision 280738
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)