Sending Simulation Object Attributes Updates

Now you’ve created your simulation objects, you can update its attributes and send to all Federates inside your Federation that have subscribed to this. If you’re not ready yet, please go to Your first Federate object post.

Updating attributes is very simple. All you will need is to take the Java object representing the RTI object you want to send updates, encode this values and send.

Lets change our CoreClass to add some useful methods. It is not a rule, but I like to do this way: A Java class to represent the Federate ( connect, join, resign, destroy ), a Java class to represent the object class ( create, update, destroy objects ) and a Java class to represent the created simulation objects (  hold object values and do the simulation itself ).

public void updateAttributes( CoreObject object ) throws Exception {
  EncoderFactory encoderFactory = RtiFactoryFactory.getRtiFactory().getEncoderFactory();
  HLAunicodeString serialNumberValue = encoderFactory.createHLAunicodeString( object.getSerialNumber() );
  HLAunicodeString ownerNodeValue = encoderFactory.createHLAunicodeString( object.getOwnerNode() );
  AttributeHandleValueMap attributes = rtiamb.getAttributeHandleValueMapFactory().create(2);
  attributes.put( serialNumberHandle, serialNumberValue.toByteArray() );
  attributes.put( ownerNodeHandle, ownerNodeValue.toByteArray() );
  rtiamb.updateAttributeValues( object.getHandle(), attributes, "My Updates".getBytes() );

First, you will need the EncoderFactory to encode the String from CoreObject to HLAunicodeString used by RTI ( see our SOM file ). You need to create an AttributeHandleValueMap with 2 positions to hold the two attributes. This is an initial value and can grow if you add more attributes.

Now you must put the attributes values in the AttributeHandleValueMap with the handle for each attribute class and its value. When you’re done, call  updateAttributeValues() and tell what object is having its attributes updated by giving the object handle ( not the class handle ).

rtiamb.updateAttributeValues( OBJECT_HANDLE, ATTRIBUTE_LIST, A_TAG );
CoreClass myCoreClass = new CoreClass( rtiamb );
myCoreClass.createNew( "The Owner", "ABC123" );
myCoreClass.updateAttributes( myCoreClass.getObjects().get( 0 ) );

All Federates that subscribed to this attributes will receive the updates.

Add Comment