/*
 * This method will update all the values of the given object instance. It will
 * set each of the values to be a string which is equal to the name of the
 * attribute plus the current time. eg "aa:10.0" if the time is 10.0.
 * <p/>
 * Note that we don't actually have to update all the attributes at once, we
 * could update them individually, in groups or not at all!
 */
void ExampleCPPFederate::updateAttributeValues( RTI::ObjectHandle objectHandle )
{
	///////////////////////////////////////////////
	// create the necessary container and values //
	///////////////////////////////////////////////
	// create the collection to store the values in, as you can see
	// this is quite a lot of work
	RTI::AttributeHandleValuePairSet *attributes = RTI::AttributeSetFactory::create( 3 );

	// generate the new values
	// we use EncodingHelpers to make things nice friendly for both Java and C++
	char aaValue[16], abValue[16], acValue[16];
	sprintf( aaValue, "aa:%f", getLbts() );
	sprintf( abValue, "ab:%f", getLbts() );
	sprintf( acValue, "ac:%f", getLbts() );
	attributes->add( aaHandle, aaValue, (RTI::ULong)strlen(aaValue)+1 );
	attributes->add( abHandle, abValue, (RTI::ULong)strlen(abValue)+1 );
	attributes->add( acHandle, acValue, (RTI::ULong)strlen(acValue)+1 );

	//////////////////////////
	// do the actual update //
	//////////////////////////
	rtiamb->updateAttributeValues( objectHandle, *attributes, "hi!" );

	// note that if you want to associate a particular timestamp with the
	// update. here we send another update, this time with a timestamp:
	RTIfedTime time = fedamb->federateTime + fedamb->federateLookahead;
	rtiamb->updateAttributeValues( objectHandle, *attributes, time, "hi!" );

	// clean up
	delete attributes;
}
Beispiel #2
0
//-----------------------------------------------------------------
//
// METHOD:
//     RTI::AttributeHandleValuePairSet* Country::CreateNVPSet()
//
// PURPOSE:
//     Create a name value pair set (aka handle value pair) for
//     the changed attributes of this country object.
//
// RETURN VALUES:
//     RTI::AttributeHandleValuePairSet* containing the
//     attributes that have changed in the instance of Country. 
//
// HISTORY:
//     1) Created 11/6/96
//     2) Updated to RTI 1.3 3/26/98
//
//-----------------------------------------------------------------
RTI::AttributeHandleValuePairSet* Country::CreateNVPSet()
{
   RTI::AttributeHandleValuePairSet* pCountryAttributes = NULL;

   // Make sure the RTI Ambassador is set.
   if ( ms_rtiAmb )
   {
      //------------------------------------------------------
      // Set up the data structure required to push this
      // object's state to the RTI.
      //------------------------------------------------------
      pCountryAttributes = RTI::AttributeSetFactory::create( 2 );

      if ( hasNameChanged == RTI::RTI_TRUE )
      {
         // We don't do any encoding here since the name type
         // is a string.
         pCountryAttributes->add( this->GetNameRtiId(),
                                  (char*) this->GetName(),
                                  ((strlen(this->GetName())+1)*sizeof(char)) );
      }
      
      if ( hasPopulationChanged == RTI::RTI_TRUE )
      {
         // Here we are encoding the double so that it is in a
         // common format so that federates on other platforms
         // know how to read it.
#if defined(_X86_) || defined(i386)
         double tmp;
         *((long*)&tmp) = htonl(*(((long*)&this->GetPopulation()) + 1));
         *(((long*)&tmp) + 1) = htonl(*((long*)&this->GetPopulation()));
         pCountryAttributes->add( this->GetPopulationRtiId(),
                                  (char*)&tmp,
                                  sizeof(double) );
#elif defined(__alpha)
         double x;
         double pop = this->GetPopulation();
         cvt_ftof(&pop, CVT_BIG_ENDIAN_IEEE_T, &x, CVT_IEEE_T, 0);
         pCountryAttributes->add( this->GetPopulationRtiId(), 
                                  (char*)&x, sizeof(double));
#else
         pCountryAttributes->add( this->GetPopulationRtiId(),
                                  (char*) &this->GetPopulation(),
                                  sizeof(double) );
#endif
      }
   }

   // pCountryAttributes is allocated on the heap and must be
   // deallocated by the federate.
   return pCountryAttributes;
}
Beispiel #3
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);
    }
}
////////////////////////////////////////////////
// TEST: testReflectTSOWithInvalidAttribute() //
////////////////////////////////////////////////
void ReflectAttributesTest::testReflectTSOWithInvalidAttribute()
{
	RTI::AttributeHandleValuePairSet *set = defaultFederate->createAHVPS( 2 );
	set->add( aaHandle, "aa", 3 );
	set->add( 10000000, "na", 3 );
	
	try
	{
		RTIfedTime time = 10.0;
		defaultFederate->rtiamb->updateAttributeValues( theObject, *set, time, "NA" );
		delete set;
		failTestMissingException( "AttributeNotDefined",
		                          "updating attributes with invalid attribute" );
	}
	catch( RTI::AttributeNotDefined& attnd )
	{
		// success
		delete set;
	}
	catch( RTI::Exception& e )
	{
		delete set;
		failTestWrongException( "AttributeNotDefined", e,
		                        "updating attributes with invalid attribute" );
	}
}
Beispiel #5
0
RTI::AttributeHandleValuePairSet* CFellow::CreateNVPSet()
{
    RTI::AttributeHandleValuePairSet *pCFellowAttrs = NULL;

    if (ms_rtiAmb)
    {
        pCFellowAttrs = RTI::AttributeSetFactory::create(3);

        if (m_bNameChanged)
        {
            pCFellowAttrs->add(this->GetFellowNameRtiId(),
                (char*)this->GetFellowName(),
                ((strlen(this->GetFellowName()) + 1) * sizeof(char)));
        }
        if (m_bColorChanged)
        {
            RTI::ULong tmp;
            *((long*)&tmp) = htonl(*(((long*)&this->GetFellowColor()) + 1));
            *(((long*)&tmp) + 1) = htonl(*((long*)&this->GetFellowColor()));
            pCFellowAttrs->add(this->GetFellowColorRtiId(),
                (char*)&tmp,
                sizeof(RTI::ULong));
        }
        if (m_bPortraitChanged)
        {
            RTI::ULong tmp;
            *((long*)&tmp) = htonl(*(((long*)&this->GetFellowPortrait()) + 1));
            *(((long*)&tmp) + 1) = htonl(*((long*)&this->GetFellowPortrait()));
            pCFellowAttrs->add(this->GetFellowPortraitRtiId(),
                (char*)&tmp,
                sizeof(RTI::ULong));
        }
    }

    return pCFellowAttrs;
}
///////////////////////////////////////////////
// TEST: testReflectROWithUnownedAttribute() //
///////////////////////////////////////////////
void ReflectAttributesTest::testReflectROWithUnownedAttribute()
{
	RTI::AttributeHandleValuePairSet *set = defaultFederate->createAHVPS( 1 );
	set->add( aaHandle, "aa", 3 );
	
	try
	{
		listenerFederate->rtiamb->updateAttributeValues( theObject, *set, "NA" );
		delete set;
		failTestMissingException( "AttributeNotOwned", "updating attributes not owned" );
	}
	catch( RTI::AttributeNotOwned& ano )
	{
		// success
		delete set;
	}
	catch( RTI::Exception& e )
	{
		delete set;
		failTestWrongException( "AttributeNotOwned", e, "updating attributes not owned" );
	}
}