simpleTime1516.cxx and simpleTimeFedAmb1516.cxx (simpleTimeFedAmb1516.h) have the RTI calls and federate ambassador calls for the HLA 1516 version of rtisimple.
This page has the code for the following files:
simpleTime1516.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#include <winsock2.h>
#include <process.h>
#else
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>
#include <netdb.h>
#endif
#include <iostream>
#include <wchar.h>
#include <sstream>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
using namespace rti1516;
inline bool parseCmdLine( int argc, char* argv[] );
std::wstring theClassName = L"BaseEntity";
string fireInteractionName = "WeaponFire";
string detonateInteractionName = "MunitionDetonation";
ObjectClassHandle theClassHandle;
InteractionClassHandle fireInteractionHandle;
InteractionClassHandle detonateInteractionHandle;
ObjectInstanceHandle theObjectHandle;
bool Master(false);
int numFederates(0);
bool dedicatedMachine(false);
bool unManagedFederate(false);
int sleepTime(850);
wstring fedFileName(L"MAKsimpletime.xml");
wstring federationName(L"MAKsimpletime");
wstring initSyncPointLabel(L"begin");
double theLookAhead = 1.0;
double theTimeAdvance = 5.0;
enum TimeAdvanceService
{
tasUnknown,
tasTimeAdvanceRequest,
tasNextMessageRequest,
tasFlushQueue
};
TimeAdvanceService theTimeAdvanceService = tasTimeAdvanceRequest;
bool theAdvanceUsesAvailable = false;
std::wstring const& fedName,
std::wstring const& fedFile)
{
std::cout <<
"createFederationExecution " <<
DtToString(fedName) <<
" " <<
DtToString(fedFile) << endl;
try
{
}
catch(FederationExecutionAlreadyExists& ex)
{
std::cout << "Could not create Federation Execution: "
<< "FederationExecutionAlreadyExists: "
}
{
std::cout <<
"RTI Exception: " <<
DtToString(ex.
what()) << endl <<
"Could not create Federation Execution: " << endl;
exit(0);
}
std::cout << "Federation Created." << endl;
}
std::wstring const& federateType, std::wstring const& federationName)
{
bool joined=false;
const int maxTry = 10;
int numTries = 0;
std::cout <<
"joinFederationExecution " <<
DtToString(federateType) <<
" " <<
DtToString(federationName) << endl;
while (!joined && numTries++ < maxTry)
{
try
{
joined = true;
}
catch(FederationExecutionDoesNotExist)
{
std::cout << "FederationExecutionDoesNotExist, try " << numTries << " out of " << maxTry << endl;
continue;
}
{
return;
}
}
if (joined)
{
std::cout << "Joined Federation." << endl;
}
else
{
std::cout << "Giving up." << endl;
exit(0);
}
}
{
try
{
}
{
std::cout << "RTI Exception: "
std::cout << "During resign and destroy." << endl;
}
}
{
theAmbData.ourAttrs.insert(string("AccelerationVector"));
theAmbData.ourAttrs.insert(string("DeadReckoningAlgorithm"));
theAmbData.ourAttrs.insert(string("Orientation"));
theAmbData.ourAttrs.insert(string("WorldLocation"));
theAmbData.ourAttrs.insert(string("VelocityVector"));
theAmbData.ourAttrs.insert(string("DamageState"));
try
{
theAmbData.objectClassMap[theClassHandle] = theClassName;
}
{
std::cout <<
"Could not get object class handle: " <<
DtToString(theClassName) << endl;
return false;
}
std::wstring attrName;
set<string>::iterator attributeSetIterator = theAmbData.ourAttrs.begin();
set<string>::iterator attributeSetEnd = theAmbData.ourAttrs.end();
string currentAttribute("");
AttributeHandle retrievedHandle;
for( ; attributeSetIterator != attributeSetEnd; ++attributeSetIterator)
{
currentAttribute = *attributeSetIterator;
try
{
}
{
std::cout <<
"Could not get attribute handle " <<
DtToString(attrName.c_str()) << endl;
return false;
}
hSet.insert(retrievedHandle);
theAmbData.attrValues->insert(
std::make_pair(retrievedHandle,
VariableLengthData(currentAttribute.c_str(), currentAttribute.length() + 1)));
theAmbData.theAttrNameHandleMap.insert(std::make_pair(
DtToWString(currentAttribute.c_str()), retrievedHandle));
}
int cnt=0;
try
{
cnt=1;
}
{
std::cout << "Could not " << (cnt ? "publish" : "subscribe") << endl;
return false;
}
std::string objectName("Talk");
try
{
unsigned int objId = abs(getpid());
std::stringstream pid;
pid << objId;
objectName += pid.str();
theAmbData.myNameReservationReturned =
theAmbData.myNameReservationSucceeded = false;
int count = 0;
while (count++ < 100 && !theAmbData.myNameReservationReturned)
{
}
if (count < 100)
{
theAmbData.objectInstanceMap[theObjectHandle] =
DtToWString(objectName.c_str());
}
else
{
std::cout << "Failed waiting for reserve object name " << objectName << endl;
return false;
}
}
{
std::cout << "Could not Register Object "
<< objectName
<< " with class "
return false;
}
std::cout << "Registered object "
<< objectName
<< " with class name "
return true;
}
{
theAmbData.ourParms.insert("EventIdentifier");
theAmbData.ourParms.insert("FiringLocation");
theAmbData.ourParms.insert("FiringObjectIdentifier");
theAmbData.ourParms.insert("MunitionObjectIdentifier");
theAmbData.ourParms.insert("TargetObjectIdentifier");
try
{
theAmbData.interactionClassMap[fireInteractionHandle] =
DtToWString(fireInteractionName.c_str());
theAmbData.interactionClassMap[detonateInteractionHandle] =
DtToWString(detonateInteractionName.c_str());
}
{
std::cout <<
"Could not get interaction class handle: " <<
DtToString(theClassName.c_str()) << endl;
return false;
}
string paramName;
set<string>::const_iterator parameterIter = theAmbData.ourParms.begin();
set<string>::const_iterator parameterEnd = theAmbData.ourParms.end();
ParameterHandle retrievedHandle;
for( ; parameterIter != parameterEnd; ++parameterIter )
{
paramName = *parameterIter;
try
{
}
{
std::cout << "Could not get parameter handle " << paramName << endl;
return false;
}
theAmbData.paramValues->insert(std::make_pair(
retrievedHandle,
paramName.c_str(),
paramName.length() + 1 )));
theAmbData.theParamNameHandleMap.insert(std::make_pair(
DtToWString(paramName.c_str()), retrievedHandle));
}
int cnt=0;
try
{
cnt=1;
}
{
std::cout << "Could not "
<< (cnt ? "publish" : "subscribe")
<< " to interaction class." << endl;
return false;
}
std::cout << "Subscribed to interaction class: "
<< fireInteractionName
<< " with handle: "
<<
DtToString(fireInteractionHandle.toString()) << endl
<< " and interaction class "
<< detonateInteractionName
<< " with handle: "
<<
DtToString(detonateInteractionHandle.toString()) << endl;
return true;
}
{
try
{
if ( theAmbData.isRegulating )
{
}
resignAndDestroy(rtiAmb);
delete rtiAmb;
}
{
std::cout << "RTI Exception (during exit): "
}
#ifdef WIN32
WSACleanup();
#endif
exit(0);
}
void printOutLogicalTime(LogicalTime& time)
{
}
{
}
void sleepFunction()
{
if ( ! dedicatedMachine )
{
#ifdef WIN32
Sleep(sleepTime);
#else
usleep(sleepTime * 1000);
#endif
}
}
void minimalSleepFunction()
{
#ifdef WIN32
Sleep(10);
#else
usleep(10 * 1000);
#endif
}
void yieldFunction()
{
if ( ! dedicatedMachine )
{
#ifdef _WIN32
Sleep( 0 );
#elif __solaris__
yield();
#else
sched_yield();
#endif
}
}
{
try
{
std::cout << "Waiting to become Time Regulating " << endl;
while ( ! theAmbData.isRegulating )
{
std::cout << ".";
{
cleanUpProgram(rtiAmb);
}
minimalSleepFunction();
}
std::cout << endl;
std::cout << "Waiting to become Time Constrained \n";
while ( ! theAmbData.isConstrained )
{
std::cout << ".";
{
cleanUpProgram(rtiAmb);
}
if ( ! theAmbData.isConstrained )
{
yieldFunction();
}
}
cout << std::endl;
}
{
std::cout << " unable to become regulating and constrained\n" << " Exiting ...\n";
cleanUpProgram(rtiAmb);
}
}
{
if ( !theAmbData.isMaster )
{
std::cout << " Ordinary federate is waiting for synchPoint message\n";
while (!theAmbData.announceSyncReceived)
{
{
cleanUpProgram(rtiAmb);
}
yieldFunction();
}
std::cout << "sync Point message received\n";
}
else
{
std::wstring syncPointLabel(initSyncPointLabel.c_str());
std::string synchPointTag("");
int maximumSize(256);
char* hostName = new char[maximumSize];
if ( -1 == gethostname(hostName, maximumSize) )
{
unsigned int objId = abs(getpid());
std::stringstream pid;
pid.str(std::string(""));
pid << "unknownHostName" << "_" << objId;
synchPointTag = pid.str();
}
else
{
unsigned int objId = abs(getpid());
std::stringstream pid;
pid.str(std::string(""));
pid << hostName << "_" << objId;
synchPointTag = pid.str();
}
delete hostName;
std::cout << " Master waiting for all federates to Join.\n";
while ( theAmbData.otherFederatesReady < (theAmbData.numFederates - 1) )
{
{
cleanUpProgram(rtiAmb);
}
yieldFunction();
}
std::cout << " Master federate has discovered the required number of other federates.\n\n";
while ( !theAmbData.registerFailed && !theAmbData.registerSucceeded )
{
{
cleanUpProgram(rtiAmb);
}
yieldFunction();
}
if ( theAmbData.registerFailed )
{
std::cout <<
" We failed to register synch point " <<
DtToString(syncPointLabel) << std::endl
<< " with tag " << synchPointTag << std::endl;
cleanUpProgram(rtiAmb);
}
std::cout << "Master successfully registered\n";
}
try
{
}
{
std::cout << " Synch point achieved failed\n" << " Exiting....\n";
cleanUpProgram(rtiAmb);
}
while ( !theAmbData.federationIsSynchronized )
{
{
cleanUpProgram(rtiAmb);
}
minimalSleepFunction();
}
}
int main(int argc, char** argv)
{
std::cout << "MAK simpleTime version 1516" << std::endl;
try
{
if (argc > 1)
{
if (! parseCmdLine(argc, argv) )
{
exit(0);
}
}
std::vector< std::wstring > args;
std::wstring federateType(L"simpletime1516");
std::cout << "Using "
std::auto_ptr < RTIambassador > rtiAmbAP =
rtiAmbFactory->createRTIambassador(args);
delete rtiAmbFactory;
rtiAmb = rtiAmbAP.release();
createFedEx(rtiAmb, federationName, fedFileName);
joinFedEx(rtiAmb, fedAmb, federateType, federationName);
#ifdef WIN32
WSADATA data;
WSAStartup(MAKEWORD(1,1), &data);
#endif
long count=0;
LogicalTime& currentTime = *theAmbData.time;
becomeConstrainedAndRegulating(rtiAmb, lookAhead);
if (!publishSubscribeAndRegisterObject(rtiAmb))
{
resignAndDestroy(rtiAmb);
delete rtiAmb;
return 0;
}
if ( !unManagedFederate )
{
synchronizeFederation(rtiAmb);
}
if (!publishAndSubscribeInteraction(rtiAmb))
{
resignAndDestroy(rtiAmb);
delete rtiAmb;
return 0;
}
bool hasDetonated = false;
while (!hasDetonated && !theAmbData.timeManagedObject.isDetonated())
{
try
{
std::string tag;
if (*theAmbData.time < updateTime)
{
theAmbData.timeManagedObject.tick();
}
else
{
updateTime += oneTimeStep;
std::cout << "========================================================================================" << endl;
std::cout << " Current Time (";
printOutLogicalTime(currentTime);
std::cout << "), Lookahead (";
printOutLogicalTimeInterval(lookAhead);
std::cout << ") Update Time (";
printOutLogicalTime(updateTime);
std::cout << ")" << endl;
std::cout << endl;
std::stringstream ss;
ss << "1516-" << count++;
tag = ss.str();
theAmbData.timeManagedObject.tick();
theAmbData.timeManagedObject.incPosition(theTimeAdvance);
theObjectHandle,
(*theAmbData.attrValues),
updateTime);
}
if ( theAmbData.timeManagedObject.shouldDetonate() )
{
string munitionString = "MunitionObjectIdentifier";
ParameterHandle retrievedHandle = theAmbData.theParamNameHandleMap[
DtToWString(munitionString.c_str())];
interactionTime = currentTime;
interactionTime += lookAhead;
interactionTime += epsilon;
ParameterHandleValueMap::iterator munitionData = theAmbData.paramValues->find( retrievedHandle );
(*theAmbData.paramValues)[munitionData->first] = emptySet;
fireInteractionHandle,
(*theAmbData.paramValues),
interactionTime);
(*theAmbData.paramValues)[munitionData->first] = prior;
hasDetonated = true;
}
if ( theAmbData.timeManagedObject.shouldFire() )
{
string fireString = "FiringObjectIdentifier";
ParameterHandleValueMap::iterator fireData =
theAmbData.paramValues->find(theAmbData.theParamNameHandleMap[
DtToWString(fireString.c_str())]);
interactionTime = currentTime;
interactionTime += lookAhead;
interactionTime += epsilon;
(*theAmbData.paramValues)[fireData->first] = emptySet;
fireInteractionHandle,
(*theAmbData.paramValues),
interactionTime);
(*theAmbData.paramValues)[fireData->first] = prior;
}
}
catch( InvalidLogicalTime& ex)
{
std::cout << "RTI Invalid Logical Time" << endl;
<< "Could not update object or send interaction"
<< endl;
cleanUpProgram(rtiAmb);
}
{
std::cout << "RTI Exception "
<< endl
<< " Could not update object or send interaction"
<< endl;
}
int kb(0);
try
{
if ( theAmbData.isRegulating )
{
if (theTimeAdvanceService == tasTimeAdvanceRequest)
{
if (theAdvanceUsesAvailable)
{
}
else
{
}
}
else if (theTimeAdvanceService == tasNextMessageRequest)
{
if (theAdvanceUsesAvailable)
{
}
else
{
}
}
else if (theTimeAdvanceService == tasFlushQueue)
{
}
else
{
}
theAmbData.timeAdvanced = false;
while ( ! theAmbData.timeAdvanced )
{
if (kb < 0)
{
break;
}
else if ( kb == 1 )
{
theAmbData.timeManagedObject.firePressed();
cout << "********** Firing at time ";
printOutLogicalTime(updateTime);
cout << " *************\n";
}
else if ( !theAmbData.timeAdvanced )
{
yieldFunction();
}
}
}
}
{
std::cout << "RTI Exception" << endl
<< "Could not advance time" << endl;
}
if ( kb == 0 )
{
}
if (kb < 0)
{
break;
}
else if ( kb == 1 )
{
theAmbData.timeManagedObject.firePressed();
cout << "********** Firing at time ";
printOutLogicalTime(updateTime);
cout << " *************\n";
}
std::cout << "========================================================================================" << endl<< endl;
sleepFunction();
}
delete theAmbData.time;
delete theAmbData.paramValues;
delete theAmbData.attrValues;
if ( theAmbData.isRegulating )
{
}
resignAndDestroy(rtiAmb);
if( rtiAmb )
{
delete rtiAmb;
rtiAmb = 0;
}
}
{
std::cout << "RTI Exception (main loop): "
if( rtiAmb )
{
delete rtiAmb;
}
}
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
bool parseCmdLine(int argc, char* argv[])
{
vector < std::string > cmdArgs;
copy(argv + 1, argv + argc, back_inserter(cmdArgs));
vector<std::string>::const_iterator cur = cmdArgs.begin();
vector<std::string>::const_iterator last = cmdArgs.end();
while (cur != last)
{
vector<std::string>::const_iterator next = cur + 1;
if (*cur == "-h")
{
return false;
}
else if (*cur == "-fedFile")
{
if (next == last)
{
return false;
}
++cur;
}
else if (*cur == "-m")
{
if (next == last || !convert<int>(*cur, *next, theAmbData.numFederates))
{
return false;
}
theAmbData.isMaster = true;
++cur;
}
else if (*cur == "-phaseLine")
{
double phaseLine(0);
if (next == last || !convert<double>(*cur, *next, phaseLine))
{
return false;
}
theAmbData.timeManagedObject.setPhaseLine(phaseLine);
++cur;
}
else if (*cur == "-sleepTime")
{
if (next == last || !convert<int>(*cur, *next, sleepTime))
{
return false;
}
sleepTime *= 1000.0;
++cur;
}
else if (*cur == "-dedicated")
{
dedicatedMachine = true;
}
else if (*cur == "-unManaged")
{
unManagedFederate = true;
}
else if (*cur == "-lookAhead")
{
if (next == last || !convert<double>(*cur, *next, theLookAhead))
{
return false;
}
++cur;
}
else if (*cur == "-timeIncrement")
{
if (next == last || !convert<double>(*cur, *next, theTimeAdvance))
{
return false;
}
++cur;
}
else if (*cur == "-available")
{
theAdvanceUsesAvailable = true;
}
else if (*cur == "-advanceTimeWith")
{
std::string timeAdvanceService;
if (next == last || !convert<string>(*cur, *next, timeAdvanceService))
{
return false;
}
++cur;
if (timeAdvanceService == "timeAdvanceRequest")
{
theTimeAdvanceService = tasTimeAdvanceRequest;
}
else if (timeAdvanceService == "nextMessageRequest")
{
theTimeAdvanceService = tasNextMessageRequest;
}
else if (timeAdvanceService == "flushQueue")
{
theTimeAdvanceService = tasFlushQueue;
}
else
{
return false;
}
}
else
{
return false;
}
++cur;
}
return true;
}
simpleTimeFedAmb1516.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
#include <iostream>
using namespace std;
isConstrained(false),
isRegulating(false),
timeAdvanced(false),
time(0),
otherFederatesReady(false)
{
}
{
}
NullFederateAmbassador(), myData(data) {}
throw () {}
void
std::wstring const & theObjectInstanceName)
throw (
UnknownName,
FederateInternalError)
{
myData.myNameReservationReturned =
myData.myNameReservationSucceeded = true;
}
std::wstring const & theObjectInstanceName)
throw (
UnknownName,
FederateInternalError)
{
myData.myNameReservationReturned = true;
myData.myNameReservationSucceeded = false;
}
ObjectInstanceHandle theObject,
ObjectClassHandle theObjectClass,
std::wstring const & theObjectInstanceName)
throw (
CouldNotDiscover,
ObjectClassNotKnown,
FederateInternalError)
{
++myData.otherFederatesReady;
if ( myData.isMaster )
{
std::cout << " Master federate is aware of "
<< myData.otherFederatesReady + 1
<< " federates, including itself\n";
}
for ( DtAttrNameHandleMap::iterator iter = myData.theAttrNameHandleMap.begin();
iter != myData.theAttrNameHandleMap.end();
++iter )
{
hSet->insert( iter->second );
}
myData.objectInstanceMap[theObject] = theObjectInstanceName;
myData.updateRequestMap.insert( std::make_pair(theObject, hSet) );
delete hSet;
}
ObjectInstanceHandle theObject,
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError)
{
myData.timeManagedObject.addAttrUpdateEvent(tmp);
}
ObjectInstanceHandle theObject,
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError)
{
theAttributeValues,
theUserSuppliedTag,
sentOrder,
theType);
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError)
{
theAttributeValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime ));
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError)
{
theAttributeValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
InvalidLogicalTime,
FederateInternalError)
{
theAttributeValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
MessageRetractionHandle theHandle,
throw (
ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
InvalidLogicalTime,
FederateInternalError)
{
theAttributeValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
InteractionClassHandle theInteraction,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError)
{
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType );
myData.timeManagedObject.addInteractionEvent(receivedInteraction);
}
InteractionClassHandle theInteraction,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError)
{
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType);
}
InteractionClassHandle theInteraction,
LogicalTime const & theTime,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError)
{
string munitionString("MunitionObjectIdentifier");
string fireString("FiringObjectIdentifier");
if ( myData.theParamNameHandleMap.find(
DtToWString(munitionString.c_str()))
!= myData.theParamNameHandleMap.end() )
{
ParameterHandle munitionHandle =
myData.theParamNameHandleMap.find(
DtToWString(munitionString.c_str()))->second;
ParameterHandleValueMap::const_iterator munitionEntry = theParameterValues.find(munitionHandle);
if ( munitionEntry != theParameterValues.end() )
{
if ( munitionData.
size() == 1 )
{
}
}
}
if ( myData.theParamNameHandleMap.find(
DtToWString(fireString.c_str()))
!= myData.theParamNameHandleMap.end())
{
ParameterHandle fireHandle = myData.theParamNameHandleMap.find(
DtToWString(fireString.c_str()))->second
;
ParameterHandleValueMap::const_iterator fireEntry = theParameterValues.find(fireHandle);
if ( fireEntry != theParameterValues.end() )
{
if ( fireData.
size() == 1 )
{
}
}
}
theInteraction,
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder,
interactionType
));
}
InteractionClassHandle theInteraction,
LogicalTime const & theTime,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError)
{
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
InteractionClassHandle theInteraction,
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
InvalidLogicalTime,
FederateInternalError)
{
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
InteractionClassHandle theInteraction,
LogicalTime const & theTime,
MessageRetractionHandle theHandle,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
InvalidLogicalTime,
FederateInternalError)
{
theParameterValues,
theUserSuppliedTag,
sentOrder,
theType,
theTime,
receivedOrder);
}
ObjectInstanceHandle theObject,
throw (
ObjectInstanceNotKnown,
FederateInternalError)
{
if ( myData.objectInstanceMap.find(theObject) != myData.objectInstanceMap.end() )
myData.objectInstanceMap.erase(theObject);
myData.timeManagedObject.reset();
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
throw (
ObjectInstanceNotKnown,
FederateInternalError)
{
if ( myData.objectInstanceMap.find(theObject) != myData.objectInstanceMap.end() )
myData.objectInstanceMap.erase(theObject);
}
ObjectInstanceHandle theObject,
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (
ObjectInstanceNotKnown,
InvalidLogicalTime,
FederateInternalError)
{
if ( myData.objectInstanceMap.find(theObject) != myData.objectInstanceMap.end() )
myData.objectInstanceMap.erase(theObject);
}
ObjectInstanceHandle theObject,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotOwned,
FederateInternalError)
{
for ( DtAttrNameHandleMap::iterator iter = myData.theAttrNameHandleMap.begin();
iter != myData.theAttrNameHandleMap.end();
++iter )
{
hSet->insert( iter->second );
}
myData.updateRequestMap.insert( std::make_pair(theObject, hSet) );
}
throw (InvalidLogicalTime,
NoRequestToEnableTimeRegulationWasPending,
FederateInternalError)
{
std::cout << " This federate is now able to Regulate time\n";
myData.isRegulating = true;
}
throw (InvalidLogicalTime,
NoRequestToEnableTimeConstrainedWasPending,
FederateInternalError)
{
std::cout << " This federate is now time Constrained\n";
myData.isConstrained = true;
}
throw (InvalidLogicalTime,
JoinedFederateIsNotInTimeAdvancingState,
FederateInternalError)
{
std::cout << " Federate Time has been advanced to "
<< std::endl;
myData.timeAdvanced = true;
if (myData.time)
{
*myData.time = theTime;
}
}
throw (FederateInternalError)
{
}
std::wstring const & label)
throw (FederateInternalError)
{
myData.registerSucceeded = true;
}
std::wstring const & label,
throw (FederateInternalError)
{
myData.registerFailed = true;
}
std::wstring const & label,
throw (FederateInternalError)
{
myData.announceSyncReceived = true;
std::cout << "Announce Sync Received for label "
<< reinterpret_cast<const char*>(theUserSuppliedTag.data()) << endl;
}
throw (FederateInternalError)
{
myData.federationIsSynchronized = true;
std::cout << " My Federation has been synchronized.\n";
}
simpleTimeKeyboard.cxx
#ifdef WIN32
#include <conio.h>
#else
#include <unistd.h>
#endif
#include <iostream>
{
#ifndef WIN32
tcgetattr(0,&initial_settings);
#endif
}
{
#ifndef WIN32
tcsetattr(0, TCSANOW, &initial_settings);
#endif
}
{
#ifdef WIN32
return _kbhit();
#else
unsigned char ch;
int nread;
nread = read(0,&ch,1);
if (nread == 1)
{
return 1;
}
return 0;
#endif
}
{
char ch;
#ifdef WIN32
ch = _getch();
#else
{
}
else
read(0,&ch,1);
#endif
return ch;
}
{
int result = 0;
{
while (key !=
'q' && key !=
'Q' &&
kbhit())
if (key == 'q' || key == 'Q')
{
result = -1;
}
else if (key == 32)
{
result = 1;
}
}
return result;
}
simpleTimeAttribute1516.cxx
#ifdef DtIFSPEC1516
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
const VariableLengthData& userSuppliedTag,
const LogicalTime& fedTime ) :
myTag(static_cast<const char*>( userSuppliedTag.data() )),
{
AttributeHandleValueMap::const_iterator iter = ahvps.begin();
AttributeHandleValueMap::const_iterator theEnd = ahvps.end();
for ( ; iter != theEnd; ++iter )
{
myAhvps.insert(std::make_pair(
iter->first,
std::string(static_cast<const char*>(
iter->second.data()))));
}
}
const VariableLengthData& userSuppliedTag,
myTag(static_cast<const char*>( userSuppliedTag.data() ))
{
AttributeHandleValueMap::const_iterator iter = ahvps.begin();
AttributeHandleValueMap::const_iterator theEnd = ahvps.end();
for ( ; iter != theEnd; ++iter )
{
myAhvps.insert(std::make_pair(
iter->first,
std::string(static_cast<const char*>(
iter->second.data()))));
}
}
{}
{
}
{
}
{
}
{
}
#endif
simpleTimeInteraction1516.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
const rti1516::InteractionClassHandle theHandle,
rti1516::MessageRetractionHandle theRetractionHandle,
interaction_type theInteractionType
):
myHandle(theHandle),
myTag(static_cast<const char*>(userSuppliedTag.data())),
myRetractionHandle(theRetractionHandle),
myInteractionType(theInteractionType)
{
rti1516::ParameterHandleValueMap::const_iterator iter = phvps.begin();
rti1516::ParameterHandleValueMap::const_iterator theEnd = phvps.end();
for ( ; iter != theEnd; ++iter )
{
myPhvps.insert( std::make_pair(
iter->first,
std::string(static_cast<const char*>(
iter->second.data())) ) );
}
}
interaction_type theInteractionType ) :
myHandle(theHandle),
myTag(static_cast<const char*>(userSuppliedTag.data())),
myRetractionHandle(),
myInteractionType(theInteractionType)
{
rti1516::ParameterHandleValueMap::const_iterator iter = phvps.begin();
rti1516::ParameterHandleValueMap::const_iterator theEnd = phvps.end();
for ( ; iter != theEnd; ++iter )
{
myPhvps.insert( std::make_pair(
iter->first,
std::string(static_cast<const char*>(iter->second.data())) ) );
}
}
interaction_type theInteractionType ) :
myHandle(theHandle),
myTag(static_cast<const char*>(userSuppliedTag.data())),
myFedTime(""),
myRetractionHandle(),
myInteractionType(theInteractionType)
{
rti1516::ParameterHandleValueMap::const_iterator iter = phvps.begin();
rti1516::ParameterHandleValueMap::const_iterator theEnd = phvps.end();
for ( ; iter != theEnd; ++iter )
{
myPhvps.insert( std::make_pair(
iter->first,
std::string(static_cast<const char*>(iter->second.data())) ) );
}
}
{ }
{
}
{
}
{
}
{
}
{
}
{
}
{
}
simpleTimeTimeManagedEntity.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
#include <assert.h>
#include <iostream>
: myFirePressed(false),
myPosition(0.0),
myVelocity(1.0),
myPhysicalState(Alive),
myState(Initial),
myPhaseLine(550.0)
{ }
{
{
}
{
}
}
{
}
{
}
{
return retVal;
}
{
return retVal;
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
{
std::cout << "XXX DEAD XXX\n";
{
}
{
}
}
else
{
{
std::string fedTime = receivedInteraction->
getFedTime();
std::string tag = receivedInteraction->
getTag();
{
std::cout << " received Fire interaction (" << tag << ") at time "
<< fedTime.c_str() << " \n" ;
}
{
std::cout << " received Detonation interaction (" << tag << ") at time "
<< fedTime.c_str() << " \n" ;
}
delete receivedInteraction;
}
{
std::string fedTime = receivedAttrUpdate->
getFedTime();
std::string tag = receivedAttrUpdate->
getTag();
std::cout << " received attribute update (" << tag << ") at time "
<< fedTime.c_str() << " \n" ;
delete receivedAttrUpdate;
}
}
}
{
}
{
}
{
{
{
{
}
break;
break;
break;
}
}
else
{
}
}
timeManagedEntity.cxx
simpleTimeFedAmb1516.h
#ifndef MyFederateAmbassador_H_
#define MyFederateAmbassador_H_
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
#include <string>
#include <map>
using namespace rti1516;
{
public:
public:
bool myNameReservationReturned;
bool myNameReservationSucceeded;
std::map<ObjectClassHandle, std::wstring> objectClassMap;
std::map<ObjectInstanceHandle, std::wstring> objectInstanceMap;
std::map<InteractionClassHandle,std::wstring> interactionClassMap;
std::map<ObjectInstanceHandle, AttributeHandleSet*> updateRequestMap;
DtAttrNameHandleMap theAttrNameHandleMap;
DtParamNameHandleMap theParamNameHandleMap;
std::set<std::string> ourAttrs;
std::set<std::string> ourParms;
bool isMaster;
int numFederates;
bool isConstrained;
bool isRegulating;
bool timeAdvanced;
LogicalTime* time;
int otherFederatesReady;
bool registerFailed;
bool registerSucceeded;
bool announceSyncReceived;
bool federationIsSynchronized;
};
{
public:
throw ();
virtual
void
objectInstanceNameReservationSucceeded(std::wstring const &
theObjectInstanceName)
throw (UnknownName,
FederateInternalError);
virtual
void
objectInstanceNameReservationFailed(std::wstring const &
theObjectInstanceName)
throw (UnknownName,
FederateInternalError);
ObjectInstanceHandle theObject,
ObjectClassHandle theObjectClass,
std::wstring const & theObjectInstanceName)
throw (
CouldNotDiscover,
ObjectClassNotKnown,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
LogicalTime const & theTime,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
LogicalTime const & theTime,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
InvalidLogicalTime,
FederateInternalError);
virtual
void
(ObjectInstanceHandle theObject,
LogicalTime const & theTime,
MessageRetractionHandle theHandle,
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotSubscribed,
InvalidLogicalTime,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
LogicalTime const & theTime,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
LogicalTime const & theTime,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
InvalidLogicalTime,
FederateInternalError);
virtual
void
(InteractionClassHandle theInteraction,
LogicalTime const & theTime,
MessageRetractionHandle theHandle,
throw (InteractionClassNotRecognized,
InteractionParameterNotRecognized,
InteractionClassNotSubscribed,
InvalidLogicalTime,
FederateInternalError);
virtual
void
throw (ObjectInstanceNotKnown,
FederateInternalError);
virtual
void
LogicalTime const & theTime,
throw (ObjectInstanceNotKnown,
FederateInternalError);
virtual
void
LogicalTime const & theTime,
MessageRetractionHandle theHandle)
throw (ObjectInstanceNotKnown,
InvalidLogicalTime,
FederateInternalError);
virtual
void
throw (ObjectInstanceNotKnown,
AttributeNotRecognized,
AttributeNotOwned,
FederateInternalError);
virtual
void
throw (InvalidLogicalTime,
NoRequestToEnableTimeRegulationWasPending,
FederateInternalError);
virtual
void
throw (InvalidLogicalTime,
NoRequestToEnableTimeConstrainedWasPending,
FederateInternalError);
virtual
void
throw (InvalidLogicalTime,
JoinedFederateIsNotInTimeAdvancingState,
FederateInternalError);
virtual
void
throw (FederateInternalError);
virtual
void
throw (FederateInternalError);
virtual
void
throw (FederateInternalError);
virtual
void
throw (FederateInternalError);
virtual
void
throw (FederateInternalError);
public:
};
#endif
simpleTimeKeyboard.h
#ifndef MYKBHIT_H_
#define MYKBHIT_H_
#ifndef WIN32
#include <termios.h>
#endif
{
public:
protected:
private:
#ifndef WIN32
#endif
};
#endif
simpleTimeTimeManagedEntity.h
#ifndef _TIMEMANAGEDOBJECTDEFINITION_
#define _TIMEMANAGEDOBJECTDEFINITION_
#ifdef DtIFSPEC1516
#elif defined(DtIFSPEC1516E)
#else
#ifdef DtIFSPEC13DLC
#else
#endif
#endif
#include <queue>
{
public:
private:
private:
};
#endif
simpleTimeStringUtil.h
#ifndef stringUtil_H_
#define stringUtil_H_
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
{
wstring temp;
while (*in_val != '\0')
temp += *in_val++;
return temp;
}
{
string temp;
wstring::const_iterator b = in_val.begin();
const wstring::const_iterator e = in_val.end();
while (b != e)
{
temp += static_cast<char>(*b);
++b;
}
return temp;
}
{
ostringstream ostr;
ostr << "Usage: simpletime13/1516(e) [-fedFile <fedFileName>][-m <#>][-phaseLine <#>][-sleepTime <#>][-dedicated]\n"
" [-unManaged][-advanceTimeWith <serviceName>][-available][-lookAhead <#>][-timeIncrement <#>]"
<< endl << endl
<< setw(35) << std::left << "-fedFile <fedFileName>"
<< "The FED file name.\n"
<< setw(35) << std::right << " " << std::left
<< "Default: MAKsimple.xml/fed.\n"
<< endl
<< setw(35) << "-m <numberOfFederates>"
<< "Whether this federate is the master and how many total federates are needed to synchronize.\n"
<< endl
<< setw(35) << "-phaseLine <numberUnits>"
<< "The open fire boundary, which when this federate crosses it, it is free to fire.\n"
<< setw(35) << std::right << " " << std::left
<< "Default: 550 units.\n"
<< endl
<< setw(35) << "-sleepTime <milliseconds>"
<< "The time in milliseconds to sleep between iterations of the main loop. \n"
<< setw(35) << std::right << " " << std::left
<< "Default: 850 ms. \n"
<< endl
<< setw(35) << "-dedicated"
<< "Execute as if this federate is on a dedicated machine for running this federate.\n"
<< setw(35) << std::right << " " << std::left
<< "Disables all sleeps and yields.\n"
<< endl
<< setw(35) << "-unManaged"
<< "Ignore the synchronization step before advancing time.\n"
<< endl
<< setw(35) << "-advanceTimeWith <rtiAmbService>"
<< "The time advance service: timeAdvanceRequest, nextMessageRequest, flushQueue. \n"
<< setw(35) << std::right << " " << std::left
<< "Default: timeAdvanceRequest. \n"
<< endl
<< setw(35) << "-available"
<< "Use the available option of time advance service.\n"
<< endl
<< setw(35) << "-lookAhead <seconds>"
<< "The lookahead time in seconds.\n"
<< setw(35) << std::right << " " << std::left
<< "Default: 1.0 s. \n"
<< endl
<< setw(35) << "-timeIncrement <seconds>"
<< "The increment to advance time in seconds.\n"
<< setw(35) << std::right << " " << std::left
<< "Default: 5.0 s. \n"
<< endl
<< endl;
return ostr.str();
}
template< class T >
const string& value,
T& dest )
{
{
std::cout << "Bad Parameter Value\n"
<< "Param: " << param
<< "\tValue: " << value << endl;
return false;
}
return true;
}
#endif
simpleTimeAttribute1516.h
#ifndef _ATTRIBUTE1516DEFINITION_
#define _ATTRIBUTE1516DEFINITION_
#ifdef DtIFSPEC1516
#include <map>
#include <string>
#include <iostream>
using namespace rti1516;
{
public:
LogicalTime const& fedTime );
const std::map<AttributeHandle, std::string>& getAhvps();
const std::string& getFedTime();
const std::string& getTag();
MessageRetractionHandle getRetractionHandle();
private:
std::map<AttributeHandle, std::string> myAhvps;
std::string myFedTime;
std::string myTag;
MessageRetractionHandle myRetractionHandle;
};
#endif // #ifdef DtIFSPEC1516
#endif // #define _ATTRIBUTE1516DEFINITION_
simpleTimeInteraction1516.h
#ifndef _INTERACTIONDEFINITION_
#define _INTERACTIONDEFINITION_
#include <map>
#include <string>
{
public:
rti1516::MessageRetractionHandle theRetractionHandle,
);
);
);
private:
rti1516::InteractionClassHandle
myHandle;
std::map<rti1516::ParameterHandle, std::string>
myPhvps;
};
#endif
stringUtil.h
timeManagedEntity.h