⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communication_inter.cpp

📁 一个用HLA编写的聊天程序
💻 CPP
字号:
//Communication_inter.cpp: the implementation of the Communication_inter class
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#if KDRTI_VERSION < 7
#include <RTI.h>
#else
#include <RTI.hh>
#include <fedtime.hh>
#endif

#include "Communication_inter.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//{{FW_PUBLIC_STATIC_PARAMETER_DEFINE_BEGIN <<----- Don't delete it!>>
RTI::InteractionClassHandle	Communication_inter::ms_Communication_interId = 0;
char*						Communication_inter::ms_Communication_interStr = "Communication";
RTI::ParameterHandle		Communication_inter::ms_nameId = 0;
char*						Communication_inter::ms_nameStr = "name";
RTI::ParameterHandle		Communication_inter::ms_messageId = 0;
char*						Communication_inter::ms_messageStr = "message";
RTI::RTIambassador*			Communication_inter::m_RtiAmb = NULL;
RTI::Boolean				Communication_inter::ms_SendInterFlag = RTI::RTI_TRUE;
//{{FW_PUBLIC_STATIC_PARAMETER_DEFINE_END <<----- Don't delete it!>>

void Communication_inter::SetRtiAmb(RTI::RTIambassador &theRtiAmb)
{
	m_RtiAmb = &theRtiAmb;
}

void Communication_inter::Init()
{
	try{
		ms_Communication_interId = m_RtiAmb->getInteractionClassHandle(ms_Communication_interStr);
		ms_nameId = m_RtiAmb->getParameterHandle(ms_nameStr, ms_Communication_interId);
		ms_messageId = m_RtiAmb->getParameterHandle(ms_messageStr, ms_Communication_interId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Init()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

RTI::InteractionClassHandle Communication_inter::GetClassId()
{
	return ms_Communication_interId;
}

RTI::ParameterHandle Communication_inter::FindParameterHandle(CString theParaName)
{
	RTI::ParameterHandle theFindValue = ATTRNOHANDLE;

	if(theParaName == ms_nameStr) {
		theFindValue = ms_nameId;
	} else if(theParaName == ms_messageStr) {
		theFindValue = ms_messageId;
	}

	return theFindValue;
}

void Communication_inter::Publishing()
{
	try{
		m_RtiAmb->publishInteractionClass(ms_Communication_interId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Publishing()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void Communication_inter::Unpublished()
{
	try{
		m_RtiAmb->unpublishInteractionClass(ms_Communication_interId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Unpublished()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void Communication_inter::SetInteractionControl(RTI::Boolean theFlag)
{
	ms_SendInterFlag = theFlag;
}

void Communication_inter::Send(RTIfedTime theTimeStamp, const char *theTag)
{
	try{
		RTI::ParameterHandleValuePairSet* pParams = NULL;

		pParams = RTI::ParameterSetFactory::create(2);
		BEGIN_PACKING_PARAMETERS_DATA(pParams)
			PACKING_STRING_PARAMETERDATA(m_name, ms_nameStr)
			PACKING_STRING_PARAMETERDATA(m_message, ms_messageStr)
		END_PACKING_PARAMETERS_DATA()
		m_RtiAmb->sendInteraction(ms_Communication_interId, *pParams, theTimeStamp, theTag);
		if(NULL != pParams) {
			delete pParams;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Send(RTIfedTime)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void Communication_inter::Send(const char *theTag)
{
	try{
		RTI::ParameterHandleValuePairSet* pParams = NULL;

		pParams = RTI::ParameterSetFactory::create(2);
		BEGIN_PACKING_PARAMETERS_DATA(pParams)
			PACKING_STRING_PARAMETERDATA(m_name, ms_nameStr)
			PACKING_STRING_PARAMETERDATA(m_message, ms_messageStr)
		END_PACKING_PARAMETERS_DATA()
		m_RtiAmb->sendInteraction(ms_Communication_interId, *pParams, theTag);
		if(NULL != pParams) {
			delete pParams;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Send()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}


void Communication_inter::Subscribing()
{
	try{
		m_RtiAmb->subscribeInteractionClass(ms_Communication_interId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Subscribing()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void Communication_inter::Unsubscribed()
{
	try{
		m_RtiAmb->unsubscribeInteractionClass(ms_Communication_interId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: Communication_inter::Unsubscribed()\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}


void Communication_inter::Receive(const RTI::ParameterHandleValuePairSet& theParameters)
{
	BEGIN_UNPACKING_PARAMETERS_DATA(&theParameters)
		UNPACKING_STRING_PARAMETERDATA(m_name, ms_nameStr)
		UNPACKING_STRING_PARAMETERDATA(m_message, ms_messageStr)
	END_UNPACKING_PARAMETERS_DATA()
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -