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

📄 pub_person.cpp

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

#include "stdafx.h"

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

#include "Pub_Person.h"
#include "Global.h"
#include "Resource.h"
#include "ChatView.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

extern CChatView* pView;
extern CFederate Fed;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//{{FW_PUBLIC_STATIC_ATTRIBUTE_DEFINE_BEGIN <<----- Don't delete it!>>
RTI::ObjectClassHandle	CPub_Person::ms_PersonId = 0;
char*					CPub_Person::ms_PersonStr = "Person";
RTI::AttributeHandle	CPub_Person::ms_ageId = 0;
char* const				CPub_Person::ms_ageStr = "age";
RTI::AttributeHandle	CPub_Person::ms_real_nameId = 0;
char* const				CPub_Person::ms_real_nameStr = "real_name";
RTI::AttributeHandle	CPub_Person::ms_user_nameId = 0;
char* const				CPub_Person::ms_user_nameStr = "user_name";
CStringArray			CPub_Person::m_AttrNamesArray;
//}}FW_PUBLIC_STATIC_ATTRIBUTE_DEFINE_END <<----- Don't delete it!>>

CPub_Person::CPub_Person()
{
	m_ObjClassName = "Person";
	ms_IsSendage = RTI::RTI_FALSE;
	ms_IsTransferage = RTI::RTI_FALSE;
	ms_IsOwnedage = RTI::RTI_FALSE;
	ms_IsSendreal_name = RTI::RTI_FALSE;
	ms_IsTransferreal_name = RTI::RTI_FALSE;
	ms_IsOwnedreal_name = RTI::RTI_FALSE;
	ms_IsSenduser_name = RTI::RTI_FALSE;
	ms_IsTransferuser_name = RTI::RTI_FALSE;
	ms_IsOwneduser_name = RTI::RTI_FALSE;

}

unsigned long CPub_Person::GetAttributeCount()
{
	return m_AttrNamesArray.GetSize();
}

void CPub_Person::Init(const RTI::ObjectClassHandle &theObject)
{
	try{
		ms_PersonId = m_RtiAmb->getObjectClassHandle(ms_PersonStr);
		ms_ageId = m_RtiAmb->getAttributeHandle(ms_ageStr,ms_PersonId);
		ms_real_nameId = m_RtiAmb->getAttributeHandle(ms_real_nameStr,ms_PersonId);
		ms_user_nameId = m_RtiAmb->getAttributeHandle(ms_user_nameStr,ms_PersonId);

		CFWBaseClass::Init(ms_PersonId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::Init(const RTI::ObjectClassHandle&)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

RTI::ObjectClassHandle CPub_Person::GetClassId()
{
	return ms_PersonId;
}

RTI::AttributeHandleSet* CPub_Person::GetAttributeHandleSet(CStringArray& theAttrNames)
{
	if(theAttrNames.GetSize() == 0) {
		return NULL;
	}
	RTI::AttributeHandleSet *theAttrHandles = 
		RTI::AttributeHandleSetFactory::create(theAttrNames.GetSize());

	RTI::AttributeHandle theHandle = ATTRNOHANDLE;
	for(int i = 0; i < theAttrNames.GetSize(); i++) {
		theHandle = FindAttributeHandle(theAttrNames.GetAt(i));
		if(ATTRNOHANDLE != theHandle) {
			theAttrHandles->add(theHandle);			
		}
		theHandle = ATTRNOHANDLE;
	}

	return theAttrHandles;
}

RTI::AttributeHandle CPub_Person::FindAttributeHandle(CString theAttrName)
{
	RTI::AttributeHandle theFindValue = ATTRNOHANDLE;

	if(theAttrName == ms_ageStr) {
		theFindValue = ms_ageId;
	} else if(theAttrName == ms_real_nameStr) {
		theFindValue = ms_real_nameId;
	} else if(theAttrName == ms_user_nameStr) {
		theFindValue = ms_user_nameId;
	} else {
		theFindValue = CFWBaseClass::FindAttributeHandle(theAttrName);
	}

	return theFindValue;
}

void CPub_Person::Publishing()
{
	try{
		RTI::AttributeHandleSet *theAttrHandles = NULL;

		//the published attributes of this student
		m_AttrNamesArray.RemoveAll();
		m_AttrNamesArray.Add("age");
		m_AttrNamesArray.Add("real_name");
		m_AttrNamesArray.Add("user_name");
		
		theAttrHandles = GetAttributeHandleSet(m_AttrNamesArray);
		m_RtiAmb->publishObjectClass(ms_PersonId, *theAttrHandles);
		delete theAttrHandles;
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

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

void CPub_Person::Unpublished()
{
	try{
		m_RtiAmb->unpublishObjectClass(ms_PersonId);
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

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

void CPub_Person::CreateNVPSet(RTI::AttributeHandleValuePairSet  *pAttributes,
	 CStringArray &theAttrNames)
{
	try{
		BEGIN_PACKING_ATTRIBUTES_DATA(pAttributes, theAttrNames)
			PACKING_BASIC_ATTRIBUTEDATA(m_age, ms_ageStr, ms_IsSendage, sizeof(m_age))
			PACKING_STRING_ATTRIBUTEDATA(m_real_name, ms_real_nameStr, ms_IsSendreal_name)
			PACKING_STRING_ATTRIBUTEDATA(m_user_name, ms_user_nameStr, ms_IsSenduser_name)
		END_PACKING_ATTRIBUTES_DATA()

		CFWBaseClass::CreateNVPSet(pAttributes, theAttrNames);

		RTI::AttributeHandleSet* theHandles = NULL;
		theHandles = GetAttributeHandleSet(theAttrNames);
		if(theHandles != NULL) {
			SetUpdateControl(RTI::RTI_FALSE, *theHandles);
			delete theHandles;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::CreateNVPSet(RTI::AttributeHandleValuePairSet*, CStringArray&)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CPub_Person::UpdateAttributeValues(CStringArray &upAttrNames, char *theTag)
{
	try {
		RTI::AttributeHandleValuePairSet* pNvpSet = NULL;
		RTI::AttributeHandleSet* theHandles = NULL;

		if(upAttrNames.GetSize() == 0)
			return;
		pNvpSet = RTI::AttributeSetFactory::create(upAttrNames.GetSize());
		theHandles = GetAttributeHandleSet(upAttrNames);
		SetUpdateControl(RTI::RTI_TRUE, *theHandles);
		delete theHandles;
		theHandles = NULL;
		CreateNVPSet(pNvpSet, upAttrNames);
		if(NULL == pNvpSet || 0 == pNvpSet->size())
			return;
		m_RtiAmb->updateAttributeValues(m_InstanceId, *pNvpSet, theTag);
		if(NULL != pNvpSet) {
			pNvpSet->empty();
			delete pNvpSet;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::UpdateAttributeValues(char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CPub_Person::UpdateAttributeValues(CStringArray &upAttrNames, RTIfedTime theTimeStamp,char *theTag)
{
	try {
		RTI::AttributeHandleValuePairSet* pNvpSet = NULL;
		RTI::AttributeHandleSet* theHandles = NULL;

		if(upAttrNames.GetSize() == 0)
			return;
		pNvpSet = RTI::AttributeSetFactory::create(upAttrNames.GetSize());
		theHandles = GetAttributeHandleSet(upAttrNames);
		SetUpdateControl(RTI::RTI_TRUE, *theHandles);
		delete theHandles;
		theHandles = NULL;
		CreateNVPSet(pNvpSet, upAttrNames);
		if(0 == pNvpSet->size()) {
			return;
		}
		m_RtiAmb->updateAttributeValues(m_InstanceId, *pNvpSet, theTimeStamp, theTag);
		if(NULL != pNvpSet) {
			pNvpSet->empty();
			delete pNvpSet;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::UpdateAttributeValues(RTIfedTime, char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

RTI::Boolean CPub_Person::IsUpdate()
{
	if(ms_IsSendage || ms_IsSendreal_name || 
		ms_IsSenduser_name) {
		return RTI::RTI_TRUE;
	}

	return CFWBaseClass::IsUpdate();
}

void CPub_Person::SetUpdateControl(RTI::Boolean updateFlag,
	 const RTI::AttributeHandleSet &theAttrHandles)
{
	RTI::AttributeHandle theHandle;

	for (unsigned int i = 0; i < theAttrHandles.size(); i++) {
		theHandle = theAttrHandles.getHandle(i);
		if(theHandle == ms_ageId) {
			ms_IsSendage = updateFlag;
		} else if(theHandle == ms_real_nameId) {
			ms_IsSendreal_name = updateFlag;
		} else if(theHandle == ms_user_nameId) {
			ms_IsSenduser_name = updateFlag;
		}
	}

	CFWBaseClass::SetUpdateControl(updateFlag, theAttrHandles);
}

void CPub_Person::ProvideUpdate(const RTI::AttributeHandleSet &theAttrHandles)
{
	SetUpdateControl(RTI::RTI_TRUE, theAttrHandles);
}

void CPub_Person::SetTransferOwnerFlag(RTI::Boolean transferFlag,
	 const RTI::AttributeHandleSet &theAttrHandles)
{
	RTI::AttributeHandle theHandle;

	for (unsigned int i = 0; i < theAttrHandles.size(); i++) {
		theHandle = theAttrHandles.getHandle(i);
		if(theHandle == ms_ageId) {
			ms_IsTransferage = transferFlag;
		} else if(theHandle == ms_real_nameId) {
			ms_IsTransferreal_name = transferFlag;
		} else if(theHandle == ms_user_nameId) {
			ms_IsTransferuser_name = transferFlag;
		}
	}

	CFWBaseClass::SetTransferOwnerFlag(transferFlag, theAttrHandles);
}

void CPub_Person::SetOwnedAttrsFlag(RTI::Boolean ownedFlag,
	 const RTI::AttributeHandleSet &theAttrHandles)
{
	RTI::AttributeHandle theHandle;

	for (unsigned int i = 0; i < theAttrHandles.size(); i++) {
		theHandle = theAttrHandles.getHandle(i);
		if(theHandle == ms_ageId) {
			ms_IsOwnedage = ownedFlag;
		} else if(theHandle == ms_real_nameId) {
			ms_IsOwnedreal_name = ownedFlag;
		} else if(theHandle == ms_user_nameId) {
			ms_IsOwneduser_name = ownedFlag;
		}
	}

	CFWBaseClass::SetOwnedAttrsFlag(ownedFlag, theAttrHandles);
}

void CPub_Person::RegisterObject(char *theobjectName)
{
	try{
		RTI::AttributeHandleSet* theAttrHandles = NULL;

		if(NULL != theobjectName) {
			m_InstanceId = m_RtiAmb->registerObjectInstance(ms_PersonId, theobjectName);
		} else {
			m_InstanceId = m_RtiAmb->registerObjectInstance(ms_PersonId);
		}
		m_IsExisting = RTI::RTI_TRUE;

		CStringArray attrNamesArray;
		for(int i = 0; i < m_AttrNamesArray.GetSize(); i++) {
			attrNamesArray.Add(m_AttrNamesArray.GetAt(i));
		}
		attrNamesArray.Add(ms_PrivilegeToDeleteStr);
		theAttrHandles = GetAttributeHandleSet(attrNamesArray);
		SetOwnedAttrsFlag(RTI::RTI_TRUE, *theAttrHandles);
		SetTransferOwnerFlag(RTI::RTI_TRUE, *theAttrHandles);
		if(NULL != theAttrHandles) {
			delete theAttrHandles;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::RegisterObject(const char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}


CPub_Person* CPub_Person::CreateAndRegisterObject(RTI::RTIambassador &theRtiAmb,
	 char *theobjectName)
{
	try{
		RTI::ObjectHandle	InstanceId = OBJNOHANDLE;
		CPub_Person*		Pub_Person = NULL;

		if(theobjectName != NULL) {
			InstanceId = m_RtiAmb->registerObjectInstance(ms_PersonId, theobjectName);
		} else {
			InstanceId = m_RtiAmb->registerObjectInstance(ms_PersonId);
		}
		if(OBJNOHANDLE != InstanceId) {
			Pub_Person = new CPub_Person;
			Pub_Person->m_InstanceId = InstanceId;
			if(theobjectName != NULL)
				Pub_Person->m_InstName = theobjectName;
			Pub_Person->m_IsExisting = RTI::RTI_TRUE;

			CStringArray attrNamesArray;
			RTI::AttributeHandleSet* theAttrHandles = NULL;
			
			for(int i = 0; i < m_AttrNamesArray.GetSize(); i++) {
				attrNamesArray.Add(m_AttrNamesArray.GetAt(i));
			}
			attrNamesArray.Add(ms_PrivilegeToDeleteStr);
			theAttrHandles = Pub_Person->GetAttributeHandleSet(attrNamesArray);
			Pub_Person->SetOwnedAttrsFlag(RTI::RTI_TRUE, *theAttrHandles);
			Pub_Person->SetTransferOwnerFlag(RTI::RTI_TRUE, *theAttrHandles);
			if(NULL != theAttrHandles) {
				delete theAttrHandles;
			}
		}

		return Pub_Person;
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Person::CreateAndRegisterObject(RTI::RTIambassador&, char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CPub_Person::DestoryObjInst(CPub_Person* pObj, RTI::RTIambassador &theRtiAmb, char *theTag)
{
	try{
		if(OBJNOHANDLE != pObj->m_InstanceId) {
			theRtiAmb.deleteObjectInstance(pObj->m_InstanceId, theTag);
		}
		if(pObj != NULL) {
			delete pObj;
			pObj = NULL;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Student::DestoryObjInst(CPub_Student*, RTI::RTIambassador&, \
			char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CPub_Person::DestoryObjInst(CPub_Person* pObj, RTI::RTIambassador &theRtiAmb,
	 const RTI::FedTime &theTimeStamp, char *theTag)
{
	try{
		if(OBJNOHANDLE != pObj->m_InstanceId) {
			theRtiAmb.deleteObjectInstance(pObj->m_InstanceId, theTimeStamp, theTag);
		}
		if(pObj != NULL) {
			delete pObj;
			pObj = NULL;
		}
	}
	catch(RTI::Exception& e)
	{
		CString errInfo;

		errInfo = "error located: CPub_Student::DestoryObjInst(CPub_Student*, RTI::RTIambassador&, \
			const RTI::FedTime&, char*)\n";
		errInfo += "error message:";
		errInfo += CString(e._reason);
		errInfo += "\n";
		TRACE0(errInfo);
		throw errInfo;
	}
}

void CPub_Person::Process(double time)
{
	//You can add some codes to process your model here 
	//进行属性更新
	CStringArray theNames;
	theNames.Add("user_name");
	theNames.Add("real_name");
	theNames.Add("age");
	m_age = atol(pView->age);
	m_real_name = pView->name;
	m_user_name = Fed.m_FederateName;
	for(int i =0; i <Fed.m_Pub_Persons.GetSize(); i++)
	{
		Fed.m_Pub_Persons[i]->UpdateAttributeValues(theNames);
	}
}

void CPub_Person::ProcessReceiveInter(RTI::InteractionClassHandle theClass,
	 const RTI::ParameterHandleValuePairSet &theParameters, const RTI::FedTime &theFederateTime, const char	*tag)
{
	//Add your code to process the received interaction
	//{{FW_EXAMPLE_CODE_BEGIN
		//if(theClass == INTERACTION::GetClassId()) {
		//	INTERACTION	theInter;
		//	theInter.Receive(theParameters);
		//
		//	//Add you code here
		//	return;
		//}	
	//}}FW_EXAMPLE_CODE_END
}

void CPub_Person::ProcessReceiveInter(RTI::InteractionClassHandle theClass,
	 const RTI::ParameterHandleValuePairSet &theParameters, const char	*tag)
{
	//Add your code to process the received interaction
	//{{FW_EXAMPLE_CODE_BEGIN
		//if(theClass == INTERACTION::GetClassId()) {
		//	INTERACTION	ReceiveInter;
		//	ReceiveInter.Receive(theParameters);
		//
		//	//Add you code here
		//	return;
		//}	
	//}}FW_EXAMPLE_CODE_END
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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