//------------------------------------------------------------------------------ // initNetwork() -- Initialize the multicast network //------------------------------------------------------------------------------ bool NetIO::initNetwork() { // --- // Create the federate unique ambassador // --- fedAmb = createFederateAmbassador(); bool ok = (fedAmb != 0); // --- // join the federation // --- if (ok) { ok = createAndJoinFederation(); doTick(); } // initialize time constraints, etc. //if (getRegulating() || getConstrained()) //{ // setLookAhead(deltaTime); // setTimeIncrement(deltaTime); // setFederationTime(0.0); // initializeTimeManagement(); // doTick(); //} if (ok) { ok = publishAndSubscribe(); doTick(); } return true; }
//------------------------------------------------------------------------------------------ // INSTANCE METHODS //------------------------------------------------------------------------------------------ /////////////////////////////////////////////////////////////////////////// ////////////////////////// Main Simulation Method ///////////////////////// /////////////////////////////////////////////////////////////////////////// void ExampleCPPFederate::runFederate( std::wstring federateName ) { ///////////////////////////////// // 1. create the RTIambassador // ///////////////////////////////// RTIambassadorFactory factory = RTIambassadorFactory(); this->rtiamb = factory.createRTIambassador().release(); /////////////////////////// // 2. connect to the RTI // /////////////////////////// // we need the federate ambassador set up before we can connect this->fedamb = new ExampleFedAmb(); try { rtiamb->connect( *this->fedamb, HLA_EVOKED ); } catch( ConnectionFailed& connectionFailed ) { wcout << L"Connection failed: " << connectionFailed.what() << endl; } catch( InvalidLocalSettingsDesignator& settings ) { wcout << L"Connection failed, InvalidLocalSettingsDesignator: " << settings.what() << endl; } catch( UnsupportedCallbackModel& callbackModel ) { wcout << L"Connection failed, UnsupportedCallbackModel: " << callbackModel.what() << endl; } catch( AlreadyConnected& connected ) { wcout << L"Connection failed, AlreadyConnected: " << connected.what() << endl; } catch( RTIinternalError& error ) { wcout << L"Connection failed, Generic Error: " << error.what() << endl; } ////////////////////////////////////////// // 3. create and join to the federation // ////////////////////////////////////////// // create // NOTE: some other federate may have already created the federation, // in that case, we'll just try and join it try { vector<wstring> foms; foms.push_back( L"testfom.fed" ); //rtiamb->createFederationExecution( L"ExampleFederation", L"testfom.fed" ); rtiamb->createFederationExecution( L"ExampleFederation", foms ); wcout << L"Created Federation" << endl; } catch( FederationExecutionAlreadyExists& exists ) { wcout << L"Didn't create federation, it already existed" << endl; } //////////////////////////// // 4. join the federation // //////////////////////////// rtiamb->joinFederationExecution( federateName, L"Example Federate", L"ExampleFederation" ); wcout << L"Joined Federation as " << federateName << endl; // initialize the handles - have to wait until we are joined initializeHandles(); //////////////////////////////// // 5. announce the sync point // //////////////////////////////// // announce a sync point to get everyone on the same page. if the point // has already been registered, we'll get a callback saying it failed, // but we don't care about that, as long as someone registered it VariableLengthData tag( (void*)"", 1 ); rtiamb->registerFederationSynchronizationPoint( READY_TO_RUN, tag ); while( fedamb->isAnnounced == false ) { rtiamb->evokeMultipleCallbacks( 0.1, 1.0 ); } // WAIT FOR USER TO KICK US OFF // So that there is time to add other federates, we will wait until the // user hits enter before proceeding. That was, you have time to start // other federates. waitForUser(); /////////////////////////////////////////////////////// // 6. achieve the point and wait for synchronization // /////////////////////////////////////////////////////// // tell the RTI we are ready to move past the sync point and then wait // until the federation has synchronized on rtiamb->synchronizationPointAchieved( READY_TO_RUN ); wcout << L"Achieved sync point: " << READY_TO_RUN << L", waiting for federation..." << endl; while( fedamb->isReadyToRun == false ) { rtiamb->evokeMultipleCallbacks( 0.1, 1.0 ); } ///////////////////////////// // 7. enable time policies // ///////////////////////////// // in this section we enable/disable all time policies // note that this step is optional! enableTimePolicy(); wcout << L"Time Policy Enabled" << endl; ////////////////////////////// // 8. publish and subscribe // ////////////////////////////// // in this section we tell the RTI of all the data we are going to // produce, and all the data we want to know about publishAndSubscribe(); wcout << L"Published and Subscribed" << endl; ///////////////////////////////////// // 9. register an object to update // ///////////////////////////////////// ObjectInstanceHandle objectHandle = registerObject(); wcout << L"Registered Object, handle=" << objectHandle << endl; ///////////////////////////////////// // 10. do the main simulation loop // ///////////////////////////////////// // here is where we do the meat of our work. in each iteration, we will // update the attribute values of the object we registered, and will // send an interaction. int i; for( i = 0; i < 20; i++ ) { // 9.1 update the attribute values of the instance // updateAttributeValues( objectHandle ); // 9.2 send an interaction sendInteraction(); // 9.3 request a time advance and wait until we get it advanceTime( 1.0 ); wcout << L"Time Advanced to " << fedamb->federateTime << endl; } ////////////////////////////////////// // 11. delete the object we created // ////////////////////////////////////// deleteObject( objectHandle ); wcout << L"Deleted Object, handle=" << objectHandle << endl; //////////////////////////////////// // 12. resign from the federation // //////////////////////////////////// rtiamb->resignFederationExecution( NO_ACTION ); wcout << L"Resigned from Federation" << endl; //////////////////////////////////////// // 13. try and destroy the federation // //////////////////////////////////////// // NOTE: we won't die if we can't do this because other federates // remain. in that case we'll leave it for them to clean up try { rtiamb->destroyFederationExecution( L"ExampleFederation" ); wcout << L"Destroyed Federation" << endl; } catch( FederationExecutionDoesNotExist& dne ) { wcout << L"No need to destroy federation, it doesn't exist" << endl; } catch( FederatesCurrentlyJoined& fcj ) { wcout << L"Didn't destroy federation, federates still joined" << endl; } ///////////////////////////////// // 14. disconnect from the RTI // ///////////////////////////////// // disconnect from the RTI this->rtiamb->disconnect(); ////////////////// // 15. clean up // ////////////////// delete this->rtiamb; }