示例#1
0
void CFellow::UpdateFellow(CFellow *pCFellow)
{
    char                                fellowName[256] = {'\0'};
    RTI::ULong                          fellowColor     = 0;
    RTI::ULong                          fellowPortrait  = 0;
    RTI::AttributeHandleValuePairSet    *theAttrs = NULL;

    try
    {
        theAttrs = RTI::AttributeSetFactory::create(3);
        if (pCFellow->GetFellowNameUpdate())
        {
            strcpy(fellowName, pCFellow->GetFellowName());
            theAttrs->add(pCFellow->GetFellowNameRtiId(), (char*)fellowName,
                sizeof(fellowName));
            pCFellow->SetFellowNameUpdate(RTI::RTI_FALSE);
        }
        if (pCFellow->GetFellowColorUpdate())
        {
            fellowColor = pCFellow->GetFellowColor();
            theAttrs->add(pCFellow->GetFellowColorRtiId(), (char*)&fellowColor,
                sizeof(fellowColor));
            pCFellow->SetFellowColorUpdate(RTI::RTI_FALSE);
        }
        if (pCFellow->GetFellowPortraitUpdate())
        {
            fellowPortrait = pCFellow->GetFellowPortrait();
            theAttrs->add(pCFellow->GetFellowPortraitRtiId(), (char*)&fellowPortrait,
                sizeof(fellowPortrait));
            pCFellow->SetFellowPortraitUpdate(RTI::RTI_FALSE);
        }
        if (theAttrs->size())
        {
            ms_rtiAmb->updateAttributeValues(pCFellow->GetInstanceId(),
                *theAttrs, "FellowUpdate");
            //ms_rtiAmb->tick(0.1, 2.0);
        }
        if (theAttrs)
        {
            theAttrs->empty();
            delete theAttrs;
        }
    }
    catch (RTI::Exception& e)
    {
        AfxMessageBox((CString)e._name);
    }
}
示例#2
0
//-----------------------------------------------------------------
//
// METHOD:
//     void Country::Update( RTIfedTime& newTime )
//
// PURPOSE:
//     Update the state of the Country's population based on
//     the new time value.  The deltaTime is calculated based
//     on the last time the Country object was updated and
//     the newTime passed in.  The deltaTime is multiplied by
//     the growth rate and current population to determine the
//     number of births in the deltaTime.  The population is
//     increased by the number of births.
//
// RETURN VALUES:
//     None. 
//
// HISTORY:
//     1) Created 11/6/96
//     2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
void Country::Update( RTI::FedTime& newTime )
{
   //------------------------------------------------------
   // we have advanced in time so calculate my next state.
   //------------------------------------------------------

   RTI::FedTime *pTime = RTI::FedTimeFactory::makeZero();
   (*pTime) = newTime;
   (*pTime) -= this->GetLastTime();
   // Set last time to new time
   this->SetLastTime( newTime );

   if ( !(pTime->isZero()))
   {
      SetPopulation( GetPopulation() + 
                     (GetPopulation()*ms_growthRate) );
   }

   if ( ms_rtiAmb )
   {
      //------------------------------------------------------
      // Update state of country
      //------------------------------------------------------
      try
      {
         //------------------------------------------------------
         // In order to send the values of our attributes, we must
         // construct an AttributeHandleValuePairSet (AHVPS) which
         // is a set comprised of attribute handles, values, and
         // the size of the values. CreateNVPSet() is a method 
         // defined on the Country class - it is not part of the RTI.
         // Look inside the method to see how to construct an AHVPS
         //------------------------------------------------------
         RTI::AttributeHandleValuePairSet* pNvpSet = this->CreateNVPSet();
         
         //------------------------------------------------------
         // Send the AHVPS to the federation.
         //
         // this call returns an event retraction handle but we 
         // don't support event retraction so no need to store it.
         //------------------------------------------------------
         (void) ms_rtiAmb->updateAttributeValues( this->GetInstanceId(),
                                                  *pNvpSet,
                                                  this->GetLastTimePlusLookahead(), 
                                                  NULL );
         // Must free the memory
         pNvpSet->empty();
         delete pNvpSet;
      }
      catch ( RTI::Exception& e )
      {
         cerr << "FED_HW: Error:" << e << endl;
      }
      
      // Periodically send an interaction to tell everyone Hello
      static int periodicMessage = 0;

      if ( (periodicMessage++%100) == 0 )
      {
         RTI::ParameterHandleValuePairSet* pParams = NULL;

                 //------------------------------------------------------
                 // Periodically stimulate an update of the "Name" 
                 // attribute for the benefit of late-arriving federates.
                 // It would be more correct to use 
                 // "requestClassAttributeValueUpdate" and 
                 // "provideAttributeValueUpdate", but let's keep things
                 // simple.
                 //------------------------------------------------------
         hasNameChanged = RTI::RTI_TRUE;

         //------------------------------------------------------
         // Set up the data structure required to push this
         // objects's state to the RTI.  The
         // ParameterHandleValuePairSet is similar to the AHVPS
         // except it contains ParameterHandles instead of
         // AttributeHandles.
         //------------------------------------------------------
         pParams = RTI::ParameterSetFactory::create( 1 );

         char *pMessage = "Hello World!";
         
         pParams->add( this->GetMessageRtiId(),
                       (char*) pMessage,
                       ((strlen(pMessage)+1)*sizeof(char)) );
         try
         {
            //------------------------------------------------------
            // this call returns an event retraction handle but we 
            // don't support event retraction so no need to store it.
            //------------------------------------------------------
            (void) ms_rtiAmb->sendInteraction( GetCommRtiId(), *pParams,
                                               this->GetLastTimePlusLookahead(),
                                               NULL );
         }
         catch ( RTI::Exception& e )
         {
            cerr << "FED_HW: Error:" << e << endl;
         }

         //------------------------------------------------------
         // Must free the memory:
         //    ParameterSetFactory::create() allocates memory on
         //    the heap.
         //------------------------------------------------------
         delete pParams;
      }
   }
   delete pTime;
}