VR-Link API Documentation for HLA 1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Object Ownership Example

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.

//********************************************************************
// Copyright (c) 2007 MaK Technologies, Inc.
// All rights reserved.
//********************************************************************
#include <vl/topoView.h>
#include <vl/fom.h>
#include <iostream>
#include <list>
int keybrdTick();
// Containers for both published and reflected objects.
// Used to manage which objects are currently owned or not
DtReflectedEntityList *entitiesReflected;
#ifdef DtHLA
void gainedOwnership(DtHlaObject* hlaObject, void* usr)
{
DtInfo << "Gained Ownership!" << std::endl;
// Create Publisher Object
DtHlaObjectWithStateRep* objWithSR = dynamic_cast<DtHlaObjectWithStateRep*>(hlaObject);
if( objWithSR )
{
DtEntityPublisher* entityPublisher = new DtEntityPublisher(objWithSR, objWithSR->exerciseConn());
// Once we acquire an object we should update it to make sure it gets correctly synced.
entityPublisher->forceUpdate();
// Now change the marking text to signal ownership and add this new publisher
// To our list of publishers.
#ifdef DtHLA_1516_EVOLVED
entityPublisher->esr()->setMarkingText(objWithSR->exerciseConn()->fedName());
#elif DtHLA
entityPublisher->esr()->setMarkingText(objWithSR->exerciseConn()->fedType());
#endif
entitiesOwned.add(entityPublisher);
}
// Remove Reflected Objects. You do not need to do this if you have
// selfReflect turned on. Which is highly recommended for simplifying ownership
// Management
#if DtHLA_1516
const RTI::ObjectInstanceHandle& objId = hlaObject->objectId();
#else
const RTI::ObjectHandle& objId = hlaObject->objectId();
#endif
DtReflectedObject* refObj = entitiesReflected->lookupObj(objId);
if(refObj)
{
refObj->detachHlaObject();
entitiesReflected->removeObject(objId);
}
}
void lostOwnership(DtHlaObject* hlaObject, void* usr)
{
DtInfo << "Lost Ownership!" << std::endl;
// Create Reflected Object. You do not need to do this if you have
// selfReflect turned on. Which is highly recommended for simplifying ownership
// Management
DtReflectedEntity* reflectedEntity = new DtReflectedEntity(hlaObject, hlaObject->exerciseConn());
entitiesReflected->addToObjectList(reflectedEntity);
// Remove Publisher Object from the list of Items we are publishing. If you do not
// do this, the object will continue to tick internally. However, VR-Link is smart enough
// to not actually send updates to attributes that are not being sent.
while(ownedEntityList != entitiesOwned.end())
{
if((*ownedEntityList)->hlaObject() == hlaObject)
{
DtEntityPublisher* unownedObject = (*ownedEntityList);
entitiesOwned.remove(unownedObject);
DtDELETE(unownedObject);
break;
}
++ownedEntityList;
}
}
#endif
int objectOwnershipExample(DtExerciseConn *exConn)
{
try
{
#ifdef DtHLA
DtDefaultOwnershipHandler defaultHandler;
// Accept all requests for ownership transfer.
// Set up callbacks when an entity gets transferred. Note that
// this callback will only trigger if the object is an entity.
// For a more generic ownership callback, remove the third parameter.
defaultHandler.setAcquireOwnershipCallback(&gainedOwnership,0,
exConn->fom()->objClassByName("BaseEntity.PhysicalEntity"));
defaultHandler.setDivestOwnershipCallback(&lostOwnership,0,
exConn->fom()->objClassByName("BaseEntity.PhysicalEntity"));
// Set the default ownership Handler. This will be the
// Ownership handler that will be set in all HLA objects created
// Note that this needs to be done before we create or reflect
// any objects as the default Ownership Handler will only apply to future
// objects.
exConn->setDefaultOwnershipHandler(&defaultHandler);
#endif
// Create an entity publisher for the entity we are simulating.
DtEntityPublisher* entityPub = new DtEntityPublisher(f18Type, exConn,
entitiesOwned.add(entityPub);
// Create Reflected Entity List
entitiesReflected = new DtReflectedEntityList(exConn);
// Hold on to the entity's state repository, where we can set data.
#ifdef DtHLA_1516_EVOLVED
esr->setMarkingText(exConn->fedName());
#elif DtHLA
esr->setMarkingText(exConn->fedType());
#endif
// Initialize VR-Link time.
DtClock* clock = exConn->clock();
// Main loop
DtTime dt = 1.0; //Update once a second
DtTime simTime = 0;
while (true)
{
// Check if user hit 'q' to quit.
if (keybrdTick() == -1)
break;
// Tell VR-Link the current value of simulation time.
clock->setSimTime(simTime);
// Tick all entities. Note that in this example
// the entities never change value except for the marking test
// signaling which federate owns what
entitiesOwned.tick();
// Process any incoming messages.
exConn->drainInput();
simTime += dt;
// Wait till real time equals simulation time of next step.
DtSleep(simTime - clock->elapsedRealTime());
}
}
DtCATCH_AND_WARN(std::cout);
return 0;
}
int keybrdTick()
{
char *keyPtr = DtPollBlockingInputLine();
if(keyPtr) {
switch(*keyPtr) {
case 'q':
case 'Q':
return -1;
break;
#ifdef DtHLA
case 'a':
case 'A':
DtInfo << "Acquire" << std::endl;
if(entitiesReflected->count())
{
DtReflectedEntity* reflEntity = entitiesReflected->first();
reflEntity->hlaObject()->acquireObject();
}
break;
case 'd':
case 'D':
DtInfo << "Divest" << std::endl;
if(!entitiesOwned.empty())
{
DtEntityPublisher* entityPublisher = *(entitiesOwned.begin());
entityPublisher->hlaObject()->divestObject();
}
break;
#endif
}
}
return 0;
}

Document ID: Generated on Fri May 15 06:36:13 EDT 2015 from SVN revision 153263
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)