// TemplateFederate.cpp: implementation of the TemplateFederate class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "handles.h"
#include "TemplateFederate.h"
#include "ship.h"
#include "detonation.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
TemplateFederate::TemplateFederate()
{
m_lookahead=1.0;
m_timeStep=1.0;
pShipHandels_struct = (struct struct_ship_objectClass_handles *)
malloc(sizeof(struct struct_ship_objectClass_handles));
pDetonationHandles_struct = (struct struct_Detonation_interactionClass_handles *)
malloc(sizeof(struct struct_Detonation_interactionClass_handles));
}
TemplateFederate::~TemplateFederate()
{
free(pShipHandels_struct);
free(pDetonationHandles_struct);
}
void TemplateFederate::run(int argc, char **args)
{
//创建并加入联邦
CreateAndJoin();
//时间设置,时间受限和时间调节
//先设置时间受限,再设置时间调节
setTimeConstrained(true);
setTimeRegulation(true);
// setTimeConstrained(true);
//对象类与交互类的发布与定购
PublishAndSubscribe();
//登记对象
RegisterObject();
CShip *cship = new CShip();//创建ship类型实例
IDetonation *idetonation = new IDetonation();//创建交互类实例
cout << endl;
cout << "//**********************************************************************//" << endl;
cout << "//****************************进入仿真主循环******************************//" << endl;
cout << "//**********************************************************************//";
cout << endl << "按任意键进入仿真主循环... ..." << endl << endl;
getchar();
//这里只循环120次
//用户可根据实际需要确定退出仿真的条件
int i=0;
while(i<120)
{
i++;
cout << "当前仿真时间为" << "m_currTime = " ;
char ctmp[20];
m_currTime.getPrintableString(ctmp);
cout << ctmp << endl;
cout << "1、对登记的对象实例进行外部更新;" << endl;
updateAttributeValues(cship);
cout << "2、发送交互;" << endl;
sendInteraction(idetonation);
cout << "3、重新计算ship实例和交互实例的内部状态" << endl;
cship->Compute();
idetonation->Compute();
cout << "4、申请时间推进,申请时间为: " ;
m_bIsTimeGranted=false;
requestTimeAdvance(m_currTime+m_timeStep);
cout << "5、等待时间推进授予,接收RTI回调函数 " << endl;
while(!m_bIsTimeGranted){
//休眠等待时间推进申请被授予
Sleep(500);
}
//这里的Sleep()是为里便于在显示器上观察输出结果
Sleep(1000);
cout << endl;
cout << endl;
cout << "... ... ... ... ... 新循环开始 ... ... ... ... ..." << endl;
}
cout << "6、退出联邦、销毁联邦" << endl << endl;
resignAndDestroy("HLATemplate");
}
void TemplateFederate::CreateAndJoin()
{
//创建联邦,第一个参数为创建的联邦名,第二个为相应的fed文件名
//如果联邦已经存在,_rtiAmbassador获取存在的联邦大使句柄
try{
_rtiAmbassador.createFederationExecution("HLATemplate", "SeaWar1.fed");
} catch (RTI::FederationExecutionAlreadyExists) {
cerr << "FED_HW: 联邦执行已经存在" << endl;
} catch (RTI::CouldNotOpenFED){
cerr << "FED_HW:不能打开fed文件";
return ;
}catch (RTI::ErrorReadingFED){
cerr << "FED_HW:无法读取fed文件";
return ;
}catch (RTI::ConcurrentAccessAttempted){
cerr << "FED_HW:Concurrent access attempted";
return ;
}
cout << "第一步:联邦创建成功" << endl;
//加入联邦,第一个参数为联邦成员类型,第二个参数为上一步创建的联邦名
//由于可能在上一步的创建工作还没完成时就申请加入联邦,所以需要反复试图加入
//试50次
int tries=0;
bool m_joined=false;;
while((!m_joined)&&(tries<50)){
tries++;
try{
_federateHandle=_rtiAmbassador.joinFederationExecution("Ship", "HLATemplate", this);//加入联邦
} catch (RTI::FederateAlreadyExecutionMember){
m_joined=true;
} catch (RTI::FederationExecutionDoesNotExist){
if(tries==50){
cout << "Can not join federation, program closed" << endl;
return;
}else{
Sleep(5);
}
cout << "FederationExecutionDoesNotExist" << endl;
} catch (RTI::SaveInProgress){
cerr << "SaveInProgress" << endl;
} catch (RTI::RestoreInProgress){
cerr << "RestoreInProgress" << endl;
} catch (RTI::RTIinternalError){
cerr << "RTIinternalError" << endl;
} catch (RTI::ConcurrentAccessAttempted){
cerr << "ConcurrentAccessAttempted" << endl;
}
}
cout << "第二步,加入联邦," ;
cout << "联邦成员句柄为: " << _federateHandle << endl;
}
void TemplateFederate::setTimeConstrained(bool m_bConstrained)
{
try{
//如果m_bConstrained为真,设置时间受限,否则取消时间受限
if( m_bConstrained )
{
cout << endl << "第三步,设置时间受限." << endl;
_rtiAmbassador.enableTimeConstrained();
}
else
{
//取消时间受限
_rtiAmbassador.disableTimeConstrained();
}
}catch(RTI::TimeRegulationAlreadyEnabled){
cout << "TimeRegulationAlreadyEnabled " << endl;
}catch(RTI::RestoreInProgress){
cout << "RestoreInProgress " << endl;
}catch(RTI::SaveInProgress){
cout << " SaveInProgress" << endl;
}catch(RTI::EnableTimeRegulationPending){
cout << "EnableTimeRegulationPending " << endl;
}catch(RTI::FederateNotExecutionMember){
cout << "FederateNotExecutionMember " << endl;
}catch(RTI::ConcurrentAccessAttempted){
cout << "ConcurrentAccessAttempted " << endl;
}catch(RTI::InvalidLookahead){
cout << " InvalidLookahead" << endl;
}catch(RTI::InvalidFederationTime){
cout << " InvalidFederationTime" << endl;
}catch(RTI::TimeAdvanceAlreadyInProgress){
cout << " TimeAdvanceAlreadyInProgress" << endl;
}catch(RTI::RTIinternalError){
cout << "RTIinternalError " << endl;
}
}
void TemplateFederate::setTimeRegulation(bool m_bRegulation)
{
try{
//查询联邦成员的当前时间,用变量m_currTime存储,用于设置时间调节
// _rtiAmbassador.queryLBTS(m_currTime);
_rtiAmbassador.queryFederateTime(m_currTime);
if ( m_currTime.isPositiveInfinity() )
{
m_currTime = 0.0;
}
else
{
// m_currTime -= m_lookahead;
}
char tmp[20];
m_currTime.getPrintableString(tmp);
cout << tmp << endl << endl;
//如果m_bRegulation为真,设置时间调节,否则取消时间调节
if( m_bRegulation )
{
cout <<"第四步,设置时间调节,当前仿真时间为:";
_rtiAmbassador.enableTimeRegulation( m_currTime, m_lookahead );
Sleep(5);
_rtiAmbassador.queryFederateTime(m_currTime);
}
else
{
_rtiAmbassador.disableTimeRegulation();
}
// char tmp[20];
m_currTime.getPrintableString(tmp);
cout << tmp << endl << endl;
}catch(RTI::TimeRegulationAlreadyEnabled){
cout << "TimeRegulationAlreadyEnabled " << endl;
}catch(RTI::RestoreInProgress){
cout << "RestoreInProgress " << endl;
}catch(RTI::SaveInProgress){
cout << " SaveInProgress" << endl;
}catch(RTI::EnableTimeRegulationPending){
cout << "EnableTimeRegulationPending " << endl;
}catch(RTI::FederateNotExecutionMember){
cout << "FederateNotExecutionMember " << endl;
}catch(RTI::ConcurrentAccessAttempted){
cout << "ConcurrentAccessAttempted " << endl;
}catch(RTI::InvalidLookahead){
cout << " InvalidLookahead" << endl;
}catch(RTI::InvalidFederationTime){
cout << " InvalidFederationTime" << endl;
}catch(RTI::TimeAdvanceAlreadyInProgress){
cout << " TimeAdvanceAlreadyInProgress" << endl;
}catch(RTI::RTIinternalError){
cout << "RTIinternalError " << endl;
}
}
void TemplateFederate::PublishAndSubscribe()
{
cout << endl << "第五步,发布和定购对象类和交互类" << endl;
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////对象类//////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//1、获取要定购或发布的对象