// The RTI may ask us to provide the latest attribute values for a car object. void HLAmodule::provideAttributeValueUpdate ( ObjectInstanceHandle theObject, const AttributeHandleSet& theAttributes, const VariableLengthData& theUserSuppliedTag) throw (FederateInternalError) { AttributeHandleValueMap attributeMap; { boost::unique_lock<boost::mutex> lock(_carMutex); HLAcarMap::right_const_iterator car_iter = _localCars.right.find(theObject); // This should not happen, it means we lost a reference to the object instance if (car_iter == _localCars.right.end()) { wcerr << L"Error in HLAmodule::provideAttributeUpdate: Objecthandle: " << theObject << L" not found!" << endl; return; } // Get the last known state of the Car with the given id HLAcarState& state = _carStateCache[car_iter->second]; // Only send values for the requested attributes AttributeHandleSet::iterator nameIter(theAttributes.find(_nameAttributeHandle)); if (nameIter != theAttributes.end()) { _hlaCarNameEncoder.set(state.name); attributeMap[_nameAttributeHandle] = _hlaCarNameEncoder.encode(); } AttributeHandleSet::iterator positionIter(theAttributes.find(_positionAttributeHandle)); if (positionIter != theAttributes.end()) { attributeMap[_positionAttributeHandle] = _hlaCarPositionEncoder.encode(state.position); } AttributeHandleSet::iterator fuelLevelIter(theAttributes.find(_fuelLevelAttributeHandle)); if (fuelLevelIter != theAttributes.end()) { _hlaCarFuelLevelEncoder.set(static_cast<int>(state.fuelLevel)); attributeMap[_fuelLevelAttributeHandle] = _hlaCarFuelLevelEncoder.encode(); } AttributeHandleSet::iterator fuelTypeIter(theAttributes.find(_fuelTypeAttributeHandle)); if (fuelTypeIter != theAttributes.end()) { attributeMap[_fuelTypeAttributeHandle] = _hlaCarFuelTypeEncoder.encode(state.fuelType); } AttributeHandleSet::iterator licensePlateIter(theAttributes.find(_licensePlateAttributeHandle)); if (licensePlateIter != theAttributes.end()) { _hlaCarLicensePlateEncoder.set(state.licensePlate); attributeMap[_licensePlateAttributeHandle] = _hlaCarLicensePlateEncoder.encode(); } } // end lock // Send the attribute values _rtiAmbassador->updateAttributeValues(theObject, attributeMap, VariableLengthData()); }
/* * This method will inform the RTI about the types of data that the federate will * be creating, and the types of data we are interested in hearing about as other * federates produce it. */ void ExampleCPPFederate::publishAndSubscribe() { //////////////////////////////////////////// // publish all attributes of ObjectRoot.A // //////////////////////////////////////////// // before we can register instance of the object class ObjectRoot.A and // update the values of the various attributes, we need to tell the RTI // that we intend to publish this information // package the information into a handle set AttributeHandleSet attributes;// = AttributeHandleSet(); attributes.insert( this->aaHandle ); attributes.insert( this->abHandle ); attributes.insert( this->acHandle ); // do the actual publication rtiamb->publishObjectClassAttributes( this->aHandle, attributes ); ///////////////////////////////////////////////// // subscribe to all attributes of ObjectRoot.A // ///////////////////////////////////////////////// // we also want to hear about the same sort of information as it is // created and altered in other federates, so we need to subscribe to it rtiamb->subscribeObjectClassAttributes( this->aHandle, attributes ); ///////////////////////////////////////////////////// // publish the interaction class InteractionRoot.X // ///////////////////////////////////////////////////// // we want to send interactions of type InteractionRoot.X, so we need // to tell the RTI that we're publishing it first. We don't need to // inform it of the parameters, only the class, making it much simpler // do the publication rtiamb->publishInteractionClass( this->xHandle ); //////////////////////////////////////////////////// // subscribe to the InteractionRoot.X interaction // //////////////////////////////////////////////////// // we also want to receive other interaction of the same type that are // sent out by other federates, so we have to subscribe to it first rtiamb->subscribeInteractionClass( this->xHandle ); }
// // Publish Car object attributes // This method is not thread safe but should only be called once during initialization! // void HLAmodule::publishCar() throw ( FederateNotExecutionMember, RestoreInProgress, SaveInProgress, NotConnected, RTIinternalError, AttributeNotDefined, ObjectClassNotDefined) { AttributeHandleSet carAttributes; carAttributes.insert(_positionAttributeHandle); carAttributes.insert(_fuelLevelAttributeHandle); carAttributes.insert(_nameAttributeHandle); carAttributes.insert(_licensePlateAttributeHandle); carAttributes.insert(_fuelTypeAttributeHandle); _rtiAmbassador->publishObjectClassAttributes(_objectClassCarHandle, carAttributes); }