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.
How to Run the Example
- Note
- The Object Ownership example is not a runnable example by default.
Sample Data
Object Ownership Example Code
#include <iostream>
#include <list>
int keybrdTick();
#ifdef DtHLA
void gainedOwnership(
DtHlaObject* hlaObject,
void* usr)
#else //DtDIS
#endif
{
DtInfo <<
"Gained Ownership!" << std::endl;
#ifdef DtHLA
#else //DtDIS
#endif
if (!objWithSR)
{
return;
}
#ifdef DtHLA_1516_EVOLVED
#elif DtHLA
#endif
entitiesOwned.
add(entityPublisher);
#if DtHLA
#if DtHLA_1516
const RTI::ObjectInstanceHandle& objId = hlaObject->
objectId();
#else
const RTI::ObjectHandle& objId = hlaObject->
objectId();
#endif
if(refObj)
{
}
#else //DtDIS
if (refObj)
{
}
#endif
}
#ifdef DtHLA
#else //DtDIS
#endif
{
DtInfo <<
"Lost Ownership!" << std::endl;
#ifdef DtHLA
#else //DtDIS
DtStateRepository* objWithSR = myExConn->disObjectManager()->stateRepOfObject(*entityId)->clone();
#endif
while(ownedEntityList != entitiesOwned.
end())
{
#ifdef DtHLA
if ((*ownedEntityList)->hlaObject() == hlaObject)
#else
if ((*ownedEntityList)->id() == *entityId)
#endif
{
entitiesOwned.
remove(unownedObject);
deletedPublishers.
add(unownedObject);
break;
}
++ownedEntityList;
}
}
{
try
{
entitiesOwned.
add(entityPub);
#ifdef DtHLA_1516_EVOLVED
#elif DtHLA
#endif
while (true)
{
if (keybrdTick() == -1)
break;
while (deletedPublishers.
first())
{
deletedPublishers.
remove(item);
}
simTime += dt;
}
}
return 0;
}
int keybrdTick()
{
if(keyPtr) {
switch(*keyPtr) {
case 'q':
case 'Q':
return -1;
break;
case 'a':
case 'A':
DtInfo <<
"Acquire" << std::endl;
if(entitiesReflected->
count())
{
}
break;
case 'd':
case 'D':
DtInfo <<
"Divest" << std::endl;
if(!entitiesOwned.
empty())
{
#ifdef DtHLA
#else //DtDIS
#endif
}
break;
}
}
return 0;
}