📄 billard.cc
字号:
D.Out(pdTerm, "Just resigned from federation"); } catch (Exception &e) { D.Out(pdExcept, "** Exception during resignFederationExecution by federate"); } // Detruire la federation if (creator) { for (;;) { tick(); try { D.Out(pdTerm, "Asking from federation destruction..."); rtiamb.destroyFederationExecution(federationName.c_str()); D.Out(pdTerm, "Federation destruction granted."); break ; } catch (FederatesCurrentlyJoined) { sleep(5); } } } D.Out(pdTerm, "Destroying RTIAmbassador and FedAmbassador."); D.Out(pdTerm, "Federation terminated.");}// ----------------------------------------------------------------------------/** Carry out publications and subscriptions */voidBillard::publishAndSubscribe(){ // Get all class and attributes handles getHandles(); // Add PositionX et PositionY to the attribute set AttributeHandleSet *AttributeSet = AttributeHandleSetFactory::create(3); AttributeSet->add(AttrXID); AttributeSet->add(AttrYID); // Subscribe to Bille objects. D[pdDebug] << "subscribe: class " << BilleClassID << ", attributes " << AttrXID << " and " << AttrYID << "... " ; rtiamb.subscribeObjectClassAttributes(BilleClassID, *AttributeSet, RTI_TRUE); D[pdDebug] << "done." << endl ; // Publish Boule Objects. AttributeSet->add(AttrColorID); rtiamb.publishObjectClass(BouleClassID, *AttributeSet); // Publish and subscribe to Bing interactions rtiamb.subscribeInteractionClass(BingClassID, RTI_TRUE); rtiamb.publishInteractionClass(BingClassID); AttributeSet->empty(); D.Out(pdInit, "Local Objects and Interactions published and subscribed.");}// ----------------------------------------------------------------------------/** get handles of objet/interaction classes */voidBillard::getHandles(){ D[pdDebug] << "Get handles..." << endl ; BilleClassID = rtiamb.getObjectClassHandle(CLA_BILLE); BouleClassID = rtiamb.getObjectClassHandle(CLA_BOULE); D.Out(pdInit, "BilleClassID = %d, BouleClassID = %d.", BilleClassID, BouleClassID); // Attributs des classes d'Objets AttrXID = rtiamb.getAttributeHandle(ATT_POSITION_X, BilleClassID); AttrYID = rtiamb.getAttributeHandle(ATT_POSITION_Y, BilleClassID); AttrColorID = rtiamb.getAttributeHandle(ATT_COLOR, BouleClassID); D.Out(pdInit, "AttrXID = %d, AttrYID = %d, AttrColorID = %d.", AttrXID, AttrYID, AttrColorID); // Interactions BingClassID = rtiamb.getInteractionClassHandle(INT_BING); ParamBoulID = rtiamb.getParameterHandle(PAR_BOUL, BingClassID); ParamDXID = rtiamb.getParameterHandle(PAR_DX, BingClassID); ParamDYID = rtiamb.getParameterHandle(PAR_DY, BingClassID); D.Out(pdInit, "BingClassID = %d, DX_ID = %d, DY_ID = %d", BingClassID, ParamDXID, ParamDYID);}// ----------------------------------------------------------------------------/*! Envoie une interaction, dont les parametres DX et DY ont pour valeur les dx et dy de la bille Local, et dont l'etiquette temporelle vaut InteractionTime.*/voidBillard::sendInteraction(double dx, double dy, const FedTime& InteractionTime, ObjectHandle id){ char buf[512] ; ParameterHandleValuePairSet *parameterSet=NULL ; parameterSet = ParameterSetFactory::create(3); sprintf(buf, "%ld", id); parameterSet->add(ParamBoulID, buf, strlen(buf)+1); D.Out(pdDebug, "SendInteraction"); D.Out(pdDebug, "SendInteraction - ParamBoulID= %u", ParamBoulID); D.Out(pdDebug, "SendInteraction - x= %d", id); D.Out(pdDebug, "SendInteraction - buf= %s", buf); // D.Out(pdDebug, "SendInteraction - ParamBoulID= %u, x= %f, buf= %s", // ParamBoulID, Id, buf); sprintf(buf, "%f", dx); parameterSet->add(ParamDXID, buf, strlen(buf)+1); D.Out(pdDebug, "SendInteraction - ParamDXID= %u, x= %f, buf= %s", ParamDXID, dx, buf); sprintf(buf, "%f", dy); parameterSet->add(ParamDYID, buf, strlen(buf)+1); D.Out(pdDebug, "SendInteraction - ParamDYID= %u, x= %f, buf= %s", ParamDYID, dy, buf); D.Out(pdRegister, "Sending interaction(DX= %f, DY= %f).", dx, dy); try { rtiamb.sendInteraction(BingClassID, *parameterSet, InteractionTime, ""); } catch (Exception& e) { D.Out(pdExcept, "**** Exception sending interaction : %d", &e); } delete parameterSet ;}// ----------------------------------------------------------------------------/** Updates a ball by sending entity position and color. \param x X position \param y Y position \param color Color \param UpdateTime Event time \param id Object handle (ball) */voidBillard::sendUpdate(double x, double y, int color, const FedTime& UpdateTime, ObjectHandle id){ char buf[512] ; AttributeHandleValuePairSet *attributeSet ; attributeSet = AttributeSetFactory::create(3); D.Out(pdTrace, "SendUpdate."); sprintf(buf, "%f", x); attributeSet->add(AttrXID, buf, strlen(buf)+1); D.Out(pdDebug, "SendUpdate - AttrXID= %u, x= %f, size= %u", AttrXID, x, attributeSet->size()); sprintf(buf, "%f", y); attributeSet->add(AttrYID, buf, strlen(buf)+1); D.Out(pdDebug, "SendUpdate - AttrYID= %u, y= %f, size= %u", AttrYID, y, attributeSet->size()); sprintf(buf, "%d", color); attributeSet->add(AttrColorID, buf, strlen(buf)+1); D.Out(pdDebug, "SendUpdate - AttrColorID= %u, color= %f, size= %u", AttrColorID, color, attributeSet->size()); try { rtiamb.updateAttributeValues(id, *attributeSet, UpdateTime, ""); // if (log) // logfile << string(((RTIfedTime) UpdateTime).getTime()) << " : UAV " // << string(Local.x) << " " << string(Local.y) << endl ; } catch (Exception& e) { D.Out(pdExcept, "**** Exception updating attribute values: %d", &e); } delete attributeSet ;}// ----------------------------------------------------------------------------/** Register a ball instance \param s Object name to provide to the RTI \return The created object handle */ObjectHandleBillard::registerBallInstance(const char *s){ return rtiamb.registerObjectInstance(BouleClassID, s);}// ============================================================================// FEDERATE AMBASSADOR CALLBACKS// ============================================================================// ----------------------------------------------------------------------------/** Callback : discover object instance */voidBillard::discoverObjectInstance(ObjectHandle theObject, ObjectClassHandle theObjectClass, const char */*theObjectName*/) throw (CouldNotDiscover, ObjectClassNotKnown, InvalidFederationTime, FederateInternalError){ if (theObjectClass != BilleClassID) { D.Out(pdError, "Object of Unknown Class discovered."); throw RTIinternalError(); } objects.discover(theObject);}// ----------------------------------------------------------------------------/** Callback announce synchronization point */voidBillard::announceSynchronizationPoint(const char *label, const char */*tag*/) throw (FederateInternalError){ if (strcmp(label, "Init") == 0) { paused = RTI_TRUE ; D.Out(pdProtocol, "announceSynchronizationPoint."); } else { cout << "Unexpected synchronization label" << endl ; exit(1); }}// ----------------------------------------------------------------------------/** Callback : federation synchronized */voidBillard::federationSynchronized(const char *label) throw (FederateInternalError){ if (strcmp(label, "Init") == 0) { paused = false ; D.Out(pdProtocol, "CALLBACK : federationSynchronized with label %s", label); }}// ----------------------------------------------------------------------------/** Callback : receive interaction */voidBillard::receiveInteraction(InteractionClassHandle theInteraction, const ParameterHandleValuePairSet& theParameters, const FedTime& /*theTime*/, const char */*theTag*/, EventRetractionHandle /*theHandle*/) throw (InteractionClassNotKnown, InteractionParameterNotKnown, InvalidFederationTime, FederateInternalError){ char *parmValue ; ULong valueLength ; int dx1 = 0 ; int dy1 = 0 ; ObjectHandle h1 = 0 ; bool bille = false ; D.Out(pdTrace, "Fed : receiveInteraction"); if (theInteraction != BingClassID) { D.Out(pdError, "CALLBACK receiveInteraction : Unknown Interaction received"); exit(-1); } D.Out(pdDebug, "receiveInteraction - nb attributs= %d", theParameters.size()); for (unsigned int j = 0 ; j < theParameters.size(); ++j) { ParameterHandle handle = theParameters.getHandle(j); valueLength = theParameters.getValueLength(j); parmValue = new char[valueLength] ; theParameters.getValue(j, parmValue, valueLength); if (handle == ParamDXID) { if (parmValue != NULL) { dx1 = atoi(parmValue); // Local.dx = atof(parmValue); D.Out(pdDebug, "receiveInteraction(*) - dx= %s", parmValue); delete[] parmValue ; } else D.Out(pdError, "Missing Attribute in RAV."); } else if (handle == ParamDYID) { if (parmValue != NULL) { dy1 = atoi(parmValue); // Local.dy = atof(parmValue); D.Out(pdDebug, "receiveInteraction(*) - dy= %s", parmValue); delete[] parmValue ; } else D.Out(pdError, "Missing Attribute in RAV."); } else if (handle == ParamBoulID) { if (parmValue != NULL) { h1 = atoi(parmValue); bille = true ; } else D.Out(pdError, "Unrecognized parameter handle"); } } if (bille) { objects.receive(h1, dx1, dy1); }}// ----------------------------------------------------------------------------/** Callback : reflect attribute values */voidBillard::reflectAttributeValues( ObjectHandle theObject, const AttributeHandleValuePairSet& theAttributes, const FedTime& /*theTime*/, const char */*theTag*/, EventRetractionHandle /*theHandle*/) throw (ObjectNotKnown, AttributeNotKnown, InvalidFederationTime, FederateInternalError){ D.Out(pdTrace, "reflectAttributeValues"); float x1 = 0 ; float y1 = 0 ; ULong valueLength ; char *attrValue ; D.Out(pdDebug, "reflectAttributeValues - nb attributs= %d", theAttributes.size()); for (unsigned int j=0 ; j<theAttributes.size(); j++) { AttributeHandle handle = theAttributes.getHandle(j); valueLength = theAttributes.getValueLength(j); attrValue = new char[valueLength] ; theAttributes.getValue(j, attrValue, valueLength); if (handle == AttrXID) { if (attrValue != NULL) { x1 = atof(attrValue); delete[] attrValue ; } else D.Out(pdError, "Fed: ERREUR: missing Attribute."); } else if (handle == AttrYID) { if (attrValue != NULL) { y1 = atof(attrValue); delete[] attrValue ; } else D.Out(pdError, "Fed: ERREUR: missing Attribute."); } else D.Out(pdError, "Fed: ERREUR: handle inconnu."); } objects.reflect(theObject, (int) x1, (int) y1);}// ----------------------------------------------------------------------------/** Callback : remove object instance */voidBillard::removeObjectInstance(ObjectHandle theObject, const FedTime &, const char *, EventRetractionHandle) throw (ObjectNotKnown, InvalidFederationTime, FederateInternalError){ objects.remove(theObject);}// ----------------------------------------------------------------------------/** Callback : time advance granted */voidBillard::timeAdvanceGrant(const FedTime& /*theTime*/) throw (InvalidFederationTime, TimeAdvanceWasNotInProgress, FederationTimeAlreadyPassed, FederateInternalError){ granted = true ;}// $Id: Billard.cc,v 3.7 2004/03/04 20:19:05 breholee Exp $
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -