int
SharedMemoryUtils::commitSM()
{

	//mark last msm: isLastNode=1
	boost::interprocess::windows_shared_memory * lastMsm = &(msmList.back());
	boost::interprocess::mapped_region region1(
			*lastMsm,
			boost::interprocess::read_write,
			SMUCONST_MSM_HEADER_ISLAST_OFFSET,
			SMUCONST_MSM_HEADER_ISLAST_SIZE
		);
	char* isLastNodePtr = static_cast<char*>(region1.get_address());
	*isLastNodePtr = '1';


	uint16_t recordNo = processCsmId - 1;

	//set status to (W)orking
	boost::interprocess::mapped_region region3(
		*csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
		SMUCONST_CSM_RECORD_STATUS_SIZE
	);
	char* recordStatusPtr = static_cast<char*>(region3.get_address());
	*recordStatusPtr = 'W';

	return 0;
}
void VendorHandler::pass3to2() {
    setValid2(true);
    setName2(name3());
    setStreet2(street3());
    setCity2(city3());
    setState2(state3());
    setRegion2(region3());
    setTime2(time3());
    setZip2(zip3());
    setPrice2(price3());
}
void
SharedMemoryUtils::refreshMsm_Edit(char type, uint64_t mirandaId, std::wstring& displayName)
{
	logger->log_p(L"SharedMemoryUtils.refreshMsm_Edit type=[%c], mirandaId=[%I64u]", type, mirandaId);

	boost::ptr_list<boost::interprocess::windows_shared_memory>::iterator msmListIter;
	for (msmListIter = msmList.begin(); msmListIter != msmList.end(); msmListIter++){

		for (int recordNo = 0; recordNo < SMUCONST_MSM_RECORDS_COUNT; recordNo++ ){

			boost::interprocess::mapped_region region1(
				*msmListIter,
				boost::interprocess::read_only,
				SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_TYPE_RECOFFSET,
				SMUCONST_MSM_RECORD_TYPE_SIZE
			);
			char* recordTypePtr = static_cast<char*>(region1.get_address());

			if (*recordTypePtr == type){

				boost::interprocess::mapped_region region2(
					*msmListIter,
					boost::interprocess::read_only,
					SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_HANDLE_RECOFFSET,
					SMUCONST_MSM_RECORD_HANDLE_SIZE
				);
				uint64_t* agcHandlePtr = static_cast<uint64_t*>(region2.get_address());

				if (*agcHandlePtr == mirandaId){

					boost::interprocess::mapped_region region3(
						*msmListIter,
						boost::interprocess::read_write,
						SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_DNAME_RECOFFSET,
						SMUCONST_MSM_RECORD_DNAME_SIZE
					);
					wchar_t* recordValuePtr = static_cast<wchar_t*>(region3.get_address());
					wcsncpy_s(recordValuePtr, SMUCONST_MSM_RECORD_DNAME_SIZEC, displayName.c_str(), _TRUNCATE);

					logger->log(L"SharedMemoryUtils::refreshMsm_Edit record found and edited");

					// unique record found and edited
					return;
				}

			}

		}

	}

	return;
}
bool
SharedMemoryUtils::isAnyMirandaCsmRecordAvailable(std::wstring& forThisProfileName)
{

	for (int recordNo = 0; recordNo < SMUCONST_CSM_RECORDS_COUNT; recordNo++ ){

		boost::interprocess::mapped_region region1(
			*csm,
			boost::interprocess::read_only,
			SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
			SMUCONST_CSM_RECORD_STATUS_SIZE
		);
		char* recordStatusPtr = static_cast<char*>(region1.get_address());

		if (*recordStatusPtr == 'W'){

			boost::interprocess::mapped_region region2(
				*csm,
				boost::interprocess::read_only,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_TYPE_RECOFFSET,
				SMUCONST_CSM_RECORD_TYPE_SIZE
			);
			char* recordTypePtr = static_cast<char*>(region2.get_address());

			if (*recordTypePtr == 'M'){

				boost::interprocess::mapped_region region3(
					*csm,
					boost::interprocess::read_only,
					SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_VISABLETO_RECOFFSET,
					SMUCONST_CSM_RECORD_VISABLETO_SIZE
				);
				wchar_t* recordVisabletoPtr = static_cast<wchar_t*>(region3.get_address());
				std::wstring visableTo = recordVisabletoPtr;

				if (isTokenOnList(forThisProfileName, visableTo)){
					return true;
				}

			}//end if M

		}//end if W

	}//end for

	return false;
}
Blob MomentCalculation2::getMomentsInaBlob(Mat boxImage, Mat cmaskImage, string blobId,string profileId){


	cvtColor(boxImage, boxImage, CV_BGR2HSV);
	//Average
	HumanProportions prop;

	int boxHeight = boxImage.rows;
	double ratio = (double)boxHeight / (double)prop.height;
	int region1Height = (int)(((double)prop.head) * ratio);
	int region2Height = (int)(((double)prop.gait) * ratio);
	int region3Height = (int)(((double)prop.leg)	* ratio);

	string placement = "Top";
	Region region1(placement, 0, 0, region1Height, boxImage.cols);
	MomentCalculation2 mcalc;
	MomentAverage momentAverage1 = mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region1);
	MomentStandardDeviation momentStandardDeviation1 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region1, &momentAverage1);
	MomentSkewness momentSkewness1 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region1, &momentAverage1);
	region1.setMoments(&momentAverage1, &momentStandardDeviation1, &momentSkewness1);


	placement = "Middle";
	Region region2(placement, 0, 0, region2Height, boxImage.cols);
	MomentAverage momentAverage2 = mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region2);
	MomentStandardDeviation momentStandardDeviation2 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region2, &momentAverage2);
	MomentSkewness momentSkewness2 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region2, &momentAverage2);
	region2.setMoments(&momentAverage2, &momentStandardDeviation2, &momentSkewness2);

	placement = "Bottum";
	Region region3(placement, 0, 0, region3Height, boxImage.cols);
	MomentAverage momentAverage3 = mcalc.getAverageColourInConvexRegion(boxImage, cmaskImage, &region3);
	MomentStandardDeviation momentStandardDeviation3 = mcalc.getStandardDeviationInConvexRegion(boxImage, cmaskImage, &region3, &momentAverage3);
	MomentSkewness momentSkewness3 = mcalc.getSkewnessnInConvexRegion(boxImage, cmaskImage, &region3, &momentAverage3);
	region3.setMoments(&momentAverage3, &momentStandardDeviation3, &momentSkewness3);


	Blob blob(blobId, boxImage.rows, boxImage.cols);
	blob.human_id_actual = profileId;
	blob.addRegion(&region1);
	blob.addRegion(&region2);
	blob.addRegion(&region3);

	return blob;
}
Пример #6
0
 double Evaluate(double *x) {
   if (x[0] <= p[16]){
     return region1(x[0],p);
   }
   else if (x[0]  <= p[17]){
     return region2(x[0]-p[16],p);
     }
   else if (x[0]  <= p[18]){
     return region3(x[0]-p[17],p);
     }
   else if (x[0]  <= p[19]){
     return region4(x[0]-p[18],p);
   }
   else {
     return region5(x[0]-p[19],p);
   }
   
 }
void
SharedMemoryUtils::initCsm(boost::interprocess::windows_shared_memory& csm)
{

	//save csm header version
	boost::interprocess::mapped_region region1(
			csm,
			boost::interprocess::read_write,
			SMUCONST_CSM_HEADER_VERSION_OFFSET,
			SMUCONST_CSM_HEADER_VERSION_SIZE
		);
	uint32_t* versionPtr = static_cast<uint32_t*>(region1.get_address());
	*versionPtr = SMUCONST_CSM_CURRENT_VERSION;


	//prepare records
	for (uint16_t recordNo = 0; recordNo < SMUCONST_CSM_RECORDS_COUNT; recordNo++ ){

		//save scm record id (=recordNo)
		boost::interprocess::mapped_region region2(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_RECID_RECOFFSET,
				SMUCONST_CSM_RECORD_RECID_SIZE
			);
		uint16_t* recordIdPtr = static_cast<uint16_t*>(region2.get_address());
		*recordIdPtr = recordNo + 1;

		//save scm record status (=E (Empty))
		boost::interprocess::mapped_region region3(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
				SMUCONST_CSM_RECORD_STATUS_SIZE
			);
		char* recordStatusPtr = static_cast<char*>(region3.get_address());
		*recordStatusPtr = 'E';

	}


}
void
MessageQueueUtils::sendMessage(int clientRecordId, char menuItemType, char userActionType, char userButton, uint64_t targetHandle, std::wstring userActionSelection)
{

	logger->log(L"MessageQueueUtils::sendMessage");

	//if text to send is too long
	if (userActionSelection.size() > MQCONST_MQSM_TEXT_SIZEC){
		userActionSelection.resize(MQCONST_MQSM_TEXT_SIZEC - 3);
		userActionSelection.append(TEXT("..."));
	}

	try{

		boost::interprocess::windows_shared_memory* sm = new boost::interprocess::windows_shared_memory(
				boost::interprocess::open_only,
				getMqName(clientRecordId).c_str(),
				boost::interprocess::read_write);

		if (sm == NULL){
			logger->log(L"MessageQueueUtils::sendMessage (sm mode) sm == NULL");
			return; //error
		}


		int counter = 0;
		const int MQ_WAIT_TIME = 200;			//[ms]
		const int MAX_MQ_WAIT_COUNTER = 10;		//10 * 200ms = 2s
		bool sentSuccess = false;

		do {

			counter++;

			WaitForSingleObject(smMutex, INFINITE);


			boost::interprocess::mapped_region region1(
					*sm,
					boost::interprocess::read_write,
					MQCONST_MQMIRSM_STATUS_OFFSET,
					MQCONST_MQMIRSM_STATUS_SIZE
				);
			char* mqmirsmStatusPtr = static_cast<char*>(region1.get_address());

			if (*mqmirsmStatusPtr == 'E'){

				boost::interprocess::mapped_region region2(
						*sm,
						boost::interprocess::read_write,
						MQCONST_MQMIRSM_MENUITEMTYPE_OFFSET,
						MQCONST_MQMIRSM_MENUITEMTYPE_SIZE
					);
				char* menuItemTypePtr = static_cast<char*>(region2.get_address());
				*menuItemTypePtr = menuItemType;


				boost::interprocess::mapped_region region3(
						*sm,
						boost::interprocess::read_write,
						MQCONST_MQMIRSM_USERACTIONTYPE_OFFSET,
						MQCONST_MQMIRSM_USERACTIONTYPE_SIZE
					);
				char* userActionTypePtr = static_cast<char*>(region3.get_address());
				*userActionTypePtr = userActionType;


				boost::interprocess::mapped_region region4(
						*sm,
						boost::interprocess::read_write,
						MQCONST_MQMIRSM_USERBUTTON_OFFSET,
						MQCONST_MQMIRSM_USERBUTTON_SIZE
					);
				char* userButtonPtr = static_cast<char*>(region4.get_address());
				*userButtonPtr = userButton;


				boost::interprocess::mapped_region region5(
						*sm,
						boost::interprocess::read_write,
						MQCONST_MQMIRSM_TARGETHANDLE_OFFSET,
						MQCONST_MQMIRSM_TARGETHANDLE_SIZE
					);
				uint64_t* targetHandlePtr = static_cast<uint64_t*>(region5.get_address());
				*targetHandlePtr = targetHandle;


				boost::interprocess::mapped_region region6(
						*sm,
						boost::interprocess::read_write,
						MQCONST_MQMIRSM_TEXTARRAY_OFFSET,
						MQCONST_MQMIRSM_TEXTARRAY_SIZE
					);
				wchar_t* userActionSelectionPtr = static_cast<wchar_t*>(region6.get_address());
				memset(userActionSelectionPtr, 0, MQCONST_MQMIRSM_TEXTARRAY_SIZE);
				memcpy(userActionSelectionPtr, userActionSelection.c_str(), userActionSelection.length() * sizeof(wchar_t));

				//update shared memory status to Message
				*mqmirsmStatusPtr = 'M';

				sentSuccess = true;

			}
			// else wait and repeat try

			ReleaseMutex(smMutex);

			if (!sentSuccess){
				SleepEx(MQ_WAIT_TIME, TRUE);
			}

		} while (sentSuccess == false && counter <= MAX_MQ_WAIT_COUNTER);

		if (!sentSuccess){
			logger->log(L"MessageQueueUtils::sendMessage (sm mode) - MAX_MQ_WAIT_COUNTER exceeded but still sentSuccess == false");
		}

		delete sm;

	} catch (const boost::interprocess::interprocess_exception& ex) {
		logger->log_p(L"MessageQueueUtils::sendMessage (sm mode) interprocess_exception: [%S] native_error=[%d] error_code=[%d]", ex.what(), ex.get_native_error(), ex.get_error_code() );
		return; //error
	} catch (...){
		logger->log(L"MessageQueueUtils::sendMessage (sm mode) EXCEPTION: unknown");
		return; //error
	}

}
/**
 * return true if message was received and variables are available
 */
bool
MessageQueueUtils::tryReceiveMessage(char& menuItemType, char& userActionType, char& userButton, uint64_t& targetHandle, wchar_t*& userActionSelection, size_t uasBuffCSize)
{

	try{

		WaitForSingleObject(smMutex, 100000);


		boost::interprocess::mapped_region region1(
				*mqMirSm,
				boost::interprocess::read_write,
				MQCONST_MQMIRSM_STATUS_OFFSET,
				MQCONST_MQMIRSM_STATUS_SIZE
			);
		char* mqmirsmStatusPtr = static_cast<char*>(region1.get_address());

		if (*mqmirsmStatusPtr != 'M'){
			ReleaseMutex(smMutex);
			return false;
		}


		//there is a new message in the queue (sm mode) *mqmirsmStatusPtr=='M'
		//transfer data from shared memory to variables

		boost::interprocess::mapped_region region2(
				*mqMirSm,
				boost::interprocess::read_only,
				MQCONST_MQMIRSM_MENUITEMTYPE_OFFSET,
				MQCONST_MQMIRSM_MENUITEMTYPE_SIZE
			);
		menuItemType = *(static_cast<char*>(region2.get_address()));

		boost::interprocess::mapped_region region3(
				*mqMirSm,
				boost::interprocess::read_only,
				MQCONST_MQMIRSM_USERACTIONTYPE_OFFSET,
				MQCONST_MQMIRSM_USERACTIONTYPE_SIZE
			);
		userActionType = *(static_cast<char*>(region3.get_address()));

		boost::interprocess::mapped_region region4(
				*mqMirSm,
				boost::interprocess::read_only,
				MQCONST_MQMIRSM_USERBUTTON_OFFSET,
				MQCONST_MQMIRSM_USERBUTTON_SIZE
			);
		userButton = *(static_cast<char*>(region4.get_address()));

		boost::interprocess::mapped_region region5(
				*mqMirSm,
				boost::interprocess::read_only,
				MQCONST_MQMIRSM_TARGETHANDLE_OFFSET,
				MQCONST_MQMIRSM_TARGETHANDLE_SIZE
			);
		targetHandle = *(static_cast<uint64_t*>(region5.get_address()));

		boost::interprocess::mapped_region region6(
				*mqMirSm,
				boost::interprocess::read_only,
				MQCONST_MQMIRSM_TEXTARRAY_OFFSET,
				MQCONST_MQMIRSM_TEXTARRAY_SIZE
			);
		wchar_t* userActionSelectionPtr = static_cast<wchar_t*>(region6.get_address());
		wcsncpy_s(userActionSelection, uasBuffCSize, userActionSelectionPtr, MQCONST_MQMIRSM_TEXTARRAY_SIZEC);


		//update shared memory status to Empty
		*mqmirsmStatusPtr = 'E';
		ReleaseMutex(smMutex);

	} catch (const boost::interprocess::interprocess_exception& ex) {
		logger->log_p(L"MessageQueueUtils::tryReceiveMessage interprocess_exception: [%S] native_error=[%d] error_code=[%d]", ex.what(), ex.get_native_error(), ex.get_error_code() );
		return false;
	} catch (...){
		logger->log(L"MessageQueueUtils::tryReceiveMessage EXCEPTION: unknown");
		return false;
	}

	logger->log_p(L"MessageQueueUtils::tryReceiveMessage (mq mode)  RECEIVED DATA:  menuItemType = [%c] userActionType = [%c] userButton = [%c] targetHandle = [%I64u]",
			menuItemType, userActionType, userButton, targetHandle);

	return true;
}
int
SharedMemoryUtils::getAccounts(boost::ptr_list<AccountClass> * accountsListPtr, unsigned short clientRecordNo)
{

	if (accountsListPtr==NULL){
		logger->log(L"SharedMemoryUtils::getAccounts contactsListPtr==NULL");
		return -1;
	}

	bool isMsmAvailable = true;
	int msmNumber = 1;
	std::string msmName;
	boost::interprocess::windows_shared_memory* msm;

	int foundAccountsCount = 0;


	do {

		//get MSM to seek accounts
		msmName = getMsmName(clientRecordNo, msmNumber);
		msmNumber++;

		try {

			msm = new boost::interprocess::windows_shared_memory(
					boost::interprocess::open_only,
					msmName.c_str(),
					boost::interprocess::read_only);

		} catch (const boost::interprocess::interprocess_exception& ex) {
			isMsmAvailable = false;
			logger->log_p(L"SharedMemoryUtils::getAccounts (isMsmAvailable = false) interprocess_exception: [%S] native_error=[%d] error_code=[%d]", ex.what(), ex.get_native_error(), ex.get_error_code() );
		} catch (...){
			isMsmAvailable = false;
			logger->log(L"SharedMemoryUtils::getAccounts (isMsmAvailable = false) EXCEPTION: unknown");
		}
		//TODO problems with this catch


		if(isMsmAvailable){

			for (int recordNo = 0; recordNo < SMUCONST_MSM_RECORDS_COUNT; recordNo++ ){

				boost::interprocess::mapped_region region1(
					*msm,
					boost::interprocess::read_only,
					SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_TYPE_RECOFFSET,
					SMUCONST_MSM_RECORD_TYPE_SIZE
				);
				char* recordTypePtr = static_cast<char*>(region1.get_address());

				if (*recordTypePtr == 'A'){  //Account

					AccountClass* accountInstance = new AccountClass();

					boost::interprocess::mapped_region region2(
						*msm,
						boost::interprocess::read_only,
						SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_DNAME_RECOFFSET,
						SMUCONST_MSM_RECORD_DNAME_SIZE
					);
					wchar_t* displayNamePtr = static_cast<wchar_t*>(region2.get_address());
					accountInstance->displayName = displayNamePtr;

					boost::interprocess::mapped_region region3(
						*msm,
						boost::interprocess::read_only,
						SMUCONST_MSM_HEADER_SIZE + (recordNo * SMUCONST_MSM_RECORD_SIZE) + SMUCONST_MSM_RECORD_HANDLE_RECOFFSET,
						SMUCONST_MSM_RECORD_HANDLE_SIZE
					);
					uint64_t* agcHandlePtr = static_cast<uint64_t*>(region3.get_address());
					accountInstance->handle = *agcHandlePtr;

					accountsListPtr->push_back(accountInstance);
					foundAccountsCount++;

				}


				//TODO - set all records to type 'E'Empty - if E break for



			} //end for


			//check SMUCONST_MSM_HEADER_ISLAST_OFFSET
			boost::interprocess::mapped_region region4(
				*msm,
				boost::interprocess::read_only,
				SMUCONST_MSM_HEADER_ISLAST_OFFSET,
				SMUCONST_MSM_HEADER_ISLAST_SIZE
			);
			char* isLastNodePtr = static_cast<char*>(region4.get_address());
			if (*isLastNodePtr == '1'){
				isMsmAvailable = false;
				logger->log(L"SharedMemoryUtils::getAccounts - isLastNodePtr - isMsmAvailable = false");
			}


		}// end if

	} while (isMsmAvailable);

	logger->log_p(L"SharedMemoryUtils::getAccounts return foundAccountsCount = %d", foundAccountsCount);

	return foundAccountsCount;
}
int
SharedMemoryUtils::getClientInstances(boost::ptr_list<ClientInstanceClass> * clientInstancesListPtr, std::wstring& forThisProfileName)
{

	if (clientInstancesListPtr==NULL){
		logger->log(L"SharedMemoryUtils::getClientInstances clientInstancesListPtr==NULL");
		return -1;
	}

	int count = 0;

	for (int recordNo = 0; recordNo < SMUCONST_CSM_RECORDS_COUNT; recordNo++ ){

		boost::interprocess::mapped_region region1(
			*csm,
			boost::interprocess::read_only,
			SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
			SMUCONST_CSM_RECORD_STATUS_SIZE
		);
		char* recordStatusPtr = static_cast<char*>(region1.get_address());

		if (*recordStatusPtr == 'W'){

			boost::interprocess::mapped_region region2(
				*csm,
				boost::interprocess::read_only,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_TYPE_RECOFFSET,
				SMUCONST_CSM_RECORD_TYPE_SIZE
			);
			char* recordTypePtr = static_cast<char*>(region2.get_address());

			if (*recordTypePtr == 'M'){

				boost::interprocess::mapped_region region3(
					*csm,
					boost::interprocess::read_only,
					SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_VISABLETO_RECOFFSET,
					SMUCONST_CSM_RECORD_VISABLETO_SIZE
				);
				wchar_t* recordVisabletoPtr = static_cast<wchar_t*>(region3.get_address());
				std::wstring visableTo = recordVisabletoPtr;

				if (isTokenOnList(forThisProfileName, visableTo)){

					//this instance match all conditions
					//get its name and id (recordNo)
					ClientInstanceClass* clientInstance = new ClientInstanceClass();

					boost::interprocess::mapped_region region4(
						*csm,
						boost::interprocess::read_write,
						SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_RECID_RECOFFSET,
						SMUCONST_CSM_RECORD_RECID_SIZE
					);
					uint16_t* recordIdPtr = static_cast<uint16_t*>(region4.get_address());
					clientInstance->recordId = *recordIdPtr;

					boost::interprocess::mapped_region region5(
						*csm,
						boost::interprocess::read_only,
						SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_DNAME_RECOFFSET,
						SMUCONST_CSM_RECORD_DNAME_SIZE
					);
					wchar_t* displayNamePtr = static_cast<wchar_t*>(region5.get_address());
					clientInstance->displayName = displayNamePtr;


					clientInstancesListPtr->push_back(clientInstance);
					count++;

				}

			}//end if M

		}//end if W

	}//end for

	logger->log_p(L"SharedMemoryUtils::getClientInstances return count = %d", count);
	return count;
}
//recordNo === processCsmId -1
void
SharedMemoryUtils::freeCsmRecord(boost::interprocess::windows_shared_memory& csm, uint16_t recordNo)
{

	logger->log_p(L"freeCsmRecord: recordNo = %u processCsmId = %u", recordNo, recordNo+1);

	//zero type
	boost::interprocess::mapped_region region1(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_TYPE_RECOFFSET,
		SMUCONST_CSM_RECORD_TYPE_SIZE
	);
	char* recordTypePtr = static_cast<char*>(region1.get_address());
	*recordTypePtr = (char)0;

	//zero keepalive timestamp
	boost::interprocess::mapped_region region2(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_KAT_RECOFFSET,
		SMUCONST_CSM_RECORD_KAT_SIZE
	);
	time_t* recordKatPtr = static_cast<time_t*>(region2.get_address());
	*recordKatPtr = (time_t)0;

	//zero msm timestamp
	boost::interprocess::mapped_region region3(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_MSMT_RECOFFSET,
		SMUCONST_CSM_RECORD_MSMT_SIZE
	);
	time_t* recordMsmtPtr = static_cast<time_t*>(region3.get_address());
	*recordMsmtPtr = (time_t)0;

	//zero displayname
	boost::interprocess::mapped_region region4(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_DNAME_RECOFFSET,
		SMUCONST_CSM_RECORD_DNAME_SIZE
	);
	memset(region4.get_address(), 0, SMUCONST_CSM_RECORD_DNAME_SIZE);

	//zero visableto
	boost::interprocess::mapped_region region5(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_VISABLETO_RECOFFSET,
		SMUCONST_CSM_RECORD_VISABLETO_SIZE
	);
	memset(region5.get_address(), 0, SMUCONST_CSM_RECORD_VISABLETO_SIZE);

	//status = E
	boost::interprocess::mapped_region region6(
		csm,
		boost::interprocess::read_write,
		SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
		SMUCONST_CSM_RECORD_STATUS_SIZE
	);
	char* recordStatusPtr = static_cast<char*>(region6.get_address());
	*recordStatusPtr = 'E';
}
int
SharedMemoryUtils::addRecordToMsm(char type, uint64_t agcHandle, uint64_t accountHandle, uint64_t groupHandle, char status, std::wstring& value)
{

	logger->log_p(L"addRecordToMsm: type=[%c], agcHandle=[%I64u], accountHandle=[%I64u], groupHandle=[%I64u], status=[%c], value=[%s], agcHandle=[%I64u]",
			type, agcHandle, accountHandle, groupHandle, status==NULL?'-':status , value.c_str(), agcHandle);


	//if there is not enought space in current msm - create new one
	if (freeMsmRecordsCount == 0){

		std::size_t msmTotalSize = getMsmTotalSize();
		std::string msmName = getMsmName(processCsmId, nextMsmNumber);

		logger->log_p(L"create new MSM msmName=[%S] " SCNuPTR L" Bytes (schould be 32768 B)",	msmName.c_str(), msmTotalSize);

		boost::interprocess::windows_shared_memory* msm;
		msm = new boost::interprocess::windows_shared_memory(
			boost::interprocess::open_or_create,
			msmName.c_str(),
			boost::interprocess::read_write,
			msmTotalSize
		); //TODO exception handling //TODO delete msm

		//zero shared memory (neccessery if sm was opened(it is old wrong msm from deleted miranda msm record so it contains old bad data))
		boost::interprocess::mapped_region regionToDel(
				*msm,
				boost::interprocess::read_write,
				0,
				msmTotalSize
			);
		memset(regionToDel.get_address(), 0, msmTotalSize);

		//set isLastNodePtr initial value to 0 , will be update at commitCSM()
		boost::interprocess::mapped_region region1(
				*msm,
				boost::interprocess::read_write,
				SMUCONST_MSM_HEADER_ISLAST_OFFSET,
				SMUCONST_MSM_HEADER_ISLAST_SIZE
			);
		char* isLastNodePtr = static_cast<char*>(region1.get_address());
		*isLastNodePtr = '0';

		//TODO - set all records to type 'E'Empty

		msmList.push_back(msm);

		nextMsmNumber++;
		freeMsmRecordsCount = SMUCONST_MSM_RECORDS_COUNT;

	}

	boost::interprocess::windows_shared_memory * currentMsm = &(msmList.back());

	//save data to record
	int actualRecordOffset = SMUCONST_MSM_RECORDS_OFFSET + ((SMUCONST_MSM_RECORDS_COUNT - freeMsmRecordsCount) * SMUCONST_MSM_RECORD_SIZE);

	//char type
	boost::interprocess::mapped_region region1(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_TYPE_RECOFFSET,
		SMUCONST_MSM_RECORD_TYPE_SIZE
	);
	char* recordTypePtr = static_cast<char*>(region1.get_address());
	*recordTypePtr = type;

	//HANDLE agcHandle
	boost::interprocess::mapped_region region2(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_HANDLE_RECOFFSET,
		SMUCONST_MSM_RECORD_HANDLE_SIZE
	);
	uint64_t* agcHandlePtr  = static_cast<uint64_t*>(region2.get_address());
	*agcHandlePtr = agcHandle;

	//HANDLE accountHandle
	boost::interprocess::mapped_region region3(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_ACCOUNTH_RECOFFSET,
		SMUCONST_MSM_RECORD_ACCOUNTH_SIZE
	);
	uint64_t* accountHandlePtr = static_cast<uint64_t*>(region3.get_address());
	*accountHandlePtr = accountHandle;

	//HANDLE groupHandle
	boost::interprocess::mapped_region region4(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_GROUPH_RECOFFSET,
		SMUCONST_MSM_RECORD_GROUPH_SIZE
	);
	uint64_t* groupHandlePtr = static_cast<uint64_t*>(region4.get_address());
	*groupHandlePtr = groupHandle;

	//char status
	boost::interprocess::mapped_region region5(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_STATUS_RECOFFSET,
		SMUCONST_MSM_RECORD_STATUS_SIZE
	);
	char* recordStatusPtr = static_cast<char*>(region5.get_address());
	*recordStatusPtr = status;

	//std::wstring& value
	boost::interprocess::mapped_region region6(
		*currentMsm,
		boost::interprocess::read_write,
		actualRecordOffset + SMUCONST_MSM_RECORD_DNAME_RECOFFSET,
		SMUCONST_MSM_RECORD_DNAME_SIZE
	);
	wchar_t* recordValuePtr = static_cast<wchar_t*>(region6.get_address());
	wcsncpy_s(recordValuePtr, SMUCONST_MSM_RECORD_DNAME_SIZEC, value.c_str(), _TRUNCATE);

	freeMsmRecordsCount --;

	return 0;
}
/**
 *
 * return
 * >0 - id of assigned record
 * -1 - no empty record found
 */
uint16_t
SharedMemoryUtils::allocateRecordInCsm(boost::interprocess::windows_shared_memory& csm, char type, std::wstring& displayName, std::wstring& visableTo, bool doCommitSM)
{

	//try to find first record with status E (Empty) in CSM
	for (unsigned short recordNo = 0; recordNo < SMUCONST_CSM_RECORDS_COUNT; recordNo++ ){
	
		boost::interprocess::mapped_region region1(
			csm,
			boost::interprocess::read_write,
			SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_STATUS_RECOFFSET,
			SMUCONST_CSM_RECORD_STATUS_SIZE
		);
		char* recordStatusPtr = static_cast<char*>(region1.get_address());
		
		if (*recordStatusPtr == 'E'){

			//save new status 'N' (New)
			*recordStatusPtr = 'N';
		
			//save type
			boost::interprocess::mapped_region region2(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_TYPE_RECOFFSET,
				SMUCONST_CSM_RECORD_TYPE_SIZE
			);
			char* recordTypePtr = static_cast<char*>(region2.get_address());
			*recordTypePtr = type;
			
			//save first keep alive timestamp
			boost::interprocess::mapped_region region3(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_KAT_RECOFFSET,
				SMUCONST_CSM_RECORD_KAT_SIZE
			);
			time_t* recordKatPtr = static_cast<time_t*>(region3.get_address());
			*recordKatPtr = mfGetCurrentTimestamp();

			//save version
			boost::interprocess::mapped_region region4(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_VERSION_RECOFFSET,
				SMUCONST_CSM_RECORD_VERSION_SIZE
			);
			uint16_t* recordVersionPtr = static_cast<uint16_t*>(region4.get_address());
			*recordVersionPtr = SMUCONST_CSM_CURRENT_RECORDVERSION;

			//save display name
			boost::interprocess::mapped_region region5(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_DNAME_RECOFFSET,
				SMUCONST_CSM_RECORD_DNAME_SIZE
			);
			wchar_t* recordDnamePtr = static_cast<wchar_t*>(region5.get_address());
			wcsncpy_s(recordDnamePtr, SMUCONST_CSM_RECORD_DNAME_SIZEC, displayName.c_str(), _TRUNCATE);

			//save visableTo
			boost::interprocess::mapped_region region6(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_VISABLETO_RECOFFSET,
				SMUCONST_CSM_RECORD_VISABLETO_SIZE
			);
			wchar_t* recordVisabletoPtr = static_cast<wchar_t*>(region6.get_address());
			wcsncpy_s(recordVisabletoPtr, SMUCONST_CSM_RECORD_VISABLETO_SIZEC, visableTo.c_str(), _TRUNCATE);
		
			//get id
			boost::interprocess::mapped_region region7(
				csm,
				boost::interprocess::read_write,
				SMUCONST_CSM_HEADER_SIZE + (recordNo * SMUCONST_CSM_RECORD_SIZE) + SMUCONST_CSM_RECORD_RECID_RECOFFSET,
				SMUCONST_CSM_RECORD_RECID_SIZE
			);
			uint16_t* recordIdPtr = static_cast<uint16_t*>(region7.get_address());
			

			if (doCommitSM){
				*recordStatusPtr = 'W';
			}

			logger->log_p(L"allocateRecordInCsm: allocated record with id (processCsmId): %u", *recordIdPtr);
			
			return *recordIdPtr;
		
		}//end if
	
	}//end for
	
	logger->log(L"allocateRecordInCsm: no empty records in CSM. return -1");
	return -2;
	
}
Пример #15
0
 double region4(double x, double *p){
   return (p[9]*x + region3(p[18]-p[17],p) );
 }
Пример #16
0
int GeometryClipmaps::updateLevel( GeometryClipmapLevel& level, 
                                   GeometryClipmapLevel* parentLevel, 
                                   const Pnt2i& newOrigin )
{
    //todo: move this into the GeometryClipmapLevel class??
    // how much samples do we have to update?
    const Pnt2i oldOrigin = level.sampleOrigin;
    const Pnt2i updateSampleCount = newOrigin - oldOrigin;
    
    const bool forceFullUpdate = level.isSampleDataDirty;
    
    if( !forceFullUpdate && oldOrigin == newOrigin )
    {
        // nothing to do:
        return 0;
    }
    
    //SLOG << "Level " << level.index << " moved by " << updateSampleCount << " oldOrigin = " << oldOrigin << " newOrigin = " << newOrigin << "\n";
    
    // whatever we need to do: the origin is now here:
    level.sampleOrigin = newOrigin;
    
    const int levelSampleCount = level.heightmap.size;
    const int levelSampleSpacing = level.sampleSpacing;
    
    const bool fullBlockUpdate = ( abs( updateSampleCount[ 0 ] ) >= levelSampleCount ) || 
        ( abs( updateSampleCount[ 1 ] ) >= levelSampleCount );
    
    int updatedSampleCount = 0;
    
    if( fullBlockUpdate || forceFullUpdate )
    {
        // full block update:
        // reset the block origin:
        level.blockOrigin.setValues( 0, 0 );
        updatedSampleCount += updateLevelBlock( level, 
                                                parentLevel, 
                                                Rectangle2i( 0, 0, levelSampleCount, levelSampleCount ) );
    }
    else if( updateSampleCount[ 0 ] != 0 || updateSampleCount[ 1 ] != 0 )
    {
        Pnt2i levelSampleCount2d( levelSampleCount, levelSampleCount );
        
        // adjust adressing..
        Pnt2i blockDelta = componentDivide( updateSampleCount, levelSampleSpacing );
        
        Pnt2i oldBlockOrigin( level.blockOrigin[ 0 ], level.blockOrigin[ 1 ] );
        
        // update block origin:
        Pnt2i newBlockOrigin = componentModulo( componentAdd( componentAdd( oldBlockOrigin, blockDelta ), levelSampleCount2d ), levelSampleCount2d );
        
        assert( newBlockOrigin[ 0 ] >= 0 );
        assert( newBlockOrigin[ 1 ] >= 0 );
        assert( newBlockOrigin[ 0 ] < levelSampleCount );
        assert( newBlockOrigin[ 1 ] < levelSampleCount );
        
        level.blockOrigin = newBlockOrigin;
        
        // update the dirty Rects:
        // in general the update region is ? shaped: (we do 1 or 2 rectangular updates)
        Rectangle2i region1( 0, 0, levelSampleCount, levelSampleCount );
        Rectangle2i region2( 0, 0, levelSampleCount, levelSampleCount );
        Rectangle2i region3( 0, 0, levelSampleCount, levelSampleCount );
        
        if( blockDelta[ 0 ] > 0 )
        {
            region1._x0 = oldBlockOrigin[ 0 ];
            region1._x1 = region1._x0 + blockDelta[ 0 ];
        }
        else if( blockDelta[ 0 ] < 0 )
        {
            region1._x0 = newBlockOrigin[ 0 ];
            region1._x1 = region1._x0 - blockDelta[ 0 ];
        }
        else
        {
            // region 1 is empty:
            region1.setValues( 0, 0, 0, 0 );
        }
        if( blockDelta[ 1 ] > 0 )
        {
            region2._y0 = oldBlockOrigin[ 1 ];
            region2._y1 = region2._y0 + blockDelta[ 1 ];
        }
        else if( blockDelta[ 1 ] < 0 )
        {
            region2._y0 = newBlockOrigin[ 1 ];
            region2._y1 = region2._y0 - blockDelta[ 1 ];
        }
        else
        {
            // region 1 is empty:
            region2.setValues( 0, 0, 0, 0 );
        }
        
        updatedSampleCount += updateBigLevelBlock( level, parentLevel, region1 );
        updatedSampleCount += updateBigLevelBlock( level, parentLevel, region2 );
    }
    
    // for debugging:
    //checkLevelBlock( level, Rectangle2i( 0, 0, levelSampleCount, levelSampleCount ) );
    
    cpuRenderer_.rebuildIndices( level );       
    
    if( parentLevel )
    {
        // todo: is this really needed every frame?
        cpuRenderer_.rebuildIndices( *parentLevel );
    }
    
    // determine min/max sample:
    level.heightmap.updateMinMaxSamples();
    
    // activate this level:
    level.isActive = true;
    
    // we just updated the data:
    level.isSampleDataDirty = false;
    
    // todo: make this work:
    return updatedSampleCount;
    //return 0;
}