![]() |
MAK RTIspy API Documentation for HLA 1516
|
rtiSimple13.cxx and rtiSimpleFedAmb13.cxx (rtiSimpleFedAmb13.h) have the RTI calls and federate ambassador calls for the HLA 1.3 version of rtisimple.
This page has the code for the following files:
/******************************************************************************* ** Copyright (c) 2004 MaK Technologies, Inc. ** All rights reserved. *******************************************************************************/ // A simple federate that updates an object with attributes whose values // are the name of the attribute. Objects from other simple federates // are discovered and reflected. The string values are byte encoded to allow // compatibility between 1.3 and 1516 #pragma warning(disable: 4786) #pragma warning(disable: 4290) #ifdef WIN32 #include <winsock2.h> #include <process.h> #else #include <unistd.h> #endif #include <stdio.h> #include <cstdlib> #include <cstring> #include <map> #include <string> #include <iostream> #include <sstream> #include "RTI.hh" #include "rtiSimpleFedAmb13.h" #include "rtiSimpleKeyboard.h" using namespace std; // Handles keyboard input without blocking keyboard input; // Map between strings and attribute handles typedef std::map<std::string, RTI::AttributeHandle> DtAttrNameHandleMap; // Map between strings and parameter handles typedef std::map<std::string, RTI::ParameterHandle> DtParamNameHandleMap; // Data shared between federate and federate ambassador DtTalkAmbData theAmbData; // The federate handle RTI::FederateHandle theFederateHandle; // The object class name std::string theClassName = "BaseEntity"; // The interaction class name std::string theInterClassName = "WeaponFire"; // The object class handle (to be retrieved from RTI). RTI::ObjectClassHandle theClassHandle; // The interaction class handle (to be retrieved from RTI). RTI::InteractionClassHandle theInterClassHandle; // The object instance handle (to be retrieved from the RTI). RTI::ObjectHandle theObjectHandle; // Map between strings and attribute handles DtAttrNameHandleMap theAttrNameHandleMap; // Map between strings and parameter handles DtParamNameHandleMap theParamNameHandleMap; // Create the federation execution void createFedEx(RTI::RTIambassador & rtiAmb, std::string const& fedName, std::string const& fedFile) { cout << "createFederationExecution " << fedName.c_str() << " " << fedFile.c_str() << endl; try { rtiAmb.createFederationExecution(fedName.c_str(), fedFile.c_str()); } catch(RTI::FederationExecutionAlreadyExists& ex) { cout << "Could not create Federation Execution: " << "FederationExecutionAlreadyExists: " << ex._name << " " << ex._reason << endl; } catch(RTI::Exception& ex) { cout << "Could not create Federation Execution: " << endl << "RTI Exception: " << ex._name << " " << ex._reason << endl; exit(0); } rtiAmb.tick(0.1, 0.2); cout << "Federation Created" << endl; } // Join the federation execution void joinFedEx( RTI::RTIambassador & rtiAmb, MyFederateAmbassador* fedAmb, std::string const& federateType, std::string const& federationName) { bool joined=false; const int maxTry = 10; int numTries = 0; cout << "joinFederationExecution " << federateType.c_str() << " " << federationName.c_str() << endl; while (!joined && numTries++ < maxTry) { try { theFederateHandle = rtiAmb.joinFederationExecution(federateType.c_str(), federationName.c_str(), fedAmb); joined = true; } catch(RTI::FederationExecutionDoesNotExist) { cout << "FederationExecutionDoesNotExist, try " << numTries << "out of " << maxTry << endl; continue; } catch(RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl; return; } rtiAmb.tick(0.1, 0.2); } if (joined) cout << "Joined Federation." << endl; else { cout << "Giving up." << endl; rtiAmb.destroyFederationExecution(federationName.c_str()); exit(0); } } // Resign and destroy the federation execution void resignAndDestroy( RTI::RTIambassador & rtiAmb, std::string const& federationName) { cout << "Resign and Destroy Federation" << endl; rtiAmb.resignFederationExecution(RTI::DELETE_OBJECTS); rtiAmb.destroyFederationExecution(federationName.c_str()); } // Save federation // Request federation save and wait for initiate // Return save status bool requestFederationSave(RTI::RTIambassador & rtiAmb, std::string const& label) { bool saveOk = true; int count = 0; // Turn the signals off theAmbData.myReceivedInitiateFederateSave = theAmbData.myReceivedFederationSaved = theAmbData.myReceivedFederationNotSaved = false; cout << "Request federation save with label " << label.c_str() << endl; // Request the save and wait for the signal that the save has been initiated // or that the federation has not saved (could be that some other federate // resigns during save) rtiAmb.requestFederationSave(label.c_str()); while (count++ < 100 && !theAmbData.myReceivedInitiateFederateSave && !theAmbData.myReceivedFederationNotSaved) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedInitiateFederateSave ) { saveOk = false; if ( !theAmbData.myReceivedFederationNotSaved ) { cout << "Timed out waiting for initiate federate save\n"; } } return saveOk; } // Wait for federation saved // Return save status bool waitForFederationSaved(RTI::RTIambassador & rtiAmb) { bool saveOk = true; int count = 0; // Wait until the RTI signals that the entire federation completed the save while ( count++ < 100 && !theAmbData.myReceivedFederationNotSaved && !theAmbData.myReceivedFederationSaved ) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedFederationSaved ) { saveOk = false; if ( !theAmbData.myReceivedFederationNotSaved ) { cout << "Timed out waiting for federation saved\n"; } } return saveOk; } // Perform save federation // Return save status bool saveFederation(RTI::RTIambassador & rtiAmb, std::string const& label, bool performRequest) { bool saveOk = true; int count = 0; try { if ( performRequest ) { saveOk = requestFederationSave(rtiAmb, label); } if ( theAmbData.myReceivedInitiateFederateSave ) { // At this point, the save has been initiated // and the federate cannot invoke any other calls except // to complete the save (or resign). It signals to the RTI that // it will begin saving its own local state cout << "Federate save begun\n"; rtiAmb.federateSaveBegun(); // Here the federate would save its own state including any context // information relating to the RTI // (i.e. what classes are published and subscribed // object instance handles for registered and discovered objects, etc. // Once the federate saves is own data, it signals to the RTI that its // save is complete. cout << "Federate save complete\n"; rtiAmb.federateSaveComplete(); // Wait until the RTI signals that the entire federation completed the save saveOk = waitForFederationSaved(rtiAmb); } } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl; cout << "Could not save federation " << label.c_str() << endl; saveOk = false; } catch(exception& stdEx) { cout << "Standard exception: " << stdEx.what() << endl; cout << "Could not save federation " << label.c_str() << endl; saveOk = false; } catch(...) { cout << "Unknown exception\n"; cout << "Could not save federation " << label.c_str() << endl; saveOk = false; } // Turn the signals off theAmbData.myReceivedInitiateFederateSave = theAmbData.myReceivedFederationSaved = theAmbData.myReceivedFederationNotSaved = false; return saveOk; } // Restore federation // Request the restore and wait for the response. // Return request status bool requestFederationRestore(RTI::RTIambassador & rtiAmb, std::string const& label) { int count = 0; bool restoreOk = true; cout << "Request federation restore with label " << label.c_str() << endl; // Turn the signals off theAmbData.myReceivedRequestFederationRestoreSucceeded = theAmbData.myReceivedRequestFederationRestoreFailed = theAmbData.myReceivedFederationRestoreBegun = theAmbData.myReceivedInitiateFederateRestore = theAmbData.myReceivedFederationRestored = theAmbData.myReceivedFederationNotRestored = false; rtiAmb.requestFederationRestore(label.c_str()); while (count++ < 100 && !theAmbData.myReceivedRequestFederationRestoreSucceeded && !theAmbData.myReceivedRequestFederationRestoreFailed) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedRequestFederationRestoreSucceeded ) { restoreOk = false; if ( !theAmbData.myReceivedRequestFederationRestoreFailed ) { cout << "Timed out waiting for request federation restore succeeded.\n"; } } return restoreOk; } // Wait for restore begun // Return begun status bool waitForRestoreBegun(RTI::RTIambassador & rtiAmb) { bool restoreOk = true; // Wait for the restore begun cout << "Wait for restore begun\n"; int count = 0; while (count++ < 100 && !theAmbData.myReceivedFederationRestoreBegun && !theAmbData.myReceivedFederationNotRestored) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedFederationRestoreBegun ) { restoreOk = false; if (!theAmbData.myReceivedFederationNotRestored) { cout << "Timed out waiting for federation restore begun.\n"; } } return restoreOk; } // Wait for initiate restore // Return initiate status bool waitForInitiateRestore(RTI::RTIambassador & rtiAmb) { bool restoreOk = true; // Wait for the initiate restore cout << "Wait for initiate restore\n"; int count = 0; while (count++ < 100 && !theAmbData.myReceivedInitiateFederateRestore && !theAmbData.myReceivedFederationNotRestored) { rtiAmb.tick(0.1, 0.2); } if ( theAmbData.myReceivedFederationNotRestored ) { restoreOk = false; } else if ( !theAmbData.myReceivedInitiateFederateRestore ) { restoreOk = false; cout << "Timed out waiting for initiate federate restore.\n"; } else if ( theAmbData.myRestoreFederateHandle != theFederateHandle ) { // In general, a federate must be able to restore any saved state of // a federate of the same type. Even if a federate submits // a unique federate type string during join, the current federate // and object handles may not match those being restored. // This simplistic federate implementation cannot handle the case where // the federate and object handles are different. cout << "Restore initiated with handle " << theAmbData.myRestoreFederateHandle << " does not match current handle " << theFederateHandle << endl; cout << "Unable to complete restore\n"; restoreOk = false; // Will revert to state before restore was begun rtiAmb.federateRestoreNotComplete(); count = 0; while ( count++ < 100 && !theAmbData.myReceivedFederationNotRestored ) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedFederationNotRestored ) { cout << "Timed out waiting for federation not restored.\n"; } } return restoreOk; } // Wait for federation (not) restored // Return restore status bool waitForFederationRestored(RTI::RTIambassador & rtiAmb) { bool restoreOk = true; // Wait until the RTI signals that the entire federation completed the restore cout << "Wait for federation restored\n"; int count = 0; while ( count++ < 100 && !theAmbData.myReceivedFederationRestored && !theAmbData.myReceivedFederationNotRestored ) { rtiAmb.tick(0.1, 0.2); } if ( !theAmbData.myReceivedFederationRestored ) { restoreOk = false; if ( !theAmbData.myReceivedFederationNotRestored ) { cout << "Timed out waiting for federation restored.\n"; } } return restoreOk; } // Perform restore federation // Return restore status bool restoreFederation(RTI::RTIambassador & rtiAmb, std::string const& label, bool performRequest) { bool restoreOk = true; int count = 0; try { if ( performRequest ) { if ( requestFederationRestore(rtiAmb, label) ) { restoreOk = waitForRestoreBegun(rtiAmb); } } if ( theAmbData.myReceivedFederationRestoreBegun ) { // At this point, the restore has begun // and the federate cannot invoke any other calls except // to complete the restore (or resign). It waits for the // initiate restore to begin restoring its local state. if ( restoreOk = waitForInitiateRestore(rtiAmb) ) { // Here the federate would restore its own state including any context // information relating to the RTI // (i.e. what classes are published and subscribed // object instance handles for registered and discovered objects, etc. // Once the federate restores its own data, it signals to the RTI that its // restore is complete. wcout << L"Federate restore complete\n"; rtiAmb.federateRestoreComplete(); // If the federate was unable to restore its state, it would respond with // federateRestoreNotComplete(); // Now wait for remaining federation to be restored restoreOk = waitForFederationRestored(rtiAmb); } } } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl; cout << "Could not save federation " << label.c_str() << endl; restoreOk = false; } catch(exception& stdEx) { cout << "Standard exception: " << stdEx.what() << endl; cout << "Could not save federation " << label.c_str() << endl; restoreOk = false; } catch(...) { cout << "Unknown exception\n"; cout << "Could not save federation " << label.c_str() << endl; restoreOk = false; } // Turn the signals off theAmbData.myReceivedRequestFederationRestoreSucceeded = theAmbData.myReceivedRequestFederationRestoreFailed = theAmbData.myReceivedFederationRestoreBegun = theAmbData.myReceivedInitiateFederateRestore = theAmbData.myReceivedFederationRestored = theAmbData.myReceivedFederationNotRestored = false; return restoreOk; } // Publish and subscribe the object class attributes. // Register an object instance of the class. bool publishSubscribeAndRegisterObject(RTI::RTIambassador & rtiAmb) { // Get the object class handle try { theClassHandle = rtiAmb.getObjectClassHandle(theClassName.c_str()); theAmbData.objectClassMap[theClassHandle] = theClassName; } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not get object class handle: " << theClassName.c_str() << endl; return false; } // Get the attribute handles and construct the name-handle map std::string attrName; try { attrName = "AccelerationVector"; theAttrNameHandleMap[attrName] = rtiAmb.getAttributeHandle(attrName.c_str(), theClassHandle); attrName = "VelocityVector"; theAttrNameHandleMap[attrName] = rtiAmb.getAttributeHandle(attrName.c_str(), theClassHandle); attrName = "Orientation"; theAttrNameHandleMap[attrName] = rtiAmb.getAttributeHandle(attrName.c_str(), theClassHandle); attrName = "DeadReckoningAlgorithm"; theAttrNameHandleMap[attrName] = rtiAmb.getAttributeHandle(attrName.c_str(), theClassHandle); attrName = "WorldLocation"; theAttrNameHandleMap[attrName] = rtiAmb.getAttributeHandle(attrName.c_str(), theClassHandle); } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not get attribute handle " << attrName.c_str() << endl; return false; } // Construct an attribute handle set RTI::AttributeHandleSet* hSet = RTI::AttributeHandleSetFactory::create(theAttrNameHandleMap.size()); for (DtAttrNameHandleMap::iterator iter = theAttrNameHandleMap.begin(); iter != theAttrNameHandleMap.end(); iter++) { hSet->add(iter->second); } // Publish and subscribe int cnt=0; try { rtiAmb.publishObjectClass(theClassHandle, *hSet); rtiAmb.tick(0.1, 0.2); cnt=1; rtiAmb.subscribeObjectClassAttributes(theClassHandle, *hSet); rtiAmb.tick(0.1, 0.2); } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not " << (cnt ? "publish" : "subscribe") << endl; delete hSet; return false; } std::string objectName("Talk"); // Register the object instance try { unsigned int objId = abs(getpid()); std::stringstream pid; pid << objId; objectName += pid.str(); theObjectHandle = rtiAmb.registerObjectInstance(theClassHandle, objectName.c_str()); //theObjectHandle = rtiAmb.registerObjectInstance(theClassHandle); // Add name-handle to map theAmbData.objectInstanceMap[theObjectHandle] = rtiAmb.getObjectInstanceName(theObjectHandle); rtiAmb.tick(0.1, 0.2); } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not Register Object " << objectName.c_str() << " with class " << theClassName.c_str() << endl; delete hSet; return false; } cout << "Registered object " << objectName.c_str() << " with class name " << theClassName.c_str() << endl; delete hSet; return true; } // Publish and Subscribe to an interaction class bool publishAndSubscribeInteraction(RTI::RTIambassador & rtiAmb) { // Get the interaction class handle try { theInterClassHandle = rtiAmb.getInteractionClassHandle(theInterClassName.c_str()); theAmbData.interactionClassMap[theInterClassHandle] = theInterClassName; } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not get interaction class handle: " << theInterClassName.c_str() << endl; return false; } // Get the parameter handles and construct the name-handle map std::string paramName; try { paramName = "EventIdentifier"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "FireControlSolutionRange"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "FireMissionIndex"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "FiringLocation"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "FiringObjectIdentifier"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "FuseType"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "InitialVelocityVector"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "MunitionObjectIdentifier"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "MunitionType"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "QuantityFired"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "RateOfFire"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "TargetObjectIdentifier"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); paramName = "WarheadType"; theParamNameHandleMap[paramName] = rtiAmb.getParameterHandle(paramName.c_str(), theInterClassHandle); } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not get parameter handle " << paramName.c_str() << endl; return false; } // Publish and subscribe int cnt=0; try { rtiAmb.publishInteractionClass(theInterClassHandle); rtiAmb.tick(0.1, 0.2); cnt=1; rtiAmb.subscribeInteractionClass(theInterClassHandle); rtiAmb.tick(0.1, 0.2); } catch (RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl << "Could not " << (cnt ? "publish" : "subscribe") << " to interaction." << endl; return false; } cout << "Subscribed to interaction class: " << theInterClassName.c_str() << " with handle: " << theInterClassHandle << endl; return true; } int main(int argc, char** argv) { std::cout << "MAK rtiSimple version 1.3" << std::endl; try { // Federate and Federation info std::string federationName("MAKsimple"); std::string federationFile("MAKsimple.fed"); std::string federateType("rtisimple13"); if (argc > 1) { if (strcmp(argv[1], "-h") == 0) { cout << "Usage: rtisimple13 [federtionName] [federationFile] [federateType]" << endl; cout << "default values: " << "\"" << federationName.c_str() << "\" " << "\"" << federationFile.c_str() << "\" " << "\"" << federateType.c_str() << "\" " << endl; return 0; } federationName = argv[1]; } if (argc > 2) { federationFile = argv[2]; } if (argc > 3) { federateType = argv[3]; } // RTI and Federate Ambassadors RTI::RTIambassador rtiAmb; MyFederateAmbassador fedAmb(theAmbData); RTI::AttributeHandleValuePairSet *attrValues = 0; RTI::ParameterHandleValuePairSet *paramValues = 0; #ifdef WIN32 WSADATA data; WSAStartup(MAKEWORD(1,1), &data); #endif rtiAmb.tick(); long count=0; bool doConnect = true; bool connected = false; while (1) { if (doConnect) { doConnect = false; // Create the federation createFedEx(rtiAmb, federationName, federationFile); // Join the federation joinFedEx(rtiAmb, &fedAmb, federateType, federationName); // Publish, subscribe and register and object if (!publishSubscribeAndRegisterObject(rtiAmb)) { resignAndDestroy(rtiAmb, federationName); return 0; } // Publish and subscribe to the required interaction if (!publishAndSubscribeInteraction(rtiAmb)) { resignAndDestroy(rtiAmb, federationName); return 0; } // Construct an attribute handle value pair set with the // values containing the attribute names attrValues = RTI::AttributeSetFactory::create(theAttrNameHandleMap.size()); for (DtAttrNameHandleMap::iterator iter = theAttrNameHandleMap.begin(); iter != theAttrNameHandleMap.end(); iter++) { // Attribute values will be just the name of the attribute. attrValues->add(iter->second, iter->first.c_str(), iter->first.length()+1); } // Construct a parameter handle value pair set with the // values containing the parameter names paramValues = RTI::ParameterSetFactory::create(theParamNameHandleMap.size()); for (DtParamNameHandleMap::iterator iter2 = theParamNameHandleMap.begin(); iter2 != theParamNameHandleMap.end(); iter2++) { // Parameter values will be just the name of the attribute. paramValues->add(iter2->second, iter2->first.c_str(), iter2->first.length()+1); } connected = true; } else if (connected) { std::stringstream ss; ss << "1.3-" << count++; std::string tag(ss.str()); // Update the object rtiAmb.updateAttributeValues( theObjectHandle, *attrValues, tag.c_str()); // Send an interaction every few passes if ( count % 5 == 0 ) { cout << "Sending interaction..." << endl; rtiAmb.sendInteraction( theInterClassHandle, *paramValues, tag.c_str()); } rtiAmb.tick(0.1, 0.5); if ( theAmbData.myReceivedInitiateFederateSave ) { // Perform a save operation saveFederation(rtiAmb, theAmbData.mySaveLabel, false); } if ( theAmbData.myReceivedFederationRestoreBegun ) { // Perform a restore operation restoreFederation(rtiAmb, theAmbData.myRestoreLabel, false); } } int key = input.keybrdTick(); if (key < 0) { break; } else if ('c' == key || 'C' == key ) { doConnect = !connected; } else if ('d' == key || 'D' == key ) { if (connected) { try { resignAndDestroy(rtiAmb, federationName); if (attrValues) { delete attrValues; attrValues = 0; } if (paramValues) { delete paramValues; paramValues = 0; } connected = false; } catch(RTI::Exception& ex) { cout << "RTI Exception: " << ex._name << " " << ex._reason << endl; } } } else if ('s' == key || 'S' == key ) { if (connected) { // Initiate a save operation const std::string saveLabel("rtiSimpleSave"); if (!saveFederation(rtiAmb, saveLabel, true)) { // Save failed break; } } } else if ('r' == key || 'R' == key ) { if (connected) { // Initiate a restore operation const std::string savedLabel("rtiSimpleSave"); if (!restoreFederation(rtiAmb, savedLabel, true)) { // Restore failed break; } } } #ifdef WIN32 Sleep(2000); #else sleep(2); #endif } // Resign and destroy federation resignAndDestroy(rtiAmb, federationName); } catch (RTI::Exception& ex) { cout << "RTI Exception (main loop): " << ex._name << " " << ex._reason << endl; } #ifdef WIN32 WSACleanup(); #endif return 0; }
/******************************************************************************* ** Copyright (c) 2004 MaK Technologies, Inc. ** All rights reserved. *******************************************************************************/ #pragma warning(disable: 4786) #pragma warning(disable: 4290) #include <stdio.h> #include <iostream> #include "rtiSimpleFedAmb13.h" using namespace std; void printAttributes(const RTI::AttributeHandleValuePairSet& attributes) { for (unsigned int iloop = 0; iloop < attributes.size(); iloop++) { RTI::ULong length = attributes.getValueLength(iloop); cout << attributes.getHandle(iloop) << " " << attributes.getValuePointer(iloop,length) << endl; } } void printParameters(const RTI::ParameterHandleValuePairSet& params) { for (unsigned int iloop = 0; iloop < params.size(); iloop++) { RTI::ULong length = params.getValueLength(iloop); cout << params.getHandle(iloop) << " " << params.getValuePointer(iloop,length) << endl; } } std::string timeToString(RTI::FedTime const& time) { char* buff = new char[time.getPrintableLength()+1]; ((RTI::FedTime &)time).getPrintableString(buff); std::string timeStr(buff); delete [] buff; return timeStr; } MyFederateAmbassador::MyFederateAmbassador(DtTalkAmbData & data) : NullFederateAmbassador(), myData(data) { } MyFederateAmbassador::~MyFederateAmbassador() throw (RTI::FederateInternalError) { int x = 1; } void MyFederateAmbassador::initiateFederateSave(const char* label) throw ( RTI::UnableToPerformSave, RTI::FederateInternalError) { cout << "initiateFederateSave: " << label << endl; myData.myReceivedInitiateFederateSave = true; myData.mySaveLabel = label; } void MyFederateAmbassador::federationSaved() throw (RTI::FederateInternalError) { cout << "federationSaved " << endl; myData.myReceivedFederationSaved = true; } void MyFederateAmbassador::federationNotSaved() throw (RTI::FederateInternalError) { cout << "federationNotSaved: "; myData.myReceivedFederationNotSaved = true; } void MyFederateAmbassador::requestFederationRestoreSucceeded(const char* label) throw (RTI::FederateInternalError) { cout << "requestFederationRestoreSucceeded " << label << endl; myData.myReceivedRequestFederationRestoreSucceeded = true; myData.myRestoreLabel = label; } void MyFederateAmbassador::requestFederationRestoreFailed(const char* label, const char* reason) throw (RTI::FederateInternalError) { cout << "requestFederationRestoreFailed " << label << " " << reason << endl; myData.myReceivedRequestFederationRestoreFailed = true; myData.myRestoreLabel = label; myData.myRestoreFailureReason = reason; } void MyFederateAmbassador::federationRestoreBegun() throw (RTI::FederateInternalError) { cout << "federationRestoreBegun" << endl; myData.myReceivedFederationRestoreBegun = true; } void MyFederateAmbassador::initiateFederateRestore( const char* label, RTI::FederateHandle handle) throw ( RTI::SpecifiedSaveLabelDoesNotExist, RTI::CouldNotRestore, RTI::FederateInternalError) { cout << "initiateFederateRestore " << label << " " << handle << endl; myData.myReceivedInitiateFederateRestore = true; myData.myRestoreLabel = label; myData.myRestoreFederateHandle = handle; } void MyFederateAmbassador::federationRestored() throw (RTI::FederateInternalError) { cout << "federationRestored" << endl; myData.myReceivedFederationRestored = true; } void MyFederateAmbassador::federationNotRestored() throw (RTI::FederateInternalError) { cout << "federationNotRestored" << endl; myData.myReceivedFederationNotRestored = true; } void MyFederateAmbassador::discoverObjectInstance ( RTI::ObjectHandle theObject, RTI::ObjectClassHandle theObjectClass, const char* theObjectName) throw ( RTI::CouldNotDiscover, RTI::ObjectClassNotKnown, RTI::FederateInternalError) { cout << "discoverObjectInstance: " << theObjectName << "(" << theObject << ") of class " << myData.objectClassMap[theObjectClass].c_str() << "( " << theObjectClass << ")" << endl;\ myData.objectInstanceMap[theObject] = theObjectName; } void MyFederateAmbassador::reflectAttributeValues ( RTI::ObjectHandle theObject, // supplied C1 const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4 const RTI::FedTime& theTime, // supplied C1 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::ObjectNotKnown, RTI::AttributeNotKnown, RTI::FederateOwnsAttributes, RTI::InvalidFederationTime, RTI::FederateInternalError) { cout << "reflectAttributeValues: " << myData.objectInstanceMap[theObject].c_str() << "(" << theObject << ") " << timeToString(theTime).c_str() << " " << (theTag ? theTag : "") << " " << "#attributes: " << theAttributes.size() << endl; printAttributes(theAttributes); } void MyFederateAmbassador::reflectAttributeValues ( RTI::ObjectHandle theObject, // supplied C1 const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4 const char *theTag) // supplied C4 throw ( RTI::ObjectNotKnown, RTI::AttributeNotKnown, RTI::FederateOwnsAttributes, RTI::FederateInternalError) { cout << "reflectAttributeValues: " << myData.objectInstanceMap[theObject].c_str() << "(" << theObject << ") " << (theTag ? theTag : "") << " " << "#attributes: " << theAttributes.size() << endl; printAttributes(theAttributes); } // 4.6 void MyFederateAmbassador::receiveInteraction ( RTI::InteractionClassHandle theInteraction, // supplied C1 const RTI::ParameterHandleValuePairSet& theParameters, // supplied C4 const RTI::FedTime& theTime, // supplied C4 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::InteractionClassNotKnown, RTI::InteractionParameterNotKnown, RTI::InvalidFederationTime, RTI::FederateInternalError) { cout << "receiveInteraction: " << myData.interactionClassMap[theInteraction].c_str() << "( " << theInteraction << ") " << timeToString(theTime).c_str() << " " << (theTag ? theTag : "") << " " << "#parameters: " << theParameters.size() << endl; printParameters(theParameters); } void MyFederateAmbassador::receiveInteraction ( RTI::InteractionClassHandle theInteraction, // supplied C1 const RTI::ParameterHandleValuePairSet& theParameters, // supplied C4 const char *theTag) // supplied C4 throw ( RTI::InteractionClassNotKnown, RTI::InteractionParameterNotKnown, RTI::FederateInternalError) { cout << "receiveInteraction: " << myData.interactionClassMap[theInteraction].c_str() << "( " << theInteraction << ") " << (theTag ? theTag : "") << " " << "#parameters: " << theParameters.size() << endl; printParameters(theParameters); } void MyFederateAmbassador::removeObjectInstance ( RTI::ObjectHandle theObject, // supplied C1 const RTI::FedTime& theTime, // supplied C4 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::ObjectNotKnown, RTI::InvalidFederationTime, RTI::FederateInternalError) { cout << "removeObjectInstance: " << myData.objectInstanceMap[theObject].c_str() << "(" << theObject << ") " << timeToString(theTime).c_str() << " " << (theTag ? theTag : "") << endl; } void MyFederateAmbassador::removeObjectInstance ( RTI::ObjectHandle theObject, // supplied C1 const char *theTag) // supplied C4 throw ( RTI::ObjectNotKnown, RTI::FederateInternalError) { cout << "removeObjectInstance: " << myData.objectInstanceMap[theObject].c_str() << "(" << theObject << ") " << (theTag ? theTag : "") << endl; }
/******************************************************************************* * Adapted from "Beginning Linux Programming", from Wrox Press -- www.wrox.com *******************************************************************************/ #include "rtiSimpleKeyboard.h" #ifdef WIN32 #include <conio.h> #else #include <unistd.h> #endif keyboard::keyboard() { #ifndef WIN32 tcgetattr(0,&initial_settings); new_settings = initial_settings; new_settings.c_lflag &= ~ICANON; new_settings.c_lflag &= ~ECHO; new_settings.c_lflag &= ~ISIG; new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; tcsetattr(0, TCSANOW, &new_settings); peek_character=-1; #endif } keyboard::~keyboard() { #ifndef WIN32 tcsetattr(0, TCSANOW, &initial_settings); #endif } int keyboard::kbhit() { #ifdef WIN32 return _kbhit(); #else unsigned char ch; int nread; if (peek_character != -1) return 1; new_settings.c_cc[VMIN]=0; tcsetattr(0, TCSANOW, &new_settings); nread = read(0,&ch,1); new_settings.c_cc[VMIN]=1; tcsetattr(0, TCSANOW, &new_settings); if (nread == 1) { peek_character = ch; return 1; } return 0; #endif } int keyboard::getkey() { char ch; #ifdef WIN32 ch = _getch(); #else if (peek_character != -1) { ch = peek_character; peek_character = -1; } else read(0,&ch,1); #endif return ch; } int keyboard::keybrdTick() { char key = ' '; if (!kbhit()) return 0; key = getkey(); while (key != 'q' && key != 'Q' && kbhit()) key = getkey(); return (key == 'q' || key == 'Q') ? -1 : key; }
/******************************************************************************* ** Copyright (c) 2010 MaK Technologies, Inc. ** All rights reserved. *******************************************************************************/ #pragma warning(disable: 4786) #pragma warning(disable: 4290) #include "NullFederateAmbassador.hh" #include <map> #include <string> class DtTalkAmbData { public: bool myReceivedInitiateFederateSave; bool myReceivedFederationSaved; bool myReceivedFederationNotSaved; std::string mySaveLabel; bool myReceivedRequestFederationRestoreSucceeded; bool myReceivedRequestFederationRestoreFailed; bool myReceivedFederationRestoreBegun; bool myReceivedInitiateFederateRestore; bool myReceivedFederationRestored; bool myReceivedFederationNotRestored; std::string myRestoreLabel; RTI::FederateHandle myRestoreFederateHandle; std::string myRestoreFailureReason; std::map<RTI::ObjectClassHandle, std::string> objectClassMap; std::map<RTI::ObjectHandle, std::string> objectInstanceMap; std::map<RTI::InteractionClassHandle, std::string> interactionClassMap; }; class MyFederateAmbassador : public NullFederateAmbassador { public: MyFederateAmbassador(DtTalkAmbData & data); virtual ~MyFederateAmbassador() throw (RTI::FederateInternalError); // Federation Management Services // virtual void initiateFederateSave ( const char *label) // supplied C4 throw ( RTI::UnableToPerformSave, RTI::FederateInternalError); virtual void federationSaved () throw ( RTI::FederateInternalError); virtual void federationNotSaved () throw ( RTI::FederateInternalError); virtual void requestFederationRestoreSucceeded ( const char *label) // supplied C4 throw ( RTI::FederateInternalError); virtual void requestFederationRestoreFailed ( const char *label, const char *reason) // supplied C4 throw ( RTI::FederateInternalError); virtual void federationRestoreBegun () throw ( RTI::FederateInternalError); virtual void initiateFederateRestore ( const char *label, // supplied C4 RTI::FederateHandle handle) // supplied C1 throw ( RTI::SpecifiedSaveLabelDoesNotExist, RTI::CouldNotRestore, RTI::FederateInternalError); virtual void federationRestored () throw ( RTI::FederateInternalError); virtual void federationNotRestored () throw ( RTI::FederateInternalError); // Object Management Services // virtual void discoverObjectInstance ( RTI::ObjectHandle theObject, // supplied C1 RTI::ObjectClassHandle theObjectClass, // supplied C1 const char* theObjectName) // supplied C4 throw ( RTI::CouldNotDiscover, RTI::ObjectClassNotKnown, RTI::FederateInternalError); virtual void reflectAttributeValues ( RTI::ObjectHandle theObject, // supplied C1 const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4 const RTI::FedTime& theTime, // supplied C1 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::ObjectNotKnown, RTI::AttributeNotKnown, RTI::FederateOwnsAttributes, RTI::InvalidFederationTime, RTI::FederateInternalError); virtual void reflectAttributeValues ( RTI::ObjectHandle theObject, // supplied C1 const RTI::AttributeHandleValuePairSet& theAttributes, // supplied C4 const char *theTag) // supplied C4 throw ( RTI::ObjectNotKnown, RTI::AttributeNotKnown, RTI::FederateOwnsAttributes, RTI::FederateInternalError); // 4.6 virtual void receiveInteraction ( RTI::InteractionClassHandle theInteraction, // supplied C1 const RTI::ParameterHandleValuePairSet& theParameters, // supplied C4 const RTI::FedTime& theTime, // supplied C4 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::InteractionClassNotKnown, RTI::InteractionParameterNotKnown, RTI::InvalidFederationTime, RTI::FederateInternalError); virtual void receiveInteraction ( RTI::InteractionClassHandle theInteraction, // supplied C1 const RTI::ParameterHandleValuePairSet& theParameters, // supplied C4 const char *theTag) // supplied C4 throw ( RTI::InteractionClassNotKnown, RTI::InteractionParameterNotKnown, RTI::FederateInternalError); virtual void removeObjectInstance ( RTI::ObjectHandle theObject, // supplied C1 const RTI::FedTime& theTime, // supplied C4 const char *theTag, // supplied C4 RTI::EventRetractionHandle theHandle) // supplied C1 throw ( RTI::ObjectNotKnown, RTI::InvalidFederationTime, RTI::FederateInternalError); virtual void removeObjectInstance ( RTI::ObjectHandle theObject, // supplied C1 const char *theTag) // supplied C4 throw ( RTI::ObjectNotKnown, RTI::FederateInternalError); public: DtTalkAmbData & myData; };
/******************************************************************************* * Adapted from "Beginning Linux Programming", from Wrox Press -- www.wrox.com *******************************************************************************/ // Utility to allow keyboard input without blocking #ifndef MYKBHIT_H_ #define MYKBHIT_H_ #ifndef WIN32 #include <termios.h> #endif class keyboard { public: keyboard(); ~keyboard(); // Returns 1 if keyboard input is ready; otherwise, 0 int kbhit(); // Returns character from keyboard if avaialable; otherwise, 0 int keybrdTick(); protected: // Return character from keyboard input int getkey(); private: #ifndef WIN32 struct termios initial_settings, new_settings; int peek_character; #endif }; #endif
/******************************************************************************* ** Copyright (c) 2010 MaK Technologies, Inc. ** All rights reserved. *******************************************************************************/ #ifndef stringUtil_H_ #define stringUtil_H_ #include <string> #include <sstream> // Convert narrow C string to wide string inline std::wstring DtToWString(const char * in_val) { std::wstring temp; while (*in_val != '\0') temp += *in_val++; return temp; } // Convert narrow string to wide string inline std::string DtToString(const std::wstring &in_val) { std::string temp; std::wstring::const_iterator b = in_val.begin(); const std::wstring::const_iterator e = in_val.end(); while (b != e) { temp += static_cast<char>(*b); ++b; } return temp; } #endif