📄 my_fed.cc
字号:
// -*- mode:C++ ; tab-width:4 ; c-basic-offset:4 ; indent-tabs-mode:nil -*-// ----------------------------------------------------------------------------// CERTI - HLA RunTime Infrastructure// Copyright (C) 2002, 2003 ONERA//// This file is part of CERTI//// CERTI is free software ; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation ; either version 2 of the License, or// (at your option) any later version.//// CERTI is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY ; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program ; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//// $Id: my_fed.cc,v 3.9 2003/03/21 15:06:46 breholee Exp $// ----------------------------------------------------------------------------#include <config.h>#include <memory.h>#include "my_fed.hh"#include "constants.hh"#include "PrettyDebug.hh"static pdCDebug D("FEDAMB", "(Fed_Amba) - ");extern bool verbose ;// ----------------------------------------------------------------------------//! Constructor.Fed::Fed(RTI::RTIambassador *rtia){ RTIA = rtia ; granted = false ; paused = false ; RemoteCount = 0 ; D.Out(pdInit, "Federate Ambassador created.");}// ----------------------------------------------------------------------------//! Destructor.Fed::~Fed(void){ D.Out(pdTerm, "Federate Ambassador destroyed.");}// ----------------------------------------------------------------------------//! Efface tous les objets locaux de la simulation a la date DeletionTime.voidFed::DeleteObjects(const FedTime& DeletionTime){ try { RTIA->deleteObjectInstance(Local.ID, DeletionTime, "DO"); D.Out(pdRegister, "Local object deleted from federation."); } catch (Exception &e) { D.Out(pdExcept, "**** Exception delete object : %d", &e); }}// ----------------------------------------------------------------------------//! discoverObjectInstance.voidFed::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(); } Remote[RemoteCount].ID = theObject ; printf("Discovered object %ld\n", theObject); RemoteCount++ ;}// ----------------------------------------------------------------------------//! Initializes all the IDs attributes by asking the RTIA.voidFed::GetHandles(void){ // Classes d'Objets BilleClassID = RTIA->getObjectClassHandle(CLA_BILLE); BouleClassID = RTIA->getObjectClassHandle(CLA_BOULE); D.Out(pdInit, "BilleClassID = %d, BouleClassID = %d.", BilleClassID, BouleClassID); // Attributs des classes d'Objets AttrXID = RTIA->getAttributeHandle(ATT_POSITION_X, BilleClassID); AttrYID = RTIA->getAttributeHandle(ATT_POSITION_Y, BilleClassID); AttrColorID = RTIA->getAttributeHandle(ATT_COLOR, BouleClassID); D.Out(pdInit, "AttrXID = %d, AttrYID = %d, AttrColorID = %d.", AttrXID, AttrYID, AttrColorID); // Interactions BingClassID = RTIA->getInteractionClassHandle(INT_BING); ParamBoulID = RTIA->getParameterHandle(PAR_BOUL, BingClassID); ParamDXID = RTIA->getParameterHandle(PAR_DX, BingClassID); ParamDYID = RTIA->getParameterHandle(PAR_DY, BingClassID); D.Out(pdInit, "BingClassID = %d, DX_ID = %d, DY_ID = %d", BingClassID, ParamDXID, ParamDYID);}// ----------------------------------------------------------------------------//! announceSynchronizationPoint.voidFed::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); }}// ----------------------------------------------------------------------------//! synchronisationPointRegistrationSucceeded.voidFed::synchronizationPointRegistrationSucceeded(const char *label) throw (FederateInternalError){ D.Out(pdProtocol, "CALLBACK : synchronizationPointRegistrationSucceeded with label %s", label);}// ----------------------------------------------------------------------------//! synchronisationPointRegistrationFailed.voidFed::synchronizationPointRegistrationFailed(const char *label) throw (FederateInternalError){ D.Out(pdProtocol, "CALLBACK : synchronizationPointRegistrationFailed with label %s", label);}// ----------------------------------------------------------------------------//! federationSynchronized.voidFed::federationSynchronized(const char *label) throw (FederateInternalError){ if (strcmp(label,"Init") == 0) { paused = false ; D.Out(pdProtocol, "CALLBACK : federationSynchronized with label %s", label); }}// ----------------------------------------------------------------------------//! Publish and subscribe Bille and Boule classes.voidFed::PublishAndsubscribe(void){ // 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. RTIA->subscribeObjectClassAttributes(BilleClassID, *AttributeSet, RTI_TRUE); // Publish Boule Objects. AttributeSet->add(AttrColorID); RTIA->publishObjectClass(BouleClassID, *AttributeSet); // Publish and subscribe to Bing interactions RTIA->subscribeInteractionClass(BingClassID, RTI_TRUE); RTIA->publishInteractionClass(BingClassID); AttributeSet->empty(); D.Out(pdInit, "Local Objects and Interactions published and subscribed.");}// ----------------------------------------------------------------------------//! receiveInteraction.voidFed::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 ; Boolean bille = RTI_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 (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); D.Out(pdDebug, "receiveInteraction - dx= %f", Local.dx); 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); D.Out(pdDebug, "receiveInteraction - dy= %f", Local.dy); delete[] parmValue ; } else D.Out(pdError, "Missing Attribute in RAV."); } else if (handle == ParamBoulID) { if (parmValue != NULL) { D.Out(pdDebug, "receiveInteraction(***) - Local.ID= %d", Local.ID); D.Out(pdDebug, "receiveInteraction(***) - parmValue= %s", parmValue); if (Local.ID == atof(parmValue)) bille = RTI_TRUE ; else bille = RTI_FALSE ; } else D.Out(pdError, "Unrecognized parameter handle"); } } if (bille) { Local.dx = dx1 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -