#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"
using namespace std;
RTI::FederateHandle theFederateHandle;
std::string theClassName = "BaseEntity";
std::string theInterClassName = "WeaponFire";
RTI::ObjectClassHandle theClassHandle;
RTI::InteractionClassHandle theInterClassHandle;
RTI::ObjectHandle theObjectHandle;
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 << "FederationExecutionAlreadyExists: " << ex._name << " " << ex._reason << endl
<< "\tCould not create Federation Execution!" << endl;
}
catch(RTI::Exception& ex)
{
cout << "RTI Exception: " << ex._name << " " << ex._reason << endl
<< "\tCould not create federation execution!" << endl;
exit(0);
}
rtiAmb.tick(0.1, 0.2);
cout << "Federation Created" << endl;
}
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
<< "\tCould not join federation execution!" << endl;
exit(0);
}
rtiAmb.tick(0.1, 0.2);
}
if (joined)
cout << "Joined Federation." << endl;
else
{
cout << "Giving up." << endl;
rtiAmb.destroyFederationExecution(federationName.c_str());
exit(0);
}
}
void resignAndDestroy( RTI::RTIambassador & rtiAmb, std::string const& federationName)
{
cout << "Resign and Destroy Federation" << endl;
rtiAmb.destroyFederationExecution(federationName.c_str());
}
bool publishSubscribeAndRegisterObject(RTI::RTIambassador & rtiAmb)
{
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;
}
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;
}
for (DtAttrNameHandleMap::iterator iter = theAttrNameHandleMap.begin(); iter != theAttrNameHandleMap.end(); iter++)
{
hSet->add(iter->second);
}
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");
try
{
unsigned int objId = abs(getpid());
std::stringstream pid;
pid << objId;
objectName += pid.str();
theObjectHandle = rtiAmb.registerObjectInstance(theClassHandle, objectName.c_str());
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;
}
bool publishAndSubscribeInteraction(RTI::RTIambassador & rtiAmb)
{
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;
}
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;
}
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
{
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 [federationName] [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];
}
#ifdef WIN32
WSADATA data;
WSAStartup(MAKEWORD(1, 1), &data);
#endif
RTI::RTIambassador rtiAmb;
RTI::AttributeHandleValuePairSet *attrValues = 0;
RTI::ParameterHandleValuePairSet *paramValues = 0;
rtiAmb.tick();
long count = 0;
bool doConnect = true;
bool connected = false;
while (1)
{
if (doConnect)
{
doConnect = false;
createFedEx(rtiAmb, federationName, federationFile);
joinFedEx(rtiAmb, &fedAmb, federateType, federationName);
if (!publishSubscribeAndRegisterObject(rtiAmb))
{
resignAndDestroy(rtiAmb, federationName);
return 0;
}
if (!publishAndSubscribeInteraction(rtiAmb))
{
resignAndDestroy(rtiAmb, federationName);
return 0;
}
attrValues = RTI::AttributeSetFactory::create(theAttrNameHandleMap.size());
DtAttrNameHandleMap::iterator attrIter = theAttrNameHandleMap.begin();
DtAttrNameHandleMap::iterator attrEnd = theAttrNameHandleMap.end();
for ( ; attrIter != attrEnd; attrIter++)
{
attrValues->add(attrIter->second, attrIter->first.c_str(), attrIter->first.length()+1);
}
paramValues = RTI::ParameterSetFactory::create(theParamNameHandleMap.size());
DtParamNameHandleMap::iterator paramIter = theParamNameHandleMap.begin();
DtParamNameHandleMap::iterator paramEnd = theParamNameHandleMap.end();
for ( ; paramIter != paramEnd; paramIter++)
{
paramValues->add(paramIter->second, paramIter->first.c_str(), paramIter->first.length()+1);
}
connected = true;
}
else if (connected)
{
std::stringstream ss;
ss << "1.3-" << count++;
std::string tag(ss.str());
rtiAmb.updateAttributeValues(theObjectHandle, *attrValues, tag.c_str());
if ( count % 5 == 0 )
{
cout << "Sending interaction..." << endl;
rtiAmb.sendInteraction(theInterClassHandle, *paramValues, tag.c_str());
}
rtiAmb.tick(0.1, 0.5);
}
int key = theInput.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;
}
}
}
#ifdef WIN32
Sleep(2000);
#else
sleep(2);
#endif
}
resignAndDestroy(rtiAmb, federationName);
}
catch (RTI::Exception& ex)
{
cout << "RTI Exception (main loop): "
<< ex._name << " "
<< ex._reason << endl;
}
#ifdef WIN32
WSACleanup();
#endif
return 0;
}
#pragma warning(disable: 4786)
#pragma warning(disable: 4290)
#include <stdio.h>
#include <iostream>
using namespace std;
void printAttrValues(const RTI::AttributeHandleValuePairSet& attributes)
{
for (unsigned int iloop = 0; iloop < attributes.size(); iloop++)
{
RTI::ULong length = attributes.getValueLength(iloop);
cout << "\t" << attributes.getHandle(iloop) << " " << attributes.getValuePointer(iloop,length) << endl;
}
}
void printParamValues(const RTI::ParameterHandleValuePairSet& params)
{
for (unsigned int iloop = 0; iloop < params.size(); iloop++)
{
RTI::ULong length = params.getValueLength(iloop);
cout << "\t" << 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;
}
: NullFederateAmbassador(), myData(data)
{
}
throw (RTI::FederateInternalError)
{
int x = 1;
}
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;
}
RTI::ObjectHandle theObject,
const RTI::AttributeHandleValuePairSet& theAttributes,
const RTI::FedTime& theTime,
const char *theTag,
RTI::EventRetractionHandle theHandle)
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;
printAttrValues(theAttributes);
}
RTI::ObjectHandle theObject,
const RTI::AttributeHandleValuePairSet& theAttributes,
const char *theTag)
throw (RTI::ObjectNotKnown, RTI::AttributeNotKnown, RTI::FederateOwnsAttributes, RTI::FederateInternalError)
{
cout << "reflectAttributeValues: "
<< myData.objectInstanceMap[theObject].c_str() << "("
<< theObject << ") "
<< (theTag ? theTag : "") << " "
<< "#attributes: " << theAttributes.size() << endl;
printAttrValues(theAttributes);
}
RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet& theParameters,
const RTI::FedTime& theTime,
const char *theTag,
RTI::EventRetractionHandle theHandle)
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;
printParamValues(theParameters);
}
RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet& theParameters,
const char *theTag)
throw (RTI::InteractionClassNotKnown, RTI::InteractionParameterNotKnown, RTI::FederateInternalError)
{
cout << "receiveInteraction: "
<< myData.interactionClassMap[theInteraction].c_str() << "( "
<< theInteraction << ") "
<< (theTag ? theTag : "") << " "
<< "#parameters: " << theParameters.size() << endl;
printParamValues(theParameters);
}
RTI::ObjectHandle theObject,
const RTI::FedTime& theTime,
const char *theTag,
RTI::EventRetractionHandle theHandle)
throw (RTI::ObjectNotKnown, RTI::InvalidFederationTime, RTI::FederateInternalError)
{
cout << "removeObjectInstance: "
<< myData.objectInstanceMap[theObject].c_str() << "("
<< theObject << ") "
<< timeToString(theTime).c_str() << " "
<< (theTag ? theTag : "") << endl;
}
RTI::ObjectHandle theObject,
const char *theTag)
throw (RTI::ObjectNotKnown, RTI::FederateInternalError)
{
cout << "removeObjectInstance: "
<< myData.objectInstanceMap[theObject].c_str() << "("
<< theObject << ") "
<< (theTag ? theTag : "") << endl;
}