simpleTime13.cxx and simpleTimeFedAmb13.cxx (simpleTimeFedAmb13.h) have the RTI calls and federate ambassador calls for the HLA 1.3 version of simpleTime.
This page has the code for the following files:
simpleTime13.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 <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
string const& fedName,
string const& fedFile)
{
std::cout << "createFederationExecution "
<< fedName.c_str() << " "
<< fedFile.c_str() << endl;
try
{
}
catch(RTI::FederationExecutionAlreadyExists& ex)
{
std::cout << "Could not create Federation Execution: "
<< "FederationExecutionAlreadyExists: "
<< ex._name << " "
<< ex._reason << endl;
}
{
std::cout << "Could not create Federation Execution: " << endl
<< "RTI Exception: "
exit(0);
}
std::cout << "Federation Created" << endl;
}
string const& federateType)
{
bool joined=false;
const int maxTry = 10;
int numTries = 0;
std::cout << "joinFederationExecution "
<< federateType.c_str() << " "
<< federationName.c_str() << endl;
while (!joined && numTries++ < maxTry)
{
try
{
federationName.c_str(), fedAmb);
joined = true;
}
catch(RTI::FederationExecutionDoesNotExist)
{
std::cout << "FederationExecutionDoesNotExist, try "
<< numTries << "out of "
<< maxTry << endl;
continue;
}
{
std::cout << "RTI Exception: "
return;
}
}
if (joined)
{
std::cout << "Joined Federation." << endl;
}
else
{
std::cout << "Giving up." << endl;
exit(0);
}
}
{
try
{
}
{
std::cout << "RTI Exception: "
<< "During resign and destroy. " << endl;
}
}
{
try
{
}
{
std::cout << "RTI Exception: "
<< "Could not get object class handle: "
return false;
}
string attrName;
string currentAttribute("");
for ( ; attributeSetIterator != attributeSetEnd; ++attributeSetIterator )
{
currentAttribute = *attributeSetIterator;
try
{
retrievedHandle =
}
{
std::cout << "RTI Exception: "
<< "Could not get attribute handle "
<< attrName.c_str() << endl;
return false;
}
hSet->
add(retrievedHandle);
currentAttribute.c_str(), currentAttribute.length()+1);
currentAttribute, retrievedHandle));
}
int cnt=0;
try
{
cnt=1;
}
{
std::cout << "RTI Exception: "
<< "Could not "
<< (cnt ? "publish" : "subscribe") << endl;
delete hSet;
return false;
}
string objectName("Talk");
try
{
unsigned int objId = abs(getpid());
stringstream pid;
pid << objId;
objectName += pid.str();
objectName.c_str());
}
{
std::cout << "RTI Exception: "
<< "Could not Register Object "
<< objectName.c_str()
<< " with class "
delete hSet;
return false;
}
std::cout << "Registered object "
<< objectName.c_str()
<< " with class name "
delete hSet;
return true;
}
{
try
{
}
{
std::cout << "RTI Exception: "
<< "Could not get interaction class handle: "
return false;
}
string paramName;
for ( ; parameterIter != parameterEnd; ++parameterIter )
{
paramName = *parameterIter;
try
{
retrievedHandle =
}
{
std::cout << "RTI Exception: "
<< "Could not get parameter handle "
<< paramName.c_str() << endl;
return false;
}
paramName.c_str(), paramName.length()+1);
paramName, retrievedHandle));
}
int cnt=0;
try
{
cnt=1;
}
{
std::cout << "RTI Exception: "
<< "Could not "
<< (cnt ? "publish" : "subscribe")
<< " to interaction." << endl;
return false;
}
std::cout << "Subscribed to interaction class: "
<< " with handle: "
<< " and interaction class "
<< " with handle: "
return true;
}
{
try
{
{
}
delete rtiAmb;
}
{
std::cout << "RTI Exception (during exit): "
}
#ifdef WIN32
WSACleanup();
#endif
exit(0);
}
{
std::cout << atof(buff) ;
delete [] buff;
}
{
{
#ifdef WIN32
#else
#endif
}
}
{
#ifdef WIN32
Sleep(10);
#else
usleep(10 * 1000);
#endif
}
{
{
#ifdef _WIN32
Sleep( 0 );
#elif __solaris__
yield();
#else
sched_yield();
#endif
}
}
{
try
{
std::cout << "Waiting to become Time Regulating \n";
{
std::cout << ".";
{
}
}
std::cout << endl;
std::cout << "Waiting to become Time Constrained \n";
{
std::cout << ".";
{
}
{
}
}
std::cout << endl;
}
{
std::cout << " unable to become regulating and constrained\n"
<< " Exiting ...\n";
}
}
{
{
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";
{
{
}
}
std::cout << " Master federate has discovered the required number of other federates.\n\n";
synchPointTag.c_str());
{
{
}
}
{
<< " with tag " << synchPointTag << std::endl;
}
std::cout << "Master successfully registered\n";
}
else
{
std::cout << " Ordinary federate is waiting for synchPoint message\n";
{
{
}
}
std::cout << "sync Point message received\n";
}
try
{
}
{
std::cout << " Synch point achieved failed\n"
<< " Exiting....\n";
}
{
{
}
}
}
int main(
int argc,
char** argv)
{
std::cout << "MAK simpleTime version 1.3" << std::endl;
try
{
string federateType("simpletime13");
if (argc > 1)
{
{
exit(0);
}
}
#ifdef WIN32
WSADATA data;
WSAStartup(MAKEWORD(1,1), &data);
#endif
long count=0;
(*currentTime) += (*oneTimeStep);
{
delete rtiAmb;
return 0;
}
{
}
{
delete rtiAmb;
return 0;
}
{
std::cout << "===================================================================\n";
std::cout << " Fedtime (";
std::cout << "), Lookahead (";
std::cout << ") and wallclock time (";
std::cout << ") " << std::endl;
std::cout << endl;
stringstream ss;
ss << "1.3-" << count++;
string tag(ss.str());
try{
(*currentTime),
tag.c_str());
{
string munitionString = "MunitionObjectIdentifier";
(*currentTime),
tag.c_str() );
munitionString.c_str(), munitionString.length() + 1);
}
{
string fireString = "FiringObjectIdentifier";
(*currentTime),
tag.c_str() );
}
}
catch(RTI::InvalidFederationTime& ex)
{
std::cout << " caught an Invalid Federation Time !!\n";
std::cout << ex._name << " " << ex._reason << endl;
}
int kb(0);
try
{
{
(*currentRTITime) += (*oneTimeStep);
(*currentTime) += (*oneTimeStep);
{
if (kb < 0)
{
break;
}
else if ( kb == 1 )
{
std::cout << "**********Firing at Fed Time " ;
std::cout << "*************\n";
}
{
}
}
}
}
{
std::cout << "RTI Exception" << endl
<< " Could not advance time" << endl;
}
if ( kb == 0 )
{
}
if ( kb < 0 )
{
break;
}
else if ( kb == 1 )
{
std::cout << "**********Firing at Fed Time " ;
std::cout << "*************\n";
}
std::cout << "===================================================================\n\n";
}
delete oneTimeStep;
delete lookAhead;
delete currentTime;
delete currentRTITime;
{
}
delete rtiAmb;
}
{
std::cout << "RTI Exception (main loop): "
}
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
{
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" )
{
{
return false;
}
++cur;
}
else if (*cur == "-m" )
{
{
return false;
}
++cur;
}
else if ( *cur == "-phaseLine" )
{
int phaseLine(0);
if ( !convert<int>(*cur, *next, phaseLine))
{
return false;
}
++cur;
}
else if ( *cur == "-sleepTime" )
{
if ( !convert<int> (*cur, *next,
sleepTime ))
{
return false;
}
++cur;
}
else if ( *cur == "-dedicated" )
{
}
else if ( *cur == "-unManaged" )
{
}
else
{
return false;
}
++cur;
}
return true;
}
simpleTimeFedAmb13.cxx
#ifdef WIN32
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#else
#include <stdio.h>
#endif
#include <cstdlib>
#include <iostream>
using namespace std;
otherFederatesReady(0),
registerSucceeded(
false),
announceSyncReceived(
false),
federationIsSynchronized(
false)
{ }
{
}
NullFederateAmbassador(), myData(data)
{ }
throw (
RTI::FederateInternalError)
{
}
const char* theObjectName)
throw (
RTI::CouldNotDiscover,
RTI::ObjectClassNotKnown,
RTI::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++)
{
}
myData.updateRequestMap.insert(make_pair(theObject,hSet));
delete hSet;
myData.objectInstanceMap[theObject] = theObjectName;
}
const char *theTag,
throw (
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateOwnsAttributes,
RTI::InvalidFederationTime,
RTI::FederateInternalError)
{
theAttributes,
theTime,
theTag,
theHandle));
}
const char *theTag)
throw (
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::FederateOwnsAttributes,
RTI::FederateInternalError)
{
theAttributes,
theTag ));
}
const char *theTag,
throw (
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError)
{
string munitionString("MunitionObjectIdentifier");
string fireString("FiringObjectIdentifier");
myData.theParamNameHandleMap.find(munitionString)->second;
myData.theParamNameHandleMap.find(fireString)->second;
for ( unsigned int i(0);
++i )
{
if ( theParameters.getHandle(i) == munitionHandle )
{
RTI::ULong length = theParameters.getValueLength(i);
if ( length == 1 )
{
}
}
if ( theParameters.getHandle(i) == fireHandle )
{
RTI::ULong length = theParameters.getValueLength(i);
if ( length == 1 )
{
}
}
}
myData.timeManagedObject.addInteractionEvent(
new interactionEvent ( theInteraction,
theParameters,
theTime,
theTag,
interactionType ));
}
const char *theTag)
throw (
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::FederateInternalError)
{
myData.timeManagedObject.addInteractionEvent(
theParameters,
theTag) );
}
const char *theTag,
throw (
RTI::ObjectNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError)
{
if ( myData.objectInstanceMap.find(theObject) != myData.objectInstanceMap.end())
myData.objectInstanceMap.erase(theObject);
myData.timeManagedObject.reset();
}
const char *theTag)
throw (
RTI::ObjectNotKnown,
RTI::FederateInternalError)
{
if ( myData.objectInstanceMap.find(theObject) != myData.objectInstanceMap.end())
myData.objectInstanceMap.erase(theObject);
myData.timeManagedObject.reset();
}
throw (
RTI::ObjectNotKnown,
RTI::AttributeNotKnown,
RTI::AttributeNotOwned,
RTI::FederateInternalError)
{
for (DtAttrNameHandleMap::iterator iter = myData.theAttrNameHandleMap.begin();
iter != myData.theAttrNameHandleMap.end();
iter++)
{
}
myData.updateRequestMap.insert( make_pair( theObject, hSet) );
delete hSet;
}
throw (
RTI::InvalidFederationTime,
RTI::EnableTimeRegulationWasNotPending,
RTI::FederateInternalError)
{
std::cout << " This federate is now able to Regulate time\n";
myData.isRegulating = true;
}
throw (
RTI::InvalidFederationTime,
RTI::EnableTimeConstrainedWasNotPending,
RTI::FederateInternalError)
{
std::cout << " This federate is now time Constrained\n";
myData.isConstrained = true;
}
throw (
RTI::InvalidFederationTime,
RTI::TimeAdvanceWasNotInProgress,
RTI::FederateInternalError)
{
int length = theTime.getPrintableLength();
char* buff = new char[length + 1];
const_cast<RTI::FedTime&
>(theTime).getPrintableString(buff);
std::cout << " Federate Time has been advanced to " << atof(buff) << endl;
delete [] buff;
myData.timeAdvanced = true;
}
const char *label)
throw (
RTI::FederateInternalError)
{
myData.registerSucceeded = true;
}
const char *label)
throw (
RTI::FederateInternalError)
{
myData.registerFailed = true;
}
const char *label,
const char *tag)
throw (
RTI::FederateInternalError)
{
myData.announceSyncReceived = true;
std::cout << "Announce Sync Received for label "
<< label << " and tag "
<< tag << endl;
}
const char *label)
throw (
RTI::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;
}
{
char key = ' ';
return 0;
while (key !=
'q' && key !=
'Q' &&
kbhit())
if ( key == 'q' || key == 'Q' )
{
return -1;
}
else if ( key == 32 )
{
return 1;
}
else
{
return 0;
}
}
simpleTimeAttribute13.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
const char* tag,
myTag(tag),
myRetractionHandle(theRetractionHandle)
{
for (
long unsigned int i(0); i < ahvps.
size(); ++i )
{
myAhvps.insert(std::make_pair(
std::string(
}
char* buff = new char[length + 1];
const_cast<RTI::FedTime&
>(fedTime).getPrintableString(buff);
myFedTime = std::string(buff);
delete [] buff;
}
const char* tag ) :
myTag(tag)
{
for (
long unsigned int i(0); i < ahvps.
size(); ++i )
{
myAhvps.insert(std::make_pair(
std::string(
}
myFedTime = std::string("no Time Given");
}
{ }
{
}
{
}
{
}
{
}
simpleTimeInteraction13.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
const char* tag,
interaction_type theInteractionType) :
myHandle(theHandle),
myTag(tag),
myInteractionType(theInteractionType)
{
for (
long unsigned int i(0); i < phvps.
size(); ++i )
{
myPhvps.insert(std::make_pair(
std::string(
}
char* buff = new char[length + 1];
const_cast<RTI::FedTime&
>(fedTime).getPrintableString(buff);
myFedTime = std::string(buff);
delete [] buff;
}
const char* tag,
interaction_type theInteractionType)
: myHandle(theHandle),
myTag(tag),
myFedTime(""),
myInteractionType(theInteractionType)
{
for (
long unsigned int i(0); i < phvps.
size(); ++i )
{
myPhvps.insert(std::make_pair(
std::string(
}
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
myHandle(handle),
myAttrHandleSet(attrHandSet)
{}
{
}
{
}
simpleTimeTimeManagedEntity.cxx
#ifdef WIN32
#pragma warning(disable: 4251)
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#endif
#include <assert.h>
#include <iostream>
myPosition(0),
myVelocity(0),
myPhysicalState(Alive),
myState(Initial),
myPhaseLine(150)
{ }
{
{
}
{
}
}
{
}
{
}
{
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
simpleTimeFedAmb13.h
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#include <map>
#include <set>
#include <string>
public :
public:
};
{
public:
throw (
RTI::FederateInternalError);
const char* theObjectName)
throw (
RTI::ObjectClassNotKnown,
RTI::FederateInternalError);
const char *theTag,
throw (
RTI::FederateOwnsAttributes,
RTI::InvalidFederationTime,
RTI::FederateInternalError);
const char *theTag)
throw (
RTI::FederateOwnsAttributes,
RTI::FederateInternalError);
const char *theTag,
throw (
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::InvalidFederationTime,
RTI::FederateInternalError);
const char *theTag)
throw (
RTI::InteractionClassNotKnown,
RTI::InteractionParameterNotKnown,
RTI::FederateInternalError);
const char *theTag,
throw (
RTI::InvalidFederationTime,
RTI::FederateInternalError);
const char *theTag)
throw (
RTI::FederateInternalError);
throw (
RTI::FederateInternalError);
throw (
RTI::InvalidFederationTime,
RTI::EnableTimeRegulationWasNotPending,
RTI::FederateInternalError);
throw (
RTI::InvalidFederationTime,
RTI::EnableTimeConstrainedWasNotPending,
RTI::FederateInternalError);
throw (
RTI::InvalidFederationTime,
RTI::TimeAdvanceWasNotInProgress,
RTI::FederateInternalError);
const char *label)
throw (
RTI::FederateInternalError);
const char *label)
throw (
RTI::FederateInternalError);
const char *label,
const char *tag)
throw (
RTI::FederateInternalError);
const char *label)
throw (
RTI::FederateInternalError);
public:
};
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(d) [-fedFile fedFileName][-m #Federates][-phaseLine #][-sleepTime s][-dedicated][-unManaged]"
<< endl << endl
<< setw( 20 ) << " -fedFile "
<< " Specify the Fed file name \n"
<< setw ( 24 ) << " " << "Default : MAKsimple.xml/fed. \n"
<< setw( 20 ) << " -m "
<< " Specify whether this federate is the master and if so, how many \n"
<< setw( 24 ) << " " << "total federates it should wait for. \n"
<< setw( 20 ) << " -phaseLine "
<< " Specify the boundary, which when this federate\n"
<< setw( 24 ) << " " << "crosses it, it starts firing.\n"
<< setw( 24 ) << " " << " Default is 75 units. \n"
<< setw( 20 ) << " -sleepTime "
<< " Specify the time in ms to sleep between iterations of the main loop. \n"
<< setw( 24 ) << " " << " Default is 850 ms. \n"
<< setw( 20 ) << " -dedicated "
<< " Specify whether this federate is on a machine dedicated to running\n"
<< setw( 24 ) << " "
<< " this federate. Invalidates all sleeps and yields.\n"
<< setw( 20 ) << " -unManaged "
<< " Specify whether this federate will respect the synchronization step\n"
<< setw( 24 ) << " " << " before time advancing.\n"
<< 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
simpleTimeAttribute13.h
#ifndef _ATTRIBUTE13DEFINITION_
#define _ATTRIBUTE13DEFINITION_
#include <map>
#include <string>
{
public:
const char* tag,
);
const char* tag
);
const std::map<unsigned long, std::string>&
getAhvps();
private:
std::map<unsigned long, std::string>
myAhvps;
};
#endif // #define _ATTRIBUTE13DEFINITION_
simpleTimeInteraction13.h
#ifndef _INTERACTIONDEFINITION_
#define _INTERACTIONDEFINITION_
#include <map>
#include <string>
{
public:
const char* tag,
);
const char* tag,
);
private:
std::map<unsigned long, std::string>
myPhvps;
};
{
public:
private:
};
#endif
stringUtil.h
timeManagedEntity.h