The following code demonstrates how to transfer ownership between federates in HLA using the simplified ownership handler classes in VR-Link
It creates an entity with the marking text of the owning federate. You can use the keys 'a' and 'd' to aquire or divest the entities and the marking text will change appropriately. This example works best when you run multiple copies of it at the same time, so there are enough entities to move ownership.
#include <iostream>
#include <list>
int keybrdTick();
#ifdef DtHLA
void gainedOwnership(
DtHlaObject* hlaObject,
void* usr)
{
DtInfo <<
"Gained Ownership!" << std::endl;
if( objWithSR )
{
#ifdef DtHLA_1516_EVOLVED
#elif DtHLA
#endif
entitiesOwned.
add(entityPublisher);
}
#if DtHLA_1516
const RTI::ObjectInstanceHandle& objId = hlaObject->
objectId();
#else
const RTI::ObjectHandle& objId = hlaObject->
objectId();
#endif
if(refObj)
{
}
}
{
DtInfo <<
"Lost Ownership!" << std::endl;
while(ownedEntityList != entitiesOwned.
end())
{
if((*ownedEntityList)->hlaObject() == hlaObject)
{
entitiesOwned.
remove(unownedObject);
break;
}
++ownedEntityList;
}
}
#endif
{
try
{
#ifdef DtHLA
#endif
entitiesOwned.
add(entityPub);
#ifdef DtHLA_1516_EVOLVED
#elif DtHLA
#endif
while (true)
{
if (keybrdTick() == -1)
break;
simTime += dt;
}
}
return 0;
}
int keybrdTick()
{
if(keyPtr) {
switch(*keyPtr) {
case 'q':
case 'Q':
return -1;
break;
#ifdef DtHLA
case 'a':
case 'A':
DtInfo <<
"Acquire" << std::endl;
if(entitiesReflected->
count())
{
}
break;
case 'd':
case 'D':
DtInfo <<
"Divest" << std::endl;
if(!entitiesOwned.
empty())
{
}
break;
#endif
}
}
return 0;
}